[BACK]Return to magic.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / file

Annotation of src/usr.bin/file/magic.c, Revision 1.10

1.10    ! doug        1: /* $OpenBSD: magic.c,v 1.9 2014/10/11 03:06:44 doug Exp $ */
1.1       tedu        2: /*
                      3:  * Copyright (c) Christos Zoulas 2003.
                      4:  * All Rights Reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice immediately at the beginning of the file, without modification,
                     11:  *    this list of conditions, and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     17:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     18:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     19:  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
                     20:  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     21:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     22:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     23:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     24:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     25:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     26:  * SUCH DAMAGE.
                     27:  */
                     28:
1.7       deraadt    29: #include <sys/types.h>
                     30: #include <sys/stat.h>
                     31:
1.1       tedu       32: #include "file.h"
                     33: #include "magic.h"
                     34:
                     35: #include <stdio.h>
                     36: #include <stdlib.h>
                     37: #include <unistd.h>
                     38: #include <string.h>
                     39: #ifdef QUICK
                     40: #include <sys/mman.h>
                     41: #endif
1.5       chl        42: #include <limits.h>    /* for PIPE_BUF */
1.1       tedu       43:
1.5       chl        44: #if defined(HAVE_UTIMES)
                     45: # include <sys/time.h>
                     46: #elif defined(HAVE_UTIME)
1.1       tedu       47: # if defined(HAVE_SYS_UTIME_H)
                     48: #  include <sys/utime.h>
                     49: # elif defined(HAVE_UTIME_H)
                     50: #  include <utime.h>
                     51: # endif
                     52: #endif
                     53:
                     54: #ifdef HAVE_UNISTD_H
                     55: #include <unistd.h>    /* for read() */
                     56: #endif
                     57:
                     58: #ifdef HAVE_LOCALE_H
                     59: #include <locale.h>
                     60: #endif
                     61:
                     62: #include <netinet/in.h>                /* for byte swapping */
                     63:
                     64: #include "patchlevel.h"
                     65:
1.6       chl        66: #ifndef PIPE_BUF
                     67: /* Get the PIPE_BUF from pathconf */
                     68: #ifdef _PC_PIPE_BUF
                     69: #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
                     70: #else
                     71: #define PIPE_BUF 512
                     72: #endif
                     73: #endif
                     74:
1.1       tedu       75: #ifdef __EMX__
                     76: private char *apptypeName = NULL;
                     77: protected int file_os2_apptype(struct magic_set *ms, const char *fn,
                     78:     const void *buf, size_t nb);
                     79: #endif /* __EMX__ */
                     80:
                     81: private void free_mlist(struct mlist *);
                     82: private void close_and_restore(const struct magic_set *, const char *, int,
                     83:     const struct stat *);
1.5       chl        84: private int info_from_stat(struct magic_set *, mode_t);
1.6       chl        85: #ifndef COMPILE_ONLY
                     86: private const char *file_or_fd(struct magic_set *, const char *, int);
                     87: #endif
1.5       chl        88:
                     89: #ifndef        STDIN_FILENO
                     90: #define        STDIN_FILENO    0
                     91: #endif
1.1       tedu       92:
                     93: public struct magic_set *
                     94: magic_open(int flags)
                     95: {
                     96:        struct magic_set *ms;
                     97:
1.5       chl        98:        if ((ms = calloc((size_t)1, sizeof(struct magic_set))) == NULL)
1.1       tedu       99:                return NULL;
                    100:
                    101:        if (magic_setflags(ms, flags) == -1) {
                    102:                errno = EINVAL;
1.6       chl       103:                goto free;
1.1       tedu      104:        }
                    105:
1.6       chl       106:        ms->o.buf = ms->o.pbuf = NULL;
1.5       chl       107:
1.9       doug      108:        ms->c.len = 10;
                    109:        ms->c.li = reallocarray(NULL, ms->c.len, sizeof(*ms->c.li));
1.5       chl       110:        if (ms->c.li == NULL)
1.6       chl       111:                goto free;
1.5       chl       112:
1.1       tedu      113:        ms->haderr = 0;
                    114:        ms->error = -1;
                    115:        ms->mlist = NULL;
1.5       chl       116:        ms->file = "unknown";
                    117:        ms->line = 0;
1.1       tedu      118:        return ms;
1.6       chl       119: free:
1.5       chl       120:        free(ms);
                    121:        return NULL;
1.1       tedu      122: }
                    123:
                    124: private void
                    125: free_mlist(struct mlist *mlist)
                    126: {
                    127:        struct mlist *ml;
                    128:
                    129:        if (mlist == NULL)
                    130:                return;
                    131:
                    132:        for (ml = mlist->next; ml != mlist;) {
                    133:                struct mlist *next = ml->next;
                    134:                struct magic *mg = ml->magic;
                    135:                file_delmagic(mg, ml->mapped, ml->nmagic);
                    136:                free(ml);
                    137:                ml = next;
                    138:        }
                    139:        free(ml);
                    140: }
                    141:
