#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#if !defined(lint)
__COPYRIGHT("@(#) Copyright (c) 1992, 1993\
The Regents of the University of California. All rights reserved.");
#if 0
static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
#endif
__RCSID("$NetBSD: cap_mkdb.c,v 1.30 2021/12/03 13:27:39 andvar Exp $");
#endif
#include <sys/param.h>
#include <sys/stat.h>
#include <assert.h>
#include <db.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
static void db_build(const char **);
static void dounlink(void);
static void usage(void) __dead;
static int count_records(char **);
static DB *capdbp;
static int verbose;
static char *capname, outfile[MAXPATHLEN];
static HASHINFO openinfo = {
4096,
16,
2048,
2048 * 1024,
NULL,
0
};
int
main(int argc, char *argv[])
{
int c, byteorder;
char *p;
capname = NULL;
byteorder = 0;
while ((c = getopt(argc, argv, "bf:lv")) != -1) {
switch(c) {
case 'b':
case 'l':
if (byteorder != 0)
usage();
byteorder = c == 'b' ? 4321 : 1234;
break;
case 'f':
capname = optarg;
break;
case 'v':
verbose = 1;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (*argv == NULL)
usage();
openinfo.lorder = byteorder;
openinfo.nelem = count_records(argv) << 1;
(void)snprintf(outfile, sizeof(outfile), "%s.db.tmp",
capname ? capname : *argv);
if ((capname = strdup(outfile)) == NULL)
err(1, "strdup");
p = strrchr(outfile, '.');
assert(p != NULL);
*p = '\0';
(void)unlink(outfile);
if ((capdbp = dbopen(capname, O_CREAT | O_TRUNC | O_RDWR,
DEFFILEMODE, DB_HASH, &openinfo)) == NULL)
err(1, "%s", outfile);
if (atexit(dounlink))
err(1, "atexit");
db_build((void *)argv);
if (capdbp->close(capdbp) < 0)
err(1, "%s", capname);
if (rename(capname, outfile) == -1)
err(1, "rename");
free(capname);
capname = NULL;
return 0;
}
static void
dounlink(void)
{
if (capname != NULL)
unlink(capname);
}
#define RECOK (char)0
#define TCERR (char)1
#define SHADOW (char)2
static void
db_build(const char **ifiles)
{
DBT key, data;
recno_t reccnt;
size_t len, bplen;
int st;
char *bp, *p, *t, *n;
data.data = NULL;
key.data = NULL;
for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0; free(bp)){
len = strlen(bp);
if (bplen <= len + 2) {
if ((n = realloc(data.data,
bplen + MAX(256, len + 2))) == NULL)
err(1, "realloc");
data.data = n;
bplen += MAX(256, len + 2);
}
if ((p = strchr(bp, ':')) == NULL) {
warnx("no name field: %.*s", (int)(MIN(len, 20)), bp);
continue;
}
switch(st) {
case 1:
((char *)(data.data))[0] = RECOK;
break;
case 2:
((char *)(data.data))[0] = TCERR;
warnx("Record not tc expanded: %.*s", (int)(p - bp),bp);
break;
}
(void)memmove(&((u_char *)(data.data))[1], bp, len + 1);
data.size = len + 2;
key.data = bp;
key.size = p - bp;
switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
case -1:
err(1, "put");
case 1:
warnx("ignored duplicate: %.*s",
(int)key.size, (char *)key.data);
continue;
}
++reccnt;
if ((p = strchr(bp, '|')) == NULL)
continue;
((char *)(data.data))[0] = SHADOW;
(void)memmove(&((u_char *)(data.data))[1], key.data, key.size);
data.size = key.size + 1;
for (p = t = bp;; ++p) {
if (p > t && (*p == ':' || *p == '|')) {
key.size = p - t;
key.data = t;
switch(capdbp->put(capdbp,
&key, &data, R_NOOVERWRITE)) {
case -1:
err(1, "put");
case 1:
warnx("ignored duplicate: %.*s",
(int)key.size, (char *)key.data);
}
t = p + 1;
}
if (*p == ':')
break;
}
}
switch(st) {
case -1:
err(1, "file argument");
case -2:
errx(1, "potential reference loop detected");
}
if (verbose)
(void)printf("cap_mkdb: %d capability records\n", reccnt);
}
static void
usage(void)
{
(void)fprintf(stderr,
"Usage: %s [-b|-l] [-v] [-f outfile] file1 [file2 ...]\n",
getprogname());
exit(1);
}
static int
count_records(char **list)
{
FILE *fp;
char *line;
size_t len;
int nelem, slash;
for(nelem = 0, slash = 0; *list && (fp = fopen(*list++, "r")); ) {
while((line = fgetln(fp, &len)) != NULL) {
if (len < 2)
continue;
if (!isspace((unsigned char) *line) && *line != ':'
&& *line != '#' && !slash)
nelem++;
slash = (line[len - 2] == '\\');
}
(void)fclose(fp);
}
if (nelem == 0) {
nelem = 1;
} else if (!powerof2(nelem)) {
int bt = 1;
while(bt < nelem) bt <<= 1;
nelem = bt;
}
return nelem;
}