1 /* $NetBSD: cap_mkdb.c,v 1.24 2008/07/21 14:19:21 lukem Exp $ */
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 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
39 The Regents of the University of California. All rights reserved.");
41 static char sccsid
[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
43 __RCSID("$NetBSD: cap_mkdb.c,v 1.24 2008/07/21 14:19:21 lukem Exp $");
46 #include <sys/param.h>
59 static void db_build(const char **);
60 static void dounlink(void);
61 static void usage(void) __unused
;
62 static int count_records(char **);
66 static char *capname
, buf
[8 * 1024];
68 static HASHINFO openinfo
= {
72 2048 * 1024, /* cachesize */
78 * Mkcapdb creates a capability hash database for quick retrieval of capability
79 * records. The database contains 2 types of entries: records and references
80 * marked by the first byte in the data. A record entry contains the actual
81 * capability record whereas a reference contains the name (key) under which
82 * the correct record is stored.
85 main(int argc
, char *argv
[])
92 while ((c
= getopt(argc
, argv
, "bf:lv")) != -1) {
98 byteorder
= c
== 'b' ? 4321 : 1234;
118 openinfo
.lorder
= byteorder
;
121 * Set nelem to twice the value returned by count_record().
123 openinfo
.nelem
= count_records(argv
) << 1;
126 * The database file is the first argument if no name is specified.
127 * Make arrangements to unlink it if exit badly.
129 (void)snprintf(buf
, sizeof(buf
), "%s.db.tmp",
130 capname
? capname
: *argv
);
131 if ((capname
= strdup(buf
)) == NULL
)
133 if ((capdbp
= dbopen(capname
, O_CREAT
| O_TRUNC
| O_RDWR
,
134 DEFFILEMODE
, DB_HASH
, &openinfo
)) == NULL
)
137 if (atexit(dounlink
))
140 db_build((const char **)argv
);
142 if (capdbp
->close(capdbp
) < 0)
143 err(1, "%s", capname
);
144 assert((p
= strrchr(buf
, '.')) != NULL
);
146 if (rename(capname
, buf
) == -1)
157 (void)unlink(capname
);
161 * Any changes to these definitions should be made also in the getcap(3)
164 #define RECOK (char)0
165 #define TCERR (char)1
166 #define SHADOW (char)2
169 * Db_build() builds the name and capabilty databases according to the
173 db_build(const char **ifiles
)
179 char *bp
, *p
, *t
, *n
;
183 for (reccnt
= 0, bplen
= 0; (st
= cgetnext(&bp
, ifiles
)) > 0; free(bp
)){
186 * Allocate enough memory to store record, terminating
187 * NULL and one extra byte.
190 if (bplen
<= len
+ 2) {
191 if ((n
= realloc(data
.data
,
192 bplen
+ MAX(256, len
+ 2))) == NULL
)
195 bplen
+= MAX(256, len
+ 2);
198 /* Find the end of the name field. */
199 if ((p
= strchr(bp
, ':')) == NULL
) {
200 warnx("no name field: %.*s", (int)(MIN(len
, 20)), bp
);
204 /* First byte of stored record indicates status. */
207 ((char *)(data
.data
))[0] = RECOK
;
210 ((char *)(data
.data
))[0] = TCERR
;
211 warnx("Record not tc expanded: %.*s", (int)(p
- bp
),bp
);
215 /* Create the stored record. */
216 (void)memmove(&((u_char
*)(data
.data
))[1], bp
, len
+ 1);
219 /* Store the record under the name field. */
223 switch(capdbp
->put(capdbp
, &key
, &data
, R_NOOVERWRITE
)) {
228 warnx("ignored duplicate: %.*s",
229 (int)key
.size
, (char *)key
.data
);
234 /* If only one name, ignore the rest. */
235 if ((p
= strchr(bp
, '|')) == NULL
)
238 /* The rest of the names reference the entire name. */
239 ((char *)(data
.data
))[0] = SHADOW
;
240 (void)memmove(&((u_char
*)(data
.data
))[1], key
.data
, key
.size
);
241 data
.size
= key
.size
+ 1;
243 /* Store references for other names. */
244 for (p
= t
= bp
;; ++p
) {
245 if (p
> t
&& (*p
== ':' || *p
== '|')) {
248 switch(capdbp
->put(capdbp
,
249 &key
, &data
, R_NOOVERWRITE
)) {
254 warnx("ignored duplicate: %.*s",
255 (int)key
.size
, (char *)key
.data
);
266 err(1, "file argument");
269 errx(1, "potential reference loop detected");
274 (void)printf("cap_mkdb: %d capability records\n", reccnt
);
280 (void)fprintf(stderr
,
281 "Usage: %s [-b|-l] [-v] [-f outfile] file1 [file2 ...]\n",
287 * Count number of records in input files. This does not need
288 * to be really accurate (the result is used only as a hint).
289 * It seems to match number of records should a cgetnext() be used, though.
292 count_records(char **list
)
299 /* scan input files and count individual records */
300 for(nelem
= 0, slash
= 0; *list
&& (fp
= fopen(*list
++, "r")); ) {
301 while((line
= fgetln(fp
, &len
)) != NULL
) {
304 if (!isspace((unsigned char) *line
) && *line
!= ':'
305 && *line
!= '#' && !slash
)
308 slash
= (line
[len
- 2] == '\\');
314 /* no records found; pass default size hint */
316 } else if (!powerof2(nelem
)) {
317 /* set nelem to nearest bigger power-of-two number */
319 while(bt
< nelem
) bt
<<= 1;