[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.13

1.13    ! tedu        1: /*     $OpenBSD: cap_mkdb.c,v 1.12 2003/09/21 22:32:02 millert 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: #ifndef lint
                     34: static char copyright[] =
                     35: "@(#) Copyright (c) 1992, 1993\n\
                     36:        The Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
                     42: #endif
1.13    ! tedu       43: static char rcsid[] = "$OpenBSD: cap_mkdb.c,v 1.12 2003/09/21 22:32:02 millert Exp $";
1.1       deraadt    44: #endif /* not lint */
                     45:
                     46: #include <sys/param.h>
                     47: #include <sys/stat.h>
                     48:
                     49: #include <db.h>
                     50: #include <err.h>
                     51: #include <errno.h>
                     52: #include <fcntl.h>
                     53: #include <limits.h>
                     54: #include <stdio.h>
                     55: #include <stdlib.h>
                     56: #include <string.h>
1.4       tholo      57: #include <ctype.h>
1.1       deraadt    58: #include <unistd.h>
                     59:
1.9       millert    60: void    db_build(char **);
                     61: void    dounlink(void);
                     62: void    usage(void);
                     63: int     igetnext(char **, char **);
                     64: int     main(int, char *[]);
1.1       deraadt    65:
                     66: DB *capdbp;
1.4       tholo      67: int info, verbose;
                     68: char *capname, buf[8 * 1024];
1.1       deraadt    69:
                     70: HASHINFO openinfo = {
                     71:        4096,           /* bsize */
                     72:        16,             /* ffactor */
                     73:        256,            /* nelem */
                     74:        2048 * 1024,    /* cachesize */
                     75:        NULL,           /* hash() */
                     76:        0               /* lorder */
                     77: };
                     78:
                     79: /*
1.4       tholo      80:  * cap_mkdb creates a capability hash database for quick retrieval of capability
1.1       deraadt    81:  * records.  The database contains 2 types of entries: records and references
                     82:  * marked by the first byte in the data.  A record entry contains the actual
                     83:  * capability record whereas a reference contains the name (key) under which
                     84:  * the correct record is stored.
                     85:  */
                     86: int
1.11      deraadt    87: main(int argc, char *argv[])
1.1       deraadt    88: {
                     89:        int c;
                     90:
                     91:        capname = NULL;
1.4       tholo      92:        while ((c = getopt(argc, argv, "f:iv")) != -1) {
1.1       deraadt    93:                switch(c) {
                     94:                case 'f':
                     95:                        capname = optarg;
                     96:                        break;
                     97:                case 'v':
                     98:                        verbose = 1;
                     99:                        break;
1.6       naddy     100:                case 'i':
1.4       tholo     101:                        info = 1;
                    102:                        break;
1.1       deraadt   103:                case '?':
                    104:                default:
                    105:                        usage();
                    106:                }
                    107:        }
                    108:        argc -= optind;
                    109:        argv += optind;
                    110:
                    111:        if (*argv == NULL)
                    112:                usage();
                    113:
                    114:        /*
                    115:         * The database file is the first argument if no name is specified.
1.7       millert   116:         * Make arrangements to unlink it if we exit badly.
1.1       deraadt   117:         */
                    118:        (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
                    119:        if ((capname = strdup(buf)) == NULL)
1.12      millert   120:                err(1, NULL);
1.1       deraadt   121:        if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
                    122:            DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
                    123:                err(1, "%s", buf);
                    124:
                    125:        if (atexit(dounlink))
                    126:                err(1, "atexit");
                    127:
                    128:        db_build(argv);
                    129:
                    130:        if (capdbp->close(capdbp) < 0)
                    131:                err(1, "%s", capname);
                    132:        capname = NULL;
                    133:        exit(0);
                    134: }
                    135:
                    136: void
1.11      deraadt   137: dounlink(void)
1.1       deraadt   138: {
                    139:        if (capname != NULL)
                    140:                (void)unlink(capname);
                    141: }
                    142:
                    143: /*
                    144:  * Any changes to these definitions should be made also in the getcap(3)
                    145:  * library routines.
                    146:  */
                    147: #define RECOK  (char)0
                    148: #define TCERR  (char)1
                    149: #define SHADOW (char)2
                    150:
                    151: /*
1.6       naddy     152:  * db_build() builds the name and capability databases according to the
1.1       deraadt   153:  * details above.
                    154:  */
                    155: void
1.11      deraadt   156: db_build(char **ifiles)
1.1       deraadt   157: {
                    158:        DBT key, data;
                    159:        recno_t reccnt;
                    160:        size_t len, bplen;
                    161:        int st;
                    162:        char *bp, *p, *t;
1.7       millert   163:
                    164:        cgetusedb(0);           /* disable reading of .db files in getcap(3) */
1.1       deraadt   165:
                    166:        data.data = NULL;
                    167:        key.data = NULL;
1.4       tholo     168:        for (reccnt = 0, bplen = 0;
                    169:             (st = (info ? igetnext(&bp, ifiles) : cgetnext(&bp, ifiles))) > 0;) {
1.1       deraadt   170:
                    171:                /*
                    172:                 * Allocate enough memory to store record, terminating
                    173:                 * NULL and one extra byte.
                    174:                 */
                    175:                len = strlen(bp);
                    176:                if (bplen <= len + 2) {
1.13    ! tedu      177:                        int newbplen = bplen + MAX(256, len + 2);
        !           178:                        void *newdata;
        !           179:
        !           180:                        if ((newdata = realloc(data.data, newbplen)) == NULL)
1.12      millert   181:                                err(1, NULL);
1.13    ! tedu      182:                        data.data = newdata;
        !           183:                        bplen = newbplen;
1.1       deraadt   184:                }
                    185:
                    186:                /* Find the end of the name field. */
1.4       tholo     187:                if ((p = strchr(bp, info ? ',' : ':')) == NULL) {
1.8       deraadt   188:                        warnx("no name field: %.*s", (int)MIN(len, 20), bp);
1.1       deraadt   189:                        continue;
                    190:                }
                    191:
                    192:                /* First byte of stored record indicates status. */
                    193:                switch(st) {
                    194:                case 1:
                    195:                        ((char *)(data.data))[0] = RECOK;
                    196:                        break;
                    197:                case 2:
                    198:                        ((char *)(data.data))[0] = TCERR;
1.4       tholo     199:                        warnx("Record not tc expanded: %.*s", (int)(p - bp), bp);
1.1       deraadt   200:                        break;
                    201:                }
                    202:
                    203:                /* Create the stored record. */
1.4       tholo     204:                if (info) {
1.5       millert   205:                        (void) memcpy(&((u_char *)(data.data))[1], bp, len + 1);
                    206:                        data.size = len + 2;
1.4       tholo     207:                        for (t = memchr((char *)data.data + 1, ',', data.size - 1);
                    208:                             t;
                    209:                             t = memchr(t, ',', data.size - (t - (char *)data.data)))
                    210:                                *t++ = ':';
                    211:
                    212:                        if (memchr((char *)data.data + 1, '\0', data.size - 2)) {
                    213:                                warnx("NUL in entry: %.*s", (int)MIN(len, 20), bp);
                    214:                                continue;
                    215:                        }
1.5       millert   216:                } else {
                    217:                        char *capbeg, *capend;
                    218:
                    219:                        t = (char *)data.data + 1;
                    220:                        /* Copy the cap name and trailing ':' */
                    221:                        len = p - bp + 1;
                    222:                        memcpy(t, bp, len);
                    223:                        t += len;
                    224:
                    225:                        /* Copy entry, collapsing empty fields. */
                    226:                        capbeg = p + 1;
                    227:                        while (*capbeg) {
                    228:                                /* Skip empty fields. */
                    229:                                if ((len = strspn(capbeg, ": \t\n\r")))
                    230:                                        capbeg += len;
                    231:
                    232:                                /* Find the end of this cap and copy it w/ : */
                    233:                                capend = strchr(capbeg, ':');
                    234:                                if (capend)
                    235:                                        len = capend - capbeg + 1;
                    236:                                else
                    237:                                        len = strlen(capbeg);
                    238:                                memcpy(t, capbeg, len);
                    239:                                t += len;
                    240:                                capbeg += len;
                    241:                        }
                    242:                        *t = '\0';
                    243:                        data.size = t - (char *)data.data + 1;
1.4       tholo     244:                }
1.1       deraadt   245:
                    246:                /* Store the record under the name field. */
                    247:                key.data = bp;
                    248:                key.size = p - bp;
                    249:
                    250:                switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
                    251:                case -1:
                    252:                        err(1, "put");
                    253:                        /* NOTREACHED */
                    254:                case 1:
                    255:                        warnx("ignored duplicate: %.*s",
1.4       tholo     256:                            (int)key.size, (char *)key.data);
1.1       deraadt   257:                        continue;
                    258:                }
                    259:                ++reccnt;
                    260:
                    261:                /* If only one name, ignore the rest. */
                    262:                if ((p = strchr(bp, '|')) == NULL)
                    263:                        continue;
                    264:
                    265:                /* The rest of the names reference the entire name. */
                    266:                ((char *)(data.data))[0] = SHADOW;
1.4       tholo     267:                (void) memmove(&((u_char *)(data.data))[1], key.data, key.size);
1.1       deraadt   268:                data.size = key.size + 1;
                    269:
                    270:                /* Store references for other names. */
                    271:                for (p = t = bp;; ++p) {
1.4       tholo     272:                        if (p > t && (*p == (info ? ',' : ':') || *p == '|')) {
1.1       deraadt   273:                                key.size = p - t;
                    274:                                key.data = t;
                    275:                                switch(capdbp->put(capdbp,
                    276:                                    &key, &data, R_NOOVERWRITE)) {
                    277:                                case -1:
                    278:                                        err(1, "put");
                    279:                                        /* NOTREACHED */
                    280:                                case 1:
                    281:                                        warnx("ignored duplicate: %.*s",
1.4       tholo     282:                                              (int)key.size, (char *)key.data);
1.1       deraadt   283:                                }
                    284:                                t = p + 1;
                    285:                        }
1.4       tholo     286:                        if (*p == (info ? ',' : ':'))
1.1       deraadt   287:                                break;
                    288:                }
                    289:        }
                    290:
                    291:        switch(st) {
                    292:        case -1:
                    293:                err(1, "file argument");
                    294:                /* NOTREACHED */
                    295:        case -2:
                    296:                errx(1, "potential reference loop detected");
                    297:                /* NOTREACHED */
                    298:        }
                    299:
                    300:        if (verbose)
                    301:                (void)printf("cap_mkdb: %d capability records\n", reccnt);
                    302: }
                    303:
                    304: void
1.11      deraadt   305: usage(void)
1.1       deraadt   306: {
                    307:        (void)fprintf(stderr,
1.4       tholo     308:            "usage: cap_mkdb [-iv] [-f outfile] file1 [file2 ...]\n");
1.1       deraadt   309:        exit(1);
                    310: }