magic.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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.102 2017/08/28 13:39:18 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 void close_and_restore(const struct magic_set *, const char *, int,
  69. const zend_stat_t *);
  70. private int unreadable_info(struct magic_set *, mode_t, const char *);
  71. #if 0
  72. private const char* get_default_magic(void);
  73. #endif
  74. private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
  75. #ifndef STDIN_FILENO
  76. #define STDIN_FILENO 0
  77. #endif
  78. public struct magic_set *
  79. magic_open(int flags)
  80. {
  81. return file_ms_alloc(flags);
  82. }
  83. private int
  84. unreadable_info(struct magic_set *ms, mode_t md, const char *file)
  85. {
  86. if (file) {
  87. /* We cannot open it, but we were able to stat it. */
  88. if (access(file, W_OK) == 0)
  89. if (file_printf(ms, "writable, ") == -1)
  90. return -1;
  91. if (access(file, X_OK) == 0)
  92. if (file_printf(ms, "executable, ") == -1)
  93. return -1;
  94. }
  95. if (S_ISREG(md))
  96. if (file_printf(ms, "regular file, ") == -1)
  97. return -1;
  98. if (file_printf(ms, "no read permission") == -1)
  99. return -1;
  100. return 0;
  101. }
  102. public void
  103. magic_close(struct magic_set *ms)
  104. {
  105. if (ms == NULL)
  106. return;
  107. file_ms_free(ms);
  108. }
  109. /*
  110. * load a magic file
  111. */
  112. public int
  113. magic_load(struct magic_set *ms, const char *magicfile)
  114. {
  115. if (ms == NULL)
  116. return -1;
  117. return file_apprentice(ms, magicfile, FILE_LOAD);
  118. }
  119. public int
  120. magic_compile(struct magic_set *ms, const char *magicfile)
  121. {
  122. if (ms == NULL)
  123. return -1;
  124. return file_apprentice(ms, magicfile, FILE_COMPILE);
  125. }
  126. public int
  127. magic_check(struct magic_set *ms, const char *magicfile)
  128. {
  129. if (ms == NULL)
  130. return -1;
  131. return file_apprentice(ms, magicfile, FILE_CHECK);
  132. }
  133. public int
  134. magic_list(struct magic_set *ms, const char *magicfile)
  135. {
  136. if (ms == NULL)
  137. return -1;
  138. return file_apprentice(ms, magicfile, FILE_LIST);
  139. }
  140. private void
  141. close_and_restore(const struct magic_set *ms, const char *name, int fd,
  142. const zend_stat_t *sb)
  143. {
  144. if (fd == STDIN_FILENO || name == NULL)
  145. return;
  146. (void) close(fd);
  147. if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
  148. /*
  149. * Try to restore access, modification times if read it.
  150. * This is really *bad* because it will modify the status
  151. * time of the file... And of course this will affect
  152. * backup programs
  153. */
  154. #ifdef HAVE_UTIMES
  155. struct timeval utsbuf[2];
  156. (void)memset(utsbuf, 0, sizeof(utsbuf));
  157. utsbuf[0].tv_sec = sb->st_atime;
  158. utsbuf[1].tv_sec = sb->st_mtime;
  159. (void) utimes(name, utsbuf); /* don't care if loses */
  160. #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
  161. struct utimbuf utbuf;
  162. (void)memset(&utbuf, 0, sizeof(utbuf));
  163. utbuf.actime = sb->st_atime;
  164. utbuf.modtime = sb->st_mtime;
  165. (void) utime(name, &utbuf); /* don't care if loses */
  166. #endif
  167. }
  168. }
  169. /*
  170. * find type of descriptor
  171. */
  172. public const char *
  173. magic_descriptor(struct magic_set *ms, int fd)
  174. {
  175. if (ms == NULL)
  176. return NULL;
  177. return file_or_stream(ms, NULL, NULL);
  178. }
  179. /*
  180. * find type of named file
  181. */
  182. public const char *
  183. magic_file(struct magic_set *ms, const char *inname)
  184. {
  185. if (ms == NULL)
  186. return NULL;
  187. return file_or_stream(ms, inname, NULL);
  188. }
  189. public const char *
  190. magic_stream(struct magic_set *ms, php_stream *stream)
  191. {
  192. if (ms == NULL)
  193. return NULL;
  194. return file_or_stream(ms, NULL, stream);
  195. }
  196. private const char *
  197. file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
  198. {
  199. int rv = -1;
  200. unsigned char *buf;
  201. zend_stat_t sb;
  202. ssize_t nbytes = 0; /* number of bytes read from a datafile */
  203. int no_in_stream = 0;
  204. if (file_reset(ms, 1) == -1)
  205. goto out;
  206. if (!inname && !stream) {
  207. return NULL;
  208. }
  209. /*
  210. * one extra for terminating '\0', and
  211. * some overlapping space for matches near EOF
  212. */
  213. #define SLOP (1 + sizeof(union VALUETYPE))
  214. if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
  215. return NULL;
  216. switch (file_fsmagic(ms, inname, &sb, stream)) {
  217. case -1: /* error */
  218. goto done;
  219. case 0: /* nothing found */
  220. break;
  221. default: /* matched it and printed type */
  222. rv = 0;
  223. goto done;
  224. }
  225. errno = 0;
  226. if (!stream && inname) {
  227. no_in_stream = 1;
  228. stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
  229. }
  230. if (!stream) {
  231. if (unreadable_info(ms, sb.st_mode, inname) == -1)
  232. goto done;
  233. rv = 0;
  234. goto done;
  235. }
  236. #ifdef O_NONBLOCK
  237. /* we should be already be in non blocking mode for network socket */
  238. #endif
  239. /*
  240. * try looking at the first ms->bytes_max bytes
  241. */
  242. if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
  243. file_error(ms, errno, "cannot read `%s'", inname);
  244. goto done;
  245. }
  246. (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
  247. if (file_buffer(ms, stream, inname, buf, (size_t)nbytes) == -1)
  248. goto done;
  249. rv = 0;
  250. done:
  251. efree(buf);
  252. if (no_in_stream && stream) {
  253. php_stream_close(stream);
  254. }
  255. out:
  256. return rv == 0 ? file_getbuffer(ms) : NULL;
  257. }
  258. public const char *
  259. magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
  260. {
  261. if (ms == NULL)
  262. return NULL;
  263. if (file_reset(ms, 1) == -1)
  264. return NULL;
  265. /*
  266. * The main work is done here!
  267. * We have the file name and/or the data buffer to be identified.
  268. */
  269. if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
  270. return NULL;
  271. }
  272. return file_getbuffer(ms);
  273. }
  274. public const char *
  275. magic_error(struct magic_set *ms)
  276. {
  277. if (ms == NULL)
  278. return "Magic database is not open";
  279. return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
  280. }
  281. public int
  282. magic_errno(struct magic_set *ms)
  283. {
  284. if (ms == NULL)
  285. return EINVAL;
  286. return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
  287. }
  288. public int
  289. magic_getflags(struct magic_set *ms)
  290. {
  291. if (ms == NULL)
  292. return -1;
  293. return ms->flags;
  294. }
  295. public int
  296. magic_setflags(struct magic_set *ms, int flags)
  297. {
  298. if (ms == NULL)
  299. return -1;
  300. #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
  301. if (flags & MAGIC_PRESERVE_ATIME)
  302. return -1;
  303. #endif
  304. ms->flags = flags;
  305. return 0;
  306. }
  307. public int
  308. magic_version(void)
  309. {
  310. return MAGIC_VERSION;
  311. }
  312. public int
  313. magic_setparam(struct magic_set *ms, int param, const void *val)
  314. {
  315. switch (param) {
  316. case MAGIC_PARAM_INDIR_MAX:
  317. ms->indir_max = (uint16_t)*(const size_t *)val;
  318. return 0;
  319. case MAGIC_PARAM_NAME_MAX:
  320. ms->name_max = (uint16_t)*(const size_t *)val;
  321. return 0;
  322. case MAGIC_PARAM_ELF_PHNUM_MAX:
  323. ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
  324. return 0;
  325. case MAGIC_PARAM_ELF_SHNUM_MAX:
  326. ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
  327. return 0;
  328. case MAGIC_PARAM_ELF_NOTES_MAX:
  329. ms->elf_notes_max = (uint16_t)*(const size_t *)val;
  330. return 0;
  331. case MAGIC_PARAM_REGEX_MAX:
  332. ms->elf_notes_max = (uint16_t)*(const size_t *)val;
  333. return 0;
  334. case MAGIC_PARAM_BYTES_MAX:
  335. ms->bytes_max = *(const size_t *)val;
  336. return 0;
  337. default:
  338. errno = EINVAL;
  339. return -1;
  340. }
  341. }
  342. public int
  343. magic_getparam(struct magic_set *ms, int param, void *val)
  344. {
  345. switch (param) {
  346. case MAGIC_PARAM_INDIR_MAX:
  347. *(size_t *)val = ms->indir_max;
  348. return 0;
  349. case MAGIC_PARAM_NAME_MAX:
  350. *(size_t *)val = ms->name_max;
  351. return 0;
  352. case MAGIC_PARAM_ELF_PHNUM_MAX:
  353. *(size_t *)val = ms->elf_phnum_max;
  354. return 0;
  355. case MAGIC_PARAM_ELF_SHNUM_MAX:
  356. *(size_t *)val = ms->elf_shnum_max;
  357. return 0;
  358. case MAGIC_PARAM_ELF_NOTES_MAX:
  359. *(size_t *)val = ms->elf_notes_max;
  360. return 0;
  361. case MAGIC_PARAM_REGEX_MAX:
  362. *(size_t *)val = ms->regex_max;
  363. return 0;
  364. case MAGIC_PARAM_BYTES_MAX:
  365. *(size_t *)val = ms->bytes_max;
  366. return 0;
  367. default:
  368. errno = EINVAL;
  369. return -1;
  370. }
  371. }