Home | History | Annotate | Line # | Download | only in cap_mkdb
cap_mkdb.c revision 1.20
      1 /*	$NetBSD: cap_mkdb.c,v 1.20 2003/10/27 00:12:43 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, 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  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 #if !defined(lint)
     38 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #if 0
     41 static char sccsid[] = "@(#)cap_mkdb.c	8.2 (Berkeley) 4/27/95";
     42 #endif
     43 __RCSID("$NetBSD: cap_mkdb.c,v 1.20 2003/10/27 00:12:43 lukem Exp $");
     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 <fcntl.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 #include <ctype.h>
     57 
     58 static void	db_build (char **);
     59 static void	dounlink (void);
     60 static void	usage (void);
     61 static int	count_records(char **);
     62 
     63 DB *capdbp;
     64 int verbose;
     65 char *capdb, *capname, buf[8 * 1024];
     66 
     67 HASHINFO openinfo = {
     68 	4096,		/* bsize */
     69 	16,		/* ffactor */
     70 	2048,		/* nelem */
     71 	2048 * 1024,	/* cachesize */
     72 	NULL,		/* hash() */
     73 	0		/* lorder */
     74 };
     75 
     76 /*
     77  * Mkcapdb creates a capability hash database for quick retrieval of capability
     78  * records.  The database contains 2 types of entries: records and references
     79  * marked by the first byte in the data.  A record entry contains the actual
     80  * capability record whereas a reference contains the name (key) under which
     81  * the correct record is stored.
     82  */
     83 int
     84 main(int argc, char *argv[])
     85 {
     86 	int c, byteorder;
     87 
     88 	capname = NULL;
     89 	byteorder = 0;
     90 	while ((c = getopt(argc, argv, "bf:lv")) != -1) {
     91 		switch(c) {
     92 		case 'b':
     93 		case 'l':
     94 			if (byteorder != 0)
     95 				usage();
     96 			byteorder = c == 'b' ? 4321 : 1234;
     97 			break;
     98 		case 'f':
     99 			capname = optarg;
    100 			break;
    101 		case 'v':
    102 			verbose = 1;
    103 			break;
    104 		case '?':
    105 		default:
    106 			usage();
    107 		}
    108 	}
    109 	argc -= optind;
    110 	argv += optind;
    111 
    112 	if (*argv == NULL)
    113 		usage();
    114 
    115 	/* Set byte order */
    116 	openinfo.lorder = byteorder;
    117 
    118 	/*
    119 	 * Set nelem to twice the value returned by count_record().
    120 	 */
    121 	openinfo.nelem = count_records(argv) << 1;
    122 
    123 	/*
    124 	 * The database file is the first argument if no name is specified.
    125 	 * Make arrangements to unlink it if exit badly.
    126 	 */
    127 	(void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
    128 	if ((capname = strdup(buf)) == NULL)
    129 		err(1, "strdup");
    130 	if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
    131 	    DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
    132 		err(1, "%s", buf);
    133 
    134 	if (atexit(dounlink))
    135 		err(1, "atexit");
    136 
    137 	db_build(argv);
    138 
    139 	if (capdbp->close(capdbp) < 0)
    140 		err(1, "%s", capname);
    141 	capname = NULL;
    142 	exit(0);
    143 }
    144 
    145 static void
    146 dounlink(void)
    147 {
    148 	if (capname != NULL)
    149 		(void)unlink(capname);
    150 }
    151 
    152 /*
    153  * Any changes to these definitions should be made also in the getcap(3)
    154  * library routines.
    155  */
    156 #define RECOK	(char)0
    157 #define TCERR	(char)1
    158 #define SHADOW	(char)2
    159 
    160 /*
    161  * Db_build() builds the name and capabilty databases according to the
    162  * details above.
    163  */
    164 static void
    165 db_build(char **ifiles)
    166 {
    167 	DBT key, data;
    168 	recno_t reccnt;
    169 	size_t len, bplen;
    170 	int st;
    171 	char *bp, *p, *t, *n;
    172 
    173 	data.data = NULL;
    174 	key.data = NULL;
    175 	for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0; free(bp)){
    176 
    177 		/*
    178 		 * Allocate enough memory to store record, terminating
    179 		 * NULL and one extra byte.
    180 		 */
    181 		len = strlen(bp);
    182 		if (bplen <= len + 2) {
    183 			if ((n = realloc(data.data,
    184 			    bplen + MAX(256, len + 2))) == NULL)
    185 				err(1, "realloc");
    186 			data.data = n;
    187 			bplen += MAX(256, len + 2);
    188 		}
    189 
    190 		/* Find the end of the name field. */
    191 		if ((p = strchr(bp, ':')) == NULL) {
    192 			warnx("no name field: %.*s", (int)(MIN(len, 20)), bp);
    193 			continue;
    194 		}
    195 
    196 		/* First byte of stored record indicates status. */
    197 		switch(st) {
    198 		case 1:
    199 			((char *)(data.data))[0] = RECOK;
    200 			break;
    201 		case 2:
    202 			((char *)(data.data))[0] = TCERR;
    203 			warnx("Record not tc expanded: %.*s", (int)(p - bp),bp);
    204 			break;
    205 		}
    206 
    207 		/* Create the stored record. */
    208 		memmove(&((u_char *)(data.data))[1], bp, len + 1);
    209 		data.size = len + 2;
    210 
    211 		/* Store the record under the name field. */
    212 		key.data = bp;
    213 		key.size = p - bp;
    214 
    215 		switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
    216 		case -1:
    217 			err(1, "put");
    218 			/* NOTREACHED */
    219 		case 1:
    220 			warnx("ignored duplicate: %.*s",
    221 			    (int)key.size, (char *)key.data);
    222 			continue;
    223 		}
    224 		++reccnt;
    225 
    226 		/* If only one name, ignore the rest. */
    227 		if ((p = strchr(bp, '|')) == NULL)
    228 			continue;
    229 
    230 		/* The rest of the names reference the entire name. */
    231 		((char *)(data.data))[0] = SHADOW;
    232 		memmove(&((u_char *)(data.data))[1], key.data, key.size);
    233 		data.size = key.size + 1;
    234 
    235 		/* Store references for other names. */
    236 		for (p = t = bp;; ++p) {
    237 			if (p > t && (*p == ':' || *p == '|')) {
    238 				key.size = p - t;
    239 				key.data = t;
    240 				switch(capdbp->put(capdbp,
    241 				    &key, &data, R_NOOVERWRITE)) {
    242 				case -1:
    243 					err(1, "put");
    244 					/* NOTREACHED */
    245 				case 1:
    246 					warnx("ignored duplicate: %.*s",
    247 					    (int)key.size, (char *)key.data);
    248 				}
    249 				t = p + 1;
    250 			}
    251 			if (*p == ':')
    252 				break;
    253 		}
    254 	}
    255 
    256 	switch(st) {
    257 	case -1:
    258 		err(1, "file argument");
    259 		/* NOTREACHED */
    260 	case -2:
    261 		errx(1, "potential reference loop detected");
    262 		/* NOTREACHED */
    263 	}
    264 
    265 	if (verbose)
    266 		(void)printf("cap_mkdb: %d capability records\n", reccnt);
    267 }
    268 
    269 static void
    270 usage(void)
    271 {
    272 	(void)fprintf(stderr,
    273 	    "usage: cap_mkdb [-b|-l] [-v] [-f outfile] file1 [file2 ...]\n");
    274 	exit(1);
    275 }
    276 
    277 /*
    278  * Count number of records in input files. This does not need
    279  * to be really accurate (the result is used only as a hint).
    280  * It seems to match number of records should a cgetnext() be used, though.
    281  */
    282 static int
    283 count_records(char **list)
    284 {
    285 	FILE *fp;
    286 	char *line;
    287 	size_t len;
    288 	int nelem, slash;
    289 
    290 	/* scan input files and count individual records */
    291 	for(nelem = 0, slash = 0; *list && (fp = fopen(*list++, "r")); ) {
    292 		while((line = fgetln(fp, &len))) {
    293 			if (len < 2)
    294 				continue;
    295 			if (!isspace((unsigned char) *line) && *line != ':'
    296 				&& *line != '#' && !slash)
    297 				nelem++;
    298 
    299 			slash = (line[len - 2] == '\\');
    300 		}
    301 		fclose(fp);
    302 	}
    303 
    304 	if (nelem == 0) {
    305 		/* no records found; pass default size hint */
    306 		nelem = 1;
    307 	} else if (!powerof2(nelem)) {
    308 		/* set nelem to nearest bigger power-of-two number */
    309 		int bt = 1;
    310 		while(bt < nelem) bt <<= 1;
    311 		nelem = bt;
    312 	}
    313 
    314 	return nelem;
    315 }
    316