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 * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 35fa146c53SArchie Cobbs static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 38cbc4699cSMark Murray #endif 399b50d902SRodney W. Grimes 409f5b04e9SDavid Malone #if 0 419b50d902SRodney W. Grimes #ifndef lint 429f5b04e9SDavid Malone static char sccsid[] = "@(#)cap_mkdb.c 8.1 (Berkeley) 6/6/93"; 4393d6b9b6SPhilippe Charnier #endif 449f5b04e9SDavid Malone #endif 459f5b04e9SDavid Malone 469f5b04e9SDavid Malone #include <sys/cdefs.h> 479f5b04e9SDavid Malone __FBSDID("$FreeBSD$"); 489b50d902SRodney W. Grimes 499b50d902SRodney W. Grimes #include <sys/param.h> 509b50d902SRodney W. Grimes #include <sys/stat.h> 519b50d902SRodney W. Grimes 529b50d902SRodney W. Grimes #include <db.h> 539b50d902SRodney W. Grimes #include <err.h> 549b50d902SRodney W. Grimes #include <fcntl.h> 559b50d902SRodney W. Grimes #include <stdio.h> 569b50d902SRodney W. Grimes #include <stdlib.h> 579b50d902SRodney W. Grimes #include <string.h> 589b50d902SRodney W. Grimes #include <unistd.h> 599b50d902SRodney W. Grimes 60f1bb2cd2SWarner Losh void db_build(char **); 61f1bb2cd2SWarner Losh void dounlink(void); 62f1bb2cd2SWarner Losh void usage(void); 639b50d902SRodney W. Grimes 649b50d902SRodney W. Grimes DB *capdbp; 659b50d902SRodney W. Grimes int verbose; 669b50d902SRodney W. Grimes char *capdb, *capname, buf[8 * 1024]; 679b50d902SRodney W. Grimes 689b50d902SRodney W. Grimes /* 699b50d902SRodney W. Grimes * Mkcapdb creates a capability hash database for quick retrieval of capability 709b50d902SRodney W. Grimes * records. The database contains 2 types of entries: records and references 719b50d902SRodney W. Grimes * marked by the first byte in the data. A record entry contains the actual 729b50d902SRodney W. Grimes * capability record whereas a reference contains the name (key) under which 739b50d902SRodney W. Grimes * the correct record is stored. 749b50d902SRodney W. Grimes */ 759b50d902SRodney W. Grimes int 7677ae8ac7SDavid Malone main(int argc, char *argv[]) 779b50d902SRodney W. Grimes { 789b50d902SRodney W. Grimes int c; 799b50d902SRodney W. Grimes 809b50d902SRodney W. Grimes capname = NULL; 811c8af878SWarner Losh while ((c = getopt(argc, argv, "f:v")) != -1) { 829b50d902SRodney W. Grimes switch(c) { 839b50d902SRodney W. Grimes case 'f': 849b50d902SRodney W. Grimes capname = optarg; 859b50d902SRodney W. Grimes break; 869b50d902SRodney W. Grimes case 'v': 879b50d902SRodney W. Grimes verbose = 1; 889b50d902SRodney W. Grimes break; 899b50d902SRodney W. Grimes case '?': 909b50d902SRodney W. Grimes default: 919b50d902SRodney W. Grimes usage(); 929b50d902SRodney W. Grimes } 939b50d902SRodney W. Grimes } 949b50d902SRodney W. Grimes argc -= optind; 959b50d902SRodney W. Grimes argv += optind; 969b50d902SRodney W. Grimes 979b50d902SRodney W. Grimes if (*argv == NULL) 989b50d902SRodney W. Grimes usage(); 999b50d902SRodney W. Grimes 1009b50d902SRodney W. Grimes /* 1019b50d902SRodney W. Grimes * The database file is the first argument if no name is specified. 1029b50d902SRodney W. Grimes * Make arrangements to unlink it if exit badly. 1039b50d902SRodney W. Grimes */ 1049b50d902SRodney W. Grimes (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv); 1059b50d902SRodney W. Grimes if ((capname = strdup(buf)) == NULL) 10693d6b9b6SPhilippe Charnier errx(1, "strdup failed"); 1079b50d902SRodney W. Grimes if ((capdbp = dbopen(capname, 1089b50d902SRodney W. Grimes O_CREAT | O_TRUNC | O_RDWR, DEFFILEMODE, DB_HASH, NULL)) == NULL) 1099b50d902SRodney W. Grimes err(1, "%s", buf); 1109b50d902SRodney W. Grimes 1119b50d902SRodney W. Grimes if (atexit(dounlink)) 1129b50d902SRodney W. Grimes err(1, "atexit"); 1139b50d902SRodney W. Grimes 1149b50d902SRodney W. Grimes db_build(argv); 1159b50d902SRodney W. Grimes 1169b50d902SRodney W. Grimes if (capdbp->close(capdbp) < 0) 1179b50d902SRodney W. Grimes err(1, "%s", capname); 1189b50d902SRodney W. Grimes capname = NULL; 1199b50d902SRodney W. Grimes exit(0); 1209b50d902SRodney W. Grimes } 1219b50d902SRodney W. Grimes 1229b50d902SRodney W. Grimes void 12377ae8ac7SDavid Malone dounlink(void) 1249b50d902SRodney W. Grimes { 1259b50d902SRodney W. Grimes if (capname != NULL) 1269b50d902SRodney W. Grimes (void)unlink(capname); 1279b50d902SRodney W. Grimes } 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes /* 1309b50d902SRodney W. Grimes * Any changes to these definitions should be made also in the getcap(3) 1319b50d902SRodney W. Grimes * library routines. 1329b50d902SRodney W. Grimes */ 1339b50d902SRodney W. Grimes #define RECOK (char)0 1349b50d902SRodney W. Grimes #define TCERR (char)1 1359b50d902SRodney W. Grimes #define SHADOW (char)2 1369b50d902SRodney W. Grimes 1379b50d902SRodney W. Grimes /* 138f66c1ecfSRuslan Ermilov * Db_build() builds the name and capability databases according to the 1399b50d902SRodney W. Grimes * details above. 1409b50d902SRodney W. Grimes */ 1419b50d902SRodney W. Grimes void 14277ae8ac7SDavid Malone db_build(char **ifiles) 1439b50d902SRodney W. Grimes { 1449b50d902SRodney W. Grimes DBT key, data; 1459b50d902SRodney W. Grimes recno_t reccnt; 1469b50d902SRodney W. Grimes size_t len, bplen; 1479b50d902SRodney W. Grimes int st; 1489b50d902SRodney W. Grimes char *bp, *p, *t; 1499b50d902SRodney W. Grimes 1509b50d902SRodney W. Grimes data.data = NULL; 1519b50d902SRodney W. Grimes key.data = NULL; 1529b50d902SRodney W. Grimes for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) { 1539b50d902SRodney W. Grimes 1549b50d902SRodney W. Grimes /* 1559b50d902SRodney W. Grimes * Allocate enough memory to store record, terminating 1569b50d902SRodney W. Grimes * NULL and one extra byte. 1579b50d902SRodney W. Grimes */ 1589b50d902SRodney W. Grimes len = strlen(bp); 1599b50d902SRodney W. Grimes if (bplen <= len + 2) { 1609b50d902SRodney W. Grimes bplen += MAX(256, len + 2); 1619b50d902SRodney W. Grimes if ((data.data = realloc(data.data, bplen)) == NULL) 16293d6b9b6SPhilippe Charnier errx(1, "malloc failed"); 1639b50d902SRodney W. Grimes } 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes /* Find the end of the name field. */ 1669b50d902SRodney W. Grimes if ((p = strchr(bp, ':')) == NULL) { 16722694ebaSBruce Evans warnx("no name field: %.*s", (int)MIN(len, 20), bp); 1689b50d902SRodney W. Grimes continue; 1699b50d902SRodney W. Grimes } 1709b50d902SRodney W. Grimes 1719b50d902SRodney W. Grimes /* First byte of stored record indicates status. */ 1729b50d902SRodney W. Grimes switch(st) { 1739b50d902SRodney W. Grimes case 1: 1749b50d902SRodney W. Grimes ((char *)(data.data))[0] = RECOK; 1759b50d902SRodney W. Grimes break; 1769b50d902SRodney W. Grimes case 2: 1779b50d902SRodney W. Grimes ((char *)(data.data))[0] = TCERR; 178653636c2SDima Dorfman warnx("record not tc expanded: %.*s", (int)(p - bp), 179653636c2SDima Dorfman bp); 1809b50d902SRodney W. Grimes break; 1819b50d902SRodney W. Grimes } 1829b50d902SRodney W. Grimes 1839b50d902SRodney W. Grimes /* Create the stored record. */ 1849b50d902SRodney W. Grimes memmove(&((u_char *)(data.data))[1], bp, len + 1); 1859b50d902SRodney W. Grimes data.size = len + 2; 1869b50d902SRodney W. Grimes 1879b50d902SRodney W. Grimes /* Store the record under the name field. */ 1889b50d902SRodney W. Grimes key.data = bp; 1899b50d902SRodney W. Grimes key.size = p - bp; 1909b50d902SRodney W. Grimes 1919b50d902SRodney W. Grimes switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) { 1929b50d902SRodney W. Grimes case -1: 1939b50d902SRodney W. Grimes err(1, "put"); 1949b50d902SRodney W. Grimes /* NOTREACHED */ 1959b50d902SRodney W. Grimes case 1: 1969b50d902SRodney W. Grimes warnx("ignored duplicate: %.*s", 19722694ebaSBruce Evans (int)key.size, (char *)key.data); 1989b50d902SRodney W. Grimes continue; 1999b50d902SRodney W. Grimes } 2009b50d902SRodney W. Grimes ++reccnt; 2019b50d902SRodney W. Grimes 2029b50d902SRodney W. Grimes /* If only one name, ignore the rest. */ 203f66c1ecfSRuslan Ermilov *p = '\0'; 204f66c1ecfSRuslan Ermilov if (strchr(bp, '|') == NULL) 2059b50d902SRodney W. Grimes continue; 206f66c1ecfSRuslan Ermilov *p = ':'; 2079b50d902SRodney W. Grimes 2089b50d902SRodney W. Grimes /* The rest of the names reference the entire name. */ 2099b50d902SRodney W. Grimes ((char *)(data.data))[0] = SHADOW; 2109b50d902SRodney W. Grimes memmove(&((u_char *)(data.data))[1], key.data, key.size); 2119b50d902SRodney W. Grimes data.size = key.size + 1; 2129b50d902SRodney W. Grimes 2139b50d902SRodney W. Grimes /* Store references for other names. */ 2149b50d902SRodney W. Grimes for (p = t = bp;; ++p) { 2159b50d902SRodney W. Grimes if (p > t && (*p == ':' || *p == '|')) { 2169b50d902SRodney W. Grimes key.size = p - t; 2179b50d902SRodney W. Grimes key.data = t; 2189b50d902SRodney W. Grimes switch(capdbp->put(capdbp, 2199b50d902SRodney W. Grimes &key, &data, R_NOOVERWRITE)) { 2209b50d902SRodney W. Grimes case -1: 2219b50d902SRodney W. Grimes err(1, "put"); 2229b50d902SRodney W. Grimes /* NOTREACHED */ 2239b50d902SRodney W. Grimes case 1: 2249b50d902SRodney W. Grimes warnx("ignored duplicate: %.*s", 22522694ebaSBruce Evans (int)key.size, (char *)key.data); 2269b50d902SRodney W. Grimes } 2279b50d902SRodney W. Grimes t = p + 1; 2289b50d902SRodney W. Grimes } 2299b50d902SRodney W. Grimes if (*p == ':') 2309b50d902SRodney W. Grimes break; 2319b50d902SRodney W. Grimes } 2329b50d902SRodney W. Grimes } 2339b50d902SRodney W. Grimes 2349b50d902SRodney W. Grimes switch(st) { 2359b50d902SRodney W. Grimes case -1: 2369b50d902SRodney W. Grimes err(1, "file argument"); 2379b50d902SRodney W. Grimes /* NOTREACHED */ 2389b50d902SRodney W. Grimes case -2: 2399b50d902SRodney W. Grimes errx(1, "potential reference loop detected"); 2409b50d902SRodney W. Grimes /* NOTREACHED */ 2419b50d902SRodney W. Grimes } 2429b50d902SRodney W. Grimes 2439b50d902SRodney W. Grimes if (verbose) 2449b50d902SRodney W. Grimes (void)printf("cap_mkdb: %d capability records\n", reccnt); 2459b50d902SRodney W. Grimes } 2469b50d902SRodney W. Grimes 2479b50d902SRodney W. Grimes void 24877ae8ac7SDavid Malone usage(void) 2499b50d902SRodney W. Grimes { 2509b50d902SRodney W. Grimes (void)fprintf(stderr, 25193d6b9b6SPhilippe Charnier "usage: cap_mkdb [-v] [-f outfile] file [file ...]\n"); 2529b50d902SRodney W. Grimes exit(1); 2539b50d902SRodney W. Grimes } 254