Home | History | Annotate | Line # | Download | only in services_mkdb
services_mkdb.c revision 1.16
      1  1.16     joerg /*	$NetBSD: services_mkdb.c,v 1.16 2010/05/05 13:19:51 joerg 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  *
     19   1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     lukem  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     lukem  */
     31   1.1     lukem 
     32   1.1     lukem #include <sys/cdefs.h>
     33   1.1     lukem #ifndef lint
     34  1.16     joerg __RCSID("$NetBSD: services_mkdb.c,v 1.16 2010/05/05 13:19:51 joerg Exp $");
     35   1.1     lukem #endif /* not lint */
     36   1.1     lukem 
     37   1.1     lukem #include <sys/param.h>
     38   1.1     lukem 
     39   1.9  christos #include <assert.h>
     40   1.1     lukem #include <err.h>
     41   1.1     lukem #include <fcntl.h>
     42   1.1     lukem #include <netdb.h>
     43   1.1     lukem #include <stdio.h>
     44   1.1     lukem #include <stdlib.h>
     45   1.1     lukem #include <string.h>
     46   1.1     lukem #include <unistd.h>
     47   1.1     lukem #include <util.h>
     48   1.3  christos #include <ctype.h>
     49   1.9  christos #include <errno.h>
     50   1.3  christos #include <stringlist.h>
     51   1.1     lukem 
     52  1.15     joerg #include "extern.h"
     53  1.15     joerg 
     54   1.3  christos static char tname[MAXPATHLEN];
     55   1.3  christos 
     56   1.9  christos #define	PMASK		0xffff
     57   1.9  christos #define PROTOMAX	5
     58   1.9  christos 
     59   1.9  christos static StringList ***parseservices(const char *, StringList *);
     60   1.3  christos static void	cleanup(void);
     61   1.5  christos static char    *getstring(const char *, size_t, char **, const char *);
     62   1.9  christos static size_t	getprotoindex(StringList *, const char *);
     63   1.9  christos static const char *getprotostr(StringList *, size_t);
     64  1.12     perry static void	usage(void) __dead;
     65   1.1     lukem 
     66   1.1     lukem int
     67   1.3  christos main(int argc, char *argv[])
     68   1.1     lukem {
     69   1.3  christos 	int	 ch;
     70   1.3  christos 	const char *fname = _PATH_SERVICES;
     71  1.15     joerg 	const char *dbname = NULL;
     72  1.15     joerg 	int	 use_db = 0;
     73  1.16     joerg 	int	 warndup = 0;
     74  1.10  christos 	int	 unique = 0;
     75  1.11  christos 	int	 otherflag = 0;
     76   1.9  christos 	size_t	 cnt = 0;
     77   1.9  christos 	StringList *sl, ***svc;
     78   1.9  christos 	size_t port, proto;
     79  1.15     joerg 	void (*addfn)(StringList *, size_t, const char *, size_t *, int);
     80  1.15     joerg 	int (*closefn)(void);
     81   1.3  christos 
     82   1.3  christos 	setprogname(argv[0]);
     83   1.1     lukem 
     84  1.15     joerg 	while ((ch = getopt(argc, argv, "qo:uV:")) != -1)
     85   1.1     lukem 		switch (ch) {
     86   1.6  christos 		case 'q':
     87  1.11  christos 			otherflag = 1;
     88   1.6  christos 			warndup = 0;
     89   1.6  christos 			break;
     90   1.1     lukem 		case 'o':
     91  1.11  christos 			otherflag = 1;
     92   1.1     lukem 			dbname = optarg;
     93   1.1     lukem 			break;
     94  1.10  christos 		case 'u':
     95  1.10  christos 			unique++;
     96  1.10  christos 			break;
     97  1.16     joerg 		case 'v':
     98  1.16     joerg 			otherflag = 1;
     99  1.16     joerg 			warndup = 1;
    100  1.16     joerg 			break;
    101  1.15     joerg 		case 'V':
    102  1.15     joerg 			if (strcmp(optarg, "db") == 0)
    103  1.15     joerg 				use_db = 1;
    104  1.15     joerg 			else if (strcmp(optarg, "cdb") == 0)
    105  1.15     joerg 				use_db = 0;
    106  1.15     joerg 			else
    107  1.15     joerg 				usage();
    108  1.15     joerg 			break;
    109   1.1     lukem 		default:
    110   1.1     lukem 			usage();
    111   1.1     lukem 		}
    112   1.1     lukem 
    113   1.1     lukem 	argc -= optind;
    114   1.1     lukem 	argv += optind;
    115   1.1     lukem 
    116  1.11  christos 	if (argc > 1 || (unique && otherflag))
    117   1.3  christos 		usage();
    118   1.1     lukem 	if (argc == 1)
    119   1.1     lukem 		fname = argv[0];
    120   1.3  christos 
    121  1.10  christos 	if (unique)
    122  1.10  christos 		uniq(fname);
    123  1.10  christos 
    124  1.15     joerg 	if (dbname == NULL)
    125  1.15     joerg 		dbname = use_db ? _PATH_SERVICES_DB : _PATH_SERVICES_CDB;
    126  1.15     joerg 
    127   1.9  christos 	svc = parseservices(fname, sl = sl_init());
    128   1.3  christos 
    129   1.1     lukem 	if (atexit(cleanup))
    130   1.1     lukem 		err(1, "Cannot install exit handler");
    131   1.3  christos 
    132   1.3  christos 	(void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
    133   1.1     lukem 
    134  1.15     joerg 	if (use_db) {
    135  1.15     joerg 		if (db_open(tname))
    136  1.15     joerg 			err(1, "Error opening temporary database `%s'", tname);
    137  1.15     joerg 		addfn = db_add;
    138  1.15     joerg 		closefn = db_close;
    139  1.15     joerg 	} else {
    140  1.15     joerg 		if (cdb_open(tname))
    141  1.15     joerg 			err(1, "Error opening temporary database `%s'", tname);
    142  1.15     joerg 		addfn = cdb_add;
    143  1.15     joerg 		closefn = cdb_close;
    144  1.15     joerg 	}
    145   1.9  christos 
    146   1.9  christos 	for (port = 0; port < PMASK + 1; port++) {
    147   1.9  christos 		if (svc[port] == NULL)
    148   1.9  christos 			continue;
    149   1.9  christos 
    150   1.9  christos 		for (proto = 0; proto < PROTOMAX; proto++) {
    151   1.9  christos 			StringList *s;
    152   1.9  christos 			if ((s = svc[port][proto]) == NULL)
    153   1.9  christos 				continue;
    154  1.15     joerg 			(addfn)(s, port, getprotostr(sl, proto), &cnt, warndup);
    155   1.9  christos 		}
    156   1.9  christos 
    157   1.9  christos 		free(svc[port]);
    158   1.9  christos 	}
    159   1.9  christos 
    160   1.9  christos 	free(svc);
    161   1.9  christos 	sl_free(sl, 1);
    162   1.9  christos 
    163  1.15     joerg 	if ((closefn)())
    164  1.15     joerg 		err(1, "Error writing temporary database `%s'", tname);
    165   1.9  christos 
    166   1.9  christos 	if (rename(tname, dbname) == -1)
    167   1.9  christos 		err(1, "Cannot rename `%s' to `%s'", tname, dbname);
    168   1.9  christos 
    169   1.9  christos 	return 0;
    170   1.9  christos }
    171   1.9  christos 
    172   1.9  christos static StringList ***
    173   1.9  christos parseservices(const char *fname, StringList *sl)
    174   1.9  christos {
    175   1.9  christos 	size_t len, line, pindex;
    176   1.9  christos 	FILE *fp;
    177   1.9  christos 	StringList ***svc, *s;
    178   1.9  christos 	char *p, *ep;
    179   1.9  christos 
    180   1.9  christos 	if ((fp = fopen(fname, "r")) == NULL)
    181   1.9  christos 		err(1, "Cannot open `%s'", fname);
    182   1.9  christos 
    183   1.9  christos 	line = 0;
    184   1.9  christos 	svc = ecalloc(PMASK + 1, sizeof(StringList **));
    185   1.9  christos 
    186   1.3  christos 	/* XXX: change NULL to "\0\0#" when fparseln fixed */
    187   1.1     lukem 	for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
    188   1.3  christos 		char	*name, *port, *proto, *aliases, *cp, *alias;
    189   1.9  christos 		unsigned long pnum;
    190   1.1     lukem 
    191   1.1     lukem 		if (len == 0)
    192   1.1     lukem 			continue;
    193   1.9  christos 
    194   1.8  christos 		for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
    195   1.8  christos 			continue;
    196   1.9  christos 
    197   1.8  christos 		if (*cp == '\0' || *cp == '#')
    198   1.8  christos 			continue;
    199   1.5  christos 
    200   1.5  christos 		if ((name = getstring(fname, line, &cp, "name")) == NULL)
    201   1.3  christos 			continue;
    202   1.5  christos 
    203   1.5  christos 		if ((port = getstring(fname, line, &cp, "port")) == NULL)
    204   1.1     lukem 			continue;
    205   1.5  christos 
    206   1.9  christos 		if (cp) {
    207   1.9  christos 			for (aliases = cp; *cp && *cp != '#'; cp++)
    208   1.9  christos 				continue;
    209   1.9  christos 
    210   1.9  christos 			if (*cp)
    211   1.9  christos 				*cp = '\0';
    212   1.9  christos 		} else
    213   1.9  christos 			aliases = NULL;
    214   1.9  christos 
    215   1.1     lukem 		proto = strchr(port, '/');
    216   1.1     lukem 		if (proto == NULL || proto[1] == '\0') {
    217   1.3  christos 			warnx("%s, %zu: no protocol found", fname, line);
    218   1.1     lukem 			continue;
    219   1.1     lukem 		}
    220   1.1     lukem 		*proto++ = '\0';
    221   1.5  christos 
    222   1.9  christos 		errno = 0;
    223   1.9  christos 		pnum = strtoul(port, &ep, 0);
    224   1.9  christos 		if (*port == '\0' || *ep != '\0') {
    225   1.9  christos 			warnx("%s, %zu: invalid port `%s'", fname, line, port);
    226   1.9  christos 			continue;
    227   1.9  christos 		}
    228   1.9  christos 		if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
    229   1.9  christos 			warnx("%s, %zu: port too big `%s'", fname, line, port);
    230   1.3  christos 			continue;
    231   1.9  christos 		}
    232   1.3  christos 
    233   1.9  christos 		if (svc[pnum] == NULL)
    234   1.9  christos 			svc[pnum] = ecalloc(PROTOMAX, sizeof(StringList *));
    235   1.1     lukem 
    236   1.9  christos 		pindex = getprotoindex(sl, proto);
    237   1.9  christos 		if (svc[pnum][pindex] == NULL)
    238   1.9  christos 			s = svc[pnum][pindex] = sl_init();
    239   1.9  christos 		else
    240   1.9  christos 			s = svc[pnum][pindex];
    241  1.15     joerg 
    242  1.15     joerg 		if (strlen(name) > 255) {
    243  1.15     joerg 			warnx("%s, %zu: invalid name too long `%s'", fname,
    244  1.15     joerg 			    line, name);
    245  1.15     joerg 			continue;
    246  1.15     joerg 		}
    247  1.15     joerg 
    248   1.3  christos 		/* build list of aliases */
    249   1.9  christos 		if (sl_find(s, name) == NULL)
    250   1.9  christos 			(void)sl_add(s, estrdup(name));
    251   1.1     lukem 
    252   1.9  christos 		if (aliases) {
    253   1.9  christos 			while ((alias = strsep(&aliases, " \t")) != NULL) {
    254   1.9  christos 				if (alias[0] == '\0')
    255   1.9  christos 					continue;
    256  1.15     joerg 				if (strlen(alias) > 255) {
    257  1.15     joerg 					warnx("%s, %zu: alias name too long `%s'",
    258  1.15     joerg 					    fname, line, alias);
    259  1.15     joerg 		    			continue;
    260  1.15     joerg 				}
    261   1.9  christos 				if (sl_find(s, alias) == NULL)
    262   1.9  christos 					(void)sl_add(s, estrdup(alias));
    263   1.9  christos 			}
    264   1.1     lukem 		}
    265   1.1     lukem 	}
    266   1.9  christos 	(void)fclose(fp);
    267   1.9  christos 	return svc;
    268   1.1     lukem }
    269   1.1     lukem 
    270   1.1     lukem /*
    271   1.1     lukem  * cleanup(): Remove temporary files upon exit
    272   1.1     lukem  */
    273   1.3  christos static void
    274   1.3  christos cleanup(void)
    275   1.1     lukem {
    276   1.3  christos 	if (tname[0])
    277   1.3  christos 		(void)unlink(tname);
    278   1.1     lukem }
    279   1.1     lukem 
    280   1.5  christos static char *
    281   1.5  christos getstring(const char *fname, size_t line, char **cp, const char *tag)
    282   1.5  christos {
    283   1.5  christos 	char *str;
    284   1.5  christos 
    285   1.5  christos 	while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
    286   1.5  christos 		continue;
    287   1.5  christos 
    288   1.5  christos 	if (str == NULL)
    289   1.5  christos 		warnx("%s, %zu: no %s found", fname, line, tag);
    290   1.5  christos 
    291   1.5  christos 	return str;
    292   1.5  christos }
    293   1.5  christos 
    294   1.9  christos static size_t
    295   1.9  christos getprotoindex(StringList *sl, const char *str)
    296   1.9  christos {
    297   1.9  christos 	size_t i;
    298   1.9  christos 
    299   1.9  christos 	for (i= 0; i < sl->sl_cur; i++)
    300   1.9  christos 		if (strcmp(sl->sl_str[i], str) == 0)
    301   1.9  christos 			return i;
    302   1.9  christos 
    303   1.9  christos 	if (i == PROTOMAX)
    304   1.9  christos 		errx(1, "Ran out of protocols adding `%s';"
    305   1.9  christos 		    " recompile with larger PROTOMAX", str);
    306   1.9  christos 	(void)sl_add(sl, estrdup(str));
    307   1.9  christos 	return i;
    308   1.9  christos }
    309   1.9  christos 
    310   1.9  christos static const char *
    311   1.9  christos getprotostr(StringList *sl, size_t i)
    312   1.9  christos {
    313   1.9  christos 	assert(i < sl->sl_cur);
    314   1.9  christos 	return sl->sl_str[i];
    315   1.9  christos }
    316   1.9  christos 
    317   1.4  christos static void
    318   1.3  christos usage(void)
    319   1.1     lukem {
    320  1.15     joerg 	(void)fprintf(stderr, "Usage:\t%s [-q] [-o <db>] [-V cdb|db] [<servicefile>]\n"
    321  1.10  christos 	    "\t%s -u [<servicefile>]\n", getprogname(), getprogname());
    322   1.1     lukem 	exit(1);
    323   1.1     lukem }
    324