cap_mkdb.c revision 1.27.8.1 1 /* $NetBSD: cap_mkdb.c,v 1.27.8.1 2013/02/25 00:30:33 tls 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_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if !defined(lint)
38 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
39 The Regents of the University of California. All rights reserved.");
40 #if 0
41 static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
42 #endif
43 __RCSID("$NetBSD: cap_mkdb.c,v 1.27.8.1 2013/02/25 00:30:33 tls Exp $");
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48
49 #include <assert.h>
50 #include <db.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <ctype.h>
58
59 static void db_build(const char **);
60 static void dounlink(void);
61 static void usage(void) __dead;
62 static int count_records(char **);
63
64 static DB *capdbp;
65 static int verbose;
66 static char *capname, buf[8 * 1024];
67
68 static HASHINFO openinfo = {
69 4096, /* bsize */
70 16, /* ffactor */
71 2048, /* nelem */
72 2048 * 1024, /* cachesize */
73 NULL, /* hash() */
74 0 /* lorder */
75 };
76
77 /*
78 * Mkcapdb creates a capability hash database for quick retrieval of capability
79 * records. The database contains 2 types of entries: records and references
80 * marked by the first byte in the data. A record entry contains the actual
81 * capability record whereas a reference contains the name (key) under which
82 * the correct record is stored.
83 */
84 int
85 main(int argc, char *argv[])
86 {
87 int c, byteorder;
88 char *p;
89
90 capname = NULL;
91 byteorder = 0;
92 while ((c = getopt(argc, argv, "bf:lv")) != -1) {
93 switch(c) {
94 case 'b':
95 case 'l':
96 if (byteorder != 0)
97 usage();
98 byteorder = c == 'b' ? 4321 : 1234;
99 break;
100 case 'f':
101 capname = optarg;
102 break;
103 case 'v':
104 verbose = 1;
105 break;
106 case '?':
107 default:
108 usage();
109 }
110 }
111 argc -= optind;
112 argv += optind;
113
114 if (*argv == NULL)
115 usage();
116
117 /* Set byte order */
118 openinfo.lorder = byteorder;
119
120 /*
121 * Set nelem to twice the value returned by count_record().
122 */
123 openinfo.nelem = count_records(argv) << 1;
124
125 /*
126 * The database file is the first argument if no name is specified.
127 * Make arrangements to unlink it if exit badly.
128 */
129 (void)snprintf(buf, sizeof(buf), "%s.db.tmp",
130 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((void *)argv);
141
142 if (capdbp->close(capdbp) < 0)
143 err(1, "%s", capname);
144 p = strrchr(buf, '.');
145 assert(p != NULL);
146 *p = '\0';
147 if (rename(capname, buf) == -1)
148 err(1, "rename");
149 free(capname);
150 capname = NULL;
151 return 0;
152 }
153
154 static void
155 dounlink(void)
156 {
157 if (capname != NULL)
158 (void)unlink(capname);
159 }
160
161 /*
162 * Any changes to these definitions should be made also in the getcap(3)
163 * library routines.
164 */
165 #define RECOK (char)0
166 #define TCERR (char)1
167 #define SHADOW (char)2
168
169 /*
170 * Db_build() builds the name and capabilty databases according to the
171 * details above.
172 */
173 static void
174 db_build(const char **ifiles)
175 {
176 DBT key, data;
177 recno_t reccnt;
178 size_t len, bplen;
179 int st;
180 char *bp, *p, *t, *n;
181
182 data.data = NULL;
183 key.data = NULL;
184 for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0; free(bp)){
185
186 /*
187 * Allocate enough memory to store record, terminating
188 * NULL and one extra byte.
189 */
190 len = strlen(bp);
191 if (bplen <= len + 2) {
192 if ((n = realloc(data.data,
193 bplen + MAX(256, len + 2))) == NULL)
194 err(1, "realloc");
195 data.data = n;
196 bplen += MAX(256, len + 2);
197 }
198
199 /* Find the end of the name field. */
200 if ((p = strchr(bp, ':')) == NULL) {
201 warnx("no name field: %.*s", (int)(MIN(len, 20)), bp);
202 continue;
203 }
204
205 /* First byte of stored record indicates status. */
206 switch(st) {
207 case 1:
208 ((char *)(data.data))[0] = RECOK;
209 break;
210 case 2:
211 ((char *)(data.data))[0] = TCERR;
212 warnx("Record not tc expanded: %.*s", (int)(p - bp),bp);
213 break;
214 }
215
216 /* Create the stored record. */
217 (void)memmove(&((u_char *)(data.data))[1], bp, len + 1);
218 data.size = len + 2;
219
220 /* Store the record under the name field. */
221 key.data = bp;
222 key.size = p - bp;
223
224 switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
225 case -1:
226 err(1, "put");
227 /* NOTREACHED */
228 case 1:
229 warnx("ignored duplicate: %.*s",
230 (int)key.size, (char *)key.data);
231 continue;
232 }
233 ++reccnt;
234
235 /* If only one name, ignore the rest. */
236 if ((p = strchr(bp, '|')) == NULL)
237 continue;
238
239 /* The rest of the names reference the entire name. */
240 ((char *)(data.data))[0] = SHADOW;
241 (void)memmove(&((u_char *)(data.data))[1], key.data, key.size);
242 data.size = key.size + 1;
243
244 /* Store references for other names. */
245 for (p = t = bp;; ++p) {
246 if (p > t && (*p == ':' || *p == '|')) {
247 key.size = p - t;
248 key.data = t;
249 switch(capdbp->put(capdbp,
250 &key, &data, R_NOOVERWRITE)) {
251 case -1:
252 err(1, "put");
253 /* NOTREACHED */
254 case 1:
255 warnx("ignored duplicate: %.*s",
256 (int)key.size, (char *)key.data);
257 }
258 t = p + 1;
259 }
260 if (*p == ':')
261 break;
262 }
263 }
264
265 switch(st) {
266 case -1:
267 err(1, "file argument");
268 /* NOTREACHED */
269 case -2:
270 errx(1, "potential reference loop detected");
271 /* NOTREACHED */
272 }
273
274 if (verbose)
275 (void)printf("cap_mkdb: %d capability records\n", reccnt);
276 }
277
278 static void
279 usage(void)
280 {
281 (void)fprintf(stderr,
282 "Usage: %s [-b|-l] [-v] [-f outfile] file1 [file2 ...]\n",
283 getprogname());
284 exit(1);
285 }
286
287 /*
288 * Count number of records in input files. This does not need
289 * to be really accurate (the result is used only as a hint).
290 * It seems to match number of records should a cgetnext() be used, though.
291 */
292 static int
293 count_records(char **list)
294 {
295 FILE *fp;
296 char *line;
297 size_t len;
298 int nelem, slash;
299
300 /* scan input files and count individual records */
301 for(nelem = 0, slash = 0; *list && (fp = fopen(*list++, "r")); ) {
302 while((line = fgetln(fp, &len)) != NULL) {
303 if (len < 2)
304 continue;
305 if (!isspace((unsigned char) *line) && *line != ':'
306 && *line != '#' && !slash)
307 nelem++;
308
309 slash = (line[len - 2] == '\\');
310 }
311 (void)fclose(fp);
312 }
313
314 if (nelem == 0) {
315 /* no records found; pass default size hint */
316 nelem = 1;
317 } else if (!powerof2(nelem)) {
318 /* set nelem to nearest bigger power-of-two number */
319 int bt = 1;
320 while(bt < nelem) bt <<= 1;
321 nelem = bt;
322 }
323
324 return nelem;
325 }
326