Annotation of src/usr.bin/cap_mkdb/cap_mkdb.c, Revision 1.10
1.10 ! millert 1: /* $OpenBSD: cap_mkdb.c,v 1.9 2002/02/16 21:27:44 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.
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.10 ! millert 43: static char rcsid[] = "$OpenBSD: cap_mkdb.c,v 1.9 2002/02/16 21:27:44 millert 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
87: main(argc, argv)
88: int argc;
89: char *argv[];
90: {
91: int c;
92:
93: capname = NULL;
1.4 tholo 94: while ((c = getopt(argc, argv, "f:iv")) != -1) {
1.1 deraadt 95: switch(c) {
96: case 'f':
97: capname = optarg;
98: break;
99: case 'v':
100: verbose = 1;
101: break;
1.6 naddy 102: case 'i':
1.4 tholo 103: info = 1;
104: break;
1.1 deraadt 105: case '?':
106: default:
107: usage();
108: }
109: }
110: argc -= optind;
111: argv += optind;
112:
113: if (*argv == NULL)
114: usage();
115:
116: /*
117: * The database file is the first argument if no name is specified.
1.7 millert 118: * Make arrangements to unlink it if we exit badly.
1.1 deraadt 119: */
120: (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
121: if ((capname = strdup(buf)) == NULL)
122: err(1, "");
123: if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
124: DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
125: err(1, "%s", buf);
126:
127: if (atexit(dounlink))
128: err(1, "atexit");
129:
130: db_build(argv);
131:
132: if (capdbp->close(capdbp) < 0)
133: err(1, "%s", capname);
134: capname = NULL;
135: exit(0);
136: }
137:
138: void
139: dounlink()
140: {
141: if (capname != NULL)
142: (void)unlink(capname);
143: }
144:
145: /*
146: * Any changes to these definitions should be made also in the getcap(3)
147: * library routines.
148: */
149: #define RECOK (char)0
150: #define TCERR (char)1
151: #define SHADOW (char)2
152:
153: /*
1.6 naddy 154: * db_build() builds the name and capability databases according to the
1.1 deraadt 155: * details above.
156: */
157: void
158: db_build(ifiles)
159: char **ifiles;
160: {
161: DBT key, data;
162: recno_t reccnt;
163: size_t len, bplen;
164: int st;
165: char *bp, *p, *t;
1.7 millert 166:
167: cgetusedb(0); /* disable reading of .db files in getcap(3) */
1.1 deraadt 168:
169: data.data = NULL;
170: key.data = NULL;
1.4 tholo 171: for (reccnt = 0, bplen = 0;
172: (st = (info ? igetnext(&bp, ifiles) : cgetnext(&bp, ifiles))) > 0;) {
1.1 deraadt 173:
174: /*
175: * Allocate enough memory to store record, terminating
176: * NULL and one extra byte.
177: */
178: len = strlen(bp);
179: if (bplen <= len + 2) {
180: bplen += MAX(256, len + 2);
181: if ((data.data = realloc(data.data, bplen)) == NULL)
182: err(1, "");
183: }
184:
185: /* Find the end of the name field. */
1.4 tholo 186: if ((p = strchr(bp, info ? ',' : ':')) == NULL) {
1.8 deraadt 187: warnx("no name field: %.*s", (int)MIN(len, 20), bp);
1.1 deraadt 188: continue;
189: }
190:
191: /* First byte of stored record indicates status. */
192: switch(st) {
193: case 1:
194: ((char *)(data.data))[0] = RECOK;
195: break;
196: case 2:
197: ((char *)(data.data))[0] = TCERR;
1.4 tholo 198: warnx("Record not tc expanded: %.*s", (int)(p - bp), bp);
1.1 deraadt 199: break;
200: }
201:
202: /* Create the stored record. */
1.4 tholo 203: if (info) {
1.5 millert 204: (void) memcpy(&((u_char *)(data.data))[1], bp, len + 1);
205: data.size = len + 2;
1.4 tholo 206: for (t = memchr((char *)data.data + 1, ',', data.size - 1);
207: t;
208: t = memchr(t, ',', data.size - (t - (char *)data.data)))
209: *t++ = ':';
210:
211: if (memchr((char *)data.data + 1, '\0', data.size - 2)) {
212: warnx("NUL in entry: %.*s", (int)MIN(len, 20), bp);
213: continue;
214: }
1.5 millert 215: } else {
216: char *capbeg, *capend;
217:
218: t = (char *)data.data + 1;
219: /* Copy the cap name and trailing ':' */
220: len = p - bp + 1;
221: memcpy(t, bp, len);
222: t += len;
223:
224: /* Copy entry, collapsing empty fields. */
225: capbeg = p + 1;
226: while (*capbeg) {
227: /* Skip empty fields. */
228: if ((len = strspn(capbeg, ": \t\n\r")))
229: capbeg += len;
230:
231: /* Find the end of this cap and copy it w/ : */
232: capend = strchr(capbeg, ':');
233: if (capend)
234: len = capend - capbeg + 1;
235: else
236: len = strlen(capbeg);
237: memcpy(t, capbeg, len);
238: t += len;
239: capbeg += len;
240: }
241: *t = '\0';
242: data.size = t - (char *)data.data + 1;
1.4 tholo 243: }
1.1 deraadt 244:
245: /* Store the record under the name field. */
246: key.data = bp;
247: key.size = p - bp;
248:
249: switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
250: case -1:
251: err(1, "put");
252: /* NOTREACHED */
253: case 1:
254: warnx("ignored duplicate: %.*s",
1.4 tholo 255: (int)key.size, (char *)key.data);
1.1 deraadt 256: continue;
257: }
258: ++reccnt;
259:
260: /* If only one name, ignore the rest. */
261: if ((p = strchr(bp, '|')) == NULL)
262: continue;
263:
264: /* The rest of the names reference the entire name. */
265: ((char *)(data.data))[0] = SHADOW;
1.4 tholo 266: (void) memmove(&((u_char *)(data.data))[1], key.data, key.size);
1.1 deraadt 267: data.size = key.size + 1;
268:
269: /* Store references for other names. */
270: for (p = t = bp;; ++p) {
1.4 tholo 271: if (p > t && (*p == (info ? ',' : ':') || *p == '|')) {
1.1 deraadt 272: key.size = p - t;
273: key.data = t;
274: switch(capdbp->put(capdbp,
275: &key, &data, R_NOOVERWRITE)) {
276: case -1:
277: err(1, "put");
278: /* NOTREACHED */
279: case 1:
280: warnx("ignored duplicate: %.*s",
1.4 tholo 281: (int)key.size, (char *)key.data);
1.1 deraadt 282: }
283: t = p + 1;
284: }
1.4 tholo 285: if (*p == (info ? ',' : ':'))
1.1 deraadt 286: break;
287: }
288: }
289:
290: switch(st) {
291: case -1:
292: err(1, "file argument");
293: /* NOTREACHED */
294: case -2:
295: errx(1, "potential reference loop detected");
296: /* NOTREACHED */
297: }
298:
299: if (verbose)
300: (void)printf("cap_mkdb: %d capability records\n", reccnt);
301: }
302:
303: void
304: usage()
305: {
306: (void)fprintf(stderr,
1.4 tholo 307: "usage: cap_mkdb [-iv] [-f outfile] file1 [file2 ...]\n");
1.1 deraadt 308: exit(1);
309: }