Annotation of src/usr.bin/cap_mkdb/cap_mkdb.c, Revision 1.4
1.4 ! tholo 1: /* $OpenBSD: cap_mkdb.c,v 1.3 1997/01/15 23:42:17 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.4 ! tholo 47: static char rcsid[] = "$OpenBSD: cap_mkdb.c,v 1.3 1997/01/15 23:42:17 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.4 ! tholo 106: case 'i':
! 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.4 ! tholo 158: * db_build() builds the name and capabilty 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: (void) memmove(&((u_char *)(data.data))[1], bp, len + 1);
1.1 deraadt 206: data.size = len + 2;
1.4 ! tholo 207: if (info) {
! 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: }
! 217: }
1.1 deraadt 218:
219: /* Store the record under the name field. */
220: key.data = bp;
221: key.size = p - bp;
222:
223: switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
224: case -1:
225: err(1, "put");
226: /* NOTREACHED */
227: case 1:
228: warnx("ignored duplicate: %.*s",
1.4 ! tholo 229: (int)key.size, (char *)key.data);
1.1 deraadt 230: continue;
231: }
232: ++reccnt;
233:
234: /* If only one name, ignore the rest. */
235: if ((p = strchr(bp, '|')) == NULL)
236: continue;
237:
238: /* The rest of the names reference the entire name. */
239: ((char *)(data.data))[0] = SHADOW;
1.4 ! tholo 240: (void) memmove(&((u_char *)(data.data))[1], key.data, key.size);
1.1 deraadt 241: data.size = key.size + 1;
242:
243: /* Store references for other names. */
244: for (p = t = bp;; ++p) {
1.4 ! tholo 245: if (p > t && (*p == (info ? ',' : ':') || *p == '|')) {
1.1 deraadt 246: key.size = p - t;
247: key.data = t;
248: switch(capdbp->put(capdbp,
249: &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: }
257: t = p + 1;
258: }
1.4 ! tholo 259: if (*p == (info ? ',' : ':'))
1.1 deraadt 260: break;
261: }
262: }
263:
264: switch(st) {
265: case -1:
266: err(1, "file argument");
267: /* NOTREACHED */
268: case -2:
269: errx(1, "potential reference loop detected");
270: /* NOTREACHED */
271: }
272:
273: if (verbose)
274: (void)printf("cap_mkdb: %d capability records\n", reccnt);
275: }
276:
277: void
278: usage()
279: {
280: (void)fprintf(stderr,
1.4 ! tholo 281: "usage: cap_mkdb [-iv] [-f outfile] file1 [file2 ...]\n");
1.1 deraadt 282: exit(1);
283: }