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