services_mkdb.c revision 1.9 1 /* $NetBSD: services_mkdb.c,v 1.9 2007/05/15 19:57:40 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: services_mkdb.c,v 1.9 2007/05/15 19:57:40 christos Exp $");
42 #endif /* not lint */
43
44 #include <sys/param.h>
45
46 #include <assert.h>
47 #include <db.h>
48 #include <err.h>
49 #include <fcntl.h>
50 #include <netdb.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <util.h>
56 #include <ctype.h>
57 #include <errno.h>
58 #include <stringlist.h>
59
60 static char tname[MAXPATHLEN];
61
62 #define PMASK 0xffff
63 #define PROTOMAX 5
64
65 static void add(DB *, StringList *, size_t, const char *, size_t *, int);
66 static StringList ***parseservices(const char *, StringList *);
67 static void cleanup(void);
68 static void store(DB *, DBT *, DBT *, int);
69 static void killproto(DBT *);
70 static char *getstring(const char *, size_t, char **, const char *);
71 static size_t getprotoindex(StringList *, const char *);
72 static const char *getprotostr(StringList *, size_t);
73 static const char *mkaliases(StringList *, char *, size_t);
74 static void usage(void) __attribute__((__noreturn__));
75
76 static const HASHINFO hinfo = {
77 .bsize = 256,
78 .ffactor = 4,
79 .nelem = 32768,
80 .cachesize = 1024,
81 .hash = NULL,
82 .lorder = 0
83 };
84
85
86 int
87 main(int argc, char *argv[])
88 {
89 DB *db;
90 int ch;
91 const char *fname = _PATH_SERVICES;
92 const char *dbname = _PATH_SERVICES_DB;
93 int warndup = 1;
94 size_t cnt = 0;
95 StringList *sl, ***svc;
96 size_t port, proto;
97
98 setprogname(argv[0]);
99
100 while ((ch = getopt(argc, argv, "qo:")) != -1)
101 switch (ch) {
102 case 'q':
103 warndup = 0;
104 break;
105 case 'o':
106 dbname = optarg;
107 break;
108 case '?':
109 default:
110 usage();
111 }
112
113 argc -= optind;
114 argv += optind;
115
116 if (argc > 1)
117 usage();
118 if (argc == 1)
119 fname = argv[0];
120
121 svc = parseservices(fname, sl = sl_init());
122
123 if (atexit(cleanup))
124 err(1, "Cannot install exit handler");
125
126 (void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
127 db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
128 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
129 if (!db)
130 err(1, "Error opening temporary database `%s'", tname);
131
132
133 for (port = 0; port < PMASK + 1; port++) {
134 if (svc[port] == NULL)
135 continue;
136
137 for (proto = 0; proto < PROTOMAX; proto++) {
138 StringList *s;
139 if ((s = svc[port][proto]) == NULL)
140 continue;
141 add(db, s, port, getprotostr(sl, proto), &cnt, warndup);
142 }
143
144 free(svc[port]);
145 }
146
147 free(svc);
148 sl_free(sl, 1);
149
150 if ((db->close)(db))
151 err(1, "Error closing temporary database `%s'", tname);
152
153 if (rename(tname, dbname) == -1)
154 err(1, "Cannot rename `%s' to `%s'", tname, dbname);
155
156 return 0;
157 }
158
159 static void
160 add(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt,
161 int warndup)
162 {
163 size_t i;
164 char keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
165 DBT data, key;
166 key.data = keyb;
167 data.data = datab;
168
169 #ifdef DEBUG
170 (void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto);
171 for (i = 1; i < sl->sl_cur; i++)
172 (void)printf("%s ", sl->sl_str[i]);
173 (void)printf("]\n");
174 #endif
175
176 /* key `indirect key', data `full line' */
177 data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
178 key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
179 sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
180 store(db, &data, &key, warndup);
181
182 /* key `\377port/proto', data = `indirect key' */
183 key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
184 port, proto) + 1;
185 store(db, &key, &data, warndup);
186
187 /* key `\377port', data = `indirect key' */
188 killproto(&key);
189 store(db, &key, &data, warndup);
190
191 /* add references for service and all aliases */
192 for (i = 0; i < sl->sl_cur; i++) {
193 /* key `\376service/proto', data = `indirect key' */
194 key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
195 sl->sl_str[i], proto) + 1;
196 store(db, &key, &data, warndup);
197
198 /* key `\376service', data = `indirect key' */
199 killproto(&key);
200 store(db, &key, &data, warndup);
201 }
202 sl_free(sl, 1);
203 }
204
205 static StringList ***
206 parseservices(const char *fname, StringList *sl)
207 {
208 size_t len, line, pindex;
209 FILE *fp;
210 StringList ***svc, *s;
211 char *p, *ep;
212
213 if ((fp = fopen(fname, "r")) == NULL)
214 err(1, "Cannot open `%s'", fname);
215
216 line = 0;
217 svc = ecalloc(PMASK + 1, sizeof(StringList **));
218
219 /* XXX: change NULL to "\0\0#" when fparseln fixed */
220 for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
221 char *name, *port, *proto, *aliases, *cp, *alias;
222 unsigned long pnum;
223
224 if (len == 0)
225 continue;
226
227 for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
228 continue;
229
230 if (*cp == '\0' || *cp == '#')
231 continue;
232
233 if ((name = getstring(fname, line, &cp, "name")) == NULL)
234 continue;
235
236 if ((port = getstring(fname, line, &cp, "port")) == NULL)
237 continue;
238
239 if (cp) {
240 for (aliases = cp; *cp && *cp != '#'; cp++)
241 continue;
242
243 if (*cp)
244 *cp = '\0';
245 } else
246 aliases = NULL;
247
248 proto = strchr(port, '/');
249 if (proto == NULL || proto[1] == '\0') {
250 warnx("%s, %zu: no protocol found", fname, line);
251 continue;
252 }
253 *proto++ = '\0';
254
255 errno = 0;
256 pnum = strtoul(port, &ep, 0);
257 if (*port == '\0' || *ep != '\0') {
258 warnx("%s, %zu: invalid port `%s'", fname, line, port);
259 continue;
260 }
261 if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
262 warnx("%s, %zu: port too big `%s'", fname, line, port);
263 continue;
264 }
265
266 if (svc[pnum] == NULL)
267 svc[pnum] = ecalloc(PROTOMAX, sizeof(StringList *));
268
269 pindex = getprotoindex(sl, proto);
270 if (svc[pnum][pindex] == NULL)
271 s = svc[pnum][pindex] = sl_init();
272 else
273 s = svc[pnum][pindex];
274
275 /* build list of aliases */
276 if (sl_find(s, name) == NULL)
277 (void)sl_add(s, estrdup(name));
278
279 if (aliases) {
280 while ((alias = strsep(&aliases, " \t")) != NULL) {
281 if (alias[0] == '\0')
282 continue;
283 if (sl_find(s, alias) == NULL)
284 (void)sl_add(s, estrdup(alias));
285 }
286 }
287 }
288 (void)fclose(fp);
289 return svc;
290 }
291
292 /*
293 * cleanup(): Remove temporary files upon exit
294 */
295 static void
296 cleanup(void)
297 {
298 if (tname[0])
299 (void)unlink(tname);
300 }
301
302 static char *
303 getstring(const char *fname, size_t line, char **cp, const char *tag)
304 {
305 char *str;
306
307 while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
308 continue;
309
310 if (str == NULL)
311 warnx("%s, %zu: no %s found", fname, line, tag);
312
313 return str;
314 }
315
316 static void
317 killproto(DBT *key)
318 {
319 char *p, *d = key->data;
320
321 if ((p = strchr(d, '/')) == NULL)
322 abort();
323 *p++ = '\0';
324 key->size = p - d;
325 }
326
327 static void
328 store(DB *db, DBT *key, DBT *data, int warndup)
329 {
330 #ifdef DEBUG
331 int k = key->size - 1;
332 int d = data->size - 1;
333 (void)printf("store [%*.*s] [%*.*s]\n",
334 k, k, (char *)key->data + 1,
335 d, d, (char *)data->data + 1);
336 #endif
337 switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
338 case 0:
339 break;
340 case 1:
341 if (warndup)
342 warnx("duplicate service `%s'",
343 &((char *)key->data)[1]);
344 break;
345 case -1:
346 err(1, "put");
347 break;
348 default:
349 abort();
350 break;
351 }
352 }
353
354 static size_t
355 getprotoindex(StringList *sl, const char *str)
356 {
357 size_t i;
358
359 for (i= 0; i < sl->sl_cur; i++)
360 if (strcmp(sl->sl_str[i], str) == 0)
361 return i;
362
363 if (i == PROTOMAX)
364 errx(1, "Ran out of protocols adding `%s';"
365 " recompile with larger PROTOMAX", str);
366 (void)sl_add(sl, estrdup(str));
367 return i;
368 }
369
370 static const char *
371 getprotostr(StringList *sl, size_t i)
372 {
373 assert(i < sl->sl_cur);
374 return sl->sl_str[i];
375 }
376
377 static const char *
378 mkaliases(StringList *sl, char *buf, size_t len)
379 {
380 size_t nc, i, pos;
381
382 for (i = 1, pos = 0; i < sl->sl_cur; i++) {
383 nc = strlcpy(buf + pos, sl->sl_str[i], len);
384 if (nc >= len)
385 goto out;
386 pos += nc;
387 len -= nc;
388 nc = strlcpy(buf + pos, " ", len);
389 if (nc >= len)
390 goto out;
391 pos += nc;
392 len -= nc;
393 }
394 return buf;
395 out:
396 warn("aliases for `%s' truncated", sl->sl_str[0]);
397 return buf;
398 }
399
400 static void
401 usage(void)
402 {
403 (void)fprintf(stderr, "Usage: %s [-q] [-o <db>] [<servicefile>]\n",
404 getprogname());
405 exit(1);
406 }
407