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