2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1992, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
56 void db_build(char **);
62 char *capdb, *capname, buf[8 * 1024];
74 * Mkcapdb creates a capability hash database for quick retrieval of capability
75 * records. The database contains 2 types of entries: records and references
76 * marked by the first byte in the data. A record entry contains the actual
77 * capability record whereas a reference contains the name (key) under which
78 * the correct record is stored.
81 main(int argc, char *argv[])
87 while ((c = getopt(argc, argv, "bf:lv")) != -1) {
93 byteorder = c == 'b' ? 4321 : 1234;
112 /* Set byte order. */
113 openinfo.lorder = byteorder;
116 * The database file is the first argument if no name is specified.
117 * Make arrangements to unlink it if exit badly.
119 (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
120 if ((capname = strdup(buf)) == NULL)
121 errx(1, "strdup failed");
122 if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
123 DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
126 if (atexit(dounlink))
131 if (capdbp->close(capdbp) < 0)
132 err(1, "%s", capname);
141 (void)unlink(capname);
145 * Any changes to these definitions should be made also in the getcap(3)
148 #define RECOK (char)0
149 #define TCERR (char)1
150 #define SHADOW (char)2
153 * Db_build() builds the name and capability databases according to the
157 db_build(char **ifiles)
167 for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
170 * Allocate enough memory to store record, terminating
171 * NULL and one extra byte.
174 if (bplen <= len + 2) {
175 bplen += MAX(256, len + 2);
176 if ((data.data = realloc(data.data, bplen)) == NULL)
177 errx(1, "malloc failed");
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);
186 /* First byte of stored record indicates status. */
189 ((char *)(data.data))[0] = RECOK;
192 ((char *)(data.data))[0] = TCERR;
193 warnx("record not tc expanded: %.*s", (int)(p - bp),
198 /* Create the stored record. */
199 memmove(&((u_char *)(data.data))[1], bp, len + 1);
202 /* Store the record under the name field. */
206 switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
211 warnx("ignored duplicate: %.*s",
212 (int)key.size, (char *)key.data);
217 /* If only one name, ignore the rest. */
219 if (strchr(bp, '|') == NULL)
223 /* The rest of the names reference the entire name. */
224 ((char *)(data.data))[0] = SHADOW;
225 memmove(&((u_char *)(data.data))[1], key.data, key.size);
226 data.size = key.size + 1;
228 /* Store references for other names. */
229 for (p = t = bp;; ++p) {
230 if (p > t && (*p == ':' || *p == '|')) {
233 switch(capdbp->put(capdbp,
234 &key, &data, R_NOOVERWRITE)) {
239 warnx("ignored duplicate: %.*s",
240 (int)key.size, (char *)key.data);
251 err(1, "file argument");
254 errx(1, "potential reference loop detected");
259 (void)printf("cap_mkdb: %d capability records\n", reccnt);
265 (void)fprintf(stderr,
266 "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n");