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