magic.c 8.4 KB

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