fsmagic.c 8.2 KB

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