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