Annotation of src/usr.bin/cap_mkdb/cap_mkdb.c, Revision 1.15
1.15 ! nicm 1: /* $OpenBSD: cap_mkdb.c,v 1.14 2006/03/04 20:32:51 otto 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.15 ! nicm 43: static char rcsid[] = "$OpenBSD: cap_mkdb.c,v 1.14 2006/03/04 20:32:51 otto 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;
1.15 ! nicm 162: char *bp, *p, *t, *out, ch;
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: /*
1.15 ! nicm 172: * Allocate enough memory to store four times the size of the
! 173: * record (so an existing ':' can be expanded to '\072' for
! 174: * terminfo) plus a terminating NULL and one extra byte.
1.1 deraadt 175: */
176: len = strlen(bp);
1.15 ! nicm 177: if (bplen <= 4 * len + 2) {
! 178: int newbplen = bplen + MAX(256, 4 * len + 2);
1.13 tedu 179: void *newdata;
180:
181: if ((newdata = realloc(data.data, newbplen)) == NULL)
1.12 millert 182: err(1, NULL);
1.13 tedu 183: data.data = newdata;
184: bplen = newbplen;
1.1 deraadt 185: }
186:
187: /* Find the end of the name field. */
1.4 tholo 188: if ((p = strchr(bp, info ? ',' : ':')) == NULL) {
1.8 deraadt 189: warnx("no name field: %.*s", (int)MIN(len, 20), bp);
1.1 deraadt 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.15 ! nicm 206: /*
! 207: * The record separator is :, so it is necessary to
! 208: * change commas into colons. However, \, should be
! 209: * left alone, unless the \ is the last part of ^\.
! 210: */
1.5 millert 211: data.size = len + 2;
1.15 ! nicm 212: out = ((char *) data.data) + 1;
! 213: t = bp;
! 214: while (t < bp + len) {
! 215: switch (ch = *t++) {
! 216: case '^':
! 217: case '\\':
! 218: *out++ = ch;
! 219: if (*t != '\0')
! 220: *out++ = *t++;
! 221: break;
! 222: case ':':
! 223: memcpy(out, "\\072", 4);
! 224: out += 4;
! 225: data.size += 3; /* : already counted */
! 226: break;
! 227: case ',':
! 228: *out++ = ':';
! 229: break;
! 230: default:
! 231: *out++ = ch;
! 232: break;
! 233: }
! 234: }
! 235: *out++ = '\0';
1.4 tholo 236: if (memchr((char *)data.data + 1, '\0', data.size - 2)) {
237: warnx("NUL in entry: %.*s", (int)MIN(len, 20), bp);
238: continue;
239: }
1.5 millert 240: } else {
241: char *capbeg, *capend;
242:
243: t = (char *)data.data + 1;
244: /* Copy the cap name and trailing ':' */
245: len = p - bp + 1;
246: memcpy(t, bp, len);
247: t += len;
248:
249: /* Copy entry, collapsing empty fields. */
250: capbeg = p + 1;
251: while (*capbeg) {
252: /* Skip empty fields. */
253: if ((len = strspn(capbeg, ": \t\n\r")))
254: capbeg += len;
255:
256: /* Find the end of this cap and copy it w/ : */
257: capend = strchr(capbeg, ':');
258: if (capend)
259: len = capend - capbeg + 1;
260: else
261: len = strlen(capbeg);
262: memcpy(t, capbeg, len);
263: t += len;
264: capbeg += len;
265: }
266: *t = '\0';
267: data.size = t - (char *)data.data + 1;
1.4 tholo 268: }
1.1 deraadt 269:
270: /* Store the record under the name field. */
271: key.data = bp;
272: key.size = p - bp;
273:
274: switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
275: case -1:
276: err(1, "put");
277: /* NOTREACHED */
278: case 1:
279: warnx("ignored duplicate: %.*s",
1.4 tholo 280: (int)key.size, (char *)key.data);
1.1 deraadt 281: continue;
282: }
283: ++reccnt;
284:
285: /* If only one name, ignore the rest. */
286: if ((p = strchr(bp, '|')) == NULL)
287: continue;
288:
289: /* The rest of the names reference the entire name. */
290: ((char *)(data.data))[0] = SHADOW;
1.4 tholo 291: (void) memmove(&((u_char *)(data.data))[1], key.data, key.size);
1.1 deraadt 292: data.size = key.size + 1;
293:
294: /* Store references for other names. */
295: for (p = t = bp;; ++p) {
1.4 tholo 296: if (p > t && (*p == (info ? ',' : ':') || *p == '|')) {
1.1 deraadt 297: key.size = p - t;
298: key.data = t;
299: switch(capdbp->put(capdbp,
300: &key, &data, R_NOOVERWRITE)) {
301: case -1:
302: err(1, "put");
303: /* NOTREACHED */
304: case 1:
305: warnx("ignored duplicate: %.*s",
1.4 tholo 306: (int)key.size, (char *)key.data);
1.1 deraadt 307: }
308: t = p + 1;
309: }
1.4 tholo 310: if (*p == (info ? ',' : ':'))
1.1 deraadt 311: break;
312: }
1.14 otto 313: free(bp);
1.1 deraadt 314: }
315:
316: switch(st) {
317: case -1:
318: err(1, "file argument");
319: /* NOTREACHED */
320: case -2:
321: errx(1, "potential reference loop detected");
322: /* NOTREACHED */
323: }
324:
325: if (verbose)
326: (void)printf("cap_mkdb: %d capability records\n", reccnt);
327: }
328:
329: void
1.11 deraadt 330: usage(void)
1.1 deraadt 331: {
332: (void)fprintf(stderr,
1.4 tholo 333: "usage: cap_mkdb [-iv] [-f outfile] file1 [file2 ...]\n");
1.1 deraadt 334: exit(1);
335: }