2 * Copyright (c) Christos Zoulas 2003.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 FILE_RCSID("@(#)$File: magic.c,v 1.62 2009/03/20 21:25:41 christos Exp $")
43 #include <limits.h> /* for PIPE_BUF */
46 #if defined(HAVE_UTIMES)
47 # include <sys/time.h>
48 #elif defined(HAVE_UTIME)
49 # if defined(HAVE_SYS_UTIME_H)
50 # include <sys/utime.h>
51 # elif defined(HAVE_UTIME_H)
57 #include <unistd.h> /* for read() */
60 #include <netinet/in.h> /* for byte swapping */
62 #include "patchlevel.h"
65 /* Get the PIPE_BUF from pathconf */
67 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
73 private void free_mlist(struct mlist *);
74 private void close_and_restore(const struct magic_set *, const char *, int,
76 private int unreadable_info(struct magic_set *, mode_t, const char *);
78 private const char *file_or_fd(struct magic_set *, const char *, int);
82 #define STDIN_FILENO 0
85 public struct magic_set *
91 if ((ms = CAST(magic_set *, calloc((size_t)1,
92 sizeof(struct magic_set)))) == NULL)
95 if (magic_setflags(ms, flags) == -1) {
100 ms->o.buf = ms->o.pbuf = NULL;
101 len = (ms->c.len = 10) * sizeof(*ms->c.li);
103 if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
109 ms->file = "unknown";
118 free_mlist(struct mlist *mlist)
125 for (ml = mlist->next; ml != mlist;) {
126 struct mlist *next = ml->next;
127 struct magic *mg = ml->magic;
128 file_delmagic(mg, ml->mapped, ml->nmagic);
136 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
138 /* We cannot open it, but we were able to stat it. */
139 if (access(file, W_OK) == 0)
140 if (file_printf(ms, "writable, ") == -1)
142 if (access(file, X_OK) == 0)
143 if (file_printf(ms, "executable, ") == -1)
146 if (file_printf(ms, "regular file, ") == -1)
148 if (file_printf(ms, "no read permission") == -1)
154 magic_close(struct magic_set *ms)
156 free_mlist(ms->mlist);
167 magic_load(struct magic_set *ms, const char *magicfile)
169 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
171 free_mlist(ms->mlist);
179 magic_compile(struct magic_set *ms, const char *magicfile)
181 struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
187 magic_check(struct magic_set *ms, const char *magicfile)
189 struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
195 close_and_restore(const struct magic_set *ms, const char *name, int fd,
196 const struct stat *sb)
198 if (fd == STDIN_FILENO)
202 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
204 * Try to restore access, modification times if read it.
205 * This is really *bad* because it will modify the status
206 * time of the file... And of course this will affect
210 struct timeval utsbuf[2];
211 (void)memset(utsbuf, 0, sizeof(utsbuf));
212 utsbuf[0].tv_sec = sb->st_atime;
213 utsbuf[1].tv_sec = sb->st_mtime;
215 (void) utimes(name, utsbuf); /* don't care if loses */
216 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
217 struct utimbuf utbuf;
219 (void)memset(&utbuf, 0, sizeof(utbuf));
220 utbuf.actime = sb->st_atime;
221 utbuf.modtime = sb->st_mtime;
222 (void) utime(name, &utbuf); /* don't care if loses */
230 * find type of descriptor
233 magic_descriptor(struct magic_set *ms, int fd)
235 return file_or_fd(ms, NULL, fd);
239 * find type of named file
242 magic_file(struct magic_set *ms, const char *inname)
244 return file_or_fd(ms, inname, STDIN_FILENO);
248 file_or_fd(struct magic_set *ms, const char *inname, int fd)
253 ssize_t nbytes = 0; /* number of bytes read from a datafile */
257 * one extra for terminating '\0', and
258 * some overlapping space for matches near EOF
260 #define SLOP (1 + sizeof(union VALUETYPE))
261 if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
264 if (file_reset(ms) == -1)
267 switch (file_fsmagic(ms, inname, &sb)) {
270 case 0: /* nothing found */
272 default: /* matched it and printed type */
277 if (inname == NULL) {
278 if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
281 int flags = O_RDONLY|O_BINARY;
283 if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
289 if ((fd = open(inname, flags)) < 0) {
290 if (unreadable_info(ms, sb.st_mode, inname) == -1)
296 if ((flags = fcntl(fd, F_GETFL)) != -1) {
297 flags &= ~O_NONBLOCK;
298 (void)fcntl(fd, F_SETFL, flags);
304 * try looking at the first HOWMANY bytes
309 while ((r = sread(fd, (void *)&buf[nbytes],
310 (size_t)(HOWMANY - nbytes), 1)) > 0) {
312 if (r < PIPE_BUF) break;
316 /* We can not read it, but we were able to stat it. */
317 if (unreadable_info(ms, sb.st_mode, inname) == -1)
324 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
325 file_error(ms, errno, "cannot read `%s'", inname);
330 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
331 if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
336 close_and_restore(ms, inname, fd, &sb);
337 return rv == 0 ? file_getbuffer(ms) : NULL;
342 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
344 if (file_reset(ms) == -1)
347 * The main work is done here!
348 * We have the file name and/or the data buffer to be identified.
350 if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
353 return file_getbuffer(ms);
358 magic_error(struct magic_set *ms)
360 return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
364 magic_errno(struct magic_set *ms)
366 return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
370 magic_setflags(struct magic_set *ms, int flags)
372 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
373 if (flags & MAGIC_PRESERVE_ATIME)