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 #include <sys/param.h>
339b50d902SRodney W. Grimes #include <sys/stat.h>
349b50d902SRodney W. Grimes
359b50d902SRodney W. Grimes #include <db.h>
369b50d902SRodney W. Grimes #include <err.h>
379b50d902SRodney W. Grimes #include <fcntl.h>
389b50d902SRodney W. Grimes #include <stdio.h>
399b50d902SRodney W. Grimes #include <stdlib.h>
409b50d902SRodney W. Grimes #include <string.h>
419b50d902SRodney W. Grimes #include <unistd.h>
429b50d902SRodney W. Grimes
43f3d61b0cSEd Schouten static void db_build(char **);
44f3d61b0cSEd Schouten static void dounlink(void);
45f3d61b0cSEd Schouten static void usage(void);
469b50d902SRodney W. Grimes
47f3d61b0cSEd Schouten static DB *capdbp;
48f3d61b0cSEd Schouten static int verbose;
49f3d61b0cSEd Schouten static char *capname, buf[8 * 1024];
509b50d902SRodney W. Grimes
51f3d61b0cSEd Schouten static HASHINFO openinfo = {
5202172b19SRuslan Ermilov 4096, /* bsize */
5302172b19SRuslan Ermilov 0, /* ffactor */
5402172b19SRuslan Ermilov 0, /* nelem */
5502172b19SRuslan Ermilov 0, /* cachesize */
5602172b19SRuslan Ermilov NULL, /* hash() */
5702172b19SRuslan Ermilov 0 /* lorder */
5802172b19SRuslan Ermilov };
5902172b19SRuslan Ermilov
609b50d902SRodney W. Grimes /*
619b50d902SRodney W. Grimes * Mkcapdb creates a capability hash database for quick retrieval of capability
629b50d902SRodney W. Grimes * records. The database contains 2 types of entries: records and references
639b50d902SRodney W. Grimes * marked by the first byte in the data. A record entry contains the actual
649b50d902SRodney W. Grimes * capability record whereas a reference contains the name (key) under which
659b50d902SRodney W. Grimes * the correct record is stored.
669b50d902SRodney W. Grimes */
679b50d902SRodney W. Grimes int
main(int argc,char * argv[])6877ae8ac7SDavid Malone main(int argc, char *argv[])
699b50d902SRodney W. Grimes {
706fe37d13SRuslan Ermilov int byteorder, c;
719b50d902SRodney W. Grimes
729b50d902SRodney W. Grimes capname = NULL;
736fe37d13SRuslan Ermilov byteorder = 0;
746fe37d13SRuslan Ermilov while ((c = getopt(argc, argv, "bf:lv")) != -1) {
759b50d902SRodney W. Grimes switch(c) {
766fe37d13SRuslan Ermilov case 'b':
776fe37d13SRuslan Ermilov case 'l':
786fe37d13SRuslan Ermilov if (byteorder != 0)
796fe37d13SRuslan Ermilov usage();
806fe37d13SRuslan Ermilov byteorder = c == 'b' ? 4321 : 1234;
816fe37d13SRuslan Ermilov break;
829b50d902SRodney W. Grimes case 'f':
839b50d902SRodney W. Grimes capname = optarg;
849b50d902SRodney W. Grimes break;
859b50d902SRodney W. Grimes case 'v':
869b50d902SRodney W. Grimes verbose = 1;
879b50d902SRodney W. Grimes break;
889b50d902SRodney W. Grimes case '?':
899b50d902SRodney W. Grimes default:
909b50d902SRodney W. Grimes usage();
919b50d902SRodney W. Grimes }
929b50d902SRodney W. Grimes }
939b50d902SRodney W. Grimes argc -= optind;
949b50d902SRodney W. Grimes argv += optind;
959b50d902SRodney W. Grimes
969b50d902SRodney W. Grimes if (*argv == NULL)
979b50d902SRodney W. Grimes usage();
989b50d902SRodney W. Grimes
996fe37d13SRuslan Ermilov /* Set byte order. */
1006fe37d13SRuslan Ermilov openinfo.lorder = byteorder;
1016fe37d13SRuslan Ermilov
1029b50d902SRodney W. Grimes /*
1039b50d902SRodney W. Grimes * The database file is the first argument if no name is specified.
1049b50d902SRodney W. Grimes * Make arrangements to unlink it if exit badly.
1059b50d902SRodney W. Grimes */
1069b50d902SRodney W. Grimes (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
1079b50d902SRodney W. Grimes if ((capname = strdup(buf)) == NULL)
10893d6b9b6SPhilippe Charnier errx(1, "strdup failed");
1092919c53cSStefan Eßer if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
11002172b19SRuslan Ermilov DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
1119b50d902SRodney W. Grimes err(1, "%s", buf);
1129b50d902SRodney W. Grimes
1139b50d902SRodney W. Grimes if (atexit(dounlink))
1149b50d902SRodney W. Grimes err(1, "atexit");
1159b50d902SRodney W. Grimes
1169b50d902SRodney W. Grimes db_build(argv);
1179b50d902SRodney W. Grimes
1189b50d902SRodney W. Grimes if (capdbp->close(capdbp) < 0)
1199b50d902SRodney W. Grimes err(1, "%s", capname);
1209b50d902SRodney W. Grimes capname = NULL;
1219b50d902SRodney W. Grimes exit(0);
1229b50d902SRodney W. Grimes }
1239b50d902SRodney W. Grimes
124f3d61b0cSEd Schouten static void
dounlink(void)12577ae8ac7SDavid Malone dounlink(void)
1269b50d902SRodney W. Grimes {
1279b50d902SRodney W. Grimes if (capname != NULL)
1289b50d902SRodney W. Grimes (void)unlink(capname);
1299b50d902SRodney W. Grimes }
1309b50d902SRodney W. Grimes
1319b50d902SRodney W. Grimes /*
1329b50d902SRodney W. Grimes * Any changes to these definitions should be made also in the getcap(3)
1339b50d902SRodney W. Grimes * library routines.
1349b50d902SRodney W. Grimes */
1359b50d902SRodney W. Grimes #define RECOK (char)0
1369b50d902SRodney W. Grimes #define TCERR (char)1
1379b50d902SRodney W. Grimes #define SHADOW (char)2
1389b50d902SRodney W. Grimes
1399b50d902SRodney W. Grimes /*
140f66c1ecfSRuslan Ermilov * Db_build() builds the name and capability databases according to the
1419b50d902SRodney W. Grimes * details above.
1429b50d902SRodney W. Grimes */
143f3d61b0cSEd Schouten static void
db_build(char ** ifiles)14477ae8ac7SDavid Malone db_build(char **ifiles)
1459b50d902SRodney W. Grimes {
1469b50d902SRodney W. Grimes DBT key, data;
1479b50d902SRodney W. Grimes recno_t reccnt;
1489b50d902SRodney W. Grimes size_t len, bplen;
1499b50d902SRodney W. Grimes int st;
1509b50d902SRodney W. Grimes char *bp, *p, *t;
1519b50d902SRodney W. Grimes
1529b50d902SRodney W. Grimes data.data = NULL;
1539b50d902SRodney W. Grimes key.data = NULL;
1549b50d902SRodney W. Grimes for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
1559b50d902SRodney W. Grimes
1569b50d902SRodney W. Grimes /*
1579b50d902SRodney W. Grimes * Allocate enough memory to store record, terminating
1589b50d902SRodney W. Grimes * NULL and one extra byte.
1599b50d902SRodney W. Grimes */
1609b50d902SRodney W. Grimes len = strlen(bp);
1619b50d902SRodney W. Grimes if (bplen <= len + 2) {
1629b50d902SRodney W. Grimes bplen += MAX(256, len + 2);
1639b50d902SRodney W. Grimes if ((data.data = realloc(data.data, bplen)) == NULL)
16493d6b9b6SPhilippe Charnier errx(1, "malloc failed");
1659b50d902SRodney W. Grimes }
1669b50d902SRodney W. Grimes
1679b50d902SRodney W. Grimes /* Find the end of the name field. */
1689b50d902SRodney W. Grimes if ((p = strchr(bp, ':')) == NULL) {
16922694ebaSBruce Evans warnx("no name field: %.*s", (int)MIN(len, 20), bp);
1709b50d902SRodney W. Grimes continue;
1719b50d902SRodney W. Grimes }
1729b50d902SRodney W. Grimes
1739b50d902SRodney W. Grimes /* First byte of stored record indicates status. */
1749b50d902SRodney W. Grimes switch(st) {
1759b50d902SRodney W. Grimes case 1:
1769b50d902SRodney W. Grimes ((char *)(data.data))[0] = RECOK;
1779b50d902SRodney W. Grimes break;
1789b50d902SRodney W. Grimes case 2:
1799b50d902SRodney W. Grimes ((char *)(data.data))[0] = TCERR;
180653636c2SDima Dorfman warnx("record not tc expanded: %.*s", (int)(p - bp),
181653636c2SDima Dorfman bp);
1829b50d902SRodney W. Grimes break;
1839b50d902SRodney W. Grimes }
1849b50d902SRodney W. Grimes
1859b50d902SRodney W. Grimes /* Create the stored record. */
1869b50d902SRodney W. Grimes memmove(&((u_char *)(data.data))[1], bp, len + 1);
1879b50d902SRodney W. Grimes data.size = len + 2;
1889b50d902SRodney W. Grimes
1899b50d902SRodney W. Grimes /* Store the record under the name field. */
1909b50d902SRodney W. Grimes key.data = bp;
1919b50d902SRodney W. Grimes key.size = p - bp;
1929b50d902SRodney W. Grimes
1939b50d902SRodney W. Grimes switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
1949b50d902SRodney W. Grimes case -1:
1959b50d902SRodney W. Grimes err(1, "put");
1969b50d902SRodney W. Grimes /* NOTREACHED */
1979b50d902SRodney W. Grimes case 1:
1989b50d902SRodney W. Grimes warnx("ignored duplicate: %.*s",
19922694ebaSBruce Evans (int)key.size, (char *)key.data);
2009b50d902SRodney W. Grimes continue;
2019b50d902SRodney W. Grimes }
2029b50d902SRodney W. Grimes ++reccnt;
2039b50d902SRodney W. Grimes
2049b50d902SRodney W. Grimes /* If only one name, ignore the rest. */
205f66c1ecfSRuslan Ermilov *p = '\0';
206f66c1ecfSRuslan Ermilov if (strchr(bp, '|') == NULL)
2079b50d902SRodney W. Grimes continue;
208f66c1ecfSRuslan Ermilov *p = ':';
2099b50d902SRodney W. Grimes
2109b50d902SRodney W. Grimes /* The rest of the names reference the entire name. */
2119b50d902SRodney W. Grimes ((char *)(data.data))[0] = SHADOW;
2129b50d902SRodney W. Grimes memmove(&((u_char *)(data.data))[1], key.data, key.size);
2139b50d902SRodney W. Grimes data.size = key.size + 1;
2149b50d902SRodney W. Grimes
2159b50d902SRodney W. Grimes /* Store references for other names. */
2169b50d902SRodney W. Grimes for (p = t = bp;; ++p) {
2179b50d902SRodney W. Grimes if (p > t && (*p == ':' || *p == '|')) {
2189b50d902SRodney W. Grimes key.size = p - t;
2199b50d902SRodney W. Grimes key.data = t;
2209b50d902SRodney W. Grimes switch(capdbp->put(capdbp,
2219b50d902SRodney W. Grimes &key, &data, R_NOOVERWRITE)) {
2229b50d902SRodney W. Grimes case -1:
2239b50d902SRodney W. Grimes err(1, "put");
2249b50d902SRodney W. Grimes /* NOTREACHED */
2259b50d902SRodney W. Grimes case 1:
2269b50d902SRodney W. Grimes warnx("ignored duplicate: %.*s",
22722694ebaSBruce Evans (int)key.size, (char *)key.data);
2289b50d902SRodney W. Grimes }
2299b50d902SRodney W. Grimes t = p + 1;
2309b50d902SRodney W. Grimes }
2319b50d902SRodney W. Grimes if (*p == ':')
2329b50d902SRodney W. Grimes break;
2339b50d902SRodney W. Grimes }
2349b50d902SRodney W. Grimes }
2359b50d902SRodney W. Grimes
2369b50d902SRodney W. Grimes switch(st) {
2379b50d902SRodney W. Grimes case -1:
2389b50d902SRodney W. Grimes err(1, "file argument");
2399b50d902SRodney W. Grimes /* NOTREACHED */
2409b50d902SRodney W. Grimes case -2:
2419b50d902SRodney W. Grimes errx(1, "potential reference loop detected");
2429b50d902SRodney W. Grimes /* NOTREACHED */
2439b50d902SRodney W. Grimes }
2449b50d902SRodney W. Grimes
2459b50d902SRodney W. Grimes if (verbose)
2469b50d902SRodney W. Grimes (void)printf("cap_mkdb: %d capability records\n", reccnt);
2479b50d902SRodney W. Grimes }
2489b50d902SRodney W. Grimes
249f3d61b0cSEd Schouten static void
usage(void)25077ae8ac7SDavid Malone usage(void)
2519b50d902SRodney W. Grimes {
2529b50d902SRodney W. Grimes (void)fprintf(stderr,
2536fe37d13SRuslan Ermilov "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n");
2549b50d902SRodney W. Grimes exit(1);
2559b50d902SRodney W. Grimes }
256