Home | History | Annotate | Line # | Download | only in services_mkdb
services_mkdb.c revision 1.10
      1  1.10  christos /*	$NetBSD: services_mkdb.c,v 1.10 2007/06/23 16:55:15 christos Exp $	*/
      2   1.1     lukem 
      3   1.1     lukem /*-
      4   1.1     lukem  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1     lukem  * All rights reserved.
      6   1.1     lukem  *
      7   1.1     lukem  * This code is derived from software contributed to The NetBSD Foundation
      8   1.5  christos  * by Luke Mewburn and Christos Zoulas.
      9   1.1     lukem  *
     10   1.1     lukem  * Redistribution and use in source and binary forms, with or without
     11   1.1     lukem  * modification, are permitted provided that the following conditions
     12   1.1     lukem  * are met:
     13   1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     14   1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     15   1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     lukem  *    documentation and/or other materials provided with the distribution.
     18   1.1     lukem  * 3. All advertising materials mentioning features or use of this software
     19   1.1     lukem  *    must display the following acknowledgement:
     20   1.1     lukem  *        This product includes software developed by the NetBSD
     21   1.1     lukem  *        Foundation, Inc. and its contributors.
     22   1.1     lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1     lukem  *    contributors may be used to endorse or promote products derived
     24   1.1     lukem  *    from this software without specific prior written permission.
     25   1.1     lukem  *
     26   1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1     lukem  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1     lukem  */
     38   1.1     lukem 
     39   1.1     lukem #include <sys/cdefs.h>
     40   1.1     lukem #ifndef lint
     41  1.10  christos __RCSID("$NetBSD: services_mkdb.c,v 1.10 2007/06/23 16:55:15 christos Exp $");
     42   1.1     lukem #endif /* not lint */
     43   1.1     lukem 
     44   1.1     lukem #include <sys/param.h>
     45   1.1     lukem 
     46   1.9  christos #include <assert.h>
     47   1.1     lukem #include <db.h>
     48   1.1     lukem #include <err.h>
     49   1.1     lukem #include <fcntl.h>
     50   1.1     lukem #include <netdb.h>
     51   1.1     lukem #include <stdio.h>
     52   1.1     lukem #include <stdlib.h>
     53   1.1     lukem #include <string.h>
     54   1.1     lukem #include <unistd.h>
     55   1.1     lukem #include <util.h>
     56   1.3  christos #include <ctype.h>
     57   1.9  christos #include <errno.h>
     58   1.3  christos #include <stringlist.h>
     59   1.1     lukem 
     60   1.3  christos static char tname[MAXPATHLEN];
     61   1.3  christos 
     62   1.9  christos #define	PMASK		0xffff
     63   1.9  christos #define PROTOMAX	5
     64   1.9  christos 
     65  1.10  christos extern void	uniq(const char *);
     66  1.10  christos 
     67   1.9  christos static void	add(DB *, StringList *, size_t, const char *, size_t *, int);
     68   1.9  christos static StringList ***parseservices(const char *, StringList *);
     69   1.3  christos static void	cleanup(void);
     70   1.9  christos static void	store(DB *, DBT *, DBT *, int);
     71   1.5  christos static void	killproto(DBT *);
     72   1.5  christos static char    *getstring(const char *, size_t, char **, const char *);
     73   1.9  christos static size_t	getprotoindex(StringList *, const char *);
     74   1.9  christos static const char *getprotostr(StringList *, size_t);
     75   1.9  christos static const char *mkaliases(StringList *, char *, size_t);
     76   1.3  christos static void	usage(void) __attribute__((__noreturn__));
     77   1.1     lukem 
     78  1.10  christos const HASHINFO hinfo = {
     79   1.8  christos 	.bsize = 256,
     80   1.8  christos 	.ffactor = 4,
     81   1.8  christos 	.nelem = 32768,
     82   1.8  christos 	.cachesize = 1024,
     83   1.8  christos 	.hash = NULL,
     84   1.8  christos 	.lorder = 0
     85   1.8  christos };
     86   1.8  christos 
     87   1.8  christos 
     88   1.1     lukem int
     89   1.3  christos main(int argc, char *argv[])
     90   1.1     lukem {
     91   1.1     lukem 	DB	*db;
     92   1.3  christos 	int	 ch;
     93   1.3  christos 	const char *fname = _PATH_SERVICES;
     94   1.3  christos 	const char *dbname = _PATH_SERVICES_DB;
     95   1.6  christos 	int	 warndup = 1;
     96  1.10  christos 	int	 unique = 0;
     97   1.9  christos 	size_t	 cnt = 0;
     98   1.9  christos 	StringList *sl, ***svc;
     99   1.9  christos 	size_t port, proto;
    100   1.3  christos 
    101   1.3  christos 	setprogname(argv[0]);
    102   1.1     lukem 
    103  1.10  christos 	while ((ch = getopt(argc, argv, "qo:u")) != -1)
    104   1.1     lukem 		switch (ch) {
    105   1.6  christos 		case 'q':
    106   1.6  christos 			warndup = 0;
    107   1.6  christos 			break;
    108   1.1     lukem 		case 'o':
    109   1.1     lukem 			dbname = optarg;
    110   1.1     lukem 			break;
    111  1.10  christos 		case 'u':
    112  1.10  christos 			unique++;
    113  1.10  christos 			break;
    114   1.1     lukem 		case '?':
    115   1.1     lukem 		default:
    116   1.1     lukem 			usage();
    117   1.1     lukem 		}
    118   1.1     lukem 
    119   1.1     lukem 	argc -= optind;
    120   1.1     lukem 	argv += optind;
    121   1.1     lukem 
    122  1.10  christos 	if (argc > 1 || (unique && (!warndup || dbname)))
    123   1.3  christos 		usage();
    124   1.1     lukem 	if (argc == 1)
    125   1.1     lukem 		fname = argv[0];
    126   1.3  christos 
    127  1.10  christos 	if (unique)
    128  1.10  christos 		uniq(fname);
    129  1.10  christos 
    130   1.9  christos 	svc = parseservices(fname, sl = sl_init());
    131   1.3  christos 
    132   1.1     lukem 	if (atexit(cleanup))
    133   1.1     lukem 		err(1, "Cannot install exit handler");
    134   1.3  christos 
    135   1.3  christos 	(void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
    136   1.1     lukem 	db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
    137   1.8  christos 	    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
    138   1.1     lukem 	if (!db)
    139   1.1     lukem 		err(1, "Error opening temporary database `%s'", tname);
    140   1.1     lukem 
    141   1.9  christos 
    142   1.9  christos 	for (port = 0; port < PMASK + 1; port++) {
    143   1.9  christos 		if (svc[port] == NULL)
    144   1.9  christos 			continue;
    145   1.9  christos 
    146   1.9  christos 		for (proto = 0; proto < PROTOMAX; proto++) {
    147   1.9  christos 			StringList *s;
    148   1.9  christos 			if ((s = svc[port][proto]) == NULL)
    149   1.9  christos 				continue;
    150   1.9  christos 			add(db, s, port, getprotostr(sl, proto), &cnt, warndup);
    151   1.9  christos 		}
    152   1.9  christos 
    153   1.9  christos 		free(svc[port]);
    154   1.9  christos 	}
    155   1.9  christos 
    156   1.9  christos 	free(svc);
    157   1.9  christos 	sl_free(sl, 1);
    158   1.9  christos 
    159   1.9  christos 	if ((db->close)(db))
    160   1.9  christos 		err(1, "Error closing temporary database `%s'", tname);
    161   1.9  christos 
    162   1.9  christos 	if (rename(tname, dbname) == -1)
    163   1.9  christos 		err(1, "Cannot rename `%s' to `%s'", tname, dbname);
    164   1.9  christos 
    165   1.9  christos 	return 0;
    166   1.9  christos }
    167   1.9  christos 
    168   1.9  christos static void
    169   1.9  christos add(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt,
    170   1.9  christos     int warndup)
    171   1.9  christos {
    172   1.9  christos 	size_t i;
    173   1.9  christos 	char	 keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
    174   1.9  christos 	DBT	 data, key;
    175   1.4  christos 	key.data = keyb;
    176   1.4  christos 	data.data = datab;
    177   1.1     lukem 
    178   1.9  christos #ifdef DEBUG
    179   1.9  christos 	(void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto);
    180   1.9  christos 	for (i = 1; i < sl->sl_cur; i++)
    181   1.9  christos 	    (void)printf("%s ", sl->sl_str[i]);
    182   1.9  christos 	(void)printf("]\n");
    183   1.9  christos #endif
    184   1.9  christos 
    185   1.9  christos 	/* key `indirect key', data `full line' */
    186   1.9  christos 	data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
    187   1.9  christos 	key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
    188   1.9  christos 	    sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
    189   1.9  christos 	store(db, &data, &key, warndup);
    190   1.9  christos 
    191   1.9  christos 	/* key `\377port/proto', data = `indirect key' */
    192   1.9  christos 	key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
    193   1.9  christos 	    port, proto) + 1;
    194   1.9  christos 	store(db, &key, &data, warndup);
    195   1.9  christos 
    196   1.9  christos 	/* key `\377port', data = `indirect key' */
    197   1.9  christos 	killproto(&key);
    198   1.9  christos 	store(db, &key, &data, warndup);
    199   1.9  christos 
    200   1.9  christos 	/* add references for service and all aliases */
    201   1.9  christos 	for (i = 0; i < sl->sl_cur; i++) {
    202   1.9  christos 		/* key `\376service/proto', data = `indirect key' */
    203   1.9  christos 		key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
    204   1.9  christos 		    sl->sl_str[i], proto) + 1;
    205   1.9  christos 		store(db, &key, &data, warndup);
    206   1.9  christos 
    207   1.9  christos 		/* key `\376service', data = `indirect key' */
    208   1.9  christos 		killproto(&key);
    209   1.9  christos 		store(db, &key, &data, warndup);
    210   1.9  christos 	}
    211   1.9  christos 	sl_free(sl, 1);
    212   1.9  christos }
    213   1.9  christos 
    214   1.9  christos static StringList ***
    215   1.9  christos parseservices(const char *fname, StringList *sl)
    216   1.9  christos {
    217   1.9  christos 	size_t len, line, pindex;
    218   1.9  christos 	FILE *fp;
    219   1.9  christos 	StringList ***svc, *s;
    220   1.9  christos 	char *p, *ep;
    221   1.9  christos 
    222   1.9  christos 	if ((fp = fopen(fname, "r")) == NULL)
    223   1.9  christos 		err(1, "Cannot open `%s'", fname);
    224   1.9  christos 
    225   1.9  christos 	line = 0;
    226   1.9  christos 	svc = ecalloc(PMASK + 1, sizeof(StringList **));
    227   1.9  christos 
    228   1.3  christos 	/* XXX: change NULL to "\0\0#" when fparseln fixed */
    229   1.1     lukem 	for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
    230   1.3  christos 		char	*name, *port, *proto, *aliases, *cp, *alias;
    231   1.9  christos 		unsigned long pnum;
    232   1.1     lukem 
    233   1.1     lukem 		if (len == 0)
    234   1.1     lukem 			continue;
    235   1.9  christos 
    236   1.8  christos 		for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
    237   1.8  christos 			continue;
    238   1.9  christos 
    239   1.8  christos 		if (*cp == '\0' || *cp == '#')
    240   1.8  christos 			continue;
    241   1.5  christos 
    242   1.5  christos 		if ((name = getstring(fname, line, &cp, "name")) == NULL)
    243   1.3  christos 			continue;
    244   1.5  christos 
    245   1.5  christos 		if ((port = getstring(fname, line, &cp, "port")) == NULL)
    246   1.1     lukem 			continue;
    247   1.5  christos 
    248   1.9  christos 		if (cp) {
    249   1.9  christos 			for (aliases = cp; *cp && *cp != '#'; cp++)
    250   1.9  christos 				continue;
    251   1.9  christos 
    252   1.9  christos 			if (*cp)
    253   1.9  christos 				*cp = '\0';
    254   1.9  christos 		} else
    255   1.9  christos 			aliases = NULL;
    256   1.9  christos 
    257   1.1     lukem 		proto = strchr(port, '/');
    258   1.1     lukem 		if (proto == NULL || proto[1] == '\0') {
    259   1.3  christos 			warnx("%s, %zu: no protocol found", fname, line);
    260   1.1     lukem 			continue;
    261   1.1     lukem 		}
    262   1.1     lukem 		*proto++ = '\0';
    263   1.5  christos 
    264   1.9  christos 		errno = 0;
    265   1.9  christos 		pnum = strtoul(port, &ep, 0);
    266   1.9  christos 		if (*port == '\0' || *ep != '\0') {
    267   1.9  christos 			warnx("%s, %zu: invalid port `%s'", fname, line, port);
    268   1.9  christos 			continue;
    269   1.9  christos 		}
    270   1.9  christos 		if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
    271   1.9  christos 			warnx("%s, %zu: port too big `%s'", fname, line, port);
    272   1.3  christos 			continue;
    273   1.9  christos 		}
    274   1.3  christos 
    275   1.9  christos 		if (svc[pnum] == NULL)
    276   1.9  christos 			svc[pnum] = ecalloc(PROTOMAX, sizeof(StringList *));
    277   1.1     lukem 
    278   1.9  christos 		pindex = getprotoindex(sl, proto);
    279   1.9  christos 		if (svc[pnum][pindex] == NULL)
    280   1.9  christos 			s = svc[pnum][pindex] = sl_init();
    281   1.9  christos 		else
    282   1.9  christos 			s = svc[pnum][pindex];
    283   1.9  christos 
    284   1.3  christos 		/* build list of aliases */
    285   1.9  christos 		if (sl_find(s, name) == NULL)
    286   1.9  christos 			(void)sl_add(s, estrdup(name));
    287   1.1     lukem 
    288   1.9  christos 		if (aliases) {
    289   1.9  christos 			while ((alias = strsep(&aliases, " \t")) != NULL) {
    290   1.9  christos 				if (alias[0] == '\0')
    291   1.9  christos 					continue;
    292   1.9  christos 				if (sl_find(s, alias) == NULL)
    293   1.9  christos 					(void)sl_add(s, estrdup(alias));
    294   1.9  christos 			}
    295   1.1     lukem 		}
    296   1.1     lukem 	}
    297   1.9  christos 	(void)fclose(fp);
    298   1.9  christos 	return svc;
    299   1.1     lukem }
    300   1.1     lukem 
    301   1.1     lukem /*
    302   1.1     lukem  * cleanup(): Remove temporary files upon exit
    303   1.1     lukem  */
    304   1.3  christos static void
    305   1.3  christos cleanup(void)
    306   1.1     lukem {
    307   1.3  christos 	if (tname[0])
    308   1.3  christos 		(void)unlink(tname);
    309   1.1     lukem }
    310   1.1     lukem 
    311   1.5  christos static char *
    312   1.5  christos getstring(const char *fname, size_t line, char **cp, const char *tag)
    313   1.5  christos {
    314   1.5  christos 	char *str;
    315   1.5  christos 
    316   1.5  christos 	while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
    317   1.5  christos 		continue;
    318   1.5  christos 
    319   1.5  christos 	if (str == NULL)
    320   1.5  christos 		warnx("%s, %zu: no %s found", fname, line, tag);
    321   1.5  christos 
    322   1.5  christos 	return str;
    323   1.5  christos }
    324   1.5  christos 
    325   1.5  christos static void
    326   1.5  christos killproto(DBT *key)
    327   1.5  christos {
    328   1.5  christos 	char *p, *d = key->data;
    329   1.5  christos 
    330   1.5  christos 	if ((p = strchr(d, '/')) == NULL)
    331   1.5  christos 		abort();
    332   1.5  christos 	*p++ = '\0';
    333   1.5  christos 	key->size = p - d;
    334   1.5  christos }
    335   1.5  christos 
    336   1.3  christos static void
    337   1.9  christos store(DB *db, DBT *key, DBT *data, int warndup)
    338   1.4  christos {
    339   1.9  christos #ifdef DEBUG
    340   1.9  christos 	int k = key->size - 1;
    341   1.9  christos 	int d = data->size - 1;
    342   1.9  christos 	(void)printf("store [%*.*s] [%*.*s]\n",
    343   1.9  christos 		k, k, (char *)key->data + 1,
    344   1.9  christos 		d, d, (char *)data->data + 1);
    345   1.9  christos #endif
    346   1.4  christos 	switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
    347   1.4  christos 	case 0:
    348   1.4  christos 		break;
    349   1.4  christos 	case 1:
    350   1.4  christos 		if (warndup)
    351   1.9  christos 			warnx("duplicate service `%s'",
    352   1.9  christos 			    &((char *)key->data)[1]);
    353   1.4  christos 		break;
    354   1.4  christos 	case -1:
    355   1.4  christos 		err(1, "put");
    356   1.4  christos 		break;
    357   1.4  christos 	default:
    358   1.4  christos 		abort();
    359   1.4  christos 		break;
    360   1.4  christos 	}
    361   1.4  christos }
    362   1.4  christos 
    363   1.9  christos static size_t
    364   1.9  christos getprotoindex(StringList *sl, const char *str)
    365   1.9  christos {
    366   1.9  christos 	size_t i;
    367   1.9  christos 
    368   1.9  christos 	for (i= 0; i < sl->sl_cur; i++)
    369   1.9  christos 		if (strcmp(sl->sl_str[i], str) == 0)
    370   1.9  christos 			return i;
    371   1.9  christos 
    372   1.9  christos 	if (i == PROTOMAX)
    373   1.9  christos 		errx(1, "Ran out of protocols adding `%s';"
    374   1.9  christos 		    " recompile with larger PROTOMAX", str);
    375   1.9  christos 	(void)sl_add(sl, estrdup(str));
    376   1.9  christos 	return i;
    377   1.9  christos }
    378   1.9  christos 
    379   1.9  christos static const char *
    380   1.9  christos getprotostr(StringList *sl, size_t i)
    381   1.9  christos {
    382   1.9  christos 	assert(i < sl->sl_cur);
    383   1.9  christos 	return sl->sl_str[i];
    384   1.9  christos }
    385   1.9  christos 
    386   1.9  christos static const char *
    387   1.9  christos mkaliases(StringList *sl, char *buf, size_t len)
    388   1.9  christos {
    389   1.9  christos 	size_t nc, i, pos;
    390   1.9  christos 
    391   1.9  christos 	for (i = 1, pos = 0; i < sl->sl_cur; i++) {
    392   1.9  christos 		nc = strlcpy(buf + pos, sl->sl_str[i], len);
    393   1.9  christos 		if (nc >= len)
    394   1.9  christos 			goto out;
    395   1.9  christos 		pos += nc;
    396   1.9  christos 		len -= nc;
    397   1.9  christos 		nc = strlcpy(buf + pos, " ", len);
    398   1.9  christos 		if (nc >= len)
    399   1.9  christos 			goto out;
    400   1.9  christos 		pos += nc;
    401   1.9  christos 		len -= nc;
    402   1.9  christos 	}
    403   1.9  christos 	return buf;
    404   1.9  christos out:
    405   1.9  christos 	warn("aliases for `%s' truncated", sl->sl_str[0]);
    406   1.9  christos 	return buf;
    407   1.9  christos }
    408   1.9  christos 
    409   1.4  christos static void
    410   1.3  christos usage(void)
    411   1.1     lukem {
    412  1.10  christos 	(void)fprintf(stderr, "Usage:\t%s [-q] [-o <db>] [<servicefile>]\n"
    413  1.10  christos 	    "\t%s -u [<servicefile>]\n", getprogname(), getprogname());
    414   1.1     lukem 	exit(1);
    415   1.1     lukem }
    416