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