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