services_mkdb.c revision 263028
1272343Sngie/*	$NetBSD: services_mkdb.c,v 1.14 2008/04/28 20:24:17 martin Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 1999 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Luke Mewburn and Christos Zoulas.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie#include <sys/cdefs.h>
33272343Sngie__FBSDID("$FreeBSD: stable/10/usr.sbin/services_mkdb/services_mkdb.c 263028 2014-03-11 15:28:41Z jhb $");
34272343Sngie
35272343Sngie#include <sys/param.h>
36272343Sngie#include <sys/stat.h>
37272343Sngie
38272343Sngie#include <assert.h>
39272343Sngie#include <db.h>
40272343Sngie#include <err.h>
41272343Sngie#include <fcntl.h>
42272343Sngie#include <netdb.h>
43272343Sngie#include <stdio.h>
44272343Sngie#include <stdlib.h>
45272343Sngie#include <string.h>
46272343Sngie#include <unistd.h>
47272343Sngie#include <libutil.h>
48272343Sngie#include <ctype.h>
49272343Sngie#include <errno.h>
50272343Sngie#include <stringlist.h>
51272343Sngie
52272343Sngie#include "extern.h"
53272343Sngie
54272343Sngiestatic char tname[MAXPATHLEN];
55272343Sngie
56272343Sngie#define	PMASK		0xffff
57272343Sngie#define PROTOMAX	5
58272343Sngie
59272343Sngiestatic void	add(DB *, StringList *, size_t, const char *, size_t *, int);
60276478Sngiestatic StringList ***parseservices(const char *, StringList *);
61276478Sngiestatic void	cleanup(void);
62276478Sngiestatic void	store(DB *, DBT *, DBT *, int);
63272343Sngiestatic void	killproto(DBT *);
64272343Sngiestatic char    *getstring(const char *, size_t, char **, const char *);
65272343Sngiestatic size_t	getprotoindex(StringList *, const char *);
66272343Sngiestatic const char *getprotostr(StringList *, size_t);
67272343Sngiestatic const char *mkaliases(StringList *, char *, size_t);
68272343Sngiestatic void	usage(void);
69272343Sngie
70272343SngieHASHINFO hinfo = {
71272343Sngie	.bsize = 256,
72272343Sngie	.ffactor = 4,
73272343Sngie	.nelem = 32768,
74272343Sngie	.cachesize = 1024,
75272343Sngie	.hash = NULL,
76272343Sngie	.lorder = 0
77272343Sngie};
78272343Sngie
79272343Sngie
80272343Sngieint
81272343Sngiemain(int argc, char *argv[])
82272343Sngie{
83272343Sngie	DB	*db;
84272343Sngie	int	 ch;
85272343Sngie	const char *fname = _PATH_SERVICES;
86272343Sngie	const char *dbname = _PATH_SERVICES_DB;
87272343Sngie	int	 warndup = 1;
88272343Sngie	int	 unique = 0;
89272343Sngie	int	 otherflag = 0;
90272343Sngie	int	 byteorder = 0;
91272343Sngie	size_t	 cnt = 0;
92272343Sngie	StringList *sl, ***svc;
93272343Sngie	size_t port, proto;
94272343Sngie
95272343Sngie	setprogname(argv[0]);
96272343Sngie
97272343Sngie	while ((ch = getopt(argc, argv, "blo:qu")) != -1)
98272343Sngie		switch (ch) {
99272343Sngie		case 'b':
100272343Sngie		case 'l':
101272343Sngie			if (byteorder != 0)
102272343Sngie				usage();
103272343Sngie			byteorder = ch == 'b' ? 4321 : 1234;
104272343Sngie			break;
105272343Sngie		case 'q':
106272343Sngie			otherflag = 1;
107272343Sngie			warndup = 0;
108272343Sngie			break;
109272343Sngie		case 'o':
110272343Sngie			otherflag = 1;
111272343Sngie			dbname = optarg;
112272343Sngie			break;
113272343Sngie		case 'u':
114272343Sngie			unique++;
115272343Sngie			break;
116272343Sngie		case '?':
117272343Sngie		default:
118272343Sngie			usage();
119272343Sngie		}
120272343Sngie
121272343Sngie	argc -= optind;
122272343Sngie	argv += optind;
123272343Sngie
124272343Sngie	if (argc > 1 || (unique && otherflag))
125272343Sngie		usage();
126272343Sngie	if (argc == 1)
127272343Sngie		fname = argv[0];
128272343Sngie
129272343Sngie	/* Set byte order. */
130272343Sngie	hinfo.lorder = byteorder;
131272343Sngie
132272343Sngie	if (unique)
133272343Sngie		uniq(fname);
134272343Sngie
135272343Sngie	svc = parseservices(fname, sl = sl_init());
136272343Sngie
137272343Sngie	if (atexit(cleanup))
138272343Sngie		err(1, "Cannot install exit handler");
139272343Sngie
140272343Sngie	(void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
141272343Sngie	db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
142272343Sngie	    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
143272343Sngie	if (!db)
144272343Sngie		err(1, "Error opening temporary database `%s'", tname);
145272343Sngie
146272343Sngie
147272343Sngie	for (port = 0; port < PMASK + 1; port++) {
148272343Sngie		if (svc[port] == NULL)
149272343Sngie			continue;
150272343Sngie
151272343Sngie		for (proto = 0; proto < PROTOMAX; proto++) {
152272343Sngie			StringList *s;
153272343Sngie			if ((s = svc[port][proto]) == NULL)
154272343Sngie				continue;
155272343Sngie			add(db, s, port, getprotostr(sl, proto), &cnt, warndup);
156272343Sngie		}
157272343Sngie
158272343Sngie		free(svc[port]);
159272343Sngie	}
160272343Sngie
161272343Sngie	free(svc);
162272343Sngie	sl_free(sl, 1);
163272343Sngie
164272343Sngie	if ((db->close)(db))
165272343Sngie		err(1, "Error closing temporary database `%s'", tname);
166272343Sngie
167272343Sngie	if (rename(tname, dbname) == -1)
168272343Sngie		err(1, "Cannot rename `%s' to `%s'", tname, dbname);
169272343Sngie
170272343Sngie	return 0;
171272343Sngie}
172272343Sngie
173272343Sngiestatic void
174272343Sngieadd(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt,
175    int warndup)
176{
177	size_t i;
178	char	 keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
179	DBT	 data, key;
180	key.data = keyb;
181	data.data = datab;
182
183#ifdef DEBUG
184	(void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto);
185	for (i = 1; i < sl->sl_cur; i++)
186	    (void)printf("%s ", sl->sl_str[i]);
187	(void)printf("]\n");
188#endif
189
190	/* key `indirect key', data `full line' */
191	data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
192	key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
193	    sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
194	store(db, &data, &key, warndup);
195
196	/* key `\377port/proto', data = `indirect key' */
197	key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
198	    port, proto) + 1;
199	store(db, &key, &data, warndup);
200
201	/* key `\377port', data = `indirect key' */
202	killproto(&key);
203	store(db, &key, &data, warndup);
204
205	/* add references for service and all aliases */
206	for (i = 0; i < sl->sl_cur; i++) {
207		/* key `\376service/proto', data = `indirect key' */
208		key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
209		    sl->sl_str[i], proto) + 1;
210		store(db, &key, &data, warndup);
211
212		/* key `\376service', data = `indirect key' */
213		killproto(&key);
214		store(db, &key, &data, warndup);
215	}
216	sl_free(sl, 1);
217}
218
219static StringList ***
220parseservices(const char *fname, StringList *sl)
221{
222	size_t len, line, pindex;
223	FILE *fp;
224	StringList ***svc, *s;
225	char *p, *ep;
226
227	if ((fp = fopen(fname, "r")) == NULL)
228		err(1, "Cannot open `%s'", fname);
229
230	line = 0;
231	if ((svc = calloc(PMASK + 1, sizeof(StringList **))) == NULL)
232		err(1, "Cannot allocate %zu bytes", (size_t)(PMASK + 1));
233
234	/* XXX: change NULL to "\0\0#" when fparseln fixed */
235	for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
236		char	*name, *port, *proto, *aliases, *cp, *alias;
237		unsigned long pnum;
238
239		if (len == 0)
240			continue;
241
242		for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
243			continue;
244
245		if (*cp == '\0' || *cp == '#')
246			continue;
247
248		if ((name = getstring(fname, line, &cp, "name")) == NULL)
249			continue;
250
251		if ((port = getstring(fname, line, &cp, "port")) == NULL)
252			continue;
253
254		if (cp) {
255			for (aliases = cp; *cp && *cp != '#'; cp++)
256				continue;
257
258			if (*cp)
259				*cp = '\0';
260		} else
261			aliases = NULL;
262
263		proto = strchr(port, '/');
264		if (proto == NULL || proto[1] == '\0') {
265			warnx("%s, %zu: no protocol found", fname, line);
266			continue;
267		}
268		*proto++ = '\0';
269
270		errno = 0;
271		pnum = strtoul(port, &ep, 0);
272		if (*port == '\0' || *ep != '\0') {
273			warnx("%s, %zu: invalid port `%s'", fname, line, port);
274			continue;
275		}
276		if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
277			warnx("%s, %zu: port too big `%s'", fname, line, port);
278			continue;
279		}
280
281		if (svc[pnum] == NULL) {
282			svc[pnum] = calloc(PROTOMAX, sizeof(StringList *));
283			if (svc[pnum] == NULL)
284				err(1, "Cannot allocate %zu bytes",
285				    (size_t)PROTOMAX);
286		}
287
288		pindex = getprotoindex(sl, proto);
289		if (svc[pnum][pindex] == NULL)
290			s = svc[pnum][pindex] = sl_init();
291		else
292			s = svc[pnum][pindex];
293
294		/* build list of aliases */
295		if (sl_find(s, name) == NULL) {
296			char *p2;
297
298			if ((p2 = strdup(name)) == NULL)
299				err(1, "Cannot copy string");
300			(void)sl_add(s, p2);
301		}
302
303		if (aliases) {
304			while ((alias = strsep(&aliases, " \t")) != NULL) {
305				if (alias[0] == '\0')
306					continue;
307				if (sl_find(s, alias) == NULL) {
308					char *p2;
309
310					if ((p2 = strdup(alias)) == NULL)
311						err(1, "Cannot copy string");
312					(void)sl_add(s, p2);
313				}
314			}
315		}
316	}
317	(void)fclose(fp);
318	return svc;
319}
320
321/*
322 * cleanup(): Remove temporary files upon exit
323 */
324static void
325cleanup(void)
326{
327	if (tname[0])
328		(void)unlink(tname);
329}
330
331static char *
332getstring(const char *fname, size_t line, char **cp, const char *tag)
333{
334	char *str;
335
336	while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
337		continue;
338
339	if (str == NULL)
340		warnx("%s, %zu: no %s found", fname, line, tag);
341
342	return str;
343}
344
345static void
346killproto(DBT *key)
347{
348	char *p, *d = key->data;
349
350	if ((p = strchr(d, '/')) == NULL)
351		abort();
352	*p++ = '\0';
353	key->size = p - d;
354}
355
356static void
357store(DB *db, DBT *key, DBT *data, int warndup)
358{
359#ifdef DEBUG
360	int k = key->size - 1;
361	int d = data->size - 1;
362	(void)printf("store [%*.*s] [%*.*s]\n",
363		k, k, (char *)key->data + 1,
364		d, d, (char *)data->data + 1);
365#endif
366	switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
367	case 0:
368		break;
369	case 1:
370		if (warndup)
371			warnx("duplicate service `%s'",
372			    &((char *)key->data)[1]);
373		break;
374	case -1:
375		err(1, "put");
376		break;
377	default:
378		abort();
379		break;
380	}
381}
382
383static size_t
384getprotoindex(StringList *sl, const char *str)
385{
386	size_t i;
387	char *p;
388
389	for (i= 0; i < sl->sl_cur; i++)
390		if (strcmp(sl->sl_str[i], str) == 0)
391			return i;
392
393	if (i == PROTOMAX)
394		errx(1, "Ran out of protocols adding `%s';"
395		    " recompile with larger PROTOMAX", str);
396	if ((p = strdup(str)) == NULL)
397		err(1, "Cannot copy string");
398	(void)sl_add(sl, p);
399	return i;
400}
401
402static const char *
403getprotostr(StringList *sl, size_t i)
404{
405	assert(i < sl->sl_cur);
406	return sl->sl_str[i];
407}
408
409static const char *
410mkaliases(StringList *sl, char *buf, size_t len)
411{
412	size_t nc, i, pos;
413
414	buf[0] = 0;
415	for (i = 1, pos = 0; i < sl->sl_cur; i++) {
416		nc = strlcpy(buf + pos, sl->sl_str[i], len);
417		if (nc >= len)
418			goto out;
419		pos += nc;
420		len -= nc;
421		nc = strlcpy(buf + pos, " ", len);
422		if (nc >= len)
423			goto out;
424		pos += nc;
425		len -= nc;
426	}
427	return buf;
428out:
429	warn("aliases for `%s' truncated", sl->sl_str[0]);
430	return buf;
431}
432
433static void
434usage(void)
435{
436	(void)fprintf(stderr,
437	    "Usage:\t%s [-b | -l] [-q] [-o <db>] [<servicefile>]\n"
438	    "\t%s -u [<servicefile>]\n", getprogname(), getprogname());
439	exit(1);
440}
441