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

1.2     ! deraadt     1: /*     $OpenBSD: cap_mkdb.c,v 1.5 1995/09/02 05:47:12 jtc 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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1992, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
                     46: #endif
1.2     ! deraadt    47: static char rcsid[] = "$OpenBSD: cap_mkdb.c,v 1.5 1995/09/02 05:47:12 jtc Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: #include <sys/param.h>
                     51: #include <sys/stat.h>
                     52:
                     53: #include <db.h>
                     54: #include <err.h>
                     55: #include <errno.h>
                     56: #include <fcntl.h>
                     57: #include <limits.h>
                     58: #include <stdio.h>
                     59: #include <stdlib.h>
                     60: #include <string.h>
                     61: #include <unistd.h>
                     62:
                     63: void    db_build __P((char **));
                     64: void    dounlink __P((void));
                     65: void    usage __P((void));
                     66:
                     67: DB *capdbp;
                     68: int verbose;
                     69: char *capdb, *capname, buf[8 * 1024];
                     70:
                     71: HASHINFO openinfo = {
                     72:        4096,           /* bsize */
                     73:        16,             /* ffactor */
                     74:        256,            /* nelem */
                     75:        2048 * 1024,    /* cachesize */
                     76:        NULL,           /* hash() */
                     77:        0               /* lorder */
                     78: };
                     79:
                     80: /*
                     81:  * Mkcapdb creates a capability hash database for quick retrieval of capability
                     82:  * records.  The database contains 2 types of entries: records and references
                     83:  * marked by the first byte in the data.  A record entry contains the actual
                     84:  * capability record whereas a reference contains the name (key) under which
                     85:  * the correct record is stored.
                     86:  */
                     87: int
                     88: main(argc, argv)
                     89:        int argc;
                     90:        char *argv[];
                     91: {
                     92:        int c;
                     93:
                     94:        capname = NULL;
                     95:        while ((c = getopt(argc, argv, "f:v")) != EOF) {
                     96:                switch(c) {
                     97:                case 'f':
                     98:                        capname = optarg;
                     99:                        break;
                    100:                case 'v':
                    101:                        verbose = 1;
                    102:                        break;
                    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.
                    116:         * Make arrangements to unlink it if exit badly.
                    117:         */
                    118:        (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
                    119:        if ((capname = strdup(buf)) == NULL)
                    120:                err(1, "");
                    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
                    137: dounlink()
                    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: /*
                    152:  * Db_build() builds the name and capabilty databases according to the
                    153:  * details above.
                    154:  */
                    155: void
                    156: db_build(ifiles)
                    157:        char **ifiles;
                    158: {
                    159:        DBT key, data;
                    160:        recno_t reccnt;
                    161:        size_t len, bplen;
                    162:        int st;
                    163:        char *bp, *p, *t;
                    164:
                    165:        data.data = NULL;
                    166:        key.data = NULL;
                    167:        for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
                    168:
                    169:                /*
                    170:                 * Allocate enough memory to store record, terminating
                    171:                 * NULL and one extra byte.
                    172:                 */
                    173:                len = strlen(bp);
                    174:                if (bplen <= len + 2) {
                    175:                        bplen += MAX(256, len + 2);
                    176:                        if ((data.data = realloc(data.data, bplen)) == NULL)
                    177:                                err(1, "");
                    178:                }
                    179:
                    180:                /* Find the end of the name field. */
                    181:                if ((p = strchr(bp, ':')) == NULL) {
                    182:                        warnx("no name field: %.*s", MIN(len, 20), bp);
                    183:                        continue;
                    184:                }
                    185:
                    186:                /* First byte of stored record indicates status. */
                    187:                switch(st) {
                    188:                case 1:
                    189:                        ((char *)(data.data))[0] = RECOK;
                    190:                        break;
                    191:                case 2:
                    192:                        ((char *)(data.data))[0] = TCERR;
                    193:                        warnx("Record not tc expanded: %.*s", p - bp, bp);
                    194:                        break;
                    195:                }
                    196:
                    197:                /* Create the stored record. */
                    198:                memmove(&((u_char *)(data.data))[1], bp, len + 1);
                    199:                data.size = len + 2;
                    200:
                    201:                /* Store the record under the name field. */
                    202:                key.data = bp;
                    203:                key.size = p - bp;
                    204:
                    205:                switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
                    206:                case -1:
                    207:                        err(1, "put");
                    208:                        /* NOTREACHED */
                    209:                case 1:
                    210:                        warnx("ignored duplicate: %.*s",
                    211:                            key.size, (char *)key.data);
                    212:                        continue;
                    213:                }
                    214:                ++reccnt;
                    215:
                    216:                /* If only one name, ignore the rest. */
                    217:                if ((p = strchr(bp, '|')) == NULL)
                    218:                        continue;
                    219:
                    220:                /* The rest of the names reference the entire name. */
                    221:                ((char *)(data.data))[0] = SHADOW;
                    222:                memmove(&((u_char *)(data.data))[1], key.data, key.size);
                    223:                data.size = key.size + 1;
                    224:
                    225:                /* Store references for other names. */
                    226:                for (p = t = bp;; ++p) {
                    227:                        if (p > t && (*p == ':' || *p == '|')) {
                    228:                                key.size = p - t;
                    229:                                key.data = t;
                    230:                                switch(capdbp->put(capdbp,
                    231:                                    &key, &data, R_NOOVERWRITE)) {
                    232:                                case -1:
                    233:                                        err(1, "put");
                    234:                                        /* NOTREACHED */
                    235:                                case 1:
                    236:                                        warnx("ignored duplicate: %.*s",
                    237:                                            key.size, (char *)key.data);
                    238:                                }
                    239:                                t = p + 1;
                    240:                        }
                    241:                        if (*p == ':')
                    242:                                break;
                    243:                }
                    244:        }
                    245:
                    246:        switch(st) {
                    247:        case -1:
                    248:                err(1, "file argument");
                    249:                /* NOTREACHED */
                    250:        case -2:
                    251:                errx(1, "potential reference loop detected");
                    252:                /* NOTREACHED */
                    253:        }
                    254:
                    255:        if (verbose)
                    256:                (void)printf("cap_mkdb: %d capability records\n", reccnt);
                    257: }
                    258:
                    259: void
                    260: usage()
                    261: {
                    262:        (void)fprintf(stderr,
                    263:            "usage: cap_mkdb [-v] [-f outfile] file1 [file2 ...]\n");
                    264:        exit(1);
                    265: }