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 359b50d902SRodney W. Grimes static char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 419b50d902SRodney W. Grimes static char sccsid[] = "@(#)cap_mkdb.c 8.1 (Berkeley) 6/6/93"; 429b50d902SRodney W. Grimes #endif /* not lint */ 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include <sys/param.h> 459b50d902SRodney W. Grimes #include <sys/stat.h> 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include <db.h> 489b50d902SRodney W. Grimes #include <err.h> 499b50d902SRodney W. Grimes #include <errno.h> 509b50d902SRodney W. Grimes #include <fcntl.h> 519b50d902SRodney W. Grimes #include <limits.h> 529b50d902SRodney W. Grimes #include <stdio.h> 539b50d902SRodney W. Grimes #include <stdlib.h> 549b50d902SRodney W. Grimes #include <string.h> 559b50d902SRodney W. Grimes #include <unistd.h> 569b50d902SRodney W. Grimes 579b50d902SRodney W. Grimes void db_build __P((char **)); 589b50d902SRodney W. Grimes void dounlink __P((void)); 599b50d902SRodney W. Grimes void usage __P((void)); 609b50d902SRodney W. Grimes 619b50d902SRodney W. Grimes DB *capdbp; 629b50d902SRodney W. Grimes int verbose; 639b50d902SRodney W. Grimes char *capdb, *capname, buf[8 * 1024]; 649b50d902SRodney W. Grimes 659b50d902SRodney W. Grimes /* 669b50d902SRodney W. Grimes * Mkcapdb creates a capability hash database for quick retrieval of capability 679b50d902SRodney W. Grimes * records. The database contains 2 types of entries: records and references 689b50d902SRodney W. Grimes * marked by the first byte in the data. A record entry contains the actual 699b50d902SRodney W. Grimes * capability record whereas a reference contains the name (key) under which 709b50d902SRodney W. Grimes * the correct record is stored. 719b50d902SRodney W. Grimes */ 729b50d902SRodney W. Grimes int 739b50d902SRodney W. Grimes main(argc, argv) 749b50d902SRodney W. Grimes int argc; 759b50d902SRodney W. Grimes char *argv[]; 769b50d902SRodney W. Grimes { 779b50d902SRodney W. Grimes int c; 789b50d902SRodney W. Grimes 799b50d902SRodney W. Grimes capname = NULL; 801c8af878SWarner Losh while ((c = getopt(argc, argv, "f:v")) != -1) { 819b50d902SRodney W. Grimes switch(c) { 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 999b50d902SRodney W. Grimes /* 1009b50d902SRodney W. Grimes * The database file is the first argument if no name is specified. 1019b50d902SRodney W. Grimes * Make arrangements to unlink it if exit badly. 1029b50d902SRodney W. Grimes */ 1039b50d902SRodney W. Grimes (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv); 1049b50d902SRodney W. Grimes if ((capname = strdup(buf)) == NULL) 1059b50d902SRodney W. Grimes err(1, ""); 1069b50d902SRodney W. Grimes if ((capdbp = dbopen(capname, 1079b50d902SRodney W. Grimes O_CREAT | O_TRUNC | O_RDWR, DEFFILEMODE, DB_HASH, NULL)) == NULL) 1089b50d902SRodney W. Grimes err(1, "%s", buf); 1099b50d902SRodney W. Grimes 1109b50d902SRodney W. Grimes if (atexit(dounlink)) 1119b50d902SRodney W. Grimes err(1, "atexit"); 1129b50d902SRodney W. Grimes 1139b50d902SRodney W. Grimes db_build(argv); 1149b50d902SRodney W. Grimes 1159b50d902SRodney W. Grimes if (capdbp->close(capdbp) < 0) 1169b50d902SRodney W. Grimes err(1, "%s", capname); 1179b50d902SRodney W. Grimes capname = NULL; 1189b50d902SRodney W. Grimes exit(0); 1199b50d902SRodney W. Grimes } 1209b50d902SRodney W. Grimes 1219b50d902SRodney W. Grimes void 1229b50d902SRodney W. Grimes dounlink() 1239b50d902SRodney W. Grimes { 1249b50d902SRodney W. Grimes if (capname != NULL) 1259b50d902SRodney W. Grimes (void)unlink(capname); 1269b50d902SRodney W. Grimes } 1279b50d902SRodney W. Grimes 1289b50d902SRodney W. Grimes /* 1299b50d902SRodney W. Grimes * Any changes to these definitions should be made also in the getcap(3) 1309b50d902SRodney W. Grimes * library routines. 1319b50d902SRodney W. Grimes */ 1329b50d902SRodney W. Grimes #define RECOK (char)0 1339b50d902SRodney W. Grimes #define TCERR (char)1 1349b50d902SRodney W. Grimes #define SHADOW (char)2 1359b50d902SRodney W. Grimes 1369b50d902SRodney W. Grimes /* 1379b50d902SRodney W. Grimes * Db_build() builds the name and capabilty databases according to the 1389b50d902SRodney W. Grimes * details above. 1399b50d902SRodney W. Grimes */ 1409b50d902SRodney W. Grimes void 1419b50d902SRodney W. Grimes db_build(ifiles) 1429b50d902SRodney W. Grimes 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) 1629b50d902SRodney W. Grimes err(1, ""); 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; 1789b50d902SRodney W. Grimes warnx("Record not tc expanded: %.*s", p - bp, bp); 1799b50d902SRodney W. Grimes break; 1809b50d902SRodney W. Grimes } 1819b50d902SRodney W. Grimes 1829b50d902SRodney W. Grimes /* Create the stored record. */ 1839b50d902SRodney W. Grimes memmove(&((u_char *)(data.data))[1], bp, len + 1); 1849b50d902SRodney W. Grimes data.size = len + 2; 1859b50d902SRodney W. Grimes 1869b50d902SRodney W. Grimes /* Store the record under the name field. */ 1879b50d902SRodney W. Grimes key.data = bp; 1889b50d902SRodney W. Grimes key.size = p - bp; 1899b50d902SRodney W. Grimes 1909b50d902SRodney W. Grimes switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) { 1919b50d902SRodney W. Grimes case -1: 1929b50d902SRodney W. Grimes err(1, "put"); 1939b50d902SRodney W. Grimes /* NOTREACHED */ 1949b50d902SRodney W. Grimes case 1: 1959b50d902SRodney W. Grimes warnx("ignored duplicate: %.*s", 19622694ebaSBruce Evans (int)key.size, (char *)key.data); 1979b50d902SRodney W. Grimes continue; 1989b50d902SRodney W. Grimes } 1999b50d902SRodney W. Grimes ++reccnt; 2009b50d902SRodney W. Grimes 2019b50d902SRodney W. Grimes /* If only one name, ignore the rest. */ 2029b50d902SRodney W. Grimes if ((p = strchr(bp, '|')) == NULL) 2039b50d902SRodney W. Grimes continue; 2049b50d902SRodney W. Grimes 2059b50d902SRodney W. Grimes /* The rest of the names reference the entire name. */ 2069b50d902SRodney W. Grimes ((char *)(data.data))[0] = SHADOW; 2079b50d902SRodney W. Grimes memmove(&((u_char *)(data.data))[1], key.data, key.size); 2089b50d902SRodney W. Grimes data.size = key.size + 1; 2099b50d902SRodney W. Grimes 2109b50d902SRodney W. Grimes /* Store references for other names. */ 2119b50d902SRodney W. Grimes for (p = t = bp;; ++p) { 2129b50d902SRodney W. Grimes if (p > t && (*p == ':' || *p == '|')) { 2139b50d902SRodney W. Grimes key.size = p - t; 2149b50d902SRodney W. Grimes key.data = t; 2159b50d902SRodney W. Grimes switch(capdbp->put(capdbp, 2169b50d902SRodney W. Grimes &key, &data, R_NOOVERWRITE)) { 2179b50d902SRodney W. Grimes case -1: 2189b50d902SRodney W. Grimes err(1, "put"); 2199b50d902SRodney W. Grimes /* NOTREACHED */ 2209b50d902SRodney W. Grimes case 1: 2219b50d902SRodney W. Grimes warnx("ignored duplicate: %.*s", 22222694ebaSBruce Evans (int)key.size, (char *)key.data); 2239b50d902SRodney W. Grimes } 2249b50d902SRodney W. Grimes t = p + 1; 2259b50d902SRodney W. Grimes } 2269b50d902SRodney W. Grimes if (*p == ':') 2279b50d902SRodney W. Grimes break; 2289b50d902SRodney W. Grimes } 2299b50d902SRodney W. Grimes } 2309b50d902SRodney W. Grimes 2319b50d902SRodney W. Grimes switch(st) { 2329b50d902SRodney W. Grimes case -1: 2339b50d902SRodney W. Grimes err(1, "file argument"); 2349b50d902SRodney W. Grimes /* NOTREACHED */ 2359b50d902SRodney W. Grimes case -2: 2369b50d902SRodney W. Grimes errx(1, "potential reference loop detected"); 2379b50d902SRodney W. Grimes /* NOTREACHED */ 2389b50d902SRodney W. Grimes } 2399b50d902SRodney W. Grimes 2409b50d902SRodney W. Grimes if (verbose) 2419b50d902SRodney W. Grimes (void)printf("cap_mkdb: %d capability records\n", reccnt); 2429b50d902SRodney W. Grimes } 2439b50d902SRodney W. Grimes 2449b50d902SRodney W. Grimes void 2459b50d902SRodney W. Grimes usage() 2469b50d902SRodney W. Grimes { 2479b50d902SRodney W. Grimes (void)fprintf(stderr, 2489b50d902SRodney W. Grimes "usage: cap_mkdb [-v] [-f outfile] file1 [file2 ...]\n"); 2499b50d902SRodney W. Grimes exit(1); 2509b50d902SRodney W. Grimes } 251