fsmagic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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.77 2017/05/24 19:17:50 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 MAJOR_IN_SYSMACROS
  47. # include <sys/sysmacros.h>
  48. # define HAVE_MAJOR
  49. #endif
  50. #ifdef major /* Might be defined in sys/types.h. */
  51. # define HAVE_MAJOR
  52. #endif
  53. #ifdef WIN32
  54. # define WIN32_LEAN_AND_MEAN
  55. # include <windows.h>
  56. #endif
  57. #ifndef HAVE_MAJOR
  58. # define major(dev) (((dev) >> 8) & 0xff)
  59. # define minor(dev) ((dev) & 0xff)
  60. #endif
  61. #undef HAVE_MAJOR
  62. #ifdef S_IFLNK
  63. private int
  64. bad_link(struct magic_set *ms, int err, char *buf)
  65. {
  66. int mime = ms->flags & MAGIC_MIME;
  67. if ((mime & MAGIC_MIME_TYPE) &&
  68. file_printf(ms, "inode/symlink")
  69. == -1)
  70. return -1;
  71. else if (!mime) {
  72. if (ms->flags & MAGIC_ERROR) {
  73. file_error(ms, err,
  74. "broken symbolic link to %s", buf);
  75. return -1;
  76. }
  77. if (file_printf(ms, "broken symbolic link to %s", buf) == -1)
  78. return -1;
  79. }
  80. return 1;
  81. }
  82. #endif
  83. private int
  84. handle_mime(struct magic_set *ms, int mime, const char *str)
  85. {
  86. if ((mime & MAGIC_MIME_TYPE)) {
  87. if (file_printf(ms, "inode/%s", str) == -1)
  88. return -1;
  89. if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
  90. "; charset=") == -1)
  91. return -1;
  92. }
  93. if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
  94. return -1;
  95. return 0;
  96. }
  97. protected int
  98. file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
  99. {
  100. int ret, did = 0;
  101. int mime = ms->flags & MAGIC_MIME;
  102. int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
  103. #ifdef S_IFLNK
  104. char buf[BUFSIZ+4];
  105. ssize_t nch;
  106. struct stat tstatbuf;
  107. #endif
  108. if (fn == NULL)
  109. return 0;
  110. #define COMMA (did++ ? ", " : "")
  111. /*
  112. * Fstat is cheaper but fails for files you don't have read perms on.
  113. * On 4.2BSD and similar systems, use lstat() to identify symlinks.
  114. */
  115. #ifdef S_IFLNK
  116. if ((ms->flags & MAGIC_SYMLINK) == 0)
  117. ret = lstat(fn, sb);
  118. else
  119. #endif
  120. ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
  121. #ifdef WIN32
  122. {
  123. HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE |
  124. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
  125. NULL);
  126. if (hFile != INVALID_HANDLE_VALUE) {
  127. /*
  128. * Stat failed, but we can still open it - assume it's
  129. * a block device, if nothing else.
  130. */
  131. if (ret) {
  132. sb->st_mode = S_IFBLK;
  133. ret = 0;
  134. }
  135. switch (GetFileType(hFile)) {
  136. case FILE_TYPE_CHAR:
  137. sb->st_mode |= S_IFCHR;
  138. sb->st_mode &= ~S_IFREG;
  139. break;
  140. case FILE_TYPE_PIPE:
  141. sb->st_mode |= S_IFIFO;
  142. sb->st_mode &= ~S_IFREG;
  143. break;
  144. }
  145. CloseHandle(hFile);
  146. }
  147. }
  148. #endif
  149. if (ret) {
  150. if (ms->flags & MAGIC_ERROR) {
  151. file_error(ms, errno, "cannot stat `%s'", fn);
  152. return -1;
  153. }
  154. if (file_printf(ms, "cannot open `%s' (%s)",
  155. fn, strerror(errno)) == -1)
  156. return -1;
  157. return 0;
  158. }
  159. ret = 1;
  160. if (!mime && !silent) {
  161. #ifdef S_ISUID
  162. if (sb->st_mode & S_ISUID)
  163. if (file_printf(ms, "%ssetuid", COMMA) == -1)
  164. return -1;
  165. #endif
  166. #ifdef S_ISGID
  167. if (sb->st_mode & S_ISGID)
  168. if (file_printf(ms, "%ssetgid", COMMA) == -1)
  169. return -1;
  170. #endif
  171. #ifdef S_ISVTX
  172. if (sb->st_mode & S_ISVTX)
  173. if (file_printf(ms, "%ssticky", COMMA) == -1)
  174. return -1;
  175. #endif
  176. }
  177. switch (sb->st_mode & S_IFMT) {
  178. case S_IFDIR:
  179. if (mime) {
  180. if (handle_mime(ms, mime, "directory") == -1)
  181. return -1;
  182. } else if (silent) {
  183. } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
  184. return -1;
  185. break;
  186. #ifdef S_IFCHR
  187. case S_IFCHR:
  188. /*
  189. * If -s has been specified, treat character special files
  190. * like ordinary files. Otherwise, just report that they
  191. * are block special files and go on to the next file.
  192. */
  193. if ((ms->flags & MAGIC_DEVICES) != 0) {
  194. ret = 0;
  195. break;
  196. }
  197. if (mime) {
  198. if (handle_mime(ms, mime, "chardevice") == -1)
  199. return -1;
  200. } else if (silent) {
  201. } else {
  202. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  203. # ifdef dv_unit
  204. if (file_printf(ms, "%scharacter special (%d/%d/%d)",
  205. COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
  206. dv_subunit(sb->st_rdev)) == -1)
  207. return -1;
  208. # else
  209. if (file_printf(ms, "%scharacter special (%ld/%ld)",
  210. COMMA, (long)major(sb->st_rdev),
  211. (long)minor(sb->st_rdev)) == -1)
  212. return -1;
  213. # endif
  214. #else
  215. if (file_printf(ms, "%scharacter special", COMMA) == -1)
  216. return -1;
  217. #endif
  218. }
  219. break;
  220. #endif
  221. #ifdef S_IFBLK
  222. case S_IFBLK:
  223. /*
  224. * If -s has been specified, treat block special files
  225. * like ordinary files. Otherwise, just report that they
  226. * are block special files and go on to the next file.
  227. */
  228. if ((ms->flags & MAGIC_DEVICES) != 0) {
  229. ret = 0;
  230. break;
  231. }
  232. if (mime) {
  233. if (handle_mime(ms, mime, "blockdevice") == -1)
  234. return -1;
  235. } else if (silent) {
  236. } else {
  237. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  238. # ifdef dv_unit
  239. if (file_printf(ms, "%sblock special (%d/%d/%d)",
  240. COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
  241. dv_subunit(sb->st_rdev)) == -1)
  242. return -1;
  243. # else
  244. if (file_printf(ms, "%sblock special (%ld/%ld)",
  245. COMMA, (long)major(sb->st_rdev),
  246. (long)minor(sb->st_rdev)) == -1)
  247. return -1;
  248. # endif
  249. #else
  250. if (file_printf(ms, "%sblock special", COMMA) == -1)
  251. return -1;
  252. #endif
  253. }
  254. break;
  255. #endif
  256. /* TODO add code to handle V7 MUX and Blit MUX files */
  257. #ifdef S_IFIFO
  258. case S_IFIFO:
  259. if((ms->flags & MAGIC_DEVICES) != 0)
  260. break;
  261. if (mime) {
  262. if (handle_mime(ms, mime, "fifo") == -1)
  263. return -1;
  264. } else if (silent) {
  265. } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
  266. return -1;
  267. break;
  268. #endif
  269. #ifdef S_IFDOOR
  270. case S_IFDOOR:
  271. if (mime) {
  272. if (handle_mime(ms, mime, "door") == -1)
  273. return -1;
  274. } else if (silent) {
  275. } else if (file_printf(ms, "%sdoor", COMMA) == -1)
  276. return -1;
  277. break;
  278. #endif
  279. #ifdef S_IFLNK
  280. case S_IFLNK:
  281. if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
  282. if (ms->flags & MAGIC_ERROR) {
  283. file_error(ms, errno, "unreadable symlink `%s'",
  284. fn);
  285. return -1;
  286. }
  287. if (mime) {
  288. if (handle_mime(ms, mime, "symlink") == -1)
  289. return -1;
  290. } else if (silent) {
  291. } else if (file_printf(ms,
  292. "%sunreadable symlink `%s' (%s)", COMMA, fn,
  293. strerror(errno)) == -1)
  294. return -1;
  295. break;
  296. }
  297. buf[nch] = '\0'; /* readlink(2) does not do this */
  298. /* If broken symlink, say so and quit early. */
  299. if (*buf == '/') {
  300. if (stat(buf, &tstatbuf) < 0)
  301. return bad_link(ms, errno, buf);
  302. } else {
  303. char *tmp;
  304. char buf2[BUFSIZ+BUFSIZ+4];
  305. if ((tmp = strrchr(fn, '/')) == NULL) {
  306. tmp = buf; /* in current directory anyway */
  307. } else {
  308. if (tmp - fn + 1 > BUFSIZ) {
  309. if (ms->flags & MAGIC_ERROR) {
  310. file_error(ms, 0,
  311. "path too long: `%s'", buf);
  312. return -1;
  313. }
  314. if (mime) {
  315. if (handle_mime(ms, mime,
  316. "x-path-too-long") == -1)
  317. return -1;
  318. } else if (silent) {
  319. } else if (file_printf(ms,
  320. "%spath too long: `%s'", COMMA,
  321. fn) == -1)
  322. return -1;
  323. break;
  324. }
  325. /* take dir part */
  326. (void)strlcpy(buf2, fn, sizeof buf2);
  327. buf2[tmp - fn + 1] = '\0';
  328. /* plus (rel) link */
  329. (void)strlcat(buf2, buf, sizeof buf2);
  330. tmp = buf2;
  331. }
  332. if (stat(tmp, &tstatbuf) < 0)
  333. return bad_link(ms, errno, buf);
  334. }
  335. /* Otherwise, handle it. */
  336. if ((ms->flags & MAGIC_SYMLINK) != 0) {
  337. const char *p;
  338. ms->flags &= MAGIC_SYMLINK;
  339. p = magic_file(ms, buf);
  340. ms->flags |= MAGIC_SYMLINK;
  341. if (p == NULL)
  342. return -1;
  343. } else { /* just print what it points to */
  344. if (mime) {
  345. if (handle_mime(ms, mime, "symlink") == -1)
  346. return -1;
  347. } else if (silent) {
  348. } else if (file_printf(ms, "%ssymbolic link to %s",
  349. COMMA, buf) == -1)
  350. return -1;
  351. }
  352. break;
  353. #endif
  354. #ifdef S_IFSOCK
  355. #ifndef __COHERENT__
  356. case S_IFSOCK:
  357. if (mime) {
  358. if (handle_mime(ms, mime, "socket") == -1)
  359. return -1;
  360. } else if (silent) {
  361. } else if (file_printf(ms, "%ssocket", COMMA) == -1)
  362. return -1;
  363. break;
  364. #endif
  365. #endif
  366. case S_IFREG:
  367. /*
  368. * regular file, check next possibility
  369. *
  370. * If stat() tells us the file has zero length, report here that
  371. * the file is empty, so we can skip all the work of opening and
  372. * reading the file.
  373. * But if the -s option has been given, we skip this
  374. * optimization, since on some systems, stat() reports zero
  375. * size for raw disk partitions. (If the block special device
  376. * really has zero length, the fact that it is empty will be
  377. * detected and reported correctly when we read the file.)
  378. */
  379. if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
  380. if (mime) {
  381. if (handle_mime(ms, mime, "x-empty") == -1)
  382. return -1;
  383. } else if (silent) {
  384. } else if (file_printf(ms, "%sempty", COMMA) == -1)
  385. return -1;
  386. break;
  387. }
  388. ret = 0;
  389. break;
  390. default:
  391. file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
  392. return -1;
  393. /*NOTREACHED*/
  394. }
  395. if (!silent && !mime && did && ret == 0) {
  396. if (file_printf(ms, " ") == -1)
  397. return -1;
  398. }
  399. return ret;
  400. }