fsmagic.c 9.3 KB

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