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