fsmagic.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.71 2013/12/01 18:01:07 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 PHP_WIN32
  59. # undef S_IFIFO
  60. #endif
  61. #ifndef S_ISDIR
  62. #define S_ISDIR(mode) ((mode) & _S_IFDIR)
  63. #endif
  64. #ifndef S_ISREG
  65. #define S_ISREG(mode) ((mode) & _S_IFREG)
  66. #endif
  67. private int
  68. handle_mime(struct magic_set *ms, int mime, const char *str)
  69. {
  70. if ((mime & MAGIC_MIME_TYPE)) {
  71. if (file_printf(ms, "inode/%s", str) == -1)
  72. return -1;
  73. if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
  74. "; charset=") == -1)
  75. return -1;
  76. }
  77. if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
  78. return -1;
  79. return 0;
  80. }
  81. protected int
  82. file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream)
  83. {
  84. int ret, did = 0;
  85. int mime = ms->flags & MAGIC_MIME;
  86. TSRMLS_FETCH();
  87. if (ms->flags & MAGIC_APPLE)
  88. return 0;
  89. if (fn == NULL && !stream) {
  90. return 0;
  91. }
  92. #define COMMA (did++ ? ", " : "")
  93. if (stream) {
  94. php_stream_statbuf ssb;
  95. if (php_stream_stat(stream, &ssb) < 0) {
  96. if (ms->flags & MAGIC_ERROR) {
  97. file_error(ms, errno, "cannot stat `%s'", fn);
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. memcpy(sb, &ssb.sb, sizeof(struct stat));
  103. } else {
  104. if (php_sys_stat(fn, sb) != 0) {
  105. if (ms->flags & MAGIC_ERROR) {
  106. file_error(ms, errno, "cannot stat `%s'", fn);
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. }
  112. ret = 1;
  113. if (!mime) {
  114. #ifdef S_ISUID
  115. if (sb->st_mode & S_ISUID)
  116. if (file_printf(ms, "%ssetuid", COMMA) == -1)
  117. return -1;
  118. #endif
  119. #ifdef S_ISGID
  120. if (sb->st_mode & S_ISGID)
  121. if (file_printf(ms, "%ssetgid", COMMA) == -1)
  122. return -1;
  123. #endif
  124. #ifdef S_ISVTX
  125. if (sb->st_mode & S_ISVTX)
  126. if (file_printf(ms, "%ssticky", COMMA) == -1)
  127. return -1;
  128. #endif
  129. }
  130. switch (sb->st_mode & S_IFMT) {
  131. #ifndef PHP_WIN32
  132. # ifdef S_IFCHR
  133. case S_IFCHR:
  134. /*
  135. * If -s has been specified, treat character special files
  136. * like ordinary files. Otherwise, just report that they
  137. * are block special files and go on to the next file.
  138. */
  139. if ((ms->flags & MAGIC_DEVICES) != 0) {
  140. ret = 0;
  141. break;
  142. }
  143. if (mime) {
  144. if (handle_mime(ms, mime, "chardevice") == -1)
  145. return -1;
  146. } else {
  147. # ifdef HAVE_STAT_ST_RDEV
  148. # ifdef dv_unit
  149. if (file_printf(ms, "%scharacter special (%d/%d/%d)",
  150. COMMA, 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, "%scharacter special (%ld/%ld)",
  155. COMMA, (long)major(sb->st_rdev),
  156. (long)minor(sb->st_rdev)) == -1)
  157. return -1;
  158. # endif
  159. #else
  160. if (file_printf(ms, "%scharacter special", COMMA) == -1)
  161. return -1;
  162. #endif
  163. }
  164. return 1;
  165. # endif
  166. #endif
  167. #ifdef S_IFIFO
  168. case S_IFIFO:
  169. if((ms->flags & MAGIC_DEVICES) != 0)
  170. break;
  171. if (mime) {
  172. if (handle_mime(ms, mime, "fifo") == -1)
  173. return -1;
  174. } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
  175. return -1;
  176. break;
  177. #endif
  178. #ifdef S_IFDOOR
  179. case S_IFDOOR:
  180. if (mime) {
  181. if (handle_mime(ms, mime, "door") == -1)
  182. return -1;
  183. } else if (file_printf(ms, "%sdoor", COMMA) == -1)
  184. return -1;
  185. break;
  186. #endif
  187. #ifdef S_IFLNK
  188. case S_IFLNK:
  189. /* stat is used, if it made here then the link is broken */
  190. if (ms->flags & MAGIC_ERROR) {
  191. file_error(ms, errno, "unreadable symlink `%s'", fn);
  192. return -1;
  193. }
  194. return 1;
  195. #endif
  196. #ifdef S_IFSOCK
  197. #ifndef __COHERENT__
  198. case S_IFSOCK:
  199. if (mime) {
  200. if (handle_mime(ms, mime, "socket") == -1)
  201. return -1;
  202. } else if (file_printf(ms, "%ssocket", COMMA) == -1)
  203. return -1;
  204. break;
  205. #endif
  206. #endif
  207. case S_IFREG:
  208. /*
  209. * regular file, check next possibility
  210. *
  211. * If stat() tells us the file has zero length, report here that
  212. * the file is empty, so we can skip all the work of opening and
  213. * reading the file.
  214. * But if the -s option has been given, we skip this
  215. * optimization, since on some systems, stat() reports zero
  216. * size for raw disk partitions. (If the block special device
  217. * really has zero length, the fact that it is empty will be
  218. * detected and reported correctly when we read the file.)
  219. */
  220. if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
  221. if (mime) {
  222. if (handle_mime(ms, mime, "x-empty") == -1)
  223. return -1;
  224. } else if (file_printf(ms, "%sempty", COMMA) == -1)
  225. return -1;
  226. break;
  227. }
  228. ret = 0;
  229. break;
  230. default:
  231. file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
  232. return -1;
  233. /*NOTREACHED*/
  234. }
  235. return ret;
  236. }