cap_mkdb.c revision 1.19 1 /* $NetBSD: cap_mkdb.c,v 1.19 2003/10/16 06:43:52 itojun Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #if HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if defined(__COPYRIGHT) && !defined(lint)
38 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #if defined(__RCSID) && !defined(lint)
43 #if 0
44 static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
45 #endif
46 __RCSID("$NetBSD: cap_mkdb.c,v 1.19 2003/10/16 06:43:52 itojun Exp $");
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/stat.h>
51
52 #include <db.h>
53 #include <err.h>
54 #include <fcntl.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <ctype.h>
60
61 static void db_build (char **);
62 static void dounlink (void);
63 static void usage (void);
64 static int count_records(char **);
65
66 DB *capdbp;
67 int verbose;
68 char *capdb, *capname, buf[8 * 1024];
69
70 HASHINFO openinfo = {
71 4096, /* bsize */
72 16, /* ffactor */
73 2048, /* nelem */
74 2048 * 1024, /* cachesize */
75 NULL, /* hash() */
76 0 /* lorder */
77 };
78
79 /*
80 * Mkcapdb creates a capability hash database for quick retrieval of capability
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(int argc, char *argv[])
88 {
89 int c, byteorder;
90
91 capname = NULL;
92 byteorder = 0;
93 while ((c = getopt(argc, argv, "bf:lv")) != -1) {
94 switch(c) {
95 case 'b':
96 case 'l':
97 if (byteorder != 0)
98 usage();
99 byteorder = c == 'b' ? 4321 : 1234;
100 break;
101 case 'f':
102 capname = optarg;
103 break;
104 case 'v':
105 verbose = 1;
106 break;
107 case '?':
108 default:
109 usage();
110 }
111 }
112 argc -= optind;
113 argv += optind;
114
115 if (*argv == NULL)
116 usage();
117
118 /* Set byte order */
119 openinfo.lorder = byteorder;
120
121 /*
122 * Set nelem to twice the value returned by count_record().
123 */
124 openinfo.nelem = count_records(argv) << 1;
125
126 /*
127 * The database file is the first argument if no name is specified.
128 * Make arrangements to unlink it if exit badly.
129 */
130 (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
131 if ((capname = strdup(buf)) == NULL)
132 err(1, "strdup");
133 if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
134 DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
135 err(1, "%s", buf);
136
137 if (atexit(dounlink))
138 err(1, "atexit");
139
140 db_build(argv);
141
142 if (capdbp->close(capdbp) < 0)
143 err(1, "%s", capname);
144 capname = NULL;
145 exit(0);
146 }
147
148 static void
149 dounlink(void)
150 {
151 if (capname != NULL)
152 (void)unlink(capname);
153 }
154
155 /*
156 * Any changes to these definitions should be made also in the getcap(3)
157 * library routines.
158 */
159 #define RECOK (char)0
160 #define TCERR (char)1
161 #define SHADOW (char)2
162
163 /*
164 * Db_build() builds the name and capabilty databases according to the
165 * details above.
166 */
167 static void
168 db_build(char **ifiles)
169 {
170 DBT key, data;
171 recno_t reccnt;
172 size_t len, bplen;
173 int st;
174 char *bp, *p, *t, *n;
175
176 data.data = NULL;
177 key.data = NULL;
178 for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0; free(bp)){
179
180 /*
181 * Allocate enough memory to store record, terminating
182 * NULL and one extra byte.
183 */
184 len = strlen(bp);
185 if (bplen <= len + 2) {
186 if ((n = realloc(data.data,
187 bplen + MAX(256, len + 2))) == NULL)
188 err(1, "realloc");
189 data.data = n;
190 bplen += MAX(256, len + 2);
191 }
192
193 /* Find the end of the name field. */
194 if ((p = strchr(bp, ':')) == NULL) {
195 warnx("no name field: %.*s", (int)(MIN(len, 20)), bp);
196 continue;
197 }
198
199 /* First byte of stored record indicates status. */
200 switch(st) {
201 case 1:
202 ((char *)(data.data))[0] = RECOK;
203 break;
204 case 2:
205 ((char *)(data.data))[0] = TCERR;
206 warnx("Record not tc expanded: %.*s", (int)(p - bp),bp);
207 break;
208 }
209
210 /* Create the stored record. */
211 memmove(&((u_char *)(data.data))[1], bp, len + 1);
212 data.size = len + 2;
213
214 /* Store the record under the name field. */
215 key.data = bp;
216 key.size = p - bp;
217
218 switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
219 case -1:
220 err(1, "put");
221 /* NOTREACHED */
222 case 1:
223 warnx("ignored duplicate: %.*s",
224 (int)key.size, (char *)key.data);
225 continue;
226 }
227 ++reccnt;
228
229 /* If only one name, ignore the rest. */
230 if ((p = strchr(bp, '|')) == NULL)
231 continue;
232
233 /* The rest of the names reference the entire name. */
234 ((char *)(data.data))[0] = SHADOW;
235 memmove(&((u_char *)(data.data))[1], key.data, key.size);
236 data.size = key.size + 1;
237
238 /* Store references for other names. */
239 for (p = t = bp;; ++p) {
240 if (p > t && (*p == ':' || *p == '|')) {
241 key.size = p - t;
242 key.data = t;
243 switch(capdbp->put(capdbp,
244 &key, &data, R_NOOVERWRITE)) {
245 case -1:
246 err(1, "put");
247 /* NOTREACHED */
248 case 1:
249 warnx("ignored duplicate: %.*s",
250 (int)key.size, (char *)key.data);
251 }
252 t = p + 1;
253 }
254 if (*p == ':')
255 break;
256 }
257 }
258
259 switch(st) {
260 case -1:
261 err(1, "file argument");
262 /* NOTREACHED */
263 case -2:
264 errx(1, "potential reference loop detected");
265 /* NOTREACHED */
266 }
267
268 if (verbose)
269 (void)printf("cap_mkdb: %d capability records\n", reccnt);
270 }
271
272 static void
273 usage(void)
274 {
275 (void)fprintf(stderr,
276 "usage: cap_mkdb [-b|-l] [-v] [-f outfile] file1 [file2 ...]\n");
277 exit(1);
278 }
279
280 /*
281 * Count number of records in input files. This does not need
282 * to be really accurate (the result is used only as a hint).
283 * It seems to match number of records should a cgetnext() be used, though.
284 */
285 static int
286 count_records(char **list)
287 {
288 FILE *fp;
289 char *line;
290 size_t len;
291 int nelem, slash;
292
293 /* scan input files and count individual records */
294 for(nelem = 0, slash = 0; *list && (fp = fopen(*list++, "r")); ) {
295 while((line = fgetln(fp, &len))) {
296 if (len < 2)
297 continue;
298 if (!isspace((unsigned char) *line) && *line != ':'
299 && *line != '#' && !slash)
300 nelem++;
301
302 slash = (line[len - 2] == '\\');
303 }
304 fclose(fp);
305 }
306
307 if (nelem == 0) {
308 /* no records found; pass default size hint */
309 nelem = 1;
310 } else if (!powerof2(nelem)) {
311 /* set nelem to nearest bigger power-of-two number */
312 int bt = 1;
313 while(bt < nelem) bt <<= 1;
314 nelem = bt;
315 }
316
317 return nelem;
318 }
319