cap_mkdb.c revision 1.7 1 /* $NetBSD: cap_mkdb.c,v 1.7 1997/10/18 12:55:39 lukem 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.7 1997/10/18 12:55:39 lukem 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 <errno.h>
55 #include <fcntl.h>
56 #include <limits.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 void db_build __P((char **));
63 void dounlink __P((void));
64 int main __P((int, char **));
65 void usage __P((void));
66
67 DB *capdbp;
68 int verbose;
69 char *capdb, *capname, buf[8 * 1024];
70
71 HASHINFO openinfo = {
72 4096, /* bsize */
73 16, /* ffactor */
74 256, /* nelem */
75 2048 * 1024, /* cachesize */
76 NULL, /* hash() */
77 0 /* lorder */
78 };
79
80 /*
81 * Mkcapdb creates a capability hash database for quick retrieval of capability
82 * records. The database contains 2 types of entries: records and references
83 * marked by the first byte in the data. A record entry contains the actual
84 * capability record whereas a reference contains the name (key) under which
85 * the correct record is stored.
86 */
87 int
88 main(argc, argv)
89 int argc;
90 char *argv[];
91 {
92 int c;
93
94 capname = NULL;
95 while ((c = getopt(argc, argv, "f:v")) != -1) {
96 switch(c) {
97 case 'f':
98 capname = optarg;
99 break;
100 case 'v':
101 verbose = 1;
102 break;
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.
116 * Make arrangements to unlink it if exit badly.
117 */
118 (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
119 if ((capname = strdup(buf)) == NULL)
120 err(1, "strdup");
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
137 dounlink()
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 /*
152 * Db_build() builds the name and capabilty databases according to the
153 * details above.
154 */
155 void
156 db_build(ifiles)
157 char **ifiles;
158 {
159 DBT key, data;
160 recno_t reccnt;
161 size_t len, bplen;
162 int st;
163 char *bp, *p, *t;
164
165 data.data = NULL;
166 key.data = NULL;
167 for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
168
169 /*
170 * Allocate enough memory to store record, terminating
171 * NULL and one extra byte.
172 */
173 len = strlen(bp);
174 if (bplen <= len + 2) {
175 bplen += MAX(256, len + 2);
176 if ((data.data = realloc(data.data, bplen)) == NULL)
177 err(1, "realloc");
178 }
179
180 /* Find the end of the name field. */
181 if ((p = strchr(bp, ':')) == NULL) {
182 warnx("no name field: %.*s", (int)(MIN(len, 20)), bp);
183 continue;
184 }
185
186 /* First byte of stored record indicates status. */
187 switch(st) {
188 case 1:
189 ((char *)(data.data))[0] = RECOK;
190 break;
191 case 2:
192 ((char *)(data.data))[0] = TCERR;
193 warnx("Record not tc expanded: %.*s", p - bp, bp);
194 break;
195 }
196
197 /* Create the stored record. */
198 memmove(&((u_char *)(data.data))[1], bp, len + 1);
199 data.size = len + 2;
200
201 /* Store the record under the name field. */
202 key.data = bp;
203 key.size = p - bp;
204
205 switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
206 case -1:
207 err(1, "put");
208 /* NOTREACHED */
209 case 1:
210 warnx("ignored duplicate: %.*s",
211 (int)key.size, (char *)key.data);
212 continue;
213 }
214 ++reccnt;
215
216 /* If only one name, ignore the rest. */
217 if ((p = strchr(bp, '|')) == NULL)
218 continue;
219
220 /* The rest of the names reference the entire name. */
221 ((char *)(data.data))[0] = SHADOW;
222 memmove(&((u_char *)(data.data))[1], key.data, key.size);
223 data.size = key.size + 1;
224
225 /* Store references for other names. */
226 for (p = t = bp;; ++p) {
227 if (p > t && (*p == ':' || *p == '|')) {
228 key.size = p - t;
229 key.data = t;
230 switch(capdbp->put(capdbp,
231 &key, &data, R_NOOVERWRITE)) {
232 case -1:
233 err(1, "put");
234 /* NOTREACHED */
235 case 1:
236 warnx("ignored duplicate: %.*s",
237 (int)key.size, (char *)key.data);
238 }
239 t = p + 1;
240 }
241 if (*p == ':')
242 break;
243 }
244 }
245
246 switch(st) {
247 case -1:
248 err(1, "file argument");
249 /* NOTREACHED */
250 case -2:
251 errx(1, "potential reference loop detected");
252 /* NOTREACHED */
253 }
254
255 if (verbose)
256 (void)printf("cap_mkdb: %d capability records\n", reccnt);
257 }
258
259 void
260 usage()
261 {
262 (void)fprintf(stderr,
263 "usage: cap_mkdb [-v] [-f outfile] file1 [file2 ...]\n");
264 exit(1);
265 }
266