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