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 * 3. 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
29 * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)cap_mkdb.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/cap_mkdb/cap_mkdb.c,v 1.14 2005/02/22 23:29:53 ru Exp $
32 * $DragonFly: src/usr.bin/cap_mkdb/cap_mkdb.c,v 1.5 2007/09/25 04:53:48 pavalos Exp $
35 #include <sys/param.h>
46 void db_build(char **);
52 char *capdb, *capname, buf[8 * 1024];
64 * Mkcapdb creates a capability hash database for quick retrieval of capability
65 * records. The database contains 2 types of entries: records and references
66 * marked by the first byte in the data. A record entry contains the actual
67 * capability record whereas a reference contains the name (key) under which
68 * the correct record is stored.
71 main(int argc, char **argv)
77 while ((c = getopt(argc, argv, "bf:lv")) != -1) {
83 byteorder = c == 'b' ? 4321 : 1234;
102 /* Set byte order. */
103 openinfo.lorder = byteorder;
106 * The database file is the first argument if no name is specified.
107 * Make arrangements to unlink it if exit badly.
109 (void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
110 if ((capname = strdup(buf)) == NULL)
111 errx(1, "strdup failed");
112 if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
113 DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
116 if (atexit(dounlink))
121 if (capdbp->close(capdbp) < 0)
122 err(1, "%s", capname);
131 (void)unlink(capname);
135 * Any changes to these definitions should be made also in the getcap(3)
138 #define RECOK (char)0
139 #define TCERR (char)1
140 #define SHADOW (char)2
143 * Db_build() builds the name and capability databases according to the
147 db_build(char **ifiles)
157 for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
160 * Allocate enough memory to store record, terminating
161 * NULL and one extra byte.
164 if (bplen <= len + 2) {
165 bplen += MAX(256, len + 2);
166 if ((data.data = realloc(data.data, bplen)) == NULL)
167 errx(1, "malloc failed");
170 /* Find the end of the name field. */
171 if ((p = strchr(bp, ':')) == NULL) {
172 warnx("no name field: %.*s", (int)MIN(len, 20), bp);
176 /* First byte of stored record indicates status. */
179 ((char *)(data.data))[0] = RECOK;
182 ((char *)(data.data))[0] = TCERR;
183 warnx("record not tc expanded: %.*s", (int)(p - bp),
188 /* Create the stored record. */
189 memmove(&((u_char *)(data.data))[1], bp, len + 1);
192 /* Store the record under the name field. */
196 switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
201 warnx("ignored duplicate: %.*s",
202 (int)key.size, (char *)key.data);
207 /* If only one name, ignore the rest. */
209 if (strchr(bp, '|') == NULL)
213 /* The rest of the names reference the entire name. */
214 ((char *)(data.data))[0] = SHADOW;
215 memmove(&((u_char *)(data.data))[1], key.data, key.size);
216 data.size = key.size + 1;
218 /* Store references for other names. */
219 for (p = t = bp;; ++p) {
220 if (p > t && (*p == ':' || *p == '|')) {
223 switch(capdbp->put(capdbp,
224 &key, &data, R_NOOVERWRITE)) {
229 warnx("ignored duplicate: %.*s",
230 (int)key.size, (char *)key.data);
241 err(1, "file argument");
244 errx(1, "potential reference loop detected");
249 (void)printf("cap_mkdb: %d capability records\n", reccnt);
255 (void)fprintf(stderr,
256 "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n");