Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / services_mkdb / services_mkdb.c
blobea9f837818e5139ce5949ff6f4021af8e6c30a45
1 /* $NetBSD: services_mkdb.c,v 1.13 2008/03/09 01:29:34 dholland Exp $ */
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn and Christos Zoulas.
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: services_mkdb.c,v 1.13 2008/03/09 01:29:34 dholland Exp $");
35 #endif /* not lint */
37 #include <sys/param.h>
39 #include <assert.h>
40 #include <db.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>
53 static char tname[MAXPATHLEN];
55 #define PMASK 0xffff
56 #define PROTOMAX 5
58 extern void uniq(const char *);
60 static void add(DB *, StringList *, size_t, const char *, size_t *, int);
61 static StringList ***parseservices(const char *, StringList *);
62 static void cleanup(void);
63 static void store(DB *, DBT *, DBT *, int);
64 static void killproto(DBT *);
65 static char *getstring(const char *, size_t, char **, const char *);
66 static size_t getprotoindex(StringList *, const char *);
67 static const char *getprotostr(StringList *, size_t);
68 static const char *mkaliases(StringList *, char *, size_t);
69 static void usage(void) __dead;
71 const HASHINFO hinfo = {
72 .bsize = 256,
73 .ffactor = 4,
74 .nelem = 32768,
75 .cachesize = 1024,
76 .hash = NULL,
77 .lorder = 0
81 int
82 main(int argc, char *argv[])
84 DB *db;
85 int ch;
86 const char *fname = _PATH_SERVICES;
87 const char *dbname = _PATH_SERVICES_DB;
88 int warndup = 1;
89 int unique = 0;
90 int otherflag = 0;
91 size_t cnt = 0;
92 StringList *sl, ***svc;
93 size_t port, proto;
95 setprogname(argv[0]);
97 while ((ch = getopt(argc, argv, "qo:u")) != -1)
98 switch (ch) {
99 case 'q':
100 otherflag = 1;
101 warndup = 0;
102 break;
103 case 'o':
104 otherflag = 1;
105 dbname = optarg;
106 break;
107 case 'u':
108 unique++;
109 break;
110 case '?':
111 default:
112 usage();
115 argc -= optind;
116 argv += optind;
118 if (argc > 1 || (unique && otherflag))
119 usage();
120 if (argc == 1)
121 fname = argv[0];
123 if (unique)
124 uniq(fname);
126 svc = parseservices(fname, sl = sl_init());
128 if (atexit(cleanup))
129 err(1, "Cannot install exit handler");
131 (void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
132 db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
133 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
134 if (!db)
135 err(1, "Error opening temporary database `%s'", tname);
138 for (port = 0; port < PMASK + 1; port++) {
139 if (svc[port] == NULL)
140 continue;
142 for (proto = 0; proto < PROTOMAX; proto++) {
143 StringList *s;
144 if ((s = svc[port][proto]) == NULL)
145 continue;
146 add(db, s, port, getprotostr(sl, proto), &cnt, warndup);
149 free(svc[port]);
152 free(svc);
153 sl_free(sl, 1);
155 if ((db->close)(db))
156 err(1, "Error closing temporary database `%s'", tname);
158 if (rename(tname, dbname) == -1)
159 err(1, "Cannot rename `%s' to `%s'", tname, dbname);
161 return 0;
164 static void
165 add(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt,
166 int warndup)
168 size_t i;
169 char keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
170 DBT data, key;
171 key.data = keyb;
172 data.data = datab;
174 #ifdef DEBUG
175 (void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto);
176 for (i = 1; i < sl->sl_cur; i++)
177 (void)printf("%s ", sl->sl_str[i]);
178 (void)printf("]\n");
179 #endif
181 /* key `indirect key', data `full line' */
182 data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
183 key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
184 sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
185 store(db, &data, &key, warndup);
187 /* key `\377port/proto', data = `indirect key' */
188 key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
189 port, proto) + 1;
190 store(db, &key, &data, warndup);
192 /* key `\377port', data = `indirect key' */
193 killproto(&key);
194 store(db, &key, &data, warndup);
196 /* add references for service and all aliases */
197 for (i = 0; i < sl->sl_cur; i++) {
198 /* key `\376service/proto', data = `indirect key' */
199 key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
200 sl->sl_str[i], proto) + 1;
201 store(db, &key, &data, warndup);
203 /* key `\376service', data = `indirect key' */
204 killproto(&key);
205 store(db, &key, &data, warndup);
207 sl_free(sl, 1);
210 static StringList ***
211 parseservices(const char *fname, StringList *sl)
213 size_t len, line, pindex;
214 FILE *fp;
215 StringList ***svc, *s;
216 char *p, *ep;
218 if ((fp = fopen(fname, "r")) == NULL)
219 err(1, "Cannot open `%s'", fname);
221 line = 0;
222 svc = ecalloc(PMASK + 1, sizeof(StringList **));
224 /* XXX: change NULL to "\0\0#" when fparseln fixed */
225 for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
226 char *name, *port, *proto, *aliases, *cp, *alias;
227 unsigned long pnum;
229 if (len == 0)
230 continue;
232 for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
233 continue;
235 if (*cp == '\0' || *cp == '#')
236 continue;
238 if ((name = getstring(fname, line, &cp, "name")) == NULL)
239 continue;
241 if ((port = getstring(fname, line, &cp, "port")) == NULL)
242 continue;
244 if (cp) {
245 for (aliases = cp; *cp && *cp != '#'; cp++)
246 continue;
248 if (*cp)
249 *cp = '\0';
250 } else
251 aliases = NULL;
253 proto = strchr(port, '/');
254 if (proto == NULL || proto[1] == '\0') {
255 warnx("%s, %zu: no protocol found", fname, line);
256 continue;
258 *proto++ = '\0';
260 errno = 0;
261 pnum = strtoul(port, &ep, 0);
262 if (*port == '\0' || *ep != '\0') {
263 warnx("%s, %zu: invalid port `%s'", fname, line, port);
264 continue;
266 if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
267 warnx("%s, %zu: port too big `%s'", fname, line, port);
268 continue;
271 if (svc[pnum] == NULL)
272 svc[pnum] = ecalloc(PROTOMAX, sizeof(StringList *));
274 pindex = getprotoindex(sl, proto);
275 if (svc[pnum][pindex] == NULL)
276 s = svc[pnum][pindex] = sl_init();
277 else
278 s = svc[pnum][pindex];
280 /* build list of aliases */
281 if (sl_find(s, name) == NULL)
282 (void)sl_add(s, estrdup(name));
284 if (aliases) {
285 while ((alias = strsep(&aliases, " \t")) != NULL) {
286 if (alias[0] == '\0')
287 continue;
288 if (sl_find(s, alias) == NULL)
289 (void)sl_add(s, estrdup(alias));
293 (void)fclose(fp);
294 return svc;
298 * cleanup(): Remove temporary files upon exit
300 static void
301 cleanup(void)
303 if (tname[0])
304 (void)unlink(tname);
307 static char *
308 getstring(const char *fname, size_t line, char **cp, const char *tag)
310 char *str;
312 while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
313 continue;
315 if (str == NULL)
316 warnx("%s, %zu: no %s found", fname, line, tag);
318 return str;
321 static void
322 killproto(DBT *key)
324 char *p, *d = key->data;
326 if ((p = strchr(d, '/')) == NULL)
327 abort();
328 *p++ = '\0';
329 key->size = p - d;
332 static void
333 store(DB *db, DBT *key, DBT *data, int warndup)
335 #ifdef DEBUG
336 int k = key->size - 1;
337 int d = data->size - 1;
338 (void)printf("store [%*.*s] [%*.*s]\n",
339 k, k, (char *)key->data + 1,
340 d, d, (char *)data->data + 1);
341 #endif
342 switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
343 case 0:
344 break;
345 case 1:
346 if (warndup)
347 warnx("duplicate service `%s'",
348 &((char *)key->data)[1]);
349 break;
350 case -1:
351 err(1, "put");
352 break;
353 default:
354 abort();
355 break;
359 static size_t
360 getprotoindex(StringList *sl, const char *str)
362 size_t i;
364 for (i= 0; i < sl->sl_cur; i++)
365 if (strcmp(sl->sl_str[i], str) == 0)
366 return i;
368 if (i == PROTOMAX)
369 errx(1, "Ran out of protocols adding `%s';"
370 " recompile with larger PROTOMAX", str);
371 (void)sl_add(sl, estrdup(str));
372 return i;
375 static const char *
376 getprotostr(StringList *sl, size_t i)
378 assert(i < sl->sl_cur);
379 return sl->sl_str[i];
382 static const char *
383 mkaliases(StringList *sl, char *buf, size_t len)
385 size_t nc, i, pos;
387 buf[0] = 0;
388 for (i = 1, pos = 0; i < sl->sl_cur; i++) {
389 nc = strlcpy(buf + pos, sl->sl_str[i], len);
390 if (nc >= len)
391 goto out;
392 pos += nc;
393 len -= nc;
394 nc = strlcpy(buf + pos, " ", len);
395 if (nc >= len)
396 goto out;
397 pos += nc;
398 len -= nc;
400 return buf;
401 out:
402 warn("aliases for `%s' truncated", sl->sl_str[0]);
403 return buf;
406 static void
407 usage(void)
409 (void)fprintf(stderr, "Usage:\t%s [-q] [-o <db>] [<servicefile>]\n"
410 "\t%s -u [<servicefile>]\n", getprogname(), getprogname());
411 exit(1);