fsmagic.c 9.6 KB

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