1.5       chl       142: private int
                    143: info_from_stat(struct magic_set *ms, mode_t md)
                    144: {
                    145:        /* We cannot open it, but we were able to stat it. */
                    146:        if (md & 0222)
                    147:                if (file_printf(ms, "writable, ") == -1)
                    148:                        return -1;
                    149:        if (md & 0111)
                    150:                if (file_printf(ms, "executable, ") == -1)
                    151:                        return -1;
                    152:        if (S_ISREG(md))
                    153:                if (file_printf(ms, "regular file, ") == -1)
                    154:                        return -1;
                    155:        if (file_printf(ms, "no read permission") == -1)
                    156:                return -1;
                    157:        return 0;
                    158: }
                    159:
1.1       tedu      160: public void
1.5       chl       161: magic_close(struct magic_set *ms)
1.1       tedu      162: {
                    163:        free_mlist(ms->mlist);
1.5       chl       164:        free(ms->o.pbuf);
1.1       tedu      165:        free(ms->o.buf);
1.5       chl       166:        free(ms->c.li);
1.1       tedu      167:        free(ms);
                    168: }
                    169:
                    170: /*
                    171:  * load a magic file
                    172:  */
                    173: public int
                    174: magic_load(struct magic_set *ms, const char *magicfile)
                    175: {
                    176:        struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
                    177:        if (ml) {
                    178:                free_mlist(ms->mlist);
                    179:                ms->mlist = ml;
                    180:                return 0;
                    181:        }
                    182:        return -1;
                    183: }
                    184:
                    185: public int
                    186: magic_compile(struct magic_set *ms, const char *magicfile)
                    187: {
                    188:        struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
                    189:        free_mlist(ml);
                    190:        return ml ? 0 : -1;
                    191: }
                    192:
                    193: public int
                    194: magic_check(struct magic_set *ms, const char *magicfile)
                    195: {
                    196:        struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
                    197:        free_mlist(ml);
                    198:        return ml ? 0 : -1;
                    199: }
                    200:
                    201: private void
                    202: close_and_restore(const struct magic_set *ms, const char *name, int fd,
                    203:     const struct stat *sb)
                    204: {
1.5       chl       205:        if (fd == STDIN_FILENO)
                    206:                return;
1.1       tedu      207:        (void) close(fd);
1.5       chl       208:
                    209:        if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
1.1       tedu      210:                /*
                    211:                 * Try to restore access, modification times if read it.
                    212:                 * This is really *bad* because it will modify the status
                    213:                 * time of the file... And of course this will affect
                    214:                 * backup programs
                    215:                 */
                    216: #ifdef HAVE_UTIMES
                    217:                struct timeval  utsbuf[2];
1.6       chl       218:                (void)memset(utsbuf, 0, sizeof(utsbuf));
1.1       tedu      219:                utsbuf[0].tv_sec = sb->st_atime;
                    220:                utsbuf[1].tv_sec = sb->st_mtime;
                    221:
                    222:                (void) utimes(name, utsbuf); /* don't care if loses */
                    223: #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
                    224:                struct utimbuf  utbuf;
                    225:
1.6       chl       226:                (void)memset(utbuf, 0, sizeof(utbuf));
1.1       tedu      227:                utbuf.actime = sb->st_atime;
                    228:                utbuf.modtime = sb->st_mtime;
                    229:                (void) utime(name, &utbuf); /* don't care if loses */
                    230: #endif
                    231:        }
                    232: }
                    233:
1.5       chl       234: #ifndef COMPILE_ONLY
1.6       chl       235:
                    236: /*
                    237:  * find type of descriptor
                    238:  */
                    239: public const char *
                    240: magic_descriptor(struct magic_set *ms, int fd)
                    241: {
                    242:        return file_or_fd(ms, NULL, fd);
                    243: }
                    244:
