Home | History | Annotate | Line # | Download | only in services_mkdb
services_mkdb.c revision 1.11
      1  1.11  christos /*	$NetBSD: services_mkdb.c,v 1.11 2007/06/23 17:02:03 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.11  christos __RCSID("$NetBSD: services_mkdb.c,v 1.11 2007/06/23 17:02:03 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.11  christos 	int	 otherflag = 0;
     98   1.9  christos 	size_t	 cnt = 0;
     99   1.9  christos 	StringList *sl, ***svc;
    100   1.9  christos 	size_t port, proto;
    101   1.3  christos 
    102   1.3  christos 	setprogname(argv[0]);
    103   1.1     lukem 
    104  1.10  christos 	while ((ch = getopt(argc, argv, "qo:u")) != -1)
    105   1.1     lukem 		switch (ch) {
    106   1.6  christos 		case 'q':
    107  1.11  christos 			otherflag = 1;
    108   1.6  christos 			warndup = 0;
    109   1.6  christos 			break;
    110   1.1     lukem 		case 'o':
    111  1.11  christos 			otherflag = 1;
    112   1.1     lukem 			dbname = optarg;
    113   1.1     lukem 			break;
    114  1.10  christos 		case 'u':
    115  1.10  christos 			unique++;
    116  1.10  christos 			break;
    117   1.1     lukem 		case '?':
    118   1.1     lukem 		default:
    119   1.1     lukem 			usage();
    120   1.1     lukem 		}
    121   1.1     lukem 
    122   1.1     lukem 	argc -= optind;
    123   1.1     lukem 	argv += optind;
    124   1.1     lukem 
    125  1.11  christos 	if (argc > 1 || (unique && otherflag))
    126   1.3  christos 		usage();
    127   1.1     lukem 	if (argc == 1)
    128   1.1     lukem 		fname = argv[0];
    129   1.3  christos 
    130  1.10  christos 	if (unique)
    131  1.10  christos 		uniq(fname);
    132  1.10  christos 
    133   1.9  christos 	svc = parseservices(fname, sl = sl_init());
    134   1.3  christos 
    135   1.1     lukem 	if (atexit(cleanup))
    136   1.1     lukem 		err(1, "Cannot install exit handler");
    137   1.3  christos 
    138   1.3  christos 	(void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
    139   1.1     lukem 	db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
    140   1.8  christos 	    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
    141   1.1     lukem 	if (!db)
    142   1.1     lukem 		err(1, "Error opening temporary database `%s'", tname);
    143   1.1     lukem 
    144   1.9  christos 
    145   1.9  christos 	for (port = 0; port < PMASK + 1; port++) {
    146   1.9  christos 		if (svc[port] == NULL)
    147   1.9  christos 			continue;
    148   1.9  christos 
    149   1.9  christos 		for (proto = 0; proto < PROTOMAX; proto++) {
    150   1.9  christos 			StringList *s;
    151   1.9  christos 			if ((s = svc[port][proto]) == NULL)
    152   1.9  christos 				continue;
    153   1.9  christos 			add(db, s, port, getprotostr(sl, proto), &cnt, warndup);
    154   1.9  christos 		}
    155   1.9  christos 
    156   1.9  christos 		free(svc[port]);
    157   1.9  christos 	}
    158   1.9  christos 
    159   1.9  christos 	free(svc);
    160   1.9  christos 	sl_free(sl, 1);
    161   1.9  christos 
    162   1.9  christos 	if ((db->close)(db))
    163   1.9  christos 		err(1, "Error closing temporary database `%s'", tname);
    164   1.9  christos 
    165   1.9  christos 	if (rename(tname, dbname) == -1)
    166   1.9  christos 		err(1, "Cannot rename `%s' to `%s'", tname, dbname);
    167   1.9  christos 
    168   1.9  christos 	return 0;
    169   1.9  christos }
    170   1.9  christos 
    171   1.9  christos static void
    172   1.9  christos add(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt,
    173   1.9  christos     int warndup)
    174   1.9  christos {
    175   1.9  christos 	size_t i;
    176   1.9  christos 	char	 keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
    177   1.9  christos 	DBT	 data, key;
    178   1.4  christos 	key.data = keyb;
    179   1.4  christos 	data.data = datab;
    180   1.1     lukem 
    181   1.9  christos #ifdef DEBUG
    182   1.9  christos 	(void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto);
    183   1.9  christos 	for (i = 1; i < sl->sl_cur; i++)
    184   1.9  christos 	    (void)printf("%s ", sl->sl_str[i]);
    185   1.9  christos 	(void)printf("]\n");
    186   1.9  christos #endif
    187   1.9  christos 
    188   1.9  christos 	/* key `indirect key', data `full line' */
    189   1.9  christos 	data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
    190   1.9  christos 	key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
    191   1.9  christos 	    sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
    192   1.9  christos 	store(db, &data, &key, warndup);
    193   1.9  christos 
    194   1.9  christos 	/* key `\377port/proto', data = `indirect key' */
    195   1.9  christos 	key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
    196   1.9  christos 	    port, proto) + 1;
    197   1.9  christos 	store(db, &key, &data, warndup);
    198   1.9  christos 
    199   1.9  christos 	/* key `\377port', data = `indirect key' */
    200   1.9  christos 	killproto(&key);
    201   1.9  christos 	store(db, &key, &data, warndup);
    202   1.9  christos 
    203   1.9  christos 	/* add references for service and all aliases */
    204   1.9  christos 	for (i = 0; i < sl->sl_cur; i++) {
    205   1.9  christos 		/* key `\376service/proto', data = `indirect key' */
    206   1.9  christos 		key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
    207   1.9  christos 		    sl->sl_str[i], proto) + 1;
    208   1.9  christos 		store(db, &key, &data, warndup);
    209   1.9  christos 
    210   1.9  christos 		/* key `\376service', data = `indirect key' */
    211   1.9  christos 		killproto(&key);
    212   1.9  christos 		store(db, &key, &data, warndup);
    213   1.9  christos 	}
    214   1.9  christos 	sl_free(sl, 1);
    215   1.9  christos }
    216   1.9  christos 
    217   1.9  christos static StringList ***
    218   1.9  christos parseservices(const char *fname, StringList *sl)
    219   1.9  christos {
    220   1.9  christos 	size_t len, line, pindex;
    221   1.9  christos 	FILE *fp;
    222   1.9  christos 	StringList ***svc, *s;
    223   1.9  christos 	char *p, *ep;
    224   1.9  christos 
    225   1.9  christos 	if ((fp = fopen(fname, "r")) == NULL)
    226   1.9  christos 		err(1, "Cannot open `%s'", fname);
    227   1.9  christos 
    228   1.9  christos 	line = 0;
    229   1.9  christos 	svc = ecalloc(PMASK + 1, sizeof(StringList **));
    230   1.9  christos 
    231   1.3  christos 	/* XXX: change NULL to "\0\0#" when fparseln fixed */
    232   1.1     lukem 	for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
    233   1.3  christos 		char	*name, *port, *proto, *aliases, *cp, *alias;
    234   1.9  christos 		unsigned long pnum;
    235   1.1     lukem 
    236   1.1     lukem 		if (len == 0)
    237   1.1     lukem 			continue;
    238   1.9  christos 
    239   1.8  christos 		for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
    240   1.8  christos 			continue;
    241   1.9  christos 
    242   1.8  christos 		if (*cp == '\0' || *cp == '#')
    243   1.8  christos 			continue;
    244   1.5  christos 
    245   1.5  christos 		if ((name = getstring(fname, line, &cp, "name")) == NULL)
    246   1.3  christos 			continue;
    247   1.5  christos 
    248   1.5  christos 		if ((port = getstring(fname, line, &cp, "port")) == NULL)
    249   1.1     lukem 			continue;
    250   1.5  christos 
    251   1.9  christos 		if (cp) {
    252   1.9  christos 			for (aliases = cp; *cp && *cp != '#'; cp++)
    253   1.9  christos 				continue;
    254   1.9  christos 
    255   1.9  christos 			if (*cp)
    256   1.9  christos 				*cp = '\0';
    257   1.9  christos 		} else
    258   1.9  christos 			aliases = NULL;
    259   1.9  christos 
    260   1.1     lukem 		proto = strchr(port, '/');
    261   1.1     lukem 		if (proto == NULL || proto[1] == '\0') {
    262   1.3  christos 			warnx("%s, %zu: no protocol found", fname, line);
    263   1.1     lukem 			continue;
    264   1.1     lukem 		}
    265   1.1     lukem 		*proto++ = '\0';
    266   1.5  christos 
    267   1.9  christos 		errno = 0;
    268   1.9  christos 		pnum = strtoul(port, &ep, 0);
    269   1.9  christos 		if (*port == '\0' || *ep != '\0') {
    270   1.9  christos 			warnx("%s, %zu: invalid port `%s'", fname, line, port);
    271   1.9  christos 			continue;
    272   1.9  christos 		}
    273   1.9  christos 		if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
    274   1.9  christos 			warnx("%s, %zu: port too big `%s'", fname, line, port);
    275   1.3  christos 			continue;
    276   1.9  christos 		}
    277   1.3  christos 
    278   1.9  christos 		if (svc[pnum] == NULL)
    279   1.9  christos 			svc[pnum] = ecalloc(PROTOMAX, sizeof(StringList *));
    280   1.1     lukem 
    281   1.9  christos 		pindex = getprotoindex(sl, proto);
    282   1.9  christos 		if (svc[pnum][pindex] == NULL)
    283   1.9  christos 			s = svc[pnum][pindex] = sl_init();
    284   1.9  christos 		else
    285   1.9  christos 			s = svc[pnum][pindex];
    286   1.9  christos 
    287   1.3  christos 		/* build list of aliases */
    288   1.9  christos 		if (sl_find(s, name) == NULL)
    289   1.9  christos 			(void)sl_add(s, estrdup(name));
    290   1.1     lukem 
    291   1.9  christos 		if (aliases) {
    292   1.9  christos 			while ((alias = strsep(&aliases, " \t")) != NULL) {
    293   1.9  christos 				if (alias[0] == '\0')
    294   1.9  christos 					continue;
    295   1.9  christos 				if (sl_find(s, alias) == NULL)
    296   1.9  christos 					(void)sl_add(s, estrdup(alias));
    297   1.9  christos 			}
    298   1.1     lukem 		}
    299   1.1     lukem 	}
    300   1.9  christos 	(void)fclose(fp);
    301   1.9  christos 	return svc;
    302   1.1     lukem }
    303   1.1     lukem 
    304   1.1     lukem /*
    305   1.1     lukem  * cleanup(): Remove temporary files upon exit
    306   1.1     lukem  */
    307   1.3  christos static void
    308   1.3  christos cleanup(void)
    309   1.1     lukem {
    310   1.3  christos 	if (tname[0])
    311   1.3  christos 		(void)unlink(tname);
    312   1.1     lukem }
    313   1.1     lukem 
    314   1.5  christos static char *
    315   1.5  christos getstring(const char *fname, size_t line, char **cp, const char *tag)
    316   1.5  christos {
    317   1.5  christos 	char *str;
    318   1.5  christos 
    319   1.5  christos 	while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
    320   1.5  christos 		continue;
    321   1.5  christos 
    322   1.5  christos 	if (str == NULL)
    323   1.5  christos 		warnx("%s, %zu: no %s found", fname, line, tag);
    324   1.5  christos 
    325   1.5  christos 	return str;
    326   1.5  christos }
    327   1.5  christos 
    328   1.5  christos static void
    329   1.5  christos killproto(DBT *key)
    330   1.5  christos {
    331   1.5  christos 	char *p, *d = key->data;
    332   1.5  christos 
    333   1.5  christos 	if ((p = strchr(d, '/')) == NULL)
    334   1.5  christos 		abort();
    335   1.5  christos 	*p++ = '\0';
    336   1.5  christos 	key->size = p - d;
    337   1.5  christos }
    338   1.5  christos 
    339   1.3  christos static void
    340   1.9  christos store(DB *db, DBT *key, DBT *data, int warndup)
    341   1.4  christos {
    342   1.9  christos #ifdef DEBUG
    343   1.9  christos 	int k = key->size - 1;
    344   1.9  christos 	int d = data->size - 1;
    345   1.9  christos 	(void)printf("store [%*.*s] [%*.*s]\n",
    346   1.9  christos 		k, k, (char *)key->data + 1,
    347   1.9  christos 		d, d, (char *)data->data + 1);
    348   1.9  christos #endif
    349   1.4  christos 	switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
    350   1.4  christos 	case 0:
    351   1.4  christos 		break;
    352   1.4  christos 	case 1:
    353   1.4  christos 		if (warndup)
    354   1.9  christos 			warnx("duplicate service `%s'",
    355   1.9  christos 			    &((char *)key->data)[1]);
    356   1.4  christos 		break;
    357   1.4  christos 	case -1:
    358   1.4  christos 		err(1, "put");
    359   1.4  christos 		break;
    360   1.4  christos 	default:
    361   1.4  christos 		abort();
    362   1.4  christos 		break;
    363   1.4  christos 	}
    364   1.4  christos }
    365   1.4  christos 
    366   1.9  christos static size_t
    367   1.9  christos getprotoindex(StringList *sl, const char *str)
    368   1.9  christos {
    369   1.9  christos 	size_t i;
    370   1.9  christos 
    371   1.9  christos 	for (i= 0; i < sl->sl_cur; i++)
    372   1.9  christos 		if (strcmp(sl->sl_str[i], str) == 0)
    373   1.9  christos 			return i;
    374   1.9  christos 
    375   1.9  christos 	if (i == PROTOMAX)
    376   1.9  christos 		errx(1, "Ran out of protocols adding `%s';"
    377   1.9  christos 		    " recompile with larger PROTOMAX", str);
    378   1.9  christos 	(void)sl_add(sl, estrdup(str));
    379   1.9  christos 	return i;
    380   1.9  christos }
    381   1.9  christos 
    382   1.9  christos static const char *
    383   1.9  christos getprotostr(StringList *sl, size_t i)
    384   1.9  christos {
    385   1.9  christos 	assert(i < sl->sl_cur);
    386   1.9  christos 	return sl->sl_str[i];
    387   1.9  christos }
    388   1.9  christos 
    389   1.9  christos static const char *
    390   1.9  christos mkaliases(StringList *sl, char *buf, size_t len)
    391   1.9  christos {
    392   1.9  christos 	size_t nc, i, pos;
    393   1.9  christos 
    394   1.9  christos 	for (i = 1, pos = 0; i < sl->sl_cur; i++) {
    395   1.9  christos 		nc = strlcpy(buf + pos, sl->sl_str[i], len);
    396   1.9  christos 		if (nc >= len)
    397   1.9  christos 			goto out;
    398   1.9  christos 		pos += nc;
    399   1.9  christos 		len -= nc;
    400   1.9  christos 		nc = strlcpy(buf + pos, " ", len);
    401   1.9  christos 		if (nc >= len)
    402   1.9  christos 			goto out;
    403   1.9  christos 		pos += nc;
    404   1.9  christos 		len -= nc;
    405   1.9  christos 	}
    406   1.9  christos 	return buf;
    407   1.9  christos out:
    408   1.9  christos 	warn("aliases for `%s' truncated", sl->sl_str[0]);
    409   1.9  christos 	return buf;
    410   1.9  christos }
    411   1.9  christos 
    412   1.4  christos static void
    413   1.3  christos usage(void)
    414   1.1     lukem {
    415  1.10  christos 	(void)fprintf(stderr, "Usage:\t%s [-q] [-o <db>] [<servicefile>]\n"
    416  1.10  christos 	    "\t%s -u [<servicefile>]\n", getprogname(), getprogname());
    417   1.1     lukem 	exit(1);
    418   1.1     lukem }
    419