magic.c 8.7 KB

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