1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-21 11:13:30 +00:00
freebsd/usr.bin/cap_mkdb/cap_mkdb.c

254 lines
6.2 KiB
C
Raw Normal View History

1994-05-27 12:33:43 +00:00
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static const char copyright[] =
1994-05-27 12:33:43 +00:00
"@(#) Copyright (c) 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
2001-12-02 22:46:47 +00:00
#endif
1994-05-27 12:33:43 +00:00
#if 0
1994-05-27 12:33:43 +00:00
#ifndef lint
static char sccsid[] = "@(#)cap_mkdb.c 8.1 (Berkeley) 6/6/93";
#endif
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
1994-05-27 12:33:43 +00:00
#include <sys/param.h>
#include <sys/stat.h>
#include <db.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2002-03-22 01:22:50 +00:00
void db_build(char **);
void dounlink(void);
void usage(void);
1994-05-27 12:33:43 +00:00
DB *capdbp;
int verbose;
char *capdb, *capname, buf[8 * 1024];
/*
* Mkcapdb creates a capability hash database for quick retrieval of capability
* records. The database contains 2 types of entries: records and references
* marked by the first byte in the data. A record entry contains the actual
* capability record whereas a reference contains the name (key) under which
* the correct record is stored.
*/
int
main(int argc, char *argv[])
1994-05-27 12:33:43 +00:00
{
int c;
capname = NULL;
while ((c = getopt(argc, argv, "f:v")) != -1) {
1994-05-27 12:33:43 +00:00
switch(c) {
case 'f':
capname = optarg;
break;
case 'v':
verbose = 1;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (*argv == NULL)
usage();
/*
* The database file is the first argument if no name is specified.
* Make arrangements to unlink it if exit badly.
*/
(void)snprintf(buf, sizeof(buf), "%s.db", capname ? capname : *argv);
if ((capname = strdup(buf)) == NULL)
errx(1, "strdup failed");
1994-05-27 12:33:43 +00:00
if ((capdbp = dbopen(capname,
O_CREAT | O_TRUNC | O_RDWR, DEFFILEMODE, DB_HASH, NULL)) == NULL)
err(1, "%s", buf);
if (atexit(dounlink))
err(1, "atexit");
db_build(argv);
if (capdbp->close(capdbp) < 0)
err(1, "%s", capname);
capname = NULL;
exit(0);
}
void
dounlink(void)
1994-05-27 12:33:43 +00:00
{
if (capname != NULL)
(void)unlink(capname);
}
/*
* Any changes to these definitions should be made also in the getcap(3)
* library routines.
*/
#define RECOK (char)0
#define TCERR (char)1
#define SHADOW (char)2
/*
* Db_build() builds the name and capability databases according to the
1994-05-27 12:33:43 +00:00
* details above.
*/
void
db_build(char **ifiles)
1994-05-27 12:33:43 +00:00
{
DBT key, data;
recno_t reccnt;
size_t len, bplen;
int st;
char *bp, *p, *t;
data.data = NULL;
key.data = NULL;
for (reccnt = 0, bplen = 0; (st = cgetnext(&bp, ifiles)) > 0;) {
/*
* Allocate enough memory to store record, terminating
* NULL and one extra byte.
*/
len = strlen(bp);
if (bplen <= len + 2) {
bplen += MAX(256, len + 2);
if ((data.data = realloc(data.data, bplen)) == NULL)
errx(1, "malloc failed");
1994-05-27 12:33:43 +00:00
}
/* Find the end of the name field. */
if ((p = strchr(bp, ':')) == NULL) {
1998-07-06 21:01:54 +00:00
warnx("no name field: %.*s", (int)MIN(len, 20), bp);
1994-05-27 12:33:43 +00:00
continue;
}
/* First byte of stored record indicates status. */
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);
1994-05-27 12:33:43 +00:00
break;
}
/* Create the stored record. */
memmove(&((u_char *)(data.data))[1], bp, len + 1);
data.size = len + 2;
/* Store the record under the name field. */
key.data = bp;
key.size = p - bp;
switch(capdbp->put(capdbp, &key, &data, R_NOOVERWRITE)) {
case -1:
err(1, "put");
/* NOTREACHED */
case 1:
warnx("ignored duplicate: %.*s",
1998-07-06 21:01:54 +00:00
(int)key.size, (char *)key.data);
1994-05-27 12:33:43 +00:00
continue;
}
++reccnt;
/* If only one name, ignore the rest. */
*p = '\0';
if (strchr(bp, '|') == NULL)
1994-05-27 12:33:43 +00:00
continue;
*p = ':';
1994-05-27 12:33:43 +00:00
/* The rest of the names reference the entire name. */
((char *)(data.data))[0] = SHADOW;
memmove(&((u_char *)(data.data))[1], key.data, key.size);
data.size = key.size + 1;
/* Store references for other names. */
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");
/* NOTREACHED */
case 1:
warnx("ignored duplicate: %.*s",
1998-07-06 21:01:54 +00:00
(int)key.size, (char *)key.data);
1994-05-27 12:33:43 +00:00
}
t = p + 1;
}
if (*p == ':')
break;
}
}
switch(st) {
case -1:
err(1, "file argument");
/* NOTREACHED */
case -2:
errx(1, "potential reference loop detected");
/* NOTREACHED */
}
if (verbose)
(void)printf("cap_mkdb: %d capability records\n", reccnt);
}
void
usage(void)
1994-05-27 12:33:43 +00:00
{
(void)fprintf(stderr,
"usage: cap_mkdb [-v] [-f outfile] file [file ...]\n");
1994-05-27 12:33:43 +00:00
exit(1);
}