2006-07-28 20:34:28 +04:00
|
|
|
/* $NetBSD: services_mkdb.c,v 1.5 2006/07/28 16:34:28 christos Exp $ */
|
2006-07-28 02:13:38 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2006-07-28 20:34:28 +04:00
|
|
|
* by Luke Mewburn and Christos Zoulas.
|
2006-07-28 02:13:38 +04:00
|
|
|
*
|
|
|
|
* 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 NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2006-07-28 20:34:28 +04:00
|
|
|
__RCSID("$NetBSD: services_mkdb.c,v 1.5 2006/07/28 16:34:28 christos Exp $");
|
2006-07-28 02:13:38 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <db.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stringlist.h>
|
|
|
|
|
|
|
|
static char tname[MAXPATHLEN];
|
|
|
|
|
|
|
|
static void cleanup(void);
|
2006-07-28 19:15:16 +04:00
|
|
|
static void store(const char *, size_t, DB *, DBT *, DBT *, int);
|
2006-07-28 20:34:28 +04:00
|
|
|
static void killproto(DBT *);
|
|
|
|
static char *getstring(const char *, size_t, char **, const char *);
|
2006-07-28 02:13:38 +04:00
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
DB *db;
|
|
|
|
FILE *fp;
|
|
|
|
int ch;
|
2006-07-28 19:15:16 +04:00
|
|
|
size_t len, line, cnt;
|
2006-07-28 02:13:38 +04:00
|
|
|
char keyb[BUFSIZ], datab[BUFSIZ], *p;
|
|
|
|
DBT data, key;
|
2006-07-28 20:34:28 +04:00
|
|
|
const char *fname = _PATH_SERVICES;
|
2006-07-28 02:13:38 +04:00
|
|
|
const char *dbname = _PATH_SERVICES_DB;
|
|
|
|
|
|
|
|
setprogname(argv[0]);
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "o:")) != -1)
|
|
|
|
switch (ch) {
|
|
|
|
case 'o':
|
|
|
|
dbname = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
if (argc == 1)
|
|
|
|
fname = argv[0];
|
|
|
|
|
|
|
|
if ((fp = fopen(fname, "r")) == NULL)
|
|
|
|
err(1, "Cannot open `%s'", fname);
|
|
|
|
|
|
|
|
if (atexit(cleanup))
|
|
|
|
err(1, "Cannot install exit handler");
|
|
|
|
|
|
|
|
(void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
|
|
|
|
db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
|
|
|
|
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, NULL);
|
|
|
|
if (!db)
|
|
|
|
err(1, "Error opening temporary database `%s'", tname);
|
|
|
|
|
2006-07-28 19:15:16 +04:00
|
|
|
key.data = keyb;
|
|
|
|
data.data = datab;
|
2006-07-28 02:13:38 +04:00
|
|
|
|
2006-07-28 19:15:16 +04:00
|
|
|
cnt = line = 0;
|
2006-07-28 02:13:38 +04:00
|
|
|
/* XXX: change NULL to "\0\0#" when fparseln fixed */
|
|
|
|
for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
|
|
|
|
char *name, *port, *proto, *aliases, *cp, *alias;
|
|
|
|
StringList *sl;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
continue;
|
|
|
|
cp = p;
|
2006-07-28 20:34:28 +04:00
|
|
|
|
|
|
|
if ((name = getstring(fname, line, &cp, "name")) == NULL)
|
2006-07-28 02:13:38 +04:00
|
|
|
continue;
|
2006-07-28 20:34:28 +04:00
|
|
|
|
|
|
|
if ((port = getstring(fname, line, &cp, "port")) == NULL)
|
2006-07-28 02:13:38 +04:00
|
|
|
continue;
|
2006-07-28 20:34:28 +04:00
|
|
|
|
2006-07-28 02:13:38 +04:00
|
|
|
proto = strchr(port, '/');
|
|
|
|
if (proto == NULL || proto[1] == '\0') {
|
|
|
|
warnx("%s, %zu: no protocol found", fname, line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*proto++ = '\0';
|
2006-07-28 20:34:28 +04:00
|
|
|
|
2006-07-28 02:13:38 +04:00
|
|
|
for (aliases = cp; cp && isspace((unsigned char)*aliases);
|
|
|
|
aliases++)
|
|
|
|
continue;
|
|
|
|
|
2006-07-28 19:15:16 +04:00
|
|
|
/* key `indirect key', data `full line' */
|
|
|
|
data.size = snprintf(datab, sizeof(datab), "%zu", cnt++) + 1;
|
|
|
|
key.size = snprintf(keyb, sizeof(keyb), "%s %s/%s %s",
|
2006-07-28 20:34:28 +04:00
|
|
|
name, port, proto, aliases ? aliases : "") + 1;
|
2006-07-28 19:15:16 +04:00
|
|
|
store(fname, line, db, &data, &key, 1);
|
2006-07-28 02:13:38 +04:00
|
|
|
|
2006-07-28 19:15:16 +04:00
|
|
|
/* key `\377port/proto', data = `indirect key' */
|
|
|
|
key.size = snprintf(keyb, sizeof(keyb), "\377%s/%s",
|
|
|
|
port, proto) + 1;
|
|
|
|
store(fname, line, db, &key, &data, 1);
|
2006-07-28 02:13:38 +04:00
|
|
|
|
2006-07-28 19:15:16 +04:00
|
|
|
/* key `\377port', data = `indirect key' */
|
2006-07-28 20:34:28 +04:00
|
|
|
killproto(&key);
|
2006-07-28 19:15:16 +04:00
|
|
|
store(fname, line, db, &key, &data, 0);
|
2006-07-28 02:13:38 +04:00
|
|
|
|
|
|
|
/* build list of aliases */
|
|
|
|
sl = sl_init();
|
2006-07-28 20:34:28 +04:00
|
|
|
(void)sl_add(sl, name);
|
2006-07-28 02:13:38 +04:00
|
|
|
while ((alias = strsep(&cp, " \t")) != NULL) {
|
|
|
|
if (alias[0] == '\0')
|
|
|
|
continue;
|
2006-07-28 20:34:28 +04:00
|
|
|
(void)sl_add(sl, alias);
|
2006-07-28 02:13:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add references for service and all aliases */
|
|
|
|
for (i = 0; i < sl->sl_cur; i++) {
|
2006-07-28 19:15:16 +04:00
|
|
|
/* key `\376service/proto', data = `indirect key' */
|
|
|
|
key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
|
2006-07-28 02:13:38 +04:00
|
|
|
sl->sl_str[i], proto) + 1;
|
2006-07-28 19:15:16 +04:00
|
|
|
store(fname, line, db, &key, &data, 1);
|
|
|
|
|
|
|
|
/* key `\376service', data = `indirect key' */
|
2006-07-28 20:34:28 +04:00
|
|
|
killproto(&key);
|
2006-07-28 19:15:16 +04:00
|
|
|
store(fname, line, db, &key, &data, 0);
|
2006-07-28 02:13:38 +04:00
|
|
|
}
|
|
|
|
sl_free(sl, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((db->close)(db))
|
|
|
|
err(1, "Error closing temporary database `%s'", tname);
|
2006-07-28 20:34:28 +04:00
|
|
|
|
2006-07-28 02:13:38 +04:00
|
|
|
if (rename(tname, dbname) == -1)
|
|
|
|
err(1, "Cannot rename `%s' to `%s'", tname, dbname);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cleanup(): Remove temporary files upon exit
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
if (tname[0])
|
|
|
|
(void)unlink(tname);
|
|
|
|
}
|
|
|
|
|
2006-07-28 20:34:28 +04:00
|
|
|
static char *
|
|
|
|
getstring(const char *fname, size_t line, char **cp, const char *tag)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
warnx("%s, %zu: no %s found", fname, line, tag);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
killproto(DBT *key)
|
|
|
|
{
|
|
|
|
char *p, *d = key->data;
|
|
|
|
|
|
|
|
if ((p = strchr(d, '/')) == NULL)
|
|
|
|
abort();
|
|
|
|
*p++ = '\0';
|
|
|
|
key->size = p - d;
|
|
|
|
}
|
|
|
|
|
2006-07-28 19:15:16 +04:00
|
|
|
static void
|
|
|
|
store(const char *fname, size_t line, DB *db, DBT *key, DBT *data, int warndup)
|
|
|
|
{
|
|
|
|
switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (warndup)
|
|
|
|
warnx("%s, %zu: duplicate service `%s'",
|
|
|
|
fname, line, &((char *)key->data)[1]);
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
err(1, "put");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-28 02:13:38 +04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr, "Usage: %s [-o <db>] [<servicefile>]\n",
|
|
|
|
getprogname());
|
|
|
|
exit(1);
|
|
|
|
}
|