2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/param.h>
43 static void db_build(char **);
44 static void dounlink(void);
45 static void usage(void);
49 static char *capname, buf[8 * 1024];
51 static HASHINFO openinfo = {
61 * Mkcapdb creates a capability hash database for quick retrieval of capability
62 * records. The database contains 2 types of entries: records and references
63 * marked by the first byte in the data. A record entry contains the actual
64 * capability record whereas a reference contains the name (key) under which
65 * the correct record is stored.
68 main(int argc, char *argv[])
74 while ((c = getopt(argc, argv, "bf:lv")) != -1) {
80 byteorder = c == 'b' ? 4321 : 1234;
100 openinfo.lorder = byteorder;
103 * The database file is the first argument if no name is specified.
104 * Make arrangements to unlink it if exit badly.
106 (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
107 if ((capname = strdup(buf)) == NULL)
108 errx(1, "strdup failed");
109 if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
110 DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
113 if (atexit(dounlink))
118 if (capdbp->close(capdbp) < 0)
119 err(1, "%s", capname);
128 (void)unlink(capname);
132 * Any changes to these definitions should be made also in the getcap(3)
135 #define RECOK (char)0
136 #define TCERR (char)1
137 #define SHADOW (char)2
140 * Db_build() builds the name and capability databases according to the
144 db_build(char **ifiles)
154 for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
157 * Allocate enough memory to store record, terminating
158 * NULL and one extra byte.
161 if (bplen <= len + 2) {
162 bplen += MAX(256, len + 2);
163 if ((data.data = realloc(data.data, bplen)) == NULL)
164 errx(1, "malloc failed");
167 /* Find the end of the name field. */
168 if ((p = strchr(bp, ':')) == NULL) {
169 warnx("no name field: %.*s", (int)MIN(len, 20), bp);
173 /* First byte of stored record indicates status. */
176 ((char *)(data.data))[0] = RECOK;
179 ((char *)(data.data))[0] = TCERR;
180 warnx("record not tc expanded: %.*s", (int)(p - bp),
185 /* Create the stored record. */
186 memmove(&((u_char *)(data.data))[1], bp, len + 1);
189 /* Store the record under the name field. */
193 switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
198 warnx("ignored duplicate: %.*s",
199 (int)key.size, (char *)key.data);
204 /* If only one name, ignore the rest. */
206 if (strchr(bp, '|') == NULL)
210 /* The rest of the names reference the entire name. */
211 ((char *)(data.data))[0] = SHADOW;
212 memmove(&((u_char *)(data.data))[1], key.data, key.size);
213 data.size = key.size + 1;
215 /* Store references for other names. */
216 for (p = t = bp;; ++p) {
217 if (p > t && (*p == ':' || *p == '|')) {
220 switch(capdbp->put(capdbp,
221 &key, &data, R_NOOVERWRITE)) {
226 warnx("ignored duplicate: %.*s",
227 (int)key.size, (char *)key.data);
238 err(1, "file argument");
241 errx(1, "potential reference loop detected");
246 (void)printf("cap_mkdb: %d capability records\n", reccnt);
252 (void)fprintf(stderr,
253 "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n");