fsmagic.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.81 2019/07/16 13:30:32 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 HAVE_SYS_SYSMACROS_H
  47. # include <sys/sysmacros.h>
  48. #endif
  49. #ifdef MAJOR_IN_SYSMACROS
  50. # define HAVE_MAJOR
  51. #endif
  52. #if defined(major) && !defined(HAVE_MAJOR)
  53. /* Might be defined in sys/types.h. */
  54. # define HAVE_MAJOR
  55. #endif
  56. #ifdef WIN32
  57. # define WIN32_LEAN_AND_MEAN
  58. # include <windows.h>
  59. #endif
  60. #ifndef HAVE_MAJOR
  61. # define major(dev) (((dev) >> 8) & 0xff)
  62. # define minor(dev) ((dev) & 0xff)
  63. #endif
  64. #undef HAVE_MAJOR
  65. #ifdef PHP_WIN32
  66. # undef S_IFIFO
  67. #endif
  68. private int
  69. handle_mime(struct magic_set *ms, int mime, const char *str)
  70. {
  71. if ((mime & MAGIC_MIME_TYPE)) {
  72. if (file_printf(ms, "inode/%s", str) == -1)
  73. return -1;
  74. if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
  75. "; charset=") == -1)
  76. return -1;
  77. }
  78. if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
  79. return -1;
  80. return 0;
  81. }
  82. protected int
  83. file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb)
  84. {
  85. int ret, did = 0;
  86. int mime = ms->flags & MAGIC_MIME;
  87. int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
  88. if (fn == NULL)
  89. return 0;
  90. #define COMMA (did++ ? ", " : "")
  91. ret = php_sys_stat(fn, sb);
  92. if (ret) {
  93. if (ms->flags & MAGIC_ERROR) {
  94. file_error(ms, errno, "cannot stat `%s'", fn);
  95. return -1;
  96. }
  97. if (file_printf(ms, "cannot open `%s' (%s)",
  98. fn, strerror(errno)) == -1)
  99. return -1;
  100. return 0;
  101. }
  102. ret = 1;
  103. if (!mime && !silent) {
  104. #ifdef S_ISUID
  105. if (sb->st_mode & S_ISUID)
  106. if (file_printf(ms, "%ssetuid", COMMA) == -1)
  107. return -1;
  108. #endif
  109. #ifdef S_ISGID
  110. if (sb->st_mode & S_ISGID)
  111. if (file_printf(ms, "%ssetgid", COMMA) == -1)
  112. return -1;
  113. #endif
  114. #ifdef S_ISVTX
  115. if (sb->st_mode & S_ISVTX)
  116. if (file_printf(ms, "%ssticky", COMMA) == -1)
  117. return -1;
  118. #endif
  119. }
  120. switch (sb->st_mode & S_IFMT) {
  121. #ifndef PHP_WIN32
  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. ret = 0;
  131. break;
  132. }
  133. if (mime) {
  134. if (handle_mime(ms, mime, "chardevice") == -1)
  135. return -1;
  136. } else {
  137. # ifdef HAVE_STAT_ST_RDEV
  138. # ifdef dv_unit
  139. if (file_printf(ms, "%scharacter special (%d/%d/%d)",
  140. COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
  141. dv_subunit(sb->st_rdev)) == -1)
  142. return -1;
  143. # else
  144. if (file_printf(ms, "%scharacter special (%ld/%ld)",
  145. COMMA, (long)major(sb->st_rdev),
  146. (long)minor(sb->st_rdev)) == -1)
  147. return -1;
  148. # endif
  149. #else
  150. if (file_printf(ms, "%scharacter special", COMMA) == -1)
  151. return -1;
  152. #endif
  153. }
  154. return 1;
  155. # endif
  156. #endif
  157. #ifdef S_IFIFO
  158. case S_IFIFO:
  159. if((ms->flags & MAGIC_DEVICES) != 0)
  160. break;
  161. if (mime) {
  162. if (handle_mime(ms, mime, "fifo") == -1)
  163. return -1;
  164. } else if (silent) {
  165. } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
  166. return -1;
  167. break;
  168. #endif
  169. #ifdef S_IFDOOR
  170. case S_IFDOOR:
  171. if (mime) {
  172. if (handle_mime(ms, mime, "door") == -1)
  173. return -1;
  174. } else if (silent) {
  175. } else if (file_printf(ms, "%sdoor", COMMA) == -1)
  176. return -1;
  177. break;
  178. #endif
  179. #ifdef S_IFLNK
  180. case S_IFLNK:
  181. /* stat is used, if it made here then the link is broken */
  182. if (ms->flags & MAGIC_ERROR) {
  183. file_error(ms, errno, "unreadable symlink `%s'", fn);
  184. return -1;
  185. }
  186. return 1;
  187. #endif
  188. #ifdef S_IFSOCK
  189. #ifndef __COHERENT__
  190. case S_IFSOCK:
  191. if (mime) {
  192. if (handle_mime(ms, mime, "socket") == -1)
  193. return -1;
  194. } else if (silent) {
  195. } else if (file_printf(ms, "%ssocket", COMMA) == -1)
  196. return -1;
  197. break;
  198. #endif
  199. #endif
  200. case S_IFREG:
  201. /*
  202. * regular file, check next possibility
  203. *
  204. * If stat() tells us the file has zero length, report here that
  205. * the file is empty, so we can skip all the work of opening and
  206. * reading the file.
  207. * But if the -s option has been given, we skip this
  208. * optimization, since on some systems, stat() reports zero
  209. * size for raw disk partitions. (If the block special device
  210. * really has zero length, the fact that it is empty will be
  211. * detected and reported correctly when we read the file.)
  212. */
  213. if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
  214. if (mime) {
  215. if (handle_mime(ms, mime, "x-empty") == -1)
  216. return -1;
  217. } else if (silent) {
  218. } else if (file_printf(ms, "%sempty", COMMA) == -1)
  219. return -1;
  220. break;
  221. }
  222. ret = 0;
  223. break;
  224. default:
  225. file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
  226. return -1;
  227. /*NOTREACHED*/
  228. }
  229. if (!silent && !mime && did && ret == 0) {
  230. if (file_printf(ms, " ") == -1)
  231. return -1;
  232. }
  233. /*
  234. * If we were looking for extensions or apple (silent) it is not our
  235. * job to print here, so don't count this as a match.
  236. */
  237. if (ret == 1 && silent)
  238. return 0;
  239. return ret;
  240. }