xref: /freebsd/usr.bin/cap_mkdb/cap_mkdb.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
19b50d902SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1992, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #ifndef lint
33fa146c53SArchie Cobbs static const char copyright[] =
349b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\
359b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
36cbc4699cSMark Murray #endif
379b50d902SRodney W. Grimes 
389f5b04e9SDavid Malone #if 0
399b50d902SRodney W. Grimes #ifndef lint
4002172b19SRuslan Ermilov static char sccsid[] = "@(#)cap_mkdb.c	8.2 (Berkeley) 4/27/95";
4193d6b9b6SPhilippe Charnier #endif
429f5b04e9SDavid Malone #endif
439f5b04e9SDavid Malone 
449f5b04e9SDavid Malone #include <sys/cdefs.h>
459f5b04e9SDavid Malone __FBSDID("$FreeBSD$");
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <sys/param.h>
489b50d902SRodney W. Grimes #include <sys/stat.h>
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <db.h>
519b50d902SRodney W. Grimes #include <err.h>
529b50d902SRodney W. Grimes #include <fcntl.h>
539b50d902SRodney W. Grimes #include <stdio.h>
549b50d902SRodney W. Grimes #include <stdlib.h>
559b50d902SRodney W. Grimes #include <string.h>
569b50d902SRodney W. Grimes #include <unistd.h>
579b50d902SRodney W. Grimes 
58f3d61b0cSEd Schouten static void	 db_build(char **);
59f3d61b0cSEd Schouten static void	 dounlink(void);
60f3d61b0cSEd Schouten static void	 usage(void);
619b50d902SRodney W. Grimes 
62f3d61b0cSEd Schouten static DB	*capdbp;
63f3d61b0cSEd Schouten static int	 verbose;
64f3d61b0cSEd Schouten static char	*capname, buf[8 * 1024];
659b50d902SRodney W. Grimes 
66f3d61b0cSEd Schouten static HASHINFO openinfo = {
6702172b19SRuslan Ermilov 	4096,		/* bsize */
6802172b19SRuslan Ermilov 	0,		/* ffactor */
6902172b19SRuslan Ermilov 	0,		/* nelem */
7002172b19SRuslan Ermilov 	0,		/* cachesize */
7102172b19SRuslan Ermilov 	NULL,		/* hash() */
7202172b19SRuslan Ermilov 	0		/* lorder */
7302172b19SRuslan Ermilov };
7402172b19SRuslan Ermilov 
759b50d902SRodney W. Grimes /*
769b50d902SRodney W. Grimes  * Mkcapdb creates a capability hash database for quick retrieval of capability
779b50d902SRodney W. Grimes  * records.  The database contains 2 types of entries: records and references
789b50d902SRodney W. Grimes  * marked by the first byte in the data.  A record entry contains the actual
799b50d902SRodney W. Grimes  * capability record whereas a reference contains the name (key) under which
809b50d902SRodney W. Grimes  * the correct record is stored.
819b50d902SRodney W. Grimes  */
829b50d902SRodney W. Grimes int
8377ae8ac7SDavid Malone main(int argc, char *argv[])
849b50d902SRodney W. Grimes {
856fe37d13SRuslan Ermilov 	int byteorder, c;
869b50d902SRodney W. Grimes 
879b50d902SRodney W. Grimes 	capname = NULL;
886fe37d13SRuslan Ermilov 	byteorder = 0;
896fe37d13SRuslan Ermilov 	while ((c = getopt(argc, argv, "bf:lv")) != -1) {
909b50d902SRodney W. Grimes 		switch(c) {
916fe37d13SRuslan Ermilov 		case 'b':
926fe37d13SRuslan Ermilov 		case 'l':
936fe37d13SRuslan Ermilov 			if (byteorder != 0)
946fe37d13SRuslan Ermilov 				usage();
956fe37d13SRuslan Ermilov 			byteorder = c == 'b' ? 4321 : 1234;
966fe37d13SRuslan Ermilov 			break;
979b50d902SRodney W. Grimes 		case 'f':
989b50d902SRodney W. Grimes 			capname = optarg;
999b50d902SRodney W. Grimes 			break;
1009b50d902SRodney W. Grimes 		case 'v':
1019b50d902SRodney W. Grimes 			verbose = 1;
1029b50d902SRodney W. Grimes 			break;
1039b50d902SRodney W. Grimes 		case '?':
1049b50d902SRodney W. Grimes 		default:
1059b50d902SRodney W. Grimes 			usage();
1069b50d902SRodney W. Grimes 		}
1079b50d902SRodney W. Grimes 	}
1089b50d902SRodney W. Grimes 	argc -= optind;
1099b50d902SRodney W. Grimes 	argv += optind;
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes 	if (*argv == NULL)
1129b50d902SRodney W. Grimes 		usage();
1139b50d902SRodney W. Grimes 
1146fe37d13SRuslan Ermilov 	/* Set byte order. */
1156fe37d13SRuslan Ermilov 	openinfo.lorder = byteorder;
1166fe37d13SRuslan Ermilov 
1179b50d902SRodney W. Grimes 	/*
1189b50d902SRodney W. Grimes 	 * The database file is the first argument if no name is specified.
1199b50d902SRodney W. Grimes 	 * Make arrangements to unlink it if exit badly.
1209b50d902SRodney W. Grimes 	 */
1219b50d902SRodney W. Grimes 	(void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
1229b50d902SRodney W. Grimes 	if ((capname = strdup(buf)) == NULL)
12393d6b9b6SPhilippe Charnier 		errx(1, "strdup failed");
1242919c53cSStefan Eßer 	if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
12502172b19SRuslan Ermilov 	    DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
1269b50d902SRodney W. Grimes 		err(1, "%s", buf);
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes 	if (atexit(dounlink))
1299b50d902SRodney W. Grimes 		err(1, "atexit");
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes 	db_build(argv);
1329b50d902SRodney W. Grimes 
1339b50d902SRodney W. Grimes 	if (capdbp->close(capdbp) < 0)
1349b50d902SRodney W. Grimes 		err(1, "%s", capname);
1359b50d902SRodney W. Grimes 	capname = NULL;
1369b50d902SRodney W. Grimes 	exit(0);
1379b50d902SRodney W. Grimes }
1389b50d902SRodney W. Grimes 
139f3d61b0cSEd Schouten static void
14077ae8ac7SDavid Malone dounlink(void)
1419b50d902SRodney W. Grimes {
1429b50d902SRodney W. Grimes 	if (capname != NULL)
1439b50d902SRodney W. Grimes 		(void)unlink(capname);
1449b50d902SRodney W. Grimes }
1459b50d902SRodney W. Grimes 
1469b50d902SRodney W. Grimes /*
1479b50d902SRodney W. Grimes  * Any changes to these definitions should be made also in the getcap(3)
1489b50d902SRodney W. Grimes  * library routines.
1499b50d902SRodney W. Grimes  */
1509b50d902SRodney W. Grimes #define RECOK	(char)0
1519b50d902SRodney W. Grimes #define TCERR	(char)1
1529b50d902SRodney W. Grimes #define SHADOW	(char)2
1539b50d902SRodney W. Grimes 
1549b50d902SRodney W. Grimes /*
155f66c1ecfSRuslan Ermilov  * Db_build() builds the name and capability databases according to the
1569b50d902SRodney W. Grimes  * details above.
1579b50d902SRodney W. Grimes  */
158f3d61b0cSEd Schouten static void
15977ae8ac7SDavid Malone db_build(char **ifiles)
1609b50d902SRodney W. Grimes {
1619b50d902SRodney W. Grimes 	DBT key, data;
1629b50d902SRodney W. Grimes 	recno_t reccnt;
1639b50d902SRodney W. Grimes 	size_t len, bplen;
1649b50d902SRodney W. Grimes 	int st;
1659b50d902SRodney W. Grimes 	char *bp, *p, *t;
1669b50d902SRodney W. Grimes 
1679b50d902SRodney W. Grimes 	data.data = NULL;
1689b50d902SRodney W. Grimes 	key.data = NULL;
1699b50d902SRodney W. Grimes 	for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes 		/*
1729b50d902SRodney W. Grimes 		 * Allocate enough memory to store record, terminating
1739b50d902SRodney W. Grimes 		 * NULL and one extra byte.
1749b50d902SRodney W. Grimes 		 */
1759b50d902SRodney W. Grimes 		len = strlen(bp);
1769b50d902SRodney W. Grimes 		if (bplen <= len + 2) {
1779b50d902SRodney W. Grimes 			bplen += MAX(256, len + 2);
1789b50d902SRodney W. Grimes 			if ((data.data = realloc(data.data, bplen)) == NULL)
17993d6b9b6SPhilippe Charnier 				errx(1, "malloc failed");
1809b50d902SRodney W. Grimes 		}
1819b50d902SRodney W. Grimes 
1829b50d902SRodney W. Grimes 		/* Find the end of the name field. */
1839b50d902SRodney W. Grimes 		if ((p = strchr(bp, ':')) == NULL) {
18422694ebaSBruce Evans 			warnx("no name field: %.*s", (int)MIN(len, 20), bp);
1859b50d902SRodney W. Grimes 			continue;
1869b50d902SRodney W. Grimes 		}
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes 		/* First byte of stored record indicates status. */
1899b50d902SRodney W. Grimes 		switch(st) {
1909b50d902SRodney W. Grimes 		case 1:
1919b50d902SRodney W. Grimes 			((char *)(data.data))[0] = RECOK;
1929b50d902SRodney W. Grimes 			break;
1939b50d902SRodney W. Grimes 		case 2:
1949b50d902SRodney W. Grimes 			((char *)(data.data))[0] = TCERR;
195653636c2SDima Dorfman 			warnx("record not tc expanded: %.*s", (int)(p - bp),
196653636c2SDima Dorfman 			    bp);
1979b50d902SRodney W. Grimes 			break;
1989b50d902SRodney W. Grimes 		}
1999b50d902SRodney W. Grimes 
2009b50d902SRodney W. Grimes 		/* Create the stored record. */
2019b50d902SRodney W. Grimes 		memmove(&((u_char *)(data.data))[1], bp, len + 1);
2029b50d902SRodney W. Grimes 		data.size = len + 2;
2039b50d902SRodney W. Grimes 
2049b50d902SRodney W. Grimes 		/* Store the record under the name field. */
2059b50d902SRodney W. Grimes 		key.data = bp;
2069b50d902SRodney W. Grimes 		key.size = p - bp;
2079b50d902SRodney W. Grimes 
2089b50d902SRodney W. Grimes 		switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
2099b50d902SRodney W. Grimes 		case -1:
2109b50d902SRodney W. Grimes 			err(1, "put");
2119b50d902SRodney W. Grimes 			/* NOTREACHED */
2129b50d902SRodney W. Grimes 		case 1:
2139b50d902SRodney W. Grimes 			warnx("ignored duplicate: %.*s",
21422694ebaSBruce Evans 			    (int)key.size, (char *)key.data);
2159b50d902SRodney W. Grimes 			continue;
2169b50d902SRodney W. Grimes 		}
2179b50d902SRodney W. Grimes 		++reccnt;
2189b50d902SRodney W. Grimes 
2199b50d902SRodney W. Grimes 		/* If only one name, ignore the rest. */
220f66c1ecfSRuslan Ermilov 		*p = '\0';
221f66c1ecfSRuslan Ermilov 		if (strchr(bp, '|') == NULL)
2229b50d902SRodney W. Grimes 			continue;
223f66c1ecfSRuslan Ermilov 		*p = ':';
2249b50d902SRodney W. Grimes 
2259b50d902SRodney W. Grimes 		/* The rest of the names reference the entire name. */
2269b50d902SRodney W. Grimes 		((char *)(data.data))[0] = SHADOW;
2279b50d902SRodney W. Grimes 		memmove(&((u_char *)(data.data))[1], key.data, key.size);
2289b50d902SRodney W. Grimes 		data.size = key.size + 1;
2299b50d902SRodney W. Grimes 
2309b50d902SRodney W. Grimes 		/* Store references for other names. */
2319b50d902SRodney W. Grimes 		for (p = t = bp;; ++p) {
2329b50d902SRodney W. Grimes 			if (p > t && (*p == ':' || *p == '|')) {
2339b50d902SRodney W. Grimes 				key.size = p - t;
2349b50d902SRodney W. Grimes 				key.data = t;
2359b50d902SRodney W. Grimes 				switch(capdbp->put(capdbp,
2369b50d902SRodney W. Grimes 				    &key, &data, R_NOOVERWRITE)) {
2379b50d902SRodney W. Grimes 				case -1:
2389b50d902SRodney W. Grimes 					err(1, "put");
2399b50d902SRodney W. Grimes 					/* NOTREACHED */
2409b50d902SRodney W. Grimes 				case 1:
2419b50d902SRodney W. Grimes 					warnx("ignored duplicate: %.*s",
24222694ebaSBruce Evans 					    (int)key.size, (char *)key.data);
2439b50d902SRodney W. Grimes 				}
2449b50d902SRodney W. Grimes 				t = p + 1;
2459b50d902SRodney W. Grimes 			}
2469b50d902SRodney W. Grimes 			if (*p == ':')
2479b50d902SRodney W. Grimes 				break;
2489b50d902SRodney W. Grimes 		}
2499b50d902SRodney W. Grimes 	}
2509b50d902SRodney W. Grimes 
2519b50d902SRodney W. Grimes 	switch(st) {
2529b50d902SRodney W. Grimes 	case -1:
2539b50d902SRodney W. Grimes 		err(1, "file argument");
2549b50d902SRodney W. Grimes 		/* NOTREACHED */
2559b50d902SRodney W. Grimes 	case -2:
2569b50d902SRodney W. Grimes 		errx(1, "potential reference loop detected");
2579b50d902SRodney W. Grimes 		/* NOTREACHED */
2589b50d902SRodney W. Grimes 	}
2599b50d902SRodney W. Grimes 
2609b50d902SRodney W. Grimes 	if (verbose)
2619b50d902SRodney W. Grimes 		(void)printf("cap_mkdb: %d capability records\n", reccnt);
2629b50d902SRodney W. Grimes }
2639b50d902SRodney W. Grimes 
264f3d61b0cSEd Schouten static void
26577ae8ac7SDavid Malone usage(void)
2669b50d902SRodney W. Grimes {
2679b50d902SRodney W. Grimes 	(void)fprintf(stderr,
2686fe37d13SRuslan Ermilov 	    "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n");
2699b50d902SRodney W. Grimes 	exit(1);
2709b50d902SRodney W. Grimes }
271