fsmagic.c 9.2 KB

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