1 1.30 andvar /* $NetBSD: cap_mkdb.c,v 1.30 2021/12/03 13:27:39 andvar Exp $ */ 2 1.4 glass 3 1.1 cgd /*- 4 1.3 mycroft * Copyright (c) 1992, 1993 5 1.3 mycroft * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.18 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 cgd * may be used to endorse or promote products derived from this software 17 1.1 cgd * without specific prior written permission. 18 1.1 cgd * 19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 cgd * SUCH DAMAGE. 30 1.1 cgd */ 31 1.1 cgd 32 1.20 lukem #if HAVE_NBTOOL_CONFIG_H 33 1.20 lukem #include "nbtool_config.h" 34 1.17 uwe #endif 35 1.17 uwe 36 1.6 lukem #include <sys/cdefs.h> 37 1.20 lukem #if !defined(lint) 38 1.24 lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993\ 39 1.24 lukem The Regents of the University of California. All rights reserved."); 40 1.4 glass #if 0 41 1.5 jtc static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95"; 42 1.4 glass #endif 43 1.30 andvar __RCSID("$NetBSD: cap_mkdb.c,v 1.30 2021/12/03 13:27:39 andvar Exp $"); 44 1.1 cgd #endif /* not lint */ 45 1.1 cgd 46 1.1 cgd #include <sys/param.h> 47 1.1 cgd #include <sys/stat.h> 48 1.1 cgd 49 1.25 christos #include <assert.h> 50 1.1 cgd #include <db.h> 51 1.1 cgd #include <err.h> 52 1.1 cgd #include <fcntl.h> 53 1.1 cgd #include <stdio.h> 54 1.1 cgd #include <stdlib.h> 55 1.1 cgd #include <string.h> 56 1.1 cgd #include <unistd.h> 57 1.15 jdolecek #include <ctype.h> 58 1.1 cgd 59 1.21 christos static void db_build(const char **); 60 1.21 christos static void dounlink(void); 61 1.27 joerg static void usage(void) __dead; 62 1.15 jdolecek static int count_records(char **); 63 1.1 cgd 64 1.21 christos static DB *capdbp; 65 1.21 christos static int verbose; 66 1.29 christos static char *capname, outfile[MAXPATHLEN]; 67 1.1 cgd 68 1.21 christos static HASHINFO openinfo = { 69 1.3 mycroft 4096, /* bsize */ 70 1.3 mycroft 16, /* ffactor */ 71 1.9 mycroft 2048, /* nelem */ 72 1.3 mycroft 2048 * 1024, /* cachesize */ 73 1.3 mycroft NULL, /* hash() */ 74 1.3 mycroft 0 /* lorder */ 75 1.3 mycroft }; 76 1.3 mycroft 77 1.1 cgd /* 78 1.1 cgd * Mkcapdb creates a capability hash database for quick retrieval of capability 79 1.1 cgd * records. The database contains 2 types of entries: records and references 80 1.1 cgd * marked by the first byte in the data. A record entry contains the actual 81 1.1 cgd * capability record whereas a reference contains the name (key) under which 82 1.1 cgd * the correct record is stored. 83 1.1 cgd */ 84 1.1 cgd int 85 1.12 mjl main(int argc, char *argv[]) 86 1.1 cgd { 87 1.10 simonb int c, byteorder; 88 1.25 christos char *p; 89 1.1 cgd 90 1.1 cgd capname = NULL; 91 1.10 simonb byteorder = 0; 92 1.10 simonb while ((c = getopt(argc, argv, "bf:lv")) != -1) { 93 1.1 cgd switch(c) { 94 1.10 simonb case 'b': 95 1.10 simonb case 'l': 96 1.10 simonb if (byteorder != 0) 97 1.10 simonb usage(); 98 1.10 simonb byteorder = c == 'b' ? 4321 : 1234; 99 1.10 simonb break; 100 1.1 cgd case 'f': 101 1.1 cgd capname = optarg; 102 1.1 cgd break; 103 1.1 cgd case 'v': 104 1.1 cgd verbose = 1; 105 1.1 cgd break; 106 1.1 cgd case '?': 107 1.1 cgd default: 108 1.1 cgd usage(); 109 1.1 cgd } 110 1.1 cgd } 111 1.1 cgd argc -= optind; 112 1.1 cgd argv += optind; 113 1.1 cgd 114 1.1 cgd if (*argv == NULL) 115 1.1 cgd usage(); 116 1.1 cgd 117 1.10 simonb /* Set byte order */ 118 1.10 simonb openinfo.lorder = byteorder; 119 1.10 simonb 120 1.1 cgd /* 121 1.15 jdolecek * Set nelem to twice the value returned by count_record(). 122 1.15 jdolecek */ 123 1.15 jdolecek openinfo.nelem = count_records(argv) << 1; 124 1.15 jdolecek 125 1.15 jdolecek /* 126 1.1 cgd * The database file is the first argument if no name is specified. 127 1.1 cgd * Make arrangements to unlink it if exit badly. 128 1.1 cgd */ 129 1.29 christos (void)snprintf(outfile, sizeof(outfile), "%s.db.tmp", 130 1.22 christos capname ? capname : *argv); 131 1.29 christos if ((capname = strdup(outfile)) == NULL) 132 1.6 lukem err(1, "strdup"); 133 1.29 christos p = strrchr(outfile, '.'); 134 1.29 christos assert(p != NULL); 135 1.29 christos *p = '\0'; 136 1.29 christos (void)unlink(outfile); 137 1.3 mycroft if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR, 138 1.3 mycroft DEFFILEMODE, DB_HASH, &openinfo)) == NULL) 139 1.29 christos err(1, "%s", outfile); 140 1.1 cgd 141 1.1 cgd if (atexit(dounlink)) 142 1.1 cgd err(1, "atexit"); 143 1.1 cgd 144 1.26 christos db_build((void *)argv); 145 1.1 cgd 146 1.1 cgd if (capdbp->close(capdbp) < 0) 147 1.1 cgd err(1, "%s", capname); 148 1.29 christos if (rename(capname, outfile) == -1) 149 1.22 christos err(1, "rename"); 150 1.22 christos free(capname); 151 1.1 cgd capname = NULL; 152 1.21 christos return 0; 153 1.1 cgd } 154 1.1 cgd 155 1.14 jdolecek static void 156 1.12 mjl dounlink(void) 157 1.1 cgd { 158 1.1 cgd if (capname != NULL) 159 1.29 christos unlink(capname); 160 1.1 cgd } 161 1.1 cgd 162 1.1 cgd /* 163 1.1 cgd * Any changes to these definitions should be made also in the getcap(3) 164 1.1 cgd * library routines. 165 1.1 cgd */ 166 1.1 cgd #define RECOK (char)0 167 1.1 cgd #define TCERR (char)1 168 1.1 cgd #define SHADOW (char)2 169 1.1 cgd 170 1.1 cgd /* 171 1.30 andvar * Db_build() builds the name and capability databases according to the 172 1.1 cgd * details above. 173 1.1 cgd */ 174 1.14 jdolecek static void 175 1.21 christos db_build(const char **ifiles) 176 1.1 cgd { 177 1.1 cgd DBT key, data; 178 1.1 cgd recno_t reccnt; 179 1.1 cgd size_t len, bplen; 180 1.1 cgd int st; 181 1.19 itojun char *bp, *p, *t, *n; 182 1.1 cgd 183 1.1 cgd data.data = NULL; 184 1.1 cgd key.data = NULL; 185 1.13 jdolecek for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0; free(bp)){ 186 1.1 cgd 187 1.1 cgd /* 188 1.1 cgd * Allocate enough memory to store record, terminating 189 1.1 cgd * NULL and one extra byte. 190 1.1 cgd */ 191 1.1 cgd len = strlen(bp); 192 1.1 cgd if (bplen <= len + 2) { 193 1.19 itojun if ((n = realloc(data.data, 194 1.19 itojun bplen + MAX(256, len + 2))) == NULL) 195 1.19 itojun err(1, "realloc"); 196 1.19 itojun data.data = n; 197 1.1 cgd bplen += MAX(256, len + 2); 198 1.1 cgd } 199 1.1 cgd 200 1.1 cgd /* Find the end of the name field. */ 201 1.1 cgd if ((p = strchr(bp, ':')) == NULL) { 202 1.6 lukem warnx("no name field: %.*s", (int)(MIN(len, 20)), bp); 203 1.1 cgd continue; 204 1.1 cgd } 205 1.1 cgd 206 1.1 cgd /* First byte of stored record indicates status. */ 207 1.1 cgd switch(st) { 208 1.1 cgd case 1: 209 1.1 cgd ((char *)(data.data))[0] = RECOK; 210 1.1 cgd break; 211 1.1 cgd case 2: 212 1.1 cgd ((char *)(data.data))[0] = TCERR; 213 1.8 mrg warnx("Record not tc expanded: %.*s", (int)(p - bp),bp); 214 1.1 cgd break; 215 1.1 cgd } 216 1.1 cgd 217 1.1 cgd /* Create the stored record. */ 218 1.21 christos (void)memmove(&((u_char *)(data.data))[1], bp, len + 1); 219 1.1 cgd data.size = len + 2; 220 1.1 cgd 221 1.1 cgd /* Store the record under the name field. */ 222 1.1 cgd key.data = bp; 223 1.1 cgd key.size = p - bp; 224 1.1 cgd 225 1.1 cgd switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) { 226 1.1 cgd case -1: 227 1.1 cgd err(1, "put"); 228 1.1 cgd /* NOTREACHED */ 229 1.1 cgd case 1: 230 1.1 cgd warnx("ignored duplicate: %.*s", 231 1.6 lukem (int)key.size, (char *)key.data); 232 1.1 cgd continue; 233 1.1 cgd } 234 1.1 cgd ++reccnt; 235 1.1 cgd 236 1.1 cgd /* If only one name, ignore the rest. */ 237 1.1 cgd if ((p = strchr(bp, '|')) == NULL) 238 1.1 cgd continue; 239 1.1 cgd 240 1.1 cgd /* The rest of the names reference the entire name. */ 241 1.1 cgd ((char *)(data.data))[0] = SHADOW; 242 1.21 christos (void)memmove(&((u_char *)(data.data))[1], key.data, key.size); 243 1.1 cgd data.size = key.size + 1; 244 1.1 cgd 245 1.1 cgd /* Store references for other names. */ 246 1.1 cgd for (p = t = bp;; ++p) { 247 1.1 cgd if (p > t && (*p == ':' || *p == '|')) { 248 1.1 cgd key.size = p - t; 249 1.1 cgd key.data = t; 250 1.1 cgd switch(capdbp->put(capdbp, 251 1.1 cgd &key, &data, R_NOOVERWRITE)) { 252 1.1 cgd case -1: 253 1.1 cgd err(1, "put"); 254 1.1 cgd /* NOTREACHED */ 255 1.1 cgd case 1: 256 1.1 cgd warnx("ignored duplicate: %.*s", 257 1.6 lukem (int)key.size, (char *)key.data); 258 1.1 cgd } 259 1.1 cgd t = p + 1; 260 1.1 cgd } 261 1.1 cgd if (*p == ':') 262 1.1 cgd break; 263 1.1 cgd } 264 1.1 cgd } 265 1.1 cgd 266 1.1 cgd switch(st) { 267 1.1 cgd case -1: 268 1.1 cgd err(1, "file argument"); 269 1.1 cgd /* NOTREACHED */ 270 1.1 cgd case -2: 271 1.1 cgd errx(1, "potential reference loop detected"); 272 1.1 cgd /* NOTREACHED */ 273 1.1 cgd } 274 1.1 cgd 275 1.1 cgd if (verbose) 276 1.1 cgd (void)printf("cap_mkdb: %d capability records\n", reccnt); 277 1.1 cgd } 278 1.1 cgd 279 1.14 jdolecek static void 280 1.12 mjl usage(void) 281 1.1 cgd { 282 1.1 cgd (void)fprintf(stderr, 283 1.21 christos "Usage: %s [-b|-l] [-v] [-f outfile] file1 [file2 ...]\n", 284 1.21 christos getprogname()); 285 1.1 cgd exit(1); 286 1.15 jdolecek } 287 1.15 jdolecek 288 1.15 jdolecek /* 289 1.15 jdolecek * Count number of records in input files. This does not need 290 1.15 jdolecek * to be really accurate (the result is used only as a hint). 291 1.15 jdolecek * It seems to match number of records should a cgetnext() be used, though. 292 1.15 jdolecek */ 293 1.15 jdolecek static int 294 1.15 jdolecek count_records(char **list) 295 1.15 jdolecek { 296 1.15 jdolecek FILE *fp; 297 1.15 jdolecek char *line; 298 1.15 jdolecek size_t len; 299 1.15 jdolecek int nelem, slash; 300 1.15 jdolecek 301 1.15 jdolecek /* scan input files and count individual records */ 302 1.15 jdolecek for(nelem = 0, slash = 0; *list && (fp = fopen(*list++, "r")); ) { 303 1.21 christos while((line = fgetln(fp, &len)) != NULL) { 304 1.15 jdolecek if (len < 2) 305 1.15 jdolecek continue; 306 1.15 jdolecek if (!isspace((unsigned char) *line) && *line != ':' 307 1.15 jdolecek && *line != '#' && !slash) 308 1.15 jdolecek nelem++; 309 1.15 jdolecek 310 1.15 jdolecek slash = (line[len - 2] == '\\'); 311 1.15 jdolecek } 312 1.21 christos (void)fclose(fp); 313 1.15 jdolecek } 314 1.15 jdolecek 315 1.15 jdolecek if (nelem == 0) { 316 1.15 jdolecek /* no records found; pass default size hint */ 317 1.15 jdolecek nelem = 1; 318 1.15 jdolecek } else if (!powerof2(nelem)) { 319 1.15 jdolecek /* set nelem to nearest bigger power-of-two number */ 320 1.15 jdolecek int bt = 1; 321 1.15 jdolecek while(bt < nelem) bt <<= 1; 322 1.15 jdolecek nelem = bt; 323 1.15 jdolecek } 324 1.15 jdolecek 325 1.15 jdolecek return nelem; 326 1.1 cgd } 327