magic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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.74 2011/05/26 01:27:59 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 free_mlist(struct mlist *);
  66. private void close_and_restore(const struct magic_set *, const char *, int,
  67. const struct stat *);
  68. private int unreadable_info(struct magic_set *, mode_t, const char *);
  69. private const char* get_default_magic(void);
  70. #ifndef COMPILE_ONLY
  71. private const char *file_or_fd(struct magic_set *, const char *, int);
  72. #endif
  73. #ifndef STDIN_FILENO
  74. #define STDIN_FILENO 0
  75. #endif
  76. private const char *
  77. get_default_magic(void)
  78. {
  79. static const char hmagic[] = "/.magic/magic.mgc";
  80. static char *default_magic;
  81. char *home, *hmagicpath;
  82. #ifndef WIN32
  83. struct stat st;
  84. if (default_magic) {
  85. free(default_magic);
  86. default_magic = NULL;
  87. }
  88. if ((home = getenv("HOME")) == NULL)
  89. return MAGIC;
  90. if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
  91. return MAGIC;
  92. if (stat(hmagicpath, &st) == -1)
  93. goto out;
  94. if (S_ISDIR(st.st_mode)) {
  95. free(hmagicpath);
  96. if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
  97. return MAGIC;
  98. if (access(hmagicpath, R_OK) == -1)
  99. goto out;
  100. }
  101. if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
  102. goto out;
  103. free(hmagicpath);
  104. return default_magic;
  105. out:
  106. default_magic = NULL;
  107. free(hmagicpath);
  108. return MAGIC;
  109. #else
  110. char *hmagicp = hmagicpath;
  111. char *tmppath = NULL;
  112. #define APPENDPATH() \
  113. do { \
  114. if (tmppath && access(tmppath, R_OK) != -1) { \
  115. if (hmagicpath == NULL) \
  116. hmagicpath = tmppath; \
  117. else { \
  118. if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
  119. PATHSEP, tmppath) >= 0) { \
  120. free(hmagicpath); \
  121. hmagicpath = hmagicp; \
  122. } \
  123. free(tmppath); \
  124. } \
  125. tmppath = NULL; \
  126. } \
  127. } while (/*CONSTCOND*/0)
  128. if (default_magic) {
  129. free(default_magic);
  130. default_magic = NULL;
  131. }
  132. /* First, try to get user-specific magic file */
  133. if ((home = getenv("LOCALAPPDATA")) == NULL) {
  134. if ((home = getenv("USERPROFILE")) != NULL)
  135. if (asprintf(&tmppath,
  136. "%s/Local Settings/Application Data%s", home,
  137. hmagic) < 0)
  138. tmppath = NULL;
  139. } else {
  140. if (asprintf(&tmppath, "%s%s", home, hmagic) < 0)
  141. tmppath = NULL;
  142. }
  143. APPENDPATH();
  144. /* Second, try to get a magic file from Common Files */
  145. if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
  146. if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0)
  147. APPENDPATH();
  148. }
  149. /* Third, try to get magic file relative to dll location */
  150. LPTSTR dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1));
  151. dllpath[MAX_PATH] = 0; /* just in case long path gets truncated and not null terminated */
  152. if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){
  153. PathRemoveFileSpecA(dllpath);
  154. if (strlen(dllpath) > 3 &&
  155. stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
  156. if (asprintf(&tmppath,
  157. "%s/../share/misc/magic.mgc", dllpath) >= 0)
  158. APPENDPATH();
  159. } else {
  160. if (asprintf(&tmppath,
  161. "%s/share/misc/magic.mgc", dllpath) >= 0)
  162. APPENDPATH();
  163. else if (asprintf(&tmppath,
  164. "%s/magic.mgc", dllpath) >= 0)
  165. APPENDPATH();
  166. }
  167. }
  168. /* Don't put MAGIC constant - it likely points to a file within MSys
  169. tree */
  170. default_magic = hmagicpath;
  171. return default_magic;
  172. #endif
  173. }
  174. public const char *
  175. magic_getpath(const char *magicfile, int action)
  176. {
  177. if (magicfile != NULL)
  178. return magicfile;
  179. magicfile = getenv("MAGIC");
  180. if (magicfile != NULL)
  181. return magicfile;
  182. return action == FILE_LOAD ? get_default_magic() : MAGIC;
  183. }
  184. public struct magic_set *
  185. magic_open(int flags)
  186. {
  187. struct magic_set *ms;
  188. size_t len;
  189. if ((ms = CAST(struct magic_set *, calloc((size_t)1,
  190. sizeof(struct magic_set)))) == NULL)
  191. return NULL;
  192. if (magic_setflags(ms, flags) == -1) {
  193. errno = EINVAL;
  194. goto free;
  195. }
  196. ms->o.buf = ms->o.pbuf = NULL;
  197. len = (ms->c.len = 10) * sizeof(*ms->c.li);
  198. if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
  199. goto free;
  200. ms->event_flags = 0;
  201. ms->error = -1;
  202. ms->mlist = NULL;
  203. ms->file = "unknown";
  204. ms->line = 0;
  205. return ms;
  206. free:
  207. free(ms);
  208. return NULL;
  209. }
  210. private void
  211. free_mlist(struct mlist *mlist)
  212. {
  213. struct mlist *ml;
  214. if (mlist == NULL)
  215. return;
  216. for (ml = mlist->next; ml != mlist;) {
  217. struct mlist *next = ml->next;
  218. struct magic *mg = ml->magic;
  219. file_delmagic(mg, ml->mapped, ml->nmagic);
  220. free(ml);
  221. ml = next;
  222. }
  223. free(ml);
  224. }
  225. private int
  226. unreadable_info(struct magic_set *ms, mode_t md, const char *file)
  227. {
  228. /* We cannot open it, but we were able to stat it. */
  229. if (access(file, W_OK) == 0)
  230. if (file_printf(ms, "writable, ") == -1)
  231. return -1;
  232. if (access(file, X_OK) == 0)
  233. if (file_printf(ms, "executable, ") == -1)
  234. return -1;
  235. if (S_ISREG(md))
  236. if (file_printf(ms, "regular file, ") == -1)
  237. return -1;
  238. if (file_printf(ms, "no read permission") == -1)
  239. return -1;
  240. return 0;
  241. }
  242. public void
  243. magic_close(struct magic_set *ms)
  244. {
  245. free_mlist(ms->mlist);
  246. free(ms->o.pbuf);
  247. free(ms->o.buf);
  248. free(ms->c.li);
  249. 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. struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
  258. if (ml) {
  259. free_mlist(ms->mlist);
  260. ms->mlist = ml;
  261. return 0;
  262. }
  263. return -1;
  264. }
  265. public int
  266. magic_compile(struct magic_set *ms, const char *magicfile)
  267. {
  268. struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
  269. free_mlist(ml);
  270. return ml ? 0 : -1;
  271. }
  272. public int
  273. magic_check(struct magic_set *ms, const char *magicfile)
  274. {
  275. struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
  276. free_mlist(ml);
  277. return ml ? 0 : -1;
  278. }
  279. public int
  280. magic_list(struct magic_set *ms, const char *magicfile)
  281. {
  282. struct mlist *ml = file_apprentice(ms, magicfile, FILE_LIST);
  283. free_mlist(ml);
  284. return ml ? 0 : -1;
  285. }
  286. private void
  287. close_and_restore(const struct magic_set *ms, const char *name, int fd,
  288. const struct stat *sb)
  289. {
  290. if (fd == STDIN_FILENO)
  291. return;
  292. (void) close(fd);
  293. if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
  294. /*
  295. * Try to restore access, modification times if read it.
  296. * This is really *bad* because it will modify the status
  297. * time of the file... And of course this will affect
  298. * backup programs
  299. */
  300. #ifdef HAVE_UTIMES
  301. struct timeval utsbuf[2];
  302. (void)memset(utsbuf, 0, sizeof(utsbuf));
  303. utsbuf[0].tv_sec = sb->st_atime;
  304. utsbuf[1].tv_sec = sb->st_mtime;
  305. (void) utimes(name, utsbuf); /* don't care if loses */
  306. #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
  307. struct utimbuf utbuf;
  308. (void)memset(&utbuf, 0, sizeof(utbuf));
  309. utbuf.actime = sb->st_atime;
  310. utbuf.modtime = sb->st_mtime;
  311. (void) utime(name, &utbuf); /* don't care if loses */
  312. #endif
  313. }
  314. }
  315. #ifndef COMPILE_ONLY
  316. /*
  317. * find type of descriptor
  318. */
  319. public const char *
  320. magic_descriptor(struct magic_set *ms, int fd)
  321. {
  322. return file_or_fd(ms, NULL, fd);
  323. }
  324. /*
  325. * find type of named file
  326. */
  327. public const char *
  328. magic_file(struct magic_set *ms, const char *inname)
  329. {
  330. return file_or_fd(ms, inname, STDIN_FILENO);
  331. }
  332. private const char *
  333. file_or_fd(struct magic_set *ms, const char *inname, int fd)
  334. {
  335. int rv = -1;
  336. unsigned char *buf;
  337. struct stat sb;
  338. ssize_t nbytes = 0; /* number of bytes read from a datafile */
  339. int ispipe = 0;
  340. /*
  341. * one extra for terminating '\0', and
  342. * some overlapping space for matches near EOF
  343. */
  344. #define SLOP (1 + sizeof(union VALUETYPE))
  345. if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
  346. return NULL;
  347. if (file_reset(ms) == -1)
  348. goto done;
  349. switch (file_fsmagic(ms, inname, &sb)) {
  350. case -1: /* error */
  351. goto done;
  352. case 0: /* nothing found */
  353. break;
  354. default: /* matched it and printed type */
  355. rv = 0;
  356. goto done;
  357. }
  358. if (inname == NULL) {
  359. if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
  360. ispipe = 1;
  361. } else {
  362. int flags = O_RDONLY|O_BINARY;
  363. if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
  364. #ifdef O_NONBLOCK
  365. flags |= O_NONBLOCK;
  366. #endif
  367. ispipe = 1;
  368. }
  369. errno = 0;
  370. if ((fd = open(inname, flags)) < 0) {
  371. if (unreadable_info(ms, sb.st_mode, inname) == -1)
  372. goto done;
  373. rv = 0;
  374. goto done;
  375. }
  376. #ifdef O_NONBLOCK
  377. if ((flags = fcntl(fd, F_GETFL)) != -1) {
  378. flags &= ~O_NONBLOCK;
  379. (void)fcntl(fd, F_SETFL, flags);
  380. }
  381. #endif
  382. }
  383. /*
  384. * try looking at the first HOWMANY bytes
  385. */
  386. if (ispipe) {
  387. ssize_t r = 0;
  388. while ((r = sread(fd, (void *)&buf[nbytes],
  389. (size_t)(HOWMANY - nbytes), 1)) > 0) {
  390. nbytes += r;
  391. if (r < PIPE_BUF) break;
  392. }
  393. if (nbytes == 0) {
  394. /* We can not read it, but we were able to stat it. */
  395. if (unreadable_info(ms, sb.st_mode, inname) == -1)
  396. goto done;
  397. rv = 0;
  398. goto done;
  399. }
  400. } else {
  401. if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
  402. file_error(ms, errno, "cannot read `%s'", inname);
  403. goto done;
  404. }
  405. }
  406. (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
  407. if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
  408. goto done;
  409. rv = 0;
  410. done:
  411. free(buf);
  412. close_and_restore(ms, inname, fd, &sb);
  413. return rv == 0 ? file_getbuffer(ms) : NULL;
  414. }
  415. public const char *
  416. magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
  417. {
  418. if (file_reset(ms) == -1)
  419. return NULL;
  420. /*
  421. * The main work is done here!
  422. * We have the file name and/or the data buffer to be identified.
  423. */
  424. if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
  425. return NULL;
  426. }
  427. return file_getbuffer(ms);
  428. }
  429. #endif
  430. public const char *
  431. magic_error(struct magic_set *ms)
  432. {
  433. return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
  434. }
  435. public int
  436. magic_errno(struct magic_set *ms)
  437. {
  438. return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
  439. }
  440. public int
  441. magic_setflags(struct magic_set *ms, int flags)
  442. {
  443. #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
  444. if (flags & MAGIC_PRESERVE_ATIME)
  445. return -1;
  446. #endif
  447. ms->flags = flags;
  448. return 0;
  449. }