magic.c 9.0 KB

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