magic.c 13 KB

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