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