1 /* $NetBSD: services_mkdb.c,v 1.14 2008/04/28 20:24:17 martin Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
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
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 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
51 #include <stringlist.h>
55 static char tname[MAXPATHLEN];
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);
82 main(int argc, char *argv[])
86 const char *fname = _PATH_SERVICES;
87 const char *dbname = _PATH_SERVICES_DB;
93 StringList *sl, ***svc;
96 int dbname_dir_fd = -1;
100 while ((ch = getopt(argc, argv, "blo:qu")) != -1)
106 byteorder = ch == 'b' ? 4321 : 1234;
127 if (argc > 1 || (unique && otherflag))
132 /* Set byte order. */
133 hinfo.lorder = byteorder;
138 svc = parseservices(fname, sl = sl_init());
141 err(1, "Cannot install exit handler");
143 (void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
144 db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
145 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
147 err(1, "Error opening temporary database `%s'", tname);
150 for (port = 0; port < PMASK + 1; port++) {
151 if (svc[port] == NULL)
154 for (proto = 0; proto < PROTOMAX; proto++) {
156 if ((s = svc[port][proto]) == NULL)
158 add(db, s, port, getprotostr(sl, proto), &cnt, warndup);
168 err(1, "Error closing temporary database `%s'", tname);
171 * Make sure file is safe on disk. To improve performance we will call
172 * fsync() to the directory where file lies
174 if (rename(tname, dbname) == -1 ||
175 (dbname_dir = dirname(dbname)) == NULL ||
176 (dbname_dir_fd = open(dbname_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
177 fsync(dbname_dir_fd) != 0) {
178 if (dbname_dir_fd != -1)
179 close(dbname_dir_fd);
180 err(1, "Cannot rename `%s' to `%s'", tname, dbname);
183 if (dbname_dir_fd != -1)
184 close(dbname_dir_fd);
190 add(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt,
194 char keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
200 (void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto);
201 for (i = 1; i < sl->sl_cur; i++)
202 (void)printf("%s ", sl->sl_str[i]);
206 /* key `indirect key', data `full line' */
207 data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
208 key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
209 sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
210 store(db, &data, &key, warndup);
212 /* key `\377port/proto', data = `indirect key' */
213 key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
215 store(db, &key, &data, warndup);
217 /* key `\377port', data = `indirect key' */
219 store(db, &key, &data, warndup);
221 /* add references for service and all aliases */
222 for (i = 0; i < sl->sl_cur; i++) {
223 /* key `\376service/proto', data = `indirect key' */
224 key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
225 sl->sl_str[i], proto) + 1;
226 store(db, &key, &data, warndup);
228 /* key `\376service', data = `indirect key' */
230 store(db, &key, &data, warndup);
235 static StringList ***
236 parseservices(const char *fname, StringList *sl)
238 size_t len, line, pindex;
240 StringList ***svc, *s;
243 if ((fp = fopen(fname, "r")) == NULL)
244 err(1, "Cannot open `%s'", fname);
247 if ((svc = calloc(PMASK + 1, sizeof(StringList **))) == NULL)
248 err(1, "Cannot allocate %zu bytes", (size_t)(PMASK + 1));
250 /* XXX: change NULL to "\0\0#" when fparseln fixed */
251 for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
252 char *name, *port, *proto, *aliases, *cp, *alias;
258 for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
261 if (*cp == '\0' || *cp == '#')
264 if ((name = getstring(fname, line, &cp, "name")) == NULL)
267 if ((port = getstring(fname, line, &cp, "port")) == NULL)
271 for (aliases = cp; *cp && *cp != '#'; cp++)
279 proto = strchr(port, '/');
280 if (proto == NULL || proto[1] == '\0') {
281 warnx("%s, %zu: no protocol found", fname, line);
287 pnum = strtoul(port, &ep, 0);
288 if (*port == '\0' || *ep != '\0') {
289 warnx("%s, %zu: invalid port `%s'", fname, line, port);
292 if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
293 warnx("%s, %zu: port too big `%s'", fname, line, port);
297 if (svc[pnum] == NULL) {
298 svc[pnum] = calloc(PROTOMAX, sizeof(StringList *));
299 if (svc[pnum] == NULL)
300 err(1, "Cannot allocate %zu bytes",
304 pindex = getprotoindex(sl, proto);
305 if (svc[pnum][pindex] == NULL)
306 s = svc[pnum][pindex] = sl_init();
308 s = svc[pnum][pindex];
310 /* build list of aliases */
311 if (sl_find(s, name) == NULL) {
314 if ((p2 = strdup(name)) == NULL)
315 err(1, "Cannot copy string");
320 while ((alias = strsep(&aliases, " \t")) != NULL) {
321 if (alias[0] == '\0')
323 if (sl_find(s, alias) == NULL) {
326 if ((p2 = strdup(alias)) == NULL)
327 err(1, "Cannot copy string");
338 * cleanup(): Remove temporary files upon exit
348 getstring(const char *fname, size_t line, char **cp, const char *tag)
352 while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
356 warnx("%s, %zu: no %s found", fname, line, tag);
364 char *p, *d = key->data;
366 if ((p = strchr(d, '/')) == NULL)
373 store(DB *db, DBT *key, DBT *data, int warndup)
376 int k = key->size - 1;
377 int d = data->size - 1;
378 (void)printf("store [%*.*s] [%*.*s]\n",
379 k, k, (char *)key->data + 1,
380 d, d, (char *)data->data + 1);
382 switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
387 warnx("duplicate service `%s'",
388 &((char *)key->data)[1]);
400 getprotoindex(StringList *sl, const char *str)
405 for (i= 0; i < sl->sl_cur; i++)
406 if (strcmp(sl->sl_str[i], str) == 0)
410 errx(1, "Ran out of protocols adding `%s';"
411 " recompile with larger PROTOMAX", str);
412 if ((p = strdup(str)) == NULL)
413 err(1, "Cannot copy string");
419 getprotostr(StringList *sl, size_t i)
421 assert(i < sl->sl_cur);
422 return sl->sl_str[i];
426 mkaliases(StringList *sl, char *buf, size_t len)
431 for (i = 1, pos = 0; i < sl->sl_cur; i++) {
432 nc = strlcpy(buf + pos, sl->sl_str[i], len);
437 nc = strlcpy(buf + pos, " ", len);
445 warn("aliases for `%s' truncated", sl->sl_str[0]);
452 (void)fprintf(stderr,
453 "Usage:\t%s [-b | -l] [-q] [-o <db>] [<servicefile>]\n"
454 "\t%s -u [<servicefile>]\n", getprogname(), getprogname());