magic.c 9.0 KB

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