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