magic.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.65 2009/09/14 17:50:38 christos Exp $")
  30. #endif /* lint */
  31. #include "magic.h"
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #ifdef QUICK
  36. #include <sys/mman.h>
  37. #endif
  38. #ifdef HAVE_LIMITS_H
  39. #include <limits.h> /* for PIPE_BUF */
  40. #endif
  41. #if defined(HAVE_UTIMES)
  42. # include <sys/time.h>
  43. #elif defined(HAVE_UTIME)
  44. # if defined(HAVE_SYS_UTIME_H)
  45. # include <sys/utime.h>
  46. # elif defined(HAVE_UTIME_H)
  47. # include <utime.h>
  48. # endif
  49. #endif
  50. #ifdef HAVE_UNISTD_H
  51. #include <unistd.h> /* for read() */
  52. #endif
  53. #include "patchlevel.h"
  54. #ifndef PIPE_BUF
  55. /* Get the PIPE_BUF from pathconf */
  56. #ifdef _PC_PIPE_BUF
  57. #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
  58. #else
  59. #define PIPE_BUF 512
  60. #endif
  61. #endif
  62. private void free_mlist(struct mlist *);
  63. private void close_and_restore(const struct magic_set *, const char *, int,
  64. const struct stat *);
  65. private int unreadable_info(struct magic_set *, mode_t, const char *);
  66. private const char* get_default_magic(void);
  67. #ifndef COMPILE_ONLY
  68. private const char *file_or_fd(struct magic_set *, const char *, int);
  69. #endif
  70. #ifndef STDIN_FILENO
  71. #define STDIN_FILENO 0
  72. #endif
  73. private const char *
  74. get_default_magic(void)
  75. {
  76. static const char hmagic[] = "/.magic";
  77. static char default_magic[2 * MAXPATHLEN + 2];
  78. char *home;
  79. char hmagicpath[MAXPATHLEN + 1];
  80. if ((home = getenv("HOME")) == NULL)
  81. return MAGIC;
  82. (void)snprintf(hmagicpath, sizeof(hmagicpath), "%s%s", home, hmagic);
  83. if (access(hmagicpath, R_OK) == -1)
  84. return MAGIC;
  85. (void)snprintf(default_magic, sizeof(default_magic), "%s:%s",
  86. hmagicpath, MAGIC);
  87. return default_magic;
  88. }
  89. public const char *
  90. magic_getpath(const char *magicfile, int action)
  91. {
  92. if (magicfile != NULL)
  93. return magicfile;
  94. magicfile = getenv("MAGIC");
  95. if (magicfile != NULL)
  96. return magicfile;
  97. return action == FILE_LOAD ? get_default_magic() : MAGIC;
  98. }
  99. public struct magic_set *
  100. magic_open(int flags)
  101. {
  102. struct magic_set *ms;
  103. size_t len;
  104. if ((ms = CAST(struct magic_set *, calloc((size_t)1,
  105. sizeof(struct magic_set)))) == NULL)
  106. return NULL;
  107. if (magic_setflags(ms, flags) == -1) {
  108. errno = EINVAL;
  109. goto free;
  110. }
  111. ms->o.buf = ms->o.pbuf = NULL;
  112. len = (ms->c.len = 10) * sizeof(*ms->c.li);
  113. if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
  114. goto free;
  115. ms->event_flags = 0;
  116. ms->error = -1;
  117. ms->mlist = NULL;
  118. ms->file = "unknown";
  119. ms->line = 0;
  120. return ms;
  121. free:
  122. free(ms);
  123. return NULL;
  124. }
  125. private void
  126. free_mlist(struct mlist *mlist)
  127. {
  128. struct mlist *ml;
  129. if (mlist == NULL)
  130. return;
  131. for (ml = mlist->next; ml != mlist;) {
  132. struct mlist *next = ml->next;
  133. struct magic *mg = ml->magic;
  134. file_delmagic(mg, ml->mapped, ml->nmagic);
  135. free(ml);
  136. ml = next;
  137. }
  138. free(ml);
  139. }
  140. private int
  141. unreadable_info(struct magic_set *ms, mode_t md, const char *file)
  142. {
  143. /* We cannot open it, but we were able to stat it. */
  144. if (access(file, W_OK) == 0)
  145. if (file_printf(ms, "writable, ") == -1)
  146. return -1;
  147. if (access(file, X_OK) == 0)
  148. if (file_printf(ms, "executable, ") == -1)
  149. return -1;
  150. if (S_ISREG(md))
  151. if (file_printf(ms, "regular file, ") == -1)
  152. return -1;
  153. if (file_printf(ms, "no read permission") == -1)
  154. return -1;
  155. return 0;
  156. }
  157. public void
  158. magic_close(struct magic_set *ms)
  159. {
  160. free_mlist(ms->mlist);
  161. free(ms->o.pbuf);
  162. free(ms->o.buf);
  163. free(ms->c.li);
  164. free(ms);
  165. }
  166. /*
  167. * load a magic file
  168. */
  169. public int
  170. magic_load(struct magic_set *ms, const char *magicfile)
  171. {
  172. struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
  173. if (ml) {
  174. free_mlist(ms->mlist);
  175. ms->mlist = ml;
  176. return 0;
  177. }
  178. return -1;
  179. }
  180. public int
  181. magic_compile(struct magic_set *ms, const char *magicfile)
  182. {
  183. struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
  184. free_mlist(ml);
  185. return ml ? 0 : -1;
  186. }
  187. public int
  188. magic_check(struct magic_set *ms, const char *magicfile)
  189. {
  190. struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
  191. free_mlist(ml);
  192. return ml ? 0 : -1;
  193. }
  194. private void
  195. close_and_restore(const struct magic_set *ms, const char *name, int fd,
  196. const struct stat *sb)
  197. {
  198. if (fd == STDIN_FILENO)
  199. return;
  200. (void) close(fd);
  201. if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
  202. /*
  203. * Try to restore access, modification times if read it.
  204. * This is really *bad* because it will modify the status
  205. * time of the file... And of course this will affect
  206. * backup programs
  207. */
  208. #ifdef HAVE_UTIMES
  209. struct timeval utsbuf[2];
  210. (void)memset(utsbuf, 0, sizeof(utsbuf));
  211. utsbuf[0].tv_sec = sb->st_atime;
  212. utsbuf[1].tv_sec = sb->st_mtime;
  213. (void) utimes(name, utsbuf); /* don't care if loses */
  214. #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
  215. struct utimbuf utbuf;
  216. (void)memset(&utbuf, 0, sizeof(utbuf));
  217. utbuf.actime = sb->st_atime;
  218. utbuf.modtime = sb->st_mtime;
  219. (void) utime(name, &utbuf); /* don't care if loses */
  220. #endif
  221. }
  222. }
  223. #ifndef COMPILE_ONLY
  224. /*
  225. * find type of descriptor
  226. */
  227. public const char *
  228. magic_descriptor(struct magic_set *ms, int fd)
  229. {
  230. return file_or_fd(ms, NULL, fd);
  231. }
  232. /*
  233. * find type of named file
  234. */
  235. public const char *
  236. magic_file(struct magic_set *ms, const char *inname)
  237. {
  238. return file_or_fd(ms, inname, STDIN_FILENO);
  239. }
  240. private const char *
  241. file_or_fd(struct magic_set *ms, const char *inname, int fd)
  242. {
  243. int rv = -1;
  244. unsigned char *buf;
  245. struct stat sb;
  246. ssize_t nbytes = 0; /* number of bytes read from a datafile */
  247. int ispipe = 0;
  248. /*
  249. * one extra for terminating '\0', and
  250. * some overlapping space for matches near EOF
  251. */
  252. #define SLOP (1 + sizeof(union VALUETYPE))
  253. if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
  254. return NULL;
  255. if (file_reset(ms) == -1)
  256. goto done;
  257. switch (file_fsmagic(ms, inname, &sb)) {
  258. case -1: /* error */
  259. goto done;
  260. case 0: /* nothing found */
  261. break;
  262. default: /* matched it and printed type */
  263. rv = 0;
  264. goto done;
  265. }
  266. if (inname == NULL) {
  267. if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
  268. ispipe = 1;
  269. } else {
  270. int flags = O_RDONLY|O_BINARY;
  271. if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
  272. flags |= O_NONBLOCK;
  273. ispipe = 1;
  274. }
  275. errno = 0;
  276. if ((fd = open(inname, flags)) < 0) {
  277. if (unreadable_info(ms, sb.st_mode, inname) == -1)
  278. goto done;
  279. rv = 0;
  280. goto done;
  281. }
  282. #ifdef O_NONBLOCK
  283. if ((flags = fcntl(fd, F_GETFL)) != -1) {
  284. flags &= ~O_NONBLOCK;
  285. (void)fcntl(fd, F_SETFL, flags);
  286. }
  287. #endif
  288. }
  289. /*
  290. * try looking at the first HOWMANY bytes
  291. */
  292. if (ispipe) {
  293. ssize_t r = 0;
  294. while ((r = sread(fd, (void *)&buf[nbytes],
  295. (size_t)(HOWMANY - nbytes), 1)) > 0) {
  296. nbytes += r;
  297. if (r < PIPE_BUF) break;
  298. }
  299. if (nbytes == 0) {
  300. /* We can not read it, but we were able to stat it. */
  301. if (unreadable_info(ms, sb.st_mode, inname) == -1)
  302. goto done;
  303. rv = 0;
  304. goto done;
  305. }
  306. } else {
  307. if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
  308. file_error(ms, errno, "cannot read `%s'", inname);
  309. goto done;
  310. }
  311. }
  312. (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
  313. if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
  314. goto done;
  315. rv = 0;
  316. done:
  317. free(buf);
  318. close_and_restore(ms, inname, fd, &sb);
  319. return rv == 0 ? file_getbuffer(ms) : NULL;
  320. }
  321. public const char *
  322. magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
  323. {
  324. if (file_reset(ms) == -1)
  325. return NULL;
  326. /*
  327. * The main work is done here!
  328. * We have the file name and/or the data buffer to be identified.
  329. */
  330. if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
  331. return NULL;
  332. }
  333. return file_getbuffer(ms);
  334. }
  335. #endif
  336. public const char *
  337. magic_error(struct magic_set *ms)
  338. {
  339. return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
  340. }
  341. public int
  342. magic_errno(struct magic_set *ms)
  343. {
  344. return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
  345. }
  346. public int
  347. magic_setflags(struct magic_set *ms, int flags)
  348. {
  349. #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
  350. if (flags & MAGIC_PRESERVE_ATIME)
  351. return -1;
  352. #endif
  353. ms->flags = flags;
  354. return 0;
  355. }