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