1.1       tedu      245: /*
                    246:  * find type of named file
                    247:  */
                    248: public const char *
                    249: magic_file(struct magic_set *ms, const char *inname)
                    250: {
1.6       chl       251:        return file_or_fd(ms, inname, STDIN_FILENO);
                    252: }
                    253:
                    254: private const char *
                    255: file_or_fd(struct magic_set *ms, const char *inname, int fd)
                    256: {
1.5       chl       257:        int     rv = -1;
                    258:        unsigned char *buf;
1.1       tedu      259:        struct stat     sb;
                    260:        ssize_t nbytes = 0;     /* number of bytes read from a datafile */
1.5       chl       261:        int     ispipe = 0;
                    262:
                    263:        /*
                    264:         * one extra for terminating '\0', and
                    265:         * some overlapping space for matches near EOF
                    266:         */
                    267: #define SLOP (1 + sizeof(union VALUETYPE))
                    268:        if ((buf = malloc(HOWMANY + SLOP)) == NULL)
                    269:                return NULL;
1.1       tedu      270:
                    271:        if (file_reset(ms) == -1)
1.5       chl       272:                goto done;
1.1       tedu      273:
                    274:        switch (file_fsmagic(ms, inname, &sb)) {
1.5       chl       275:        case -1:                /* error */
                    276:                goto done;
                    277:        case 0:                 /* nothing found */
1.1       tedu      278:                break;
1.5       chl       279:        default:                /* matched it and printed type */
                    280:                rv = 0;
                    281:                goto done;
1.1       tedu      282:        }
                    283:
1.5       chl       284:        if (inname == NULL) {
                    285:                if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
                    286:                        ispipe = 1;
                    287:        } else {
                    288:                int flags = O_RDONLY|O_BINARY;
                    289:
                    290:                if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
                    291:                        flags |= O_NONBLOCK;
                    292:                        ispipe = 1;
                    293:                }
                    294:
                    295:                errno = 0;
                    296:                if ((fd = open(inname, flags)) < 0) {
                    297: #ifdef __CYGWIN__
1.6       chl       298:                        /* FIXME: Do this with EXEEXT from autotools */
                    299:                        char *tmp = alloca(strlen(inname) + 5);
                    300:                        (void)strcat(strcpy(tmp, inname), ".exe");
                    301:                        if ((fd = open(tmp, flags)) < 0) {
1.5       chl       302: #endif
1.6       chl       303:                                fprintf(stderr, "couldn't open file\n");
                    304:                                if (info_from_stat(ms, sb.st_mode) == -1)
                    305:                                        goto done;
                    306:                                rv = 0;
                    307:                                goto done;
1.5       chl       308: #ifdef __CYGWIN__
1.6       chl       309:                        }
1.5       chl       310: #endif
                    311:                }
                    312: #ifdef O_NONBLOCK
                    313:                if ((flags = fcntl(fd, F_GETFL)) != -1) {
                    314:                        flags &= ~O_NONBLOCK;
                    315:                        (void)fcntl(fd, F_SETFL, flags);
                    316:                }
1.1       tedu      317: #endif
                    318:        }
                    319:
                    320:        /*
                    321:         * try looking at the first HOWMANY bytes
                    322:         */
1.5       chl       323:        if (ispipe) {
                    324:                ssize_t r = 0;
                    325:
                    326:                while ((r = sread(fd, (void *)&buf[nbytes],
                    327:                    (size_t)(HOWMANY - nbytes), 1)) > 0) {
                    328:                        nbytes += r;
                    329:                        if (r < PIPE_BUF) break;
                    330:                }
                    331:
                    332:                if (nbytes == 0) {
                    333:                        /* We can not read it, but we were able to stat it. */
                    334:                        if (info_from_stat(ms, sb.st_mode) == -1)
                    335:                                goto done;
                    336:                        rv = 0;
                    337:                        goto done;
                    338:                }
                    339:
                    340:        } else {
                    341:                if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
                    342:                        file_error(ms, errno, "cannot read `%s'", inname);
                    343:                        goto done;
                    344:                }
1.1       tedu      345:        }
                    346:
1.6       chl       347:        (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
                    348:        if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
                    349:                goto done;
1.5       chl       350:        rv = 0;
1.1       tedu      351: done:
1.5       chl       352:        free(buf);
1.1       tedu      353:        close_and_restore(ms, inname, fd, &sb);
1.5       chl       354:        return rv == 0 ? file_getbuffer(ms) : NULL;
1.1       tedu      355: }
                    356:
                    357:
                    358: public const char *
                    359: magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
                    360: {
                    361:        if (file_reset(ms) == -1)
                    362:                return NULL;
                    363:        /*
                    364:         * The main work is done here!
                    365:         * We have the file name and/or the data buffer to be identified.
                    366:         */
1.5       chl       367:        if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
1.1       tedu      368:                return NULL;
                    369:        }
                    370:        return file_getbuffer(ms);
                    371: }
1.5       chl       372: #endif
1.1       tedu      373:
                    374: public const char *
                    375: magic_error(struct magic_set *ms)
                    376: {
                    377:        return ms->haderr ? ms->o.buf : NULL;
                    378: }
                    379:
                    380: public int
                    381: magic_errno(struct magic_set *ms)
                    382: {
                    383:        return ms->haderr ? ms->error : 0;
                    384: }
                    385:
                    386: public int
                    387: magic_setflags(struct magic_set *ms, int flags)
                    388: {
                    389: #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
                    390:        if (flags & MAGIC_PRESERVE_ATIME)
                    391:                return -1;
                    392: #endif
                    393:        ms->flags = flags;
                    394:        return 0;
                    395: }