magic.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) Christos Zoulas 2003.
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice immediately at the beginning of the file, without modification,
  10. * this list of conditions, and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "file.h"
  28. #ifndef lint
  29. FILE_RCSID("@(#)$File: magic.c,v 1.114 2021/02/05 21:33:49 christos Exp $")
  30. #endif /* lint */
  31. #include "magic.h"
  32. #include <stdlib.h>
  33. #ifdef PHP_WIN32
  34. #include "win32/unistd.h"
  35. #else
  36. #include <unistd.h>
  37. #endif
  38. #include <string.h>
  39. #include "config.h"
  40. #ifdef PHP_WIN32
  41. #include <shlwapi.h>
  42. #endif
  43. #include <limits.h> /* for PIPE_BUF */
  44. #if defined(HAVE_UTIMES)
  45. # include <sys/time.h>
  46. #elif defined(HAVE_UTIME)
  47. # if defined(HAVE_SYS_UTIME_H)
  48. # include <sys/utime.h>
  49. # elif defined(HAVE_UTIME_H)
  50. # include <utime.h>
  51. # endif
  52. #endif
  53. #ifdef HAVE_UNISTD_H
  54. #include <unistd.h> /* for read() */
  55. #endif
  56. #ifndef PIPE_BUF
  57. /* Get the PIPE_BUF from pathconf */
  58. #ifdef _PC_PIPE_BUF
  59. #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
  60. #else
  61. #define PIPE_BUF 512
  62. #endif
  63. #endif
  64. #ifdef PHP_WIN32
  65. # undef S_IFLNK
  66. # undef S_IFIFO
  67. #endif
  68. private int unreadable_info(struct magic_set *, mode_t, const char *);
  69. private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
  70. #ifndef STDIN_FILENO
  71. #define STDIN_FILENO 0
  72. #endif
  73. public struct magic_set *
  74. magic_open(int flags)
  75. {
  76. return file_ms_alloc(flags);
  77. }
  78. private int
  79. unreadable_info(struct magic_set *ms, mode_t md, const char *file)
  80. {
  81. if (file) {
  82. /* We cannot open it, but we were able to stat it. */
  83. if (access(file, W_OK) == 0)
  84. if (file_printf(ms, "writable, ") == -1)
  85. return -1;
  86. if (access(file, X_OK) == 0)
  87. if (file_printf(ms, "executable, ") == -1)
  88. return -1;
  89. }
  90. if (S_ISREG(md))
  91. if (file_printf(ms, "regular file, ") == -1)
  92. return -1;
  93. if (file_printf(ms, "no read permission") == -1)
  94. return -1;
  95. return 0;
  96. }
  97. public void
  98. magic_close(struct magic_set *ms)
  99. {
  100. if (ms == NULL)
  101. return;
  102. file_ms_free(ms);
  103. }
  104. /*
  105. * load a magic file
  106. */
  107. public int
  108. magic_load(struct magic_set *ms, const char *magicfile)
  109. {
  110. if (ms == NULL)
  111. return -1;
  112. return file_apprentice(ms, magicfile, FILE_LOAD);
  113. }
  114. public int
  115. magic_compile(struct magic_set *ms, const char *magicfile)
  116. {
  117. if (ms == NULL)
  118. return -1;
  119. return file_apprentice(ms, magicfile, FILE_COMPILE);
  120. }
  121. public int
  122. magic_check(struct magic_set *ms, const char *magicfile)
  123. {
  124. if (ms == NULL)
  125. return -1;
  126. return file_apprentice(ms, magicfile, FILE_CHECK);
  127. }
  128. public int
  129. magic_list(struct magic_set *ms, const char *magicfile)
  130. {
  131. if (ms == NULL)
  132. return -1;
  133. return file_apprentice(ms, magicfile, FILE_LIST);
  134. }
  135. #ifndef COMPILE_ONLY
  136. /*
  137. * find type of descriptor
  138. */
  139. public const char *
  140. magic_descriptor(struct magic_set *ms, int fd)
  141. {
  142. if (ms == NULL)
  143. return NULL;
  144. return file_or_stream(ms, NULL, NULL);
  145. }
  146. /*
  147. * find type of named file
  148. */
  149. public const char *
  150. magic_file(struct magic_set *ms, const char *inname)
  151. {
  152. if (ms == NULL)
  153. return NULL;
  154. return file_or_stream(ms, inname, NULL);
  155. }
  156. public const char *
  157. magic_stream(struct magic_set *ms, php_stream *stream)
  158. {
  159. if (ms == NULL)
  160. return NULL;
  161. return file_or_stream(ms, NULL, stream);
  162. }
  163. private const char *
  164. file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
  165. {
  166. int rv = -1;
  167. unsigned char *buf;
  168. zend_stat_t sb;
  169. ssize_t nbytes = 0; /* number of bytes read from a datafile */
  170. int no_in_stream = 0;
  171. if (file_reset(ms, 1) == -1)
  172. goto out;
  173. /*
  174. * one extra for terminating '\0', and
  175. * some overlapping space for matches near EOF
  176. */
  177. #define SLOP (1 + sizeof(union VALUETYPE))
  178. if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
  179. return NULL;
  180. switch (file_fsmagic(ms, inname, &sb)) {
  181. case -1: /* error */
  182. goto done;
  183. case 0: /* nothing found */
  184. break;
  185. default: /* matched it and printed type */
  186. rv = 0;
  187. goto done;
  188. }
  189. errno = 0;
  190. if (inname && !stream) {
  191. no_in_stream = 1;
  192. stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
  193. if (!stream) {
  194. if (unreadable_info(ms, sb.st_mode, inname) == -1)
  195. goto done;
  196. rv = -1;
  197. goto done;
  198. }
  199. }
  200. php_stream_statbuf ssb;
  201. if (php_stream_stat(stream, &ssb) < 0) {
  202. if (ms->flags & MAGIC_ERROR) {
  203. file_error(ms, errno, "cannot stat `%s'", inname);
  204. rv = -1;
  205. goto done;
  206. }
  207. }
  208. memcpy(&sb, &ssb.sb, sizeof(zend_stat_t));
  209. /*
  210. * try looking at the first ms->bytes_max bytes
  211. */
  212. if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
  213. file_error(ms, errno, "cannot read `%s'", inname);
  214. goto done;
  215. }
  216. (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
  217. if (file_buffer(ms, stream, &sb, inname, buf, CAST(size_t, nbytes)) == -1)
  218. goto done;
  219. rv = 0;
  220. done:
  221. efree(buf);
  222. if (no_in_stream && stream) {
  223. php_stream_close(stream);
  224. }
  225. out:
  226. return rv == 0 ? file_getbuffer(ms) : NULL;
  227. }
  228. public const char *
  229. magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
  230. {
  231. if (ms == NULL)
  232. return NULL;
  233. if (file_reset(ms, 1) == -1)
  234. return NULL;
  235. /*
  236. * The main work is done here!
  237. * We have the file name and/or the data buffer to be identified.
  238. */
  239. if (file_buffer(ms, NULL, NULL, NULL, buf, nb) == -1) {
  240. return NULL;
  241. }
  242. return file_getbuffer(ms);
  243. }
  244. #endif
  245. public const char *
  246. magic_error(struct magic_set *ms)
  247. {
  248. if (ms == NULL)
  249. return "Magic database is not open";
  250. return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
  251. }
  252. public int
  253. magic_errno(struct magic_set *ms)
  254. {
  255. if (ms == NULL)
  256. return EINVAL;
  257. return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
  258. }
  259. public int
  260. magic_getflags(struct magic_set *ms)
  261. {
  262. if (ms == NULL)
  263. return -1;
  264. return ms->flags;
  265. }
  266. public int
  267. magic_setflags(struct magic_set *ms, int flags)
  268. {
  269. if (ms == NULL)
  270. return -1;
  271. #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
  272. if (flags & MAGIC_PRESERVE_ATIME)
  273. return -1;
  274. #endif
  275. ms->flags = flags;
  276. return 0;
  277. }
  278. public int
  279. magic_version(void)
  280. {
  281. return MAGIC_VERSION;
  282. }
  283. public int
  284. magic_setparam(struct magic_set *ms, int param, const void *val)
  285. {
  286. if (ms == NULL)
  287. return -1;
  288. switch (param) {
  289. case MAGIC_PARAM_INDIR_MAX:
  290. ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val));
  291. return 0;
  292. case MAGIC_PARAM_NAME_MAX:
  293. ms->name_max = CAST(uint16_t, *CAST(const size_t *, val));
  294. return 0;
  295. case MAGIC_PARAM_ELF_PHNUM_MAX:
  296. ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val));
  297. return 0;
  298. case MAGIC_PARAM_ELF_SHNUM_MAX:
  299. ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
  300. return 0;
  301. case MAGIC_PARAM_ELF_NOTES_MAX:
  302. ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
  303. return 0;
  304. case MAGIC_PARAM_REGEX_MAX:
  305. ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val));
  306. return 0;
  307. case MAGIC_PARAM_BYTES_MAX:
  308. ms->bytes_max = *CAST(const size_t *, val);
  309. return 0;
  310. case MAGIC_PARAM_ENCODING_MAX:
  311. ms->encoding_max = *CAST(const size_t *, val);
  312. return 0;
  313. default:
  314. errno = EINVAL;
  315. return -1;
  316. }
  317. }
  318. public int
  319. magic_getparam(struct magic_set *ms, int param, void *val)
  320. {
  321. if (ms == NULL)
  322. return -1;
  323. switch (param) {
  324. case MAGIC_PARAM_INDIR_MAX:
  325. *CAST(size_t *, val) = ms->indir_max;
  326. return 0;
  327. case MAGIC_PARAM_NAME_MAX:
  328. *CAST(size_t *, val) = ms->name_max;
  329. return 0;
  330. case MAGIC_PARAM_ELF_PHNUM_MAX:
  331. *CAST(size_t *, val) = ms->elf_phnum_max;
  332. return 0;
  333. case MAGIC_PARAM_ELF_SHNUM_MAX:
  334. *CAST(size_t *, val) = ms->elf_shnum_max;
  335. return 0;
  336. case MAGIC_PARAM_ELF_NOTES_MAX:
  337. *CAST(size_t *, val) = ms->elf_notes_max;
  338. return 0;
  339. case MAGIC_PARAM_REGEX_MAX:
  340. *CAST(size_t *, val) = ms->regex_max;
  341. return 0;
  342. case MAGIC_PARAM_BYTES_MAX:
  343. *CAST(size_t *, val) = ms->bytes_max;
  344. return 0;
  345. case MAGIC_PARAM_ENCODING_MAX:
  346. *CAST(size_t *, val) = ms->encoding_max;
  347. return 0;
  348. default:
  349. errno = EINVAL;
  350. return -1;
  351. }
  352. }