fsmagic.c 8.1 KB

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