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