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>
50 #include <stringlist.h>
52 static char tname[MAXPATHLEN];
57 extern void uniq(const char *);
59 static void add(DB *, StringList *, size_t, const char *, size_t *, int);
60 static StringList ***parseservices(const char *, StringList *);
61 static void cleanup(void);
62 static void store(DB *, DBT *, DBT *, int);
63 static void killproto(DBT *);
64 static char *getstring(const char *, size_t, char **, const char *);
65 static size_t getprotoindex(StringList *, const char *);
66 static const char *getprotostr(StringList *, size_t);
67 static const char *mkaliases(StringList *, char *, size_t);
68 static void usage(void);
70 const HASHINFO hinfo = {
81 main(int argc, char *argv[])
85 const char *fname = _PATH_SERVICES;
86 const char *dbname = _PATH_SERVICES_DB;
91 StringList *sl, ***svc;
96 while ((ch = getopt(argc, argv, "qo:u")) != -1)
117 if (argc > 1 || (unique && otherflag))
125 svc = parseservices(fname, sl = sl_init());
128 err(1, "Cannot install exit handler");
130 (void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
131 db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
132 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
134 err(1, "Error opening temporary database `%s'", tname);
137 for (port = 0; port < PMASK + 1; port++) {
138 if (svc[port] == NULL)
141 for (proto = 0; proto < PROTOMAX; proto++) {
143 if ((s = svc[port][proto]) == NULL)
145 add(db, s, port, getprotostr(sl, proto), &cnt, warndup);
155 err(1, "Error closing temporary database `%s'", tname);
157 if (rename(tname, dbname) == -1)
158 err(1, "Cannot rename `%s' to `%s'", tname, dbname);
164 add(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt,
168 char keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
174 (void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto);
175 for (i = 1; i < sl->sl_cur; i++)
176 (void)printf("%s ", sl->sl_str[i]);
180 /* key `indirect key', data `full line' */
181 data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
182 key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
183 sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
184 store(db, &data, &key, warndup);
186 /* key `\377port/proto', data = `indirect key' */
187 key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
189 store(db, &key, &data, warndup);
191 /* key `\377port', data = `indirect key' */
193 store(db, &key, &data, warndup);
195 /* add references for service and all aliases */
196 for (i = 0; i < sl->sl_cur; i++) {
197 /* key `\376service/proto', data = `indirect key' */
198 key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
199 sl->sl_str[i], proto) + 1;
200 store(db, &key, &data, warndup);
202 /* key `\376service', data = `indirect key' */
204 store(db, &key, &data, warndup);
209 static StringList ***
210 parseservices(const char *fname, StringList *sl)
212 size_t len, line, pindex;
214 StringList ***svc, *s;
217 if ((fp = fopen(fname, "r")) == NULL)
218 err(1, "Cannot open `%s'", fname);
221 if ((svc = calloc(PMASK + 1, sizeof(StringList **))) == NULL)
222 err(1, "Cannot allocate %zu bytes", (size_t)(PMASK + 1));
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;
232 for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
235 if (*cp == '\0' || *cp == '#')
238 if ((name = getstring(fname, line, &cp, "name")) == NULL)
241 if ((port = getstring(fname, line, &cp, "port")) == NULL)
245 for (aliases = cp; *cp && *cp != '#'; cp++)
253 proto = strchr(port, '/');
254 if (proto == NULL || proto[1] == '\0') {
255 warnx("%s, %zu: no protocol found", fname, line);
261 pnum = strtoul(port, &ep, 0);
262 if (*port == '\0' || *ep != '\0') {
263 warnx("%s, %zu: invalid port `%s'", fname, line, port);
266 if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
267 warnx("%s, %zu: port too big `%s'", fname, line, port);
271 if (svc[pnum] == NULL) {
272 svc[pnum] = calloc(PROTOMAX, sizeof(StringList *));
273 if (svc[pnum] == NULL)
274 err(1, "Cannot allocate %zu bytes",
278 pindex = getprotoindex(sl, proto);
279 if (svc[pnum][pindex] == NULL)
280 s = svc[pnum][pindex] = sl_init();
282 s = svc[pnum][pindex];
284 /* build list of aliases */
285 if (sl_find(s, name) == NULL) {
288 if ((p2 = strdup(name)) == NULL)
289 err(1, "Cannot copy string");
294 while ((alias = strsep(&aliases, " \t")) != NULL) {
295 if (alias[0] == '\0')
297 if (sl_find(s, alias) == NULL) {
300 if ((p2 = strdup(alias)) == NULL)
301 err(1, "Cannot copy string");
312 * cleanup(): Remove temporary files upon exit
322 getstring(const char *fname, size_t line, char **cp, const char *tag)
326 while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
330 warnx("%s, %zu: no %s found", fname, line, tag);
338 char *p, *d = key->data;
340 if ((p = strchr(d, '/')) == NULL)
347 store(DB *db, DBT *key, DBT *data, int warndup)
350 int k = key->size - 1;
351 int d = data->size - 1;
352 (void)printf("store [%*.*s] [%*.*s]\n",
353 k, k, (char *)key->data + 1,
354 d, d, (char *)data->data + 1);
356 switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
361 warnx("duplicate service `%s'",
362 &((char *)key->data)[1]);
374 getprotoindex(StringList *sl, const char *str)
379 for (i= 0; i < sl->sl_cur; i++)
380 if (strcmp(sl->sl_str[i], str) == 0)
384 errx(1, "Ran out of protocols adding `%s';"
385 " recompile with larger PROTOMAX", str);
386 if ((p = strdup(str)) == NULL)
387 err(1, "Cannot copy string");
393 getprotostr(StringList *sl, size_t i)
395 assert(i < sl->sl_cur);
396 return sl->sl_str[i];
400 mkaliases(StringList *sl, char *buf, size_t len)
405 for (i = 1, pos = 0; i < sl->sl_cur; i++) {
406 nc = strlcpy(buf + pos, sl->sl_str[i], len);
411 nc = strlcpy(buf + pos, " ", len);
419 warn("aliases for `%s' truncated", sl->sl_str[0]);
426 (void)fprintf(stderr, "Usage:\t%s [-q] [-o <db>] [<servicefile>]\n"
427 "\t%s -u [<servicefile>]\n", getprogname(), getprogname());