fsmagic.c 9.3 KB

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