cap_mkdb.c revision 1.2 1 /*-
2 * Copyright (c) 1992 The Regents of the University of California.
3 * 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 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)cap_mkdb.c 5.6 (Berkeley) 3/8/93";*/
42 static char rcsid[] = "$Id: cap_mkdb.c,v 1.2 1993/08/01 18:18:08 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <db.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 void db_build __P((char **));
59 void dounlink __P((void));
60 void usage __P((void));
61
62 DB *capdbp;
63 int verbose;
64 char *capdb, *capname, buf[8 * 1024];
65
66 /*
67 * Mkcapdb creates a capability hash database for quick retrieval of capability
68 * records. The database contains 2 types of entries: records and references
69 * marked by the first byte in the data. A record entry contains the actual
70 * capability record whereas a reference contains the name (key) under which
71 * the correct record is stored.
72 */
73 int
74 main(argc, argv)
75 int argc;
76 char *argv[];
77 {
78 int c;
79
80 capname = NULL;
81 while ((c = getopt(argc, argv, "f:v")) != EOF) {
82 switch(c) {
83 case 'f':
84 capname = optarg;
85 break;
86 case 'v':
87 verbose = 1;
88 break;
89 case '?':
90 default:
91 usage();
92 }
93 }
94 argc -= optind;
95 argv += optind;
96
97 if (*argv == NULL)
98 usage();
99
100 /*
101 * The database file is the first argument if no name is specified.
102 * Make arrangements to unlink it if exit badly.
103 */
104 (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
105 if ((capname = strdup(buf)) == NULL)
106 err(1, "");
107 if ((capdbp = dbopen(capname,
108 O_CREAT | O_TRUNC | O_RDWR, DEFFILEMODE, DB_HASH, NULL)) == NULL)
109 err(1, "%s", buf);
110
111 if (atexit(dounlink))
112 err(1, "atexit");
113
114 db_build(argv);
115
116 if (capdbp->close(capdbp) < 0)
117 err(1, "%s", capname);
118 capname = NULL;
119 exit(0);
120 }
121
122 void
123 dounlink()
124 {
125 if (capname != NULL)
126 (void)unlink(capname);
127 }
128
129 /*
130 * Any changes to these definitions should be made also in the getcap(3)
131 * library routines.
132 */
133 #define RECOK (char)0
134 #define TCERR (char)1
135 #define SHADOW (char)2
136
137 /*
138 * Db_build() builds the name and capabilty databases according to the
139 * details above.
140 */
141 void
142 db_build(ifiles)
143 char **ifiles;
144 {
145 DBT key, data;
146 recno_t reccnt;
147 size_t len, bplen;
148 int st;
149 char *bp, *p, *t;
150
151 data.data = NULL;
152 key.data = NULL;
153 for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
154
155 /*
156 * Allocate enough memory to store record, terminating
157 * NULL and one extra byte.
158 */
159 len = strlen(bp);
160 if (bplen <= len + 2) {
161 bplen += MAX(256, len + 2);
162 if ((data.data = realloc(data.data, bplen)) == NULL)
163 err(1, "");
164 }
165
166 /* Find the end of the name field. */
167 if ((p = strchr(bp, ':')) == NULL) {
168 warnx("no name field: %.*s", MIN(len, 20), bp);
169 continue;
170 }
171
172 /* First byte of stored record indicates status. */
173 switch(st) {
174 case 1:
175 ((char *)(data.data))[0] = RECOK;
176 break;
177 case 2:
178 ((char *)(data.data))[0] = TCERR;
179 warnx("Record not tc expanded: %.*s", p - bp, bp);
180 break;
181 }
182
183 /* Create the stored record. */
184 memmove(&((u_char *)(data.data))[1], bp, len + 1);
185 data.size = len + 2;
186
187 /* Store the record under the name field. */
188 key.data = bp;
189 key.size = p - bp;
190
191 switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
192 case -1:
193 err(1, "put");
194 /* NOTREACHED */
195 case 1:
196 warnx("ignored duplicate: %.*s",
197 key.size, (char *)key.data);
198 continue;
199 }
200 ++reccnt;
201
202 /* If only one name, ignore the rest. */
203 if ((p = strchr(bp, '|')) == NULL)
204 continue;
205
206 /* The rest of the names reference the entire name. */
207 ((char *)(data.data))[0] = SHADOW;
208 memmove(&((u_char *)(data.data))[1], key.data, key.size);
209 data.size = key.size + 1;
210
211 /* Store references for other names. */
212 for (p = t = bp;; ++p) {
213 if (p > t && (*p == ':' || *p == '|')) {
214 key.size = p - t;
215 key.data = t;
216 switch(capdbp->put(capdbp,
217 &key, &data, R_NOOVERWRITE)) {
218 case -1:
219 err(1, "put");
220 /* NOTREACHED */
221 case 1:
222 warnx("ignored duplicate: %.*s",
223 key.size, (char *)key.data);
224 }
225 t = p + 1;
226 }
227 if (*p == ':')
228 break;
229 }
230 }
231
232 switch(st) {
233 case -1:
234 err(1, "file argument");
235 /* NOTREACHED */
236 case -2:
237 errx(1, "potential reference loop detected");
238 /* NOTREACHED */
239 }
240
241 if (verbose)
242 (void)printf("cap_mkdb: %d capability records\n", reccnt);
243 }
244
245 void
246 usage()
247 {
248 (void)fprintf(stderr,
249 "usage: cap_mkdb [-v] [-f outfile] file1 [file2 ...]\n");
250 exit(1);
251 }
252