Annotation of src/usr.bin/cap_mkdb/cap_mkdb.c, Revision 1.7
1.7 ! millert 1: /* $OpenBSD: cap_mkdb.c,v 1.6 2000/09/22 22:22:22 naddy 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.7 ! millert 47: static char rcsid[] = "$OpenBSD: cap_mkdb.c,v 1.6 2000/09/22 22:22:22 naddy 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.
1.7 ! millert 122: * Make arrangements to unlink it if we exit badly.
1.1 deraadt 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;
1.7 ! millert 170:
! 171: cgetusedb(0); /* disable reading of .db files in getcap(3) */
1.1 deraadt 172:
173: data.data = NULL;
174: key.data = NULL;
1.4 tholo 175: for (reccnt = 0, bplen = 0;
176: (st = (info ? igetnext(&bp, ifiles) : cgetnext(&bp, ifiles))) > 0;) {
1.1 deraadt 177:
178: /*
179: * Allocate enough memory to store record, terminating
180: * NULL and one extra byte.
181: */
182: len = strlen(bp);
183: if (bplen <= len + 2) {
184: bplen += MAX(256, len + 2);
185: if ((data.data = realloc(data.data, bplen)) == NULL)
186: err(1, "");
187: }
188:
189: /* Find the end of the name field. */
1.4 tholo 190: if ((p = strchr(bp, info ? ',' : ':')) == NULL) {
1.1 deraadt 191: warnx("no name field: %.*s", MIN(len, 20), bp);
192: continue;
193: }
194:
195: /* First byte of stored record indicates status. */
196: switch(st) {
197: case 1:
198: ((char *)(data.data))[0] = RECOK;
199: break;
200: case 2:
201: ((char *)(data.data))[0] = TCERR;
1.4 tholo 202: warnx("Record not tc expanded: %.*s", (int)(p - bp), bp);
1.1 deraadt 203: break;
204: }
205:
206: /* Create the stored record. */
1.4 tholo 207: if (info) {
1.5 millert 208: (void) memcpy(&((u_char *)(data.data))[1], bp, len + 1);
209: data.size = len + 2;
1.4 tholo 210: for (t = memchr((char *)data.data + 1, ',', data.size - 1);
211: t;
212: t = memchr(t, ',', data.size - (t - (char *)data.data)))
213: *t++ = ':';
214:
215: if (memchr((char *)data.data + 1, '\0', data.size - 2)) {
216: warnx("NUL in entry: %.*s", (int)MIN(len, 20), bp);
217: continue;
218: }
1.5 millert 219: } else {
220: char *capbeg, *capend;
221:
222: t = (char *)data.data + 1;
223: /* Copy the cap name and trailing ':' */
224: len = p - bp + 1;
225: memcpy(t, bp, len);
226: t += len;
227:
228: /* Copy entry, collapsing empty fields. */
229: capbeg = p + 1;
230: while (*capbeg) {
231: /* Skip empty fields. */
232: if ((len = strspn(capbeg, ": \t\n\r")))
233: capbeg += len;
234:
235: /* Find the end of this cap and copy it w/ : */
236: capend = strchr(capbeg, ':');
237: if (capend)
238: len = capend - capbeg + 1;
239: else
240: len = strlen(capbeg);
241: memcpy(t, capbeg, len);
242: t += len;
243: capbeg += len;
244: }
245: *t = '\0';
246: data.size = t - (char *)data.data + 1;
1.4 tholo 247: }
1.1 deraadt 248:
249: /* Store the record under the name field. */
250: key.data = bp;
251: key.size = p - bp;
252:
253: switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
254: case -1:
255: err(1, "put");
256: /* NOTREACHED */
257: case 1:
258: warnx("ignored duplicate: %.*s",
1.4 tholo 259: (int)key.size, (char *)key.data);
1.1 deraadt 260: continue;
261: }
262: ++reccnt;
263:
264: /* If only one name, ignore the rest. */
265: if ((p = strchr(bp, '|')) == NULL)
266: continue;
267:
268: /* The rest of the names reference the entire name. */
269: ((char *)(data.data))[0] = SHADOW;
1.4 tholo 270: (void) memmove(&((u_char *)(data.data))[1], key.data, key.size);
1.1 deraadt 271: data.size = key.size + 1;
272:
273: /* Store references for other names. */
274: for (p = t = bp;; ++p) {
1.4 tholo 275: if (p > t && (*p == (info ? ',' : ':') || *p == '|')) {
1.1 deraadt 276: key.size = p - t;
277: key.data = t;
278: switch(capdbp->put(capdbp,
279: &key, &data, R_NOOVERWRITE)) {
280: case -1:
281: err(1, "put");
282: /* NOTREACHED */
283: case 1:
284: warnx("ignored duplicate: %.*s",
1.4 tholo 285: (int)key.size, (char *)key.data);
1.1 deraadt 286: }
287: t = p + 1;
288: }
1.4 tholo 289: if (*p == (info ? ',' : ':'))
1.1 deraadt 290: break;
291: }
292: }
293:
294: switch(st) {
295: case -1:
296: err(1, "file argument");
297: /* NOTREACHED */
298: case -2:
299: errx(1, "potential reference loop detected");
300: /* NOTREACHED */
301: }
302:
303: if (verbose)
304: (void)printf("cap_mkdb: %d capability records\n", reccnt);
305: }
306:
307: void
308: usage()
309: {
310: (void)fprintf(stderr,
1.4 tholo 311: "usage: cap_mkdb [-iv] [-f outfile] file1 [file2 ...]\n");
1.1 deraadt 312: exit(1);
313: }