19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1992, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 149b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 159b50d902SRodney W. Grimes * without specific prior written permission. 169b50d902SRodney W. Grimes * 179b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 189b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 199b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 209b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 219b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 229b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 239b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 249b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 259b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 269b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 279b50d902SRodney W. Grimes * SUCH DAMAGE. 289b50d902SRodney W. Grimes */ 299b50d902SRodney W. Grimes 309b50d902SRodney W. Grimes #ifndef lint 31fa146c53SArchie Cobbs static const char copyright[] = 329b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\ 339b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 34cbc4699cSMark Murray #endif 359b50d902SRodney W. Grimes 369f5b04e9SDavid Malone #if 0 379b50d902SRodney W. Grimes #ifndef lint 3802172b19SRuslan Ermilov static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95"; 3993d6b9b6SPhilippe Charnier #endif 409f5b04e9SDavid Malone #endif 419f5b04e9SDavid Malone 429f5b04e9SDavid Malone #include <sys/cdefs.h> 439f5b04e9SDavid Malone __FBSDID("$FreeBSD$"); 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <sys/param.h> 469b50d902SRodney W. Grimes #include <sys/stat.h> 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <db.h> 499b50d902SRodney W. Grimes #include <err.h> 509b50d902SRodney W. Grimes #include <fcntl.h> 519b50d902SRodney W. Grimes #include <stdio.h> 529b50d902SRodney W. Grimes #include <stdlib.h> 539b50d902SRodney W. Grimes #include <string.h> 549b50d902SRodney W. Grimes #include <unistd.h> 559b50d902SRodney W. Grimes 56f3d61b0cSEd Schouten static void db_build(char **); 57f3d61b0cSEd Schouten static void dounlink(void); 58f3d61b0cSEd Schouten static void usage(void); 599b50d902SRodney W. Grimes 60f3d61b0cSEd Schouten static DB *capdbp; 61f3d61b0cSEd Schouten static int verbose; 62f3d61b0cSEd Schouten static char *capname, buf[8 * 1024]; 639b50d902SRodney W. Grimes 64f3d61b0cSEd Schouten static HASHINFO openinfo = { 6502172b19SRuslan Ermilov 4096, /* bsize */ 6602172b19SRuslan Ermilov 0, /* ffactor */ 6702172b19SRuslan Ermilov 0, /* nelem */ 6802172b19SRuslan Ermilov 0, /* cachesize */ 6902172b19SRuslan Ermilov NULL, /* hash() */ 7002172b19SRuslan Ermilov 0 /* lorder */ 7102172b19SRuslan Ermilov }; 7202172b19SRuslan Ermilov 739b50d902SRodney W. Grimes /* 749b50d902SRodney W. Grimes * Mkcapdb creates a capability hash database for quick retrieval of capability 759b50d902SRodney W. Grimes * records. The database contains 2 types of entries: records and references 769b50d902SRodney W. Grimes * marked by the first byte in the data. A record entry contains the actual 779b50d902SRodney W. Grimes * capability record whereas a reference contains the name (key) under which 789b50d902SRodney W. Grimes * the correct record is stored. 799b50d902SRodney W. Grimes */ 809b50d902SRodney W. Grimes int 8177ae8ac7SDavid Malone main(int argc, char *argv[]) 829b50d902SRodney W. Grimes { 836fe37d13SRuslan Ermilov int byteorder, c; 849b50d902SRodney W. Grimes 859b50d902SRodney W. Grimes capname = NULL; 866fe37d13SRuslan Ermilov byteorder = 0; 876fe37d13SRuslan Ermilov while ((c = getopt(argc, argv, "bf:lv")) != -1) { 889b50d902SRodney W. Grimes switch(c) { 896fe37d13SRuslan Ermilov case 'b': 906fe37d13SRuslan Ermilov case 'l': 916fe37d13SRuslan Ermilov if (byteorder != 0) 926fe37d13SRuslan Ermilov usage(); 936fe37d13SRuslan Ermilov byteorder = c == 'b' ? 4321 : 1234; 946fe37d13SRuslan Ermilov break; 959b50d902SRodney W. Grimes case 'f': 969b50d902SRodney W. Grimes capname = optarg; 979b50d902SRodney W. Grimes break; 989b50d902SRodney W. Grimes case 'v': 999b50d902SRodney W. Grimes verbose = 1; 1009b50d902SRodney W. Grimes break; 1019b50d902SRodney W. Grimes case '?': 1029b50d902SRodney W. Grimes default: 1039b50d902SRodney W. Grimes usage(); 1049b50d902SRodney W. Grimes } 1059b50d902SRodney W. Grimes } 1069b50d902SRodney W. Grimes argc -= optind; 1079b50d902SRodney W. Grimes argv += optind; 1089b50d902SRodney W. Grimes 1099b50d902SRodney W. Grimes if (*argv == NULL) 1109b50d902SRodney W. Grimes usage(); 1119b50d902SRodney W. Grimes 1126fe37d13SRuslan Ermilov /* Set byte order. */ 1136fe37d13SRuslan Ermilov openinfo.lorder = byteorder; 1146fe37d13SRuslan Ermilov 1159b50d902SRodney W. Grimes /* 1169b50d902SRodney W. Grimes * The database file is the first argument if no name is specified. 1179b50d902SRodney W. Grimes * Make arrangements to unlink it if exit badly. 1189b50d902SRodney W. Grimes */ 1199b50d902SRodney W. Grimes (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv); 1209b50d902SRodney W. Grimes if ((capname = strdup(buf)) == NULL) 12193d6b9b6SPhilippe Charnier errx(1, "strdup failed"); 122*2919c53cSStefan Eßer if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR, 12302172b19SRuslan Ermilov DEFFILEMODE, DB_HASH, &openinfo)) == NULL) 1249b50d902SRodney W. Grimes err(1, "%s", buf); 1259b50d902SRodney W. Grimes 1269b50d902SRodney W. Grimes if (atexit(dounlink)) 1279b50d902SRodney W. Grimes err(1, "atexit"); 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes db_build(argv); 1309b50d902SRodney W. Grimes 1319b50d902SRodney W. Grimes if (capdbp->close(capdbp) < 0) 1329b50d902SRodney W. Grimes err(1, "%s", capname); 1339b50d902SRodney W. Grimes capname = NULL; 1349b50d902SRodney W. Grimes exit(0); 1359b50d902SRodney W. Grimes } 1369b50d902SRodney W. Grimes 137f3d61b0cSEd Schouten static void 13877ae8ac7SDavid Malone dounlink(void) 1399b50d902SRodney W. Grimes { 1409b50d902SRodney W. Grimes if (capname != NULL) 1419b50d902SRodney W. Grimes (void)unlink(capname); 1429b50d902SRodney W. Grimes } 1439b50d902SRodney W. Grimes 1449b50d902SRodney W. Grimes /* 1459b50d902SRodney W. Grimes * Any changes to these definitions should be made also in the getcap(3) 1469b50d902SRodney W. Grimes * library routines. 1479b50d902SRodney W. Grimes */ 1489b50d902SRodney W. Grimes #define RECOK (char)0 1499b50d902SRodney W. Grimes #define TCERR (char)1 1509b50d902SRodney W. Grimes #define SHADOW (char)2 1519b50d902SRodney W. Grimes 1529b50d902SRodney W. Grimes /* 153f66c1ecfSRuslan Ermilov * Db_build() builds the name and capability databases according to the 1549b50d902SRodney W. Grimes * details above. 1559b50d902SRodney W. Grimes */ 156f3d61b0cSEd Schouten static void 15777ae8ac7SDavid Malone db_build(char **ifiles) 1589b50d902SRodney W. Grimes { 1599b50d902SRodney W. Grimes DBT key, data; 1609b50d902SRodney W. Grimes recno_t reccnt; 1619b50d902SRodney W. Grimes size_t len, bplen; 1629b50d902SRodney W. Grimes int st; 1639b50d902SRodney W. Grimes char *bp, *p, *t; 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes data.data = NULL; 1669b50d902SRodney W. Grimes key.data = NULL; 1679b50d902SRodney W. Grimes for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) { 1689b50d902SRodney W. Grimes 1699b50d902SRodney W. Grimes /* 1709b50d902SRodney W. Grimes * Allocate enough memory to store record, terminating 1719b50d902SRodney W. Grimes * NULL and one extra byte. 1729b50d902SRodney W. Grimes */ 1739b50d902SRodney W. Grimes len = strlen(bp); 1749b50d902SRodney W. Grimes if (bplen <= len + 2) { 1759b50d902SRodney W. Grimes bplen += MAX(256, len + 2); 1769b50d902SRodney W. Grimes if ((data.data = realloc(data.data, bplen)) == NULL) 17793d6b9b6SPhilippe Charnier errx(1, "malloc failed"); 1789b50d902SRodney W. Grimes } 1799b50d902SRodney W. Grimes 1809b50d902SRodney W. Grimes /* Find the end of the name field. */ 1819b50d902SRodney W. Grimes if ((p = strchr(bp, ':')) == NULL) { 18222694ebaSBruce Evans warnx("no name field: %.*s", (int)MIN(len, 20), bp); 1839b50d902SRodney W. Grimes continue; 1849b50d902SRodney W. Grimes } 1859b50d902SRodney W. Grimes 1869b50d902SRodney W. Grimes /* First byte of stored record indicates status. */ 1879b50d902SRodney W. Grimes switch(st) { 1889b50d902SRodney W. Grimes case 1: 1899b50d902SRodney W. Grimes ((char *)(data.data))[0] = RECOK; 1909b50d902SRodney W. Grimes break; 1919b50d902SRodney W. Grimes case 2: 1929b50d902SRodney W. Grimes ((char *)(data.data))[0] = TCERR; 193653636c2SDima Dorfman warnx("record not tc expanded: %.*s", (int)(p - bp), 194653636c2SDima Dorfman bp); 1959b50d902SRodney W. Grimes break; 1969b50d902SRodney W. Grimes } 1979b50d902SRodney W. Grimes 1989b50d902SRodney W. Grimes /* Create the stored record. */ 1999b50d902SRodney W. Grimes memmove(&((u_char *)(data.data))[1], bp, len + 1); 2009b50d902SRodney W. Grimes data.size = len + 2; 2019b50d902SRodney W. Grimes 2029b50d902SRodney W. Grimes /* Store the record under the name field. */ 2039b50d902SRodney W. Grimes key.data = bp; 2049b50d902SRodney W. Grimes key.size = p - bp; 2059b50d902SRodney W. Grimes 2069b50d902SRodney W. Grimes switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) { 2079b50d902SRodney W. Grimes case -1: 2089b50d902SRodney W. Grimes err(1, "put"); 2099b50d902SRodney W. Grimes /* NOTREACHED */ 2109b50d902SRodney W. Grimes case 1: 2119b50d902SRodney W. Grimes warnx("ignored duplicate: %.*s", 21222694ebaSBruce Evans (int)key.size, (char *)key.data); 2139b50d902SRodney W. Grimes continue; 2149b50d902SRodney W. Grimes } 2159b50d902SRodney W. Grimes ++reccnt; 2169b50d902SRodney W. Grimes 2179b50d902SRodney W. Grimes /* If only one name, ignore the rest. */ 218f66c1ecfSRuslan Ermilov *p = '\0'; 219f66c1ecfSRuslan Ermilov if (strchr(bp, '|') == NULL) 2209b50d902SRodney W. Grimes continue; 221f66c1ecfSRuslan Ermilov *p = ':'; 2229b50d902SRodney W. Grimes 2239b50d902SRodney W. Grimes /* The rest of the names reference the entire name. */ 2249b50d902SRodney W. Grimes ((char *)(data.data))[0] = SHADOW; 2259b50d902SRodney W. Grimes memmove(&((u_char *)(data.data))[1], key.data, key.size); 2269b50d902SRodney W. Grimes data.size = key.size + 1; 2279b50d902SRodney W. Grimes 2289b50d902SRodney W. Grimes /* Store references for other names. */ 2299b50d902SRodney W. Grimes for (p = t = bp;; ++p) { 2309b50d902SRodney W. Grimes if (p > t && (*p == ':' || *p == '|')) { 2319b50d902SRodney W. Grimes key.size = p - t; 2329b50d902SRodney W. Grimes key.data = t; 2339b50d902SRodney W. Grimes switch(capdbp->put(capdbp, 2349b50d902SRodney W. Grimes &key, &data, R_NOOVERWRITE)) { 2359b50d902SRodney W. Grimes case -1: 2369b50d902SRodney W. Grimes err(1, "put"); 2379b50d902SRodney W. Grimes /* NOTREACHED */ 2389b50d902SRodney W. Grimes case 1: 2399b50d902SRodney W. Grimes warnx("ignored duplicate: %.*s", 24022694ebaSBruce Evans (int)key.size, (char *)key.data); 2419b50d902SRodney W. Grimes } 2429b50d902SRodney W. Grimes t = p + 1; 2439b50d902SRodney W. Grimes } 2449b50d902SRodney W. Grimes if (*p == ':') 2459b50d902SRodney W. Grimes break; 2469b50d902SRodney W. Grimes } 2479b50d902SRodney W. Grimes } 2489b50d902SRodney W. Grimes 2499b50d902SRodney W. Grimes switch(st) { 2509b50d902SRodney W. Grimes case -1: 2519b50d902SRodney W. Grimes err(1, "file argument"); 2529b50d902SRodney W. Grimes /* NOTREACHED */ 2539b50d902SRodney W. Grimes case -2: 2549b50d902SRodney W. Grimes errx(1, "potential reference loop detected"); 2559b50d902SRodney W. Grimes /* NOTREACHED */ 2569b50d902SRodney W. Grimes } 2579b50d902SRodney W. Grimes 2589b50d902SRodney W. Grimes if (verbose) 2599b50d902SRodney W. Grimes (void)printf("cap_mkdb: %d capability records\n", reccnt); 2609b50d902SRodney W. Grimes } 2619b50d902SRodney W. Grimes 262f3d61b0cSEd Schouten static void 26377ae8ac7SDavid Malone usage(void) 2649b50d902SRodney W. Grimes { 2659b50d902SRodney W. Grimes (void)fprintf(stderr, 2666fe37d13SRuslan Ermilov "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n"); 2679b50d902SRodney W. Grimes exit(1); 2689b50d902SRodney W. Grimes } 269