fsmagic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * fsmagic - magic based on filesystem info - directory, special files, etc.
  30. */
  31. #include "file.h"
  32. #ifndef lint
  33. FILE_RCSID("@(#)$File: fsmagic.c,v 1.81 2019/07/16 13:30:32 christos Exp $")
  34. #endif /* lint */
  35. #include "magic.h"
  36. #include <string.h>
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #include <stdlib.h>
  41. /* Since major is a function on SVR4, we cannot use `ifndef major'. */
  42. #ifdef MAJOR_IN_MKDEV
  43. # include <sys/mkdev.h>
  44. # define HAVE_MAJOR
  45. #endif
  46. #ifdef HAVE_SYS_SYSMACROS_H
  47. # include <sys/sysmacros.h>
  48. #endif
  49. #ifdef MAJOR_IN_SYSMACROS
  50. # define HAVE_MAJOR
  51. #endif
  52. #if defined(major) && !defined(HAVE_MAJOR)
  53. /* Might be defined in sys/types.h. */
  54. # define HAVE_MAJOR
  55. #endif
  56. #ifdef WIN32
  57. # define WIN32_LEAN_AND_MEAN
  58. # include <windows.h>
  59. #endif
  60. #ifndef HAVE_MAJOR
  61. # define major(dev) (((dev) >> 8) & 0xff)
  62. # define minor(dev) ((dev) & 0xff)
  63. #endif
  64. #undef HAVE_MAJOR
  65. #ifdef S_IFLNK
  66. private int
  67. bad_link(struct magic_set *ms, int err, char *buf)
  68. {
  69. int mime = ms->flags & MAGIC_MIME;
  70. if ((mime & MAGIC_MIME_TYPE) &&
  71. file_printf(ms, "inode/symlink")
  72. == -1)
  73. return -1;
  74. else if (!mime) {
  75. if (ms->flags & MAGIC_ERROR) {
  76. file_error(ms, err,
  77. "broken symbolic link to %s", buf);
  78. return -1;
  79. }
  80. if (file_printf(ms, "broken symbolic link to %s", buf) == -1)
  81. return -1;
  82. }
  83. return 1;
  84. }
  85. #endif
  86. private int
  87. handle_mime(struct magic_set *ms, int mime, const char *str)
  88. {
  89. if ((mime & MAGIC_MIME_TYPE)) {
  90. if (file_printf(ms, "inode/%s", str) == -1)
  91. return -1;
  92. if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
  93. "; charset=") == -1)
  94. return -1;
  95. }
  96. if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
  97. return -1;
  98. return 0;
  99. }
  100. protected int
  101. file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
  102. {
  103. int ret, did = 0;
  104. int mime = ms->flags & MAGIC_MIME;
  105. int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
  106. #ifdef S_IFLNK
  107. char buf[BUFSIZ+4];
  108. ssize_t nch;
  109. struct stat tstatbuf;
  110. #endif
  111. if (fn == NULL)
  112. return 0;
  113. #define COMMA (did++ ? ", " : "")
  114. /*
  115. * Fstat is cheaper but fails for files you don't have read perms on.
  116. * On 4.2BSD and similar systems, use lstat() to identify symlinks.
  117. */
  118. #ifdef S_IFLNK
  119. if ((ms->flags & MAGIC_SYMLINK) == 0)
  120. ret = lstat(fn, sb);
  121. else
  122. #endif
  123. ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
  124. #ifdef WIN32
  125. {
  126. HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE |
  127. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
  128. NULL);
  129. if (hFile != INVALID_HANDLE_VALUE) {
  130. /*
  131. * Stat failed, but we can still open it - assume it's
  132. * a block device, if nothing else.
  133. */
  134. if (ret) {
  135. sb->st_mode = S_IFBLK;
  136. ret = 0;
  137. }
  138. switch (GetFileType(hFile)) {
  139. case FILE_TYPE_CHAR:
  140. sb->st_mode |= S_IFCHR;
  141. sb->st_mode &= ~S_IFREG;
  142. break;
  143. case FILE_TYPE_PIPE:
  144. sb->st_mode |= S_IFIFO;
  145. sb->st_mode &= ~S_IFREG;
  146. break;
  147. }
  148. CloseHandle(hFile);
  149. }
  150. }
  151. #endif
  152. if (ret) {
  153. if (ms->flags & MAGIC_ERROR) {
  154. file_error(ms, errno, "cannot stat `%s'", fn);
  155. return -1;
  156. }
  157. if (file_printf(ms, "cannot open `%s' (%s)",
  158. fn, strerror(errno)) == -1)
  159. return -1;
  160. return 0;
  161. }
  162. ret = 1;
  163. if (!mime && !silent) {
  164. #ifdef S_ISUID
  165. if (sb->st_mode & S_ISUID)
  166. if (file_printf(ms, "%ssetuid", COMMA) == -1)
  167. return -1;
  168. #endif
  169. #ifdef S_ISGID
  170. if (sb->st_mode & S_ISGID)
  171. if (file_printf(ms, "%ssetgid", COMMA) == -1)
  172. return -1;
  173. #endif
  174. #ifdef S_ISVTX
  175. if (sb->st_mode & S_ISVTX)
  176. if (file_printf(ms, "%ssticky", COMMA) == -1)
  177. return -1;
  178. #endif
  179. }
  180. switch (sb->st_mode & S_IFMT) {
  181. case S_IFDIR:
  182. if (mime) {
  183. if (handle_mime(ms, mime, "directory") == -1)
  184. return -1;
  185. } else if (silent) {
  186. } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
  187. return -1;
  188. break;
  189. #ifdef S_IFCHR
  190. case S_IFCHR:
  191. /*
  192. * If -s has been specified, treat character special files
  193. * like ordinary files. Otherwise, just report that they
  194. * are block special files and go on to the next file.
  195. */
  196. if ((ms->flags & MAGIC_DEVICES) != 0) {
  197. ret = 0;
  198. break;
  199. }
  200. if (mime) {
  201. if (handle_mime(ms, mime, "chardevice") == -1)
  202. return -1;
  203. } else if (silent) {
  204. } else {
  205. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  206. # ifdef dv_unit
  207. if (file_printf(ms, "%scharacter special (%d/%d/%d)",
  208. COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
  209. dv_subunit(sb->st_rdev)) == -1)
  210. return -1;
  211. # else
  212. if (file_printf(ms, "%scharacter special (%ld/%ld)",
  213. COMMA, (long)major(sb->st_rdev),
  214. (long)minor(sb->st_rdev)) == -1)
  215. return -1;
  216. # endif
  217. #else
  218. if (file_printf(ms, "%scharacter special", COMMA) == -1)
  219. return -1;
  220. #endif
  221. }
  222. break;
  223. #endif
  224. #ifdef S_IFBLK
  225. case S_IFBLK:
  226. /*
  227. * If -s has been specified, treat block special files
  228. * like ordinary files. Otherwise, just report that they
  229. * are block special files and go on to the next file.
  230. */
  231. if ((ms->flags & MAGIC_DEVICES) != 0) {
  232. ret = 0;
  233. break;
  234. }
  235. if (mime) {
  236. if (handle_mime(ms, mime, "blockdevice") == -1)
  237. return -1;
  238. } else if (silent) {
  239. } else {
  240. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  241. # ifdef dv_unit
  242. if (file_printf(ms, "%sblock special (%d/%d/%d)",
  243. COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
  244. dv_subunit(sb->st_rdev)) == -1)
  245. return -1;
  246. # else
  247. if (file_printf(ms, "%sblock special (%ld/%ld)",
  248. COMMA, (long)major(sb->st_rdev),
  249. (long)minor(sb->st_rdev)) == -1)
  250. return -1;
  251. # endif
  252. #else
  253. if (file_printf(ms, "%sblock special", COMMA) == -1)
  254. return -1;
  255. #endif
  256. }
  257. break;
  258. #endif
  259. /* TODO add code to handle V7 MUX and Blit MUX files */
  260. #ifdef S_IFIFO
  261. case S_IFIFO:
  262. if((ms->flags & MAGIC_DEVICES) != 0)
  263. break;
  264. if (mime) {
  265. if (handle_mime(ms, mime, "fifo") == -1)
  266. return -1;
  267. } else if (silent) {
  268. } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
  269. return -1;
  270. break;
  271. #endif
  272. #ifdef S_IFDOOR
  273. case S_IFDOOR:
  274. if (mime) {
  275. if (handle_mime(ms, mime, "door") == -1)
  276. return -1;
  277. } else if (silent) {
  278. } else if (file_printf(ms, "%sdoor", COMMA) == -1)
  279. return -1;
  280. break;
  281. #endif
  282. #ifdef S_IFLNK
  283. case S_IFLNK:
  284. if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
  285. if (ms->flags & MAGIC_ERROR) {
  286. file_error(ms, errno, "unreadable symlink `%s'",
  287. fn);
  288. return -1;
  289. }
  290. if (mime) {
  291. if (handle_mime(ms, mime, "symlink") == -1)
  292. return -1;
  293. } else if (silent) {
  294. } else if (file_printf(ms,
  295. "%sunreadable symlink `%s' (%s)", COMMA, fn,
  296. strerror(errno)) == -1)
  297. return -1;
  298. break;
  299. }
  300. buf[nch] = '\0'; /* readlink(2) does not do this */
  301. /* If broken symlink, say so and quit early. */
  302. #ifdef __linux__
  303. /*
  304. * linux procfs/devfs makes symlinks like pipe:[3515864880]
  305. * that we can't stat their readlink output, so stat the
  306. * original filename instead.
  307. */
  308. if (stat(fn, &tstatbuf) < 0)
  309. return bad_link(ms, errno, buf);
  310. #else
  311. if (*buf == '/') {
  312. if (stat(buf, &tstatbuf) < 0)
  313. return bad_link(ms, errno, buf);
  314. } else {
  315. char *tmp;
  316. char buf2[BUFSIZ+BUFSIZ+4];
  317. if ((tmp = strrchr(fn, '/')) == NULL) {
  318. tmp = buf; /* in current directory anyway */
  319. } else {
  320. if (tmp - fn + 1 > BUFSIZ) {
  321. if (ms->flags & MAGIC_ERROR) {
  322. file_error(ms, 0,
  323. "path too long: `%s'", buf);
  324. return -1;
  325. }
  326. if (mime) {
  327. if (handle_mime(ms, mime,
  328. "x-path-too-long") == -1)
  329. return -1;
  330. } else if (silent) {
  331. } else if (file_printf(ms,
  332. "%spath too long: `%s'", COMMA,
  333. fn) == -1)
  334. return -1;
  335. break;
  336. }
  337. /* take dir part */
  338. (void)strlcpy(buf2, fn, sizeof buf2);
  339. buf2[tmp - fn + 1] = '\0';
  340. /* plus (rel) link */
  341. (void)strlcat(buf2, buf, sizeof buf2);
  342. tmp = buf2;
  343. }
  344. if (stat(tmp, &tstatbuf) < 0)
  345. return bad_link(ms, errno, buf);
  346. }
  347. #endif
  348. /* Otherwise, handle it. */
  349. if ((ms->flags & MAGIC_SYMLINK) != 0) {
  350. const char *p;
  351. ms->flags &= MAGIC_SYMLINK;
  352. p = magic_file(ms, buf);
  353. ms->flags |= MAGIC_SYMLINK;
  354. if (p == NULL)
  355. return -1;
  356. } else { /* just print what it points to */
  357. if (mime) {
  358. if (handle_mime(ms, mime, "symlink") == -1)
  359. return -1;
  360. } else if (silent) {
  361. } else if (file_printf(ms, "%ssymbolic link to %s",
  362. COMMA, buf) == -1)
  363. return -1;
  364. }
  365. break;
  366. #endif
  367. #ifdef S_IFSOCK
  368. #ifndef __COHERENT__
  369. case S_IFSOCK:
  370. if (mime) {
  371. if (handle_mime(ms, mime, "socket") == -1)
  372. return -1;
  373. } else if (silent) {
  374. } else if (file_printf(ms, "%ssocket", COMMA) == -1)
  375. return -1;
  376. break;
  377. #endif
  378. #endif
  379. case S_IFREG:
  380. /*
  381. * regular file, check next possibility
  382. *
  383. * If stat() tells us the file has zero length, report here that
  384. * the file is empty, so we can skip all the work of opening and
  385. * reading the file.
  386. * But if the -s option has been given, we skip this
  387. * optimization, since on some systems, stat() reports zero
  388. * size for raw disk partitions. (If the block special device
  389. * really has zero length, the fact that it is empty will be
  390. * detected and reported correctly when we read the file.)
  391. */
  392. if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
  393. if (mime) {
  394. if (handle_mime(ms, mime, "x-empty") == -1)
  395. return -1;
  396. } else if (silent) {
  397. } else if (file_printf(ms, "%sempty", COMMA) == -1)
  398. return -1;
  399. break;
  400. }
  401. ret = 0;
  402. break;
  403. default:
  404. file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
  405. return -1;
  406. /*NOTREACHED*/
  407. }
  408. if (!silent && !mime && did && ret == 0) {
  409. if (file_printf(ms, " ") == -1)
  410. return -1;
  411. }
  412. /*
  413. * If we were looking for extensions or apple (silent) it is not our
  414. * job to print here, so don't count this as a match.
  415. */
  416. if (ret == 1 && silent)
  417. return 0;
  418. return ret;
  419. }