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