1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/stat.h> 34 35 #include <db.h> 36 #include <err.h> 37 #include <fcntl.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 43 static void db_build(char **); 44 static void dounlink(void); 45 static void usage(void); 46 47 static DB *capdbp; 48 static int verbose; 49 static char *capname, buf[8 * 1024]; 50 51 static HASHINFO openinfo = { 52 4096, /* bsize */ 53 0, /* ffactor */ 54 0, /* nelem */ 55 0, /* cachesize */ 56 NULL, /* hash() */ 57 0 /* lorder */ 58 }; 59 60 /* 61 * Mkcapdb creates a capability hash database for quick retrieval of capability 62 * records. The database contains 2 types of entries: records and references 63 * marked by the first byte in the data. A record entry contains the actual 64 * capability record whereas a reference contains the name (key) under which 65 * the correct record is stored. 66 */ 67 int 68 main(int argc, char *argv[]) 69 { 70 int byteorder, c; 71 72 capname = NULL; 73 byteorder = 0; 74 while ((c = getopt(argc, argv, "bf:lv")) != -1) { 75 switch(c) { 76 case 'b': 77 case 'l': 78 if (byteorder != 0) 79 usage(); 80 byteorder = c == 'b' ? 4321 : 1234; 81 break; 82 case 'f': 83 capname = optarg; 84 break; 85 case 'v': 86 verbose = 1; 87 break; 88 case '?': 89 default: 90 usage(); 91 } 92 } 93 argc -= optind; 94 argv += optind; 95 96 if (*argv == NULL) 97 usage(); 98 99 /* Set byte order. */ 100 openinfo.lorder = byteorder; 101 102 /* 103 * The database file is the first argument if no name is specified. 104 * Make arrangements to unlink it if exit badly. 105 */ 106 (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv); 107 if ((capname = strdup(buf)) == NULL) 108 errx(1, "strdup failed"); 109 if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR, 110 DEFFILEMODE, DB_HASH, &openinfo)) == NULL) 111 err(1, "%s", buf); 112 113 if (atexit(dounlink)) 114 err(1, "atexit"); 115 116 db_build(argv); 117 118 if (capdbp->close(capdbp) < 0) 119 err(1, "%s", capname); 120 free(capname); 121 capname = NULL; 122 exit(0); 123 } 124 125 static void 126 dounlink(void) 127 { 128 if (capname != NULL) 129 (void)unlink(capname); 130 } 131 132 /* 133 * Any changes to these definitions should be made also in the getcap(3) 134 * library routines. 135 */ 136 #define RECOK (char)0 137 #define TCERR (char)1 138 #define SHADOW (char)2 139 140 /* 141 * Db_build() builds the name and capability databases according to the 142 * details above. 143 */ 144 static void 145 db_build(char **ifiles) 146 { 147 DBT key, data; 148 recno_t reccnt; 149 size_t len, bplen; 150 int st; 151 char *bp, *p, *t; 152 153 data.data = NULL; 154 key.data = NULL; 155 for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0; free(bp)) { 156 157 /* 158 * Allocate enough memory to store record, terminating 159 * NULL and one extra byte. 160 */ 161 len = strlen(bp); 162 if (bplen <= len + 2) { 163 bplen += MAX(256, len + 2); 164 if ((data.data = realloc(data.data, bplen)) == NULL) 165 errx(1, "malloc failed"); 166 } 167 168 /* Find the end of the name field. */ 169 if ((p = strchr(bp, ':')) == NULL) { 170 warnx("no name field: %.*s", (int)MIN(len, 20), bp); 171 continue; 172 } 173 174 /* First byte of stored record indicates status. */ 175 switch(st) { 176 case 1: 177 ((char *)(data.data))[0] = RECOK; 178 break; 179 case 2: 180 ((char *)(data.data))[0] = TCERR; 181 warnx("record not tc expanded: %.*s", (int)(p - bp), 182 bp); 183 break; 184 } 185 186 /* Create the stored record. */ 187 memmove(&((u_char *)(data.data))[1], bp, len + 1); 188 data.size = len + 2; 189 190 /* Store the record under the name field. */ 191 key.data = bp; 192 key.size = p - bp; 193 194 switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) { 195 case -1: 196 err(1, "put"); 197 /* NOTREACHED */ 198 case 1: 199 warnx("ignored duplicate: %.*s", 200 (int)key.size, (char *)key.data); 201 continue; 202 } 203 ++reccnt; 204 205 /* If only one name, ignore the rest. */ 206 *p = '\0'; 207 if (strchr(bp, '|') == NULL) 208 continue; 209 *p = ':'; 210 211 /* The rest of the names reference the entire name. */ 212 ((char *)(data.data))[0] = SHADOW; 213 memmove(&((u_char *)(data.data))[1], key.data, key.size); 214 data.size = key.size + 1; 215 216 /* Store references for other names. */ 217 for (p = t = bp;; ++p) { 218 if (p > t && (*p == ':' || *p == '|')) { 219 key.size = p - t; 220 key.data = t; 221 switch(capdbp->put(capdbp, 222 &key, &data, R_NOOVERWRITE)) { 223 case -1: 224 err(1, "put"); 225 /* NOTREACHED */ 226 case 1: 227 warnx("ignored duplicate: %.*s", 228 (int)key.size, (char *)key.data); 229 } 230 t = p + 1; 231 } 232 if (*p == ':') 233 break; 234 } 235 } 236 237 switch(st) { 238 case -1: 239 err(1, "file argument"); 240 /* NOTREACHED */ 241 case -2: 242 errx(1, "potential reference loop detected"); 243 /* NOTREACHED */ 244 } 245 246 if (verbose) 247 (void)printf("cap_mkdb: %d capability records\n", reccnt); 248 } 249 250 static void 251 usage(void) 252 { 253 (void)fprintf(stderr, 254 "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n"); 255 exit(1); 256 } 257