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

Annotation of src/usr.bin/cap_mkdb/cap_mkdb.c, Revision 1.19

1.19    ! deraadt     1: /*     $OpenBSD: cap_mkdb.c,v 1.18 2011/07/04 21:34:54 nicm Exp $      */
1.1       deraadt     2: /*     $NetBSD: cap_mkdb.c,v 1.5 1995/09/02 05:47:12 jtc Exp $ */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1992, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.10      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <sys/stat.h>
                     34:
                     35: #include <db.h>
                     36: #include <err.h>
                     37: #include <errno.h>
                     38: #include <fcntl.h>
                     39: #include <limits.h>
                     40: #include <stdio.h>
                     41: #include <stdlib.h>
                     42: #include <string.h>
1.4       tholo      43: #include <ctype.h>
1.1       deraadt    44: #include <unistd.h>
                     45:
1.19    ! deraadt    46: #define MINIMUM(a, b)  (((a) < (b)) ? (a) : (b))
        !            47: #define MAXIMUM(a, b)  (((a) > (b)) ? (a) : (b))
        !            48:
1.9       millert    49: void    db_build(char **);
                     50: void    dounlink(void);
                     51: void    usage(void);
                     52: int     igetnext(char **, char **);
                     53: int     main(int, char *[]);
1.1       deraadt    54:
                     55: DB *capdbp;
1.4       tholo      56: int info, verbose;
                     57: char *capname, buf[8 * 1024];
1.1       deraadt    58:
                     59: HASHINFO openinfo = {
                     60:        4096,           /* bsize */
                     61:        16,             /* ffactor */
                     62:        256,            /* nelem */
                     63:        2048 * 1024,    /* cachesize */
                     64:        NULL,           /* hash() */
                     65:        0               /* lorder */
                     66: };
                     67:
                     68: /*
1.4       tholo      69:  * cap_mkdb creates a capability hash database for quick retrieval of capability
1.1       deraadt    70:  * records.  The database contains 2 types of entries: records and references
                     71:  * marked by the first byte in the data.  A record entry contains the actual
                     72:  * capability record whereas a reference contains the name (key) under which
                     73:  * the correct record is stored.
                     74:  */
                     75: int
1.11      deraadt    76: main(int argc, char *argv[])
1.1       deraadt    77: {
                     78:        int c;
                     79:
                     80:        capname = NULL;
1.4       tholo      81:        while ((c = getopt(argc, argv, "f:iv")) != -1) {
1.1       deraadt    82:                switch(c) {
                     83:                case 'f':
                     84:                        capname = optarg;
                     85:                        break;
                     86:                case 'v':
                     87:                        verbose = 1;
                     88:                        break;
1.6       naddy      89:                case 'i':
1.4       tholo      90:                        info = 1;
                     91:                        break;
1.1       deraadt    92:                case '?':
                     93:                default:
                     94:                        usage();
                     95:                }
                     96:        }
                     97:        argc -= optind;
                     98:        argv += optind;
                     99:
                    100:        if (*argv == NULL)
                    101:                usage();
                    102:
                    103:        /*
                    104:         * The database file is the first argument if no name is specified.
1.7       millert   105:         * Make arrangements to unlink it if we exit badly.
1.1       deraadt   106:         */
                    107:        (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
                    108:        if ((capname = strdup(buf)) == NULL)
1.12      millert   109:                err(1, NULL);
1.1       deraadt   110:        if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
                    111:            DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
                    112:                err(1, "%s", buf);
                    113:
                    114:        if (atexit(dounlink))
                    115:                err(1, "atexit");
                    116:
                    117:        db_build(argv);
                    118:
                    119:        if (capdbp->close(capdbp) < 0)
                    120:                err(1, "%s", capname);
                    121:        capname = NULL;
                    122:        exit(0);
                    123: }
                    124:
                    125: void
1.11      deraadt   126: dounlink(void)
1.1       deraadt   127: {
                    128:        if (capname != NULL)
                    129:                (void)unlink(capname);
                    130: }
                    131:
                    132: /*
                    133:  * Any changes to these definitions should be made also in the getcap(3)
                    134:  * library routines.
                    135:  */
                    136: #define RECOK  (char)0
                    137: #define TCERR  (char)1
                    138: #define SHADOW (char)2
                    139:
                    140: /*
1.6       naddy     141:  * db_build() builds the name and capability databases according to the
1.1       deraadt   142:  * details above.
                    143:  */
                    144: void
1.11      deraadt   145: db_build(char **ifiles)
1.1       deraadt   146: {
                    147:        DBT key, data;
                    148:        recno_t reccnt;
                    149:        size_t len, bplen;
                    150:        int st;
1.15      nicm      151:        char *bp, *p, *t, *out, ch;
1.7       millert   152:
                    153:        cgetusedb(0);           /* disable reading of .db files in getcap(3) */
1.1       deraadt   154:
                    155:        data.data = NULL;
                    156:        key.data = NULL;
1.4       tholo     157:        for (reccnt = 0, bplen = 0;
                    158:             (st = (info ? igetnext(&bp, ifiles) : cgetnext(&bp, ifiles))) > 0;) {
1.1       deraadt   159:
                    160:                /*
1.15      nicm      161:                 * Allocate enough memory to store four times the size of the
                    162:                 * record (so an existing ':' can be expanded to '\072' for
                    163:                 * terminfo) plus a terminating NULL and one extra byte.
1.1       deraadt   164:                 */
                    165:                len = strlen(bp);
1.15      nicm      166:                if (bplen <= 4 * len + 2) {
1.19    ! deraadt   167:                        int newbplen = bplen + MAXIMUM(256, 4 * len + 2);
1.13      tedu      168:                        void *newdata;
                    169:
                    170:                        if ((newdata = realloc(data.data, newbplen)) == NULL)
1.12      millert   171:                                err(1, NULL);
1.13      tedu      172:                        data.data = newdata;
                    173:                        bplen = newbplen;
1.1       deraadt   174:                }
                    175:
                    176:                /* Find the end of the name field. */
1.4       tholo     177:                if ((p = strchr(bp, info ? ',' : ':')) == NULL) {
1.19    ! deraadt   178:                        warnx("no name field: %.*s", (int)MINIMUM(len, 20), bp);
1.1       deraadt   179:                        continue;
                    180:                }
                    181:
                    182:                /* First byte of stored record indicates status. */
                    183:                switch(st) {
                    184:                case 1:
                    185:                        ((char *)(data.data))[0] = RECOK;
                    186:                        break;
                    187:                case 2:
                    188:                        ((char *)(data.data))[0] = TCERR;
1.4       tholo     189:                        warnx("Record not tc expanded: %.*s", (int)(p - bp), bp);
1.1       deraadt   190:                        break;
                    191:                }
                    192:
                    193:                /* Create the stored record. */
1.4       tholo     194:                if (info) {
1.15      nicm      195:                        /*
                    196:                         * The record separator is :, so it is necessary to
                    197:                         * change commas into colons. However, \, should be
                    198:                         * left alone, unless the \ is the last part of ^\.
                    199:                         */
1.5       millert   200:                        data.size = len + 2;
1.15      nicm      201:                        out = ((char *) data.data) + 1;
                    202:                        t = bp;
                    203:                        while (t < bp + len) {
                    204:                                switch (ch = *t++) {
                    205:                                case '^':
                    206:                                case '\\':
                    207:                                        *out++ = ch;
                    208:                                        if (*t != '\0')
                    209:                                                *out++ = *t++;
                    210:                                        break;
                    211:                                case ':':
                    212:                                        memcpy(out, "\\072", 4);
                    213:                                        out += 4;
                    214:                                        data.size += 3; /* : already counted */
                    215:                                        break;
                    216:                                case ',':
                    217:                                        *out++ = ':';
                    218:                                        break;
                    219:                                default:
                    220:                                        *out++ = ch;
                    221:                                        break;
                    222:                                }
                    223:                        }
                    224:                        *out++ = '\0';
1.4       tholo     225:                        if (memchr((char *)data.data + 1, '\0', data.size - 2)) {
1.19    ! deraadt   226:                                warnx("NUL in entry: %.*s", (int)MINIMUM(len, 20), bp);
1.4       tholo     227:                                continue;
                    228:                        }
1.5       millert   229:                } else {
                    230:                        char *capbeg, *capend;
                    231:
                    232:                        t = (char *)data.data + 1;
                    233:                        /* Copy the cap name and trailing ':' */
                    234:                        len = p - bp + 1;
                    235:                        memcpy(t, bp, len);
                    236:                        t += len;
                    237:
                    238:                        /* Copy entry, collapsing empty fields. */
                    239:                        capbeg = p + 1;
                    240:                        while (*capbeg) {
                    241:                                /* Skip empty fields. */
                    242:                                if ((len = strspn(capbeg, ": \t\n\r")))
                    243:                                        capbeg += len;
                    244:
                    245:                                /* Find the end of this cap and copy it w/ : */
                    246:                                capend = strchr(capbeg, ':');
                    247:                                if (capend)
                    248:                                        len = capend - capbeg + 1;
                    249:                                else
                    250:                                        len = strlen(capbeg);
                    251:                                memcpy(t, capbeg, len);
                    252:                                t += len;
                    253:                                capbeg += len;
                    254:                        }
                    255:                        *t = '\0';
                    256:                        data.size = t - (char *)data.data + 1;
1.4       tholo     257:                }
1.1       deraadt   258:
                    259:                /* Store the record under the name field. */
                    260:                key.data = bp;
                    261:                key.size = p - bp;
                    262:
                    263:                switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
                    264:                case -1:
                    265:                        err(1, "put");
                    266:                        /* NOTREACHED */
                    267:                case 1:
                    268:                        warnx("ignored duplicate: %.*s",
1.4       tholo     269:                            (int)key.size, (char *)key.data);
1.1       deraadt   270:                        continue;
                    271:                }
                    272:                ++reccnt;
                    273:
                    274:                /* If only one name, ignore the rest. */
                    275:                if ((p = strchr(bp, '|')) == NULL)
                    276:                        continue;
                    277:
                    278:                /* The rest of the names reference the entire name. */
                    279:                ((char *)(data.data))[0] = SHADOW;
1.4       tholo     280:                (void) memmove(&((u_char *)(data.data))[1], key.data, key.size);
1.1       deraadt   281:                data.size = key.size + 1;
                    282:
                    283:                /* Store references for other names. */
                    284:                for (p = t = bp;; ++p) {
1.4       tholo     285:                        if (p > t && (*p == (info ? ',' : ':') || *p == '|')) {
1.1       deraadt   286:                                key.size = p - t;
                    287:                                key.data = t;
1.17      nicm      288:
                    289:                                /*
                    290:                                 * If this is the last entry and contains any
                    291:                                 * spaces, it is a description rather than an
                    292:                                 * alias, so skip it and break.
                    293:                                 */
1.18      nicm      294:                                if (*p != '|' &&
1.17      nicm      295:                                    memchr(key.data, ' ', key.size) != NULL)
                    296:                                        break;
1.18      nicm      297:
1.1       deraadt   298:                                switch(capdbp->put(capdbp,
                    299:                                    &key, &data, R_NOOVERWRITE)) {
                    300:                                case -1:
                    301:                                        err(1, "put");
                    302:                                        /* NOTREACHED */
                    303:                                case 1:
                    304:                                        warnx("ignored duplicate: %.*s",
1.4       tholo     305:                                              (int)key.size, (char *)key.data);
1.1       deraadt   306:                                }
                    307:                                t = p + 1;
                    308:                        }
1.4       tholo     309:                        if (*p == (info ? ',' : ':'))
1.1       deraadt   310:                                break;
                    311:                }
1.14      otto      312:                free(bp);
1.1       deraadt   313:        }
                    314:
                    315:        switch(st) {
                    316:        case -1:
                    317:                err(1, "file argument");
                    318:                /* NOTREACHED */
                    319:        case -2:
                    320:                errx(1, "potential reference loop detected");
                    321:                /* NOTREACHED */
                    322:        }
                    323:
                    324:        if (verbose)
                    325:                (void)printf("cap_mkdb: %d capability records\n", reccnt);
                    326: }
                    327:
                    328: void
1.11      deraadt   329: usage(void)
1.1       deraadt   330: {
                    331:        (void)fprintf(stderr,
1.4       tholo     332:            "usage: cap_mkdb [-iv] [-f outfile] file1 [file2 ...]\n");
1.1       deraadt   333:        exit(1);
                    334: }