magic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. #ifdef WIN32
  28. #include <windows.h>
  29. #include <shlwapi.h>
  30. #endif
  31. #include "file.h"
  32. #ifndef lint
  33. FILE_RCSID("@(#)$File: magic.c,v 1.95 2015/09/11 17:24:09 christos Exp $")
  34. #endif /* lint */
  35. #include "magic.h"
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #ifdef QUICK
  40. #include <sys/mman.h>
  41. #endif
  42. #ifdef HAVE_LIMITS_H
  43. #include <limits.h> /* for PIPE_BUF */
  44. #endif
  45. #if defined(HAVE_UTIMES)
  46. # include <sys/time.h>
  47. #elif defined(HAVE_UTIME)
  48. # if defined(HAVE_SYS_UTIME_H)
  49. # include <sys/utime.h>
  50. # elif defined(HAVE_UTIME_H)
  51. # include <utime.h>
  52. # endif
  53. #endif
  54. #ifdef HAVE_UNISTD_H
  55. #include <unistd.h> /* for read() */
  56. #endif
  57. #ifndef PIPE_BUF
  58. /* Get the PIPE_BUF from pathconf */
  59. #ifdef _PC_PIPE_BUF
  60. #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
  61. #else
  62. #define PIPE_BUF 512
  63. #endif
  64. #endif
  65. private void close_and_restore(const struct magic_set *, const char *, int,
  66. const struct stat *);
  67. private int unreadable_info(struct magic_set *, mode_t, const char *);
  68. private const char* get_default_magic(void);
  69. #ifndef COMPILE_ONLY
  70. private const char *file_or_fd(struct magic_set *, const char *, int);
  71. #endif
  72. #ifndef STDIN_FILENO
  73. #define STDIN_FILENO 0
  74. #endif
  75. #ifdef WIN32
  76. /* HINSTANCE of this shared library. Needed for get_default_magic() */
  77. static HINSTANCE _w32_dll_instance = NULL;
  78. static void
  79. _w32_append_path(char **hmagicpath, const char *fmt, ...)
  80. {
  81. char *tmppath;
  82. char *newpath;
  83. va_list ap;
  84. va_start(ap, fmt);
  85. if (vasprintf(&tmppath, fmt, ap) < 0) {
  86. va_end(ap);
  87. return;
  88. }
  89. va_end(ap);
  90. if (access(tmppath, R_OK) == -1)
  91. goto out;
  92. if (*hmagicpath == NULL) {
  93. *hmagicpath = tmppath;
  94. return;
  95. }
  96. if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0)
  97. goto out;
  98. free(*hmagicpath);
  99. free(tmppath);
  100. *hmagicpath = newpath;
  101. return;
  102. out:
  103. free(tmppath);
  104. }
  105. static void
  106. _w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
  107. {
  108. static const char *trypaths[] = {
  109. "%s/share/misc/magic.mgc",
  110. "%s/magic.mgc",
  111. };
  112. LPSTR dllpath;
  113. size_t sp;
  114. dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath));
  115. if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
  116. goto out;
  117. PathRemoveFileSpecA(dllpath);
  118. if (module) {
  119. char exepath[MAX_PATH];
  120. GetModuleFileNameA(NULL, exepath, MAX_PATH);
  121. PathRemoveFileSpecA(exepath);
  122. if (stricmp(exepath, dllpath) == 0)
  123. goto out;
  124. }
  125. sp = strlen(dllpath);
  126. if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
  127. _w32_append_path(hmagicpath,
  128. "%s/../share/misc/magic.mgc", dllpath);
  129. goto out;
  130. }
  131. for (sp = 0; sp < __arraycount(trypaths); sp++)
  132. _w32_append_path(hmagicpath, trypaths[sp], dllpath);
  133. out:
  134. free(dllpath);
  135. }
  136. /* Placate GCC by offering a sacrificial previous prototype */
  137. BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
  138. BOOL WINAPI
  139. DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
  140. LPVOID lpvReserved __attribute__((__unused__)))
  141. {
  142. if (fdwReason == DLL_PROCESS_ATTACH)
  143. _w32_dll_instance = hinstDLL;
  144. return TRUE;
  145. }
  146. #endif
  147. private const char *
  148. get_default_magic(void)
  149. {
  150. static const char hmagic[] = "/.magic/magic.mgc";
  151. static char *default_magic;
  152. char *home, *hmagicpath;
  153. #ifndef WIN32
  154. struct stat st;
  155. if (default_magic) {
  156. free(default_magic);
  157. default_magic = NULL;
  158. }
  159. if ((home = getenv("HOME")) == NULL)
  160. return MAGIC;
  161. if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
  162. return MAGIC;
  163. if (stat(hmagicpath, &st) == -1) {
  164. free(hmagicpath);
  165. if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
  166. return MAGIC;
  167. if (stat(hmagicpath, &st) == -1)
  168. goto out;
  169. if (S_ISDIR(st.st_mode)) {
  170. free(hmagicpath);
  171. if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
  172. return MAGIC;
  173. if (access(hmagicpath, R_OK) == -1)
  174. goto out;
  175. }
  176. }
  177. if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
  178. goto out;
  179. free(hmagicpath);
  180. return default_magic;
  181. out:
  182. default_magic = NULL;
  183. free(hmagicpath);
  184. return MAGIC;
  185. #else
  186. hmagicpath = NULL;
  187. if (default_magic) {
  188. free(default_magic);
  189. default_magic = NULL;
  190. }
  191. /* First, try to get a magic file from user-application data */
  192. if ((home = getenv("LOCALAPPDATA")) != NULL)
  193. _w32_append_path(&hmagicpath, "%s%s", home, hmagic);
  194. /* Second, try to get a magic file from the user profile data */
  195. if ((home = getenv("USERPROFILE")) != NULL)
  196. _w32_append_path(&hmagicpath,
  197. "%s/Local Settings/Application Data%s", home, hmagic);
  198. /* Third, try to get a magic file from Common Files */
  199. if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
  200. _w32_append_path(&hmagicpath, "%s%s", home, hmagic);
  201. /* Fourth, try to get magic file relative to exe location */
  202. _w32_get_magic_relative_to(&hmagicpath, NULL);
  203. /* Fifth, try to get magic file relative to dll location */
  204. _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
  205. /* Avoid MAGIC constant - it likely points to a file within MSys tree */
  206. default_magic = hmagicpath;
  207. return default_magic;
  208. #endif
  209. }
  210. public const char *
  211. magic_getpath(const char *magicfile, int action)
  212. {
  213. if (magicfile != NULL)
  214. return magicfile;
  215. magicfile = getenv("MAGIC");
  216. if (magicfile != NULL)
  217. return magicfile;
  218. return action == FILE_LOAD ? get_default_magic() : MAGIC;
  219. }
  220. public struct magic_set *
  221. magic_open(int flags)
  222. {
  223. return file_ms_alloc(flags);
  224. }
  225. private int
  226. unreadable_info(struct magic_set *ms, mode_t md, const char *file)
  227. {
  228. if (file) {
  229. /* We cannot open it, but we were able to stat it. */
  230. if (access(file, W_OK) == 0)
  231. if (file_printf(ms, "writable, ") == -1)
  232. return -1;
  233. if (access(file, X_OK) == 0)
  234. if (file_printf(ms, "executable, ") == -1)
  235. return -1;
  236. }
  237. if (S_ISREG(md))
  238. if (file_printf(ms, "regular file, ") == -1)
  239. return -1;
  240. if (file_printf(ms, "no read permission") == -1)
  241. return -1;
  242. return 0;
  243. }
  244. public void
  245. magic_close(struct magic_set *ms)
  246. {
  247. if (ms == NULL)
  248. return;
  249. file_ms_free(ms);
  250. }
  251. /*
  252. * load a magic file
  253. */
  254. public int
  255. magic_load(struct magic_set *ms, const char *magicfile)
  256. {
  257. if (ms == NULL)
  258. return -1;
  259. return file_apprentice(ms, magicfile, FILE_LOAD);
  260. }
  261. #ifndef COMPILE_ONLY
  262. /*
  263. * Install a set of compiled magic buffers.
  264. */
  265. public int
  266. magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
  267. size_t nbufs)
  268. {
  269. if (ms == NULL)
  270. return -1;
  271. return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
  272. }
  273. #endif
  274. public int
  275. magic_compile(struct magic_set *ms, const char *magicfile)
  276. {
  277. if (ms == NULL)
  278. return -1;
  279. return file_apprentice(ms, magicfile, FILE_COMPILE);
  280. }
  281. public int
  282. magic_check(struct magic_set *ms, const char *magicfile)
  283. {
  284. if (ms == NULL)
  285. return -1;
  286. return file_apprentice(ms, magicfile, FILE_CHECK);
  287. }
  288. public int
  289. magic_list(struct magic_set *ms, const char *magicfile)
  290. {
  291. if (ms == NULL)
  292. return -1;
  293. return file_apprentice(ms, magicfile, FILE_LIST);
  294. }
  295. private void
  296. close_and_restore(const struct magic_set *ms, const char *name, int fd,
  297. const struct stat *sb)
  298. {
  299. if (fd == STDIN_FILENO || name == NULL)
  300. return;
  301. (void) close(fd);
  302. if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
  303. /*
  304. * Try to restore access, modification times if read it.
  305. * This is really *bad* because it will modify the status
  306. * time of the file... And of course this will affect
  307. * backup programs
  308. */
  309. #ifdef HAVE_UTIMES
  310. struct timeval utsbuf[2];
  311. (void)memset(utsbuf, 0, sizeof(utsbuf));
  312. utsbuf[0].tv_sec = sb->st_atime;
  313. utsbuf[1].tv_sec = sb->st_mtime;
  314. (void) utimes(name, utsbuf); /* don't care if loses */
  315. #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
  316. struct utimbuf utbuf;
  317. (void)memset(&utbuf, 0, sizeof(utbuf));
  318. utbuf.actime = sb->st_atime;
  319. utbuf.modtime = sb->st_mtime;
  320. (void) utime(name, &utbuf); /* don't care if loses */
  321. #endif
  322. }
  323. }
  324. #ifndef COMPILE_ONLY
  325. /*
  326. * find type of descriptor
  327. */
  328. public const char *
  329. magic_descriptor(struct magic_set *ms, int fd)
  330. {
  331. if (ms == NULL)
  332. return NULL;
  333. return file_or_fd(ms, NULL, fd);
  334. }
  335. /*
  336. * find type of named file
  337. */
  338. public const char *
  339. magic_file(struct magic_set *ms, const char *inname)
  340. {
  341. if (ms == NULL)
  342. return NULL;
  343. return file_or_fd(ms, inname, STDIN_FILENO);
  344. }
  345. private const char *
  346. file_or_fd(struct magic_set *ms, const char *inname, int fd)
  347. {
  348. int rv = -1;
  349. unsigned char *buf;
  350. struct stat sb;
  351. ssize_t nbytes = 0; /* number of bytes read from a datafile */
  352. int ispipe = 0;
  353. off_t pos = (off_t)-1;
  354. if (file_reset(ms) == -1)
  355. goto out;
  356. /*
  357. * one extra for terminating '\0', and
  358. * some overlapping space for matches near EOF
  359. */
  360. #define SLOP (1 + sizeof(union VALUETYPE))
  361. if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
  362. return NULL;
  363. switch (file_fsmagic(ms, inname, &sb)) {
  364. case -1: /* error */
  365. goto done;
  366. case 0: /* nothing found */
  367. break;
  368. default: /* matched it and printed type */
  369. rv = 0;
  370. goto done;
  371. }
  372. #ifdef WIN32
  373. /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
  374. if (fd == STDIN_FILENO)
  375. _setmode(STDIN_FILENO, O_BINARY);
  376. #endif
  377. if (inname == NULL) {
  378. if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
  379. ispipe = 1;
  380. else
  381. pos = lseek(fd, (off_t)0, SEEK_CUR);
  382. } else {
  383. int flags = O_RDONLY|O_BINARY;
  384. int okstat = stat(inname, &sb) == 0;
  385. if (okstat && S_ISFIFO(sb.st_mode)) {
  386. #ifdef O_NONBLOCK
  387. flags |= O_NONBLOCK;
  388. #endif
  389. ispipe = 1;
  390. }
  391. errno = 0;
  392. if ((fd = open(inname, flags)) < 0) {
  393. #ifdef WIN32
  394. /*
  395. * Can't stat, can't open. It may have been opened in
  396. * fsmagic, so if the user doesn't have read permission,
  397. * allow it to say so; otherwise an error was probably
  398. * displayed in fsmagic.
  399. */
  400. if (!okstat && errno == EACCES) {
  401. sb.st_mode = S_IFBLK;
  402. okstat = 1;
  403. }
  404. #endif
  405. if (okstat &&
  406. unreadable_info(ms, sb.st_mode, inname) == -1)
  407. goto done;
  408. rv = 0;
  409. goto done;
  410. }
  411. #ifdef O_NONBLOCK
  412. if ((flags = fcntl(fd, F_GETFL)) != -1) {
  413. flags &= ~O_NONBLOCK;
  414. (void)fcntl(fd, F_SETFL, flags);
  415. }
  416. #endif
  417. }
  418. /*
  419. * try looking at the first HOWMANY bytes
  420. */
  421. if (ispipe) {
  422. ssize_t r = 0;
  423. while ((r = sread(fd, (void *)&buf[nbytes],
  424. (size_t)(HOWMANY - nbytes), 1)) > 0) {
  425. nbytes += r;
  426. if (r < PIPE_BUF) break;
  427. }
  428. if (nbytes == 0) {
  429. /* We can not read it, but we were able to stat it. */
  430. if (unreadable_info(ms, sb.st_mode, inname) == -1)
  431. goto done;
  432. rv = 0;
  433. goto done;
  434. }
  435. } else {
  436. /* Windows refuses to read from a big console buffer. */
  437. size_t howmany =
  438. #if defined(WIN32) && HOWMANY > 8 * 1024
  439. _isatty(fd) ? 8 * 1024 :
  440. #endif
  441. HOWMANY;
  442. if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
  443. if (inname == NULL && fd != STDIN_FILENO)
  444. file_error(ms, errno, "cannot read fd %d", fd);
  445. else
  446. file_error(ms, errno, "cannot read `%s'",
  447. inname == NULL ? "/dev/stdin" : inname);
  448. goto done;
  449. }
  450. }
  451. (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
  452. if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
  453. goto done;
  454. rv = 0;
  455. done:
  456. free(buf);
  457. if (pos != (off_t)-1)
  458. (void)lseek(fd, pos, SEEK_SET);
  459. close_and_restore(ms, inname, fd, &sb);
  460. out:
  461. return rv == 0 ? file_getbuffer(ms) : NULL;
  462. }
  463. public const char *
  464. magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
  465. {
  466. if (ms == NULL)
  467. return NULL;
  468. if (file_reset(ms) == -1)
  469. return NULL;
  470. /*
  471. * The main work is done here!
  472. * We have the file name and/or the data buffer to be identified.
  473. */
  474. if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
  475. return NULL;
  476. }
  477. return file_getbuffer(ms);
  478. }
  479. #endif
  480. public const char *
  481. magic_error(struct magic_set *ms)
  482. {
  483. if (ms == NULL)
  484. return "Magic database is not open";
  485. return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
  486. }
  487. public int
  488. magic_errno(struct magic_set *ms)
  489. {
  490. if (ms == NULL)
  491. return EINVAL;
  492. return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
  493. }
  494. public int
  495. magic_setflags(struct magic_set *ms, int flags)
  496. {
  497. if (ms == NULL)
  498. return -1;
  499. #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
  500. if (flags & MAGIC_PRESERVE_ATIME)
  501. return -1;
  502. #endif
  503. ms->flags = flags;
  504. return 0;
  505. }
  506. public int
  507. magic_version(void)
  508. {
  509. return MAGIC_VERSION;
  510. }
  511. public int
  512. magic_setparam(struct magic_set *ms, int param, const void *val)
  513. {
  514. switch (param) {
  515. case MAGIC_PARAM_INDIR_MAX:
  516. ms->indir_max = (uint16_t)*(const size_t *)val;
  517. return 0;
  518. case MAGIC_PARAM_NAME_MAX:
  519. ms->name_max = (uint16_t)*(const size_t *)val;
  520. return 0;
  521. case MAGIC_PARAM_ELF_PHNUM_MAX:
  522. ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
  523. return 0;
  524. case MAGIC_PARAM_ELF_SHNUM_MAX:
  525. ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
  526. return 0;
  527. case MAGIC_PARAM_ELF_NOTES_MAX:
  528. ms->elf_notes_max = (uint16_t)*(const size_t *)val;
  529. return 0;
  530. case MAGIC_PARAM_REGEX_MAX:
  531. ms->elf_notes_max = (uint16_t)*(const size_t *)val;
  532. return 0;
  533. default:
  534. errno = EINVAL;
  535. return -1;
  536. }
  537. }
  538. public int
  539. magic_getparam(struct magic_set *ms, int param, void *val)
  540. {
  541. switch (param) {
  542. case MAGIC_PARAM_INDIR_MAX:
  543. *(size_t *)val = ms->indir_max;
  544. return 0;
  545. case MAGIC_PARAM_NAME_MAX:
  546. *(size_t *)val = ms->name_max;
  547. return 0;
  548. case MAGIC_PARAM_ELF_PHNUM_MAX:
  549. *(size_t *)val = ms->elf_phnum_max;
  550. return 0;
  551. case MAGIC_PARAM_ELF_SHNUM_MAX:
  552. *(size_t *)val = ms->elf_shnum_max;
  553. return 0;
  554. case MAGIC_PARAM_ELF_NOTES_MAX:
  555. *(size_t *)val = ms->elf_notes_max;
  556. return 0;
  557. case MAGIC_PARAM_REGEX_MAX:
  558. *(size_t *)val = ms->regex_max;
  559. return 0;
  560. default:
  561. errno = EINVAL;
  562. return -1;
  563. }
  564. }