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