services_mkdb.c revision 1.6 1 1.6 christos /* $NetBSD: services_mkdb.c,v 1.6 2007/05/08 20:14:59 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.6 christos __RCSID("$NetBSD: services_mkdb.c,v 1.6 2007/05/08 20:14:59 christos Exp $");
42 1.1 lukem #endif /* not lint */
43 1.1 lukem
44 1.1 lukem
45 1.1 lukem #include <sys/param.h>
46 1.1 lukem
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.3 christos #include <stringlist.h>
58 1.1 lukem
59 1.3 christos static char tname[MAXPATHLEN];
60 1.3 christos
61 1.3 christos static void cleanup(void);
62 1.4 christos static void store(const char *, size_t, DB *, DBT *, DBT *, int);
63 1.5 christos static void killproto(DBT *);
64 1.5 christos static char *getstring(const char *, size_t, char **, const char *);
65 1.3 christos static void usage(void) __attribute__((__noreturn__));
66 1.1 lukem
67 1.1 lukem int
68 1.3 christos main(int argc, char *argv[])
69 1.1 lukem {
70 1.1 lukem DB *db;
71 1.1 lukem FILE *fp;
72 1.3 christos int ch;
73 1.4 christos size_t len, line, cnt;
74 1.3 christos char keyb[BUFSIZ], datab[BUFSIZ], *p;
75 1.5 christos DBT data, key;
76 1.3 christos const char *fname = _PATH_SERVICES;
77 1.3 christos const char *dbname = _PATH_SERVICES_DB;
78 1.6 christos int warndup = 1;
79 1.3 christos
80 1.3 christos setprogname(argv[0]);
81 1.1 lukem
82 1.6 christos while ((ch = getopt(argc, argv, "qo:")) != -1)
83 1.1 lukem switch (ch) {
84 1.6 christos case 'q':
85 1.6 christos warndup = 0;
86 1.6 christos break;
87 1.1 lukem case 'o':
88 1.1 lukem dbname = optarg;
89 1.1 lukem break;
90 1.1 lukem case '?':
91 1.1 lukem default:
92 1.1 lukem usage();
93 1.1 lukem }
94 1.1 lukem
95 1.1 lukem argc -= optind;
96 1.1 lukem argv += optind;
97 1.1 lukem
98 1.3 christos if (argc > 1)
99 1.3 christos usage();
100 1.1 lukem if (argc == 1)
101 1.1 lukem fname = argv[0];
102 1.3 christos
103 1.1 lukem if ((fp = fopen(fname, "r")) == NULL)
104 1.1 lukem err(1, "Cannot open `%s'", fname);
105 1.3 christos
106 1.1 lukem if (atexit(cleanup))
107 1.1 lukem err(1, "Cannot install exit handler");
108 1.3 christos
109 1.3 christos (void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
110 1.1 lukem db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
111 1.3 christos (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, NULL);
112 1.1 lukem if (!db)
113 1.1 lukem err(1, "Error opening temporary database `%s'", tname);
114 1.1 lukem
115 1.4 christos key.data = keyb;
116 1.4 christos data.data = datab;
117 1.1 lukem
118 1.4 christos cnt = line = 0;
119 1.3 christos /* XXX: change NULL to "\0\0#" when fparseln fixed */
120 1.1 lukem for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
121 1.3 christos char *name, *port, *proto, *aliases, *cp, *alias;
122 1.3 christos StringList *sl;
123 1.3 christos size_t i;
124 1.1 lukem
125 1.1 lukem if (len == 0)
126 1.1 lukem continue;
127 1.1 lukem cp = p;
128 1.5 christos
129 1.5 christos if ((name = getstring(fname, line, &cp, "name")) == NULL)
130 1.3 christos continue;
131 1.5 christos
132 1.5 christos if ((port = getstring(fname, line, &cp, "port")) == NULL)
133 1.1 lukem continue;
134 1.5 christos
135 1.1 lukem proto = strchr(port, '/');
136 1.1 lukem if (proto == NULL || proto[1] == '\0') {
137 1.3 christos warnx("%s, %zu: no protocol found", fname, line);
138 1.1 lukem continue;
139 1.1 lukem }
140 1.1 lukem *proto++ = '\0';
141 1.5 christos
142 1.3 christos for (aliases = cp; cp && isspace((unsigned char)*aliases);
143 1.3 christos aliases++)
144 1.3 christos continue;
145 1.3 christos
146 1.4 christos /* key `indirect key', data `full line' */
147 1.4 christos data.size = snprintf(datab, sizeof(datab), "%zu", cnt++) + 1;
148 1.4 christos key.size = snprintf(keyb, sizeof(keyb), "%s %s/%s %s",
149 1.5 christos name, port, proto, aliases ? aliases : "") + 1;
150 1.6 christos store(fname, line, db, &data, &key, warndup);
151 1.1 lukem
152 1.4 christos /* key `\377port/proto', data = `indirect key' */
153 1.4 christos key.size = snprintf(keyb, sizeof(keyb), "\377%s/%s",
154 1.4 christos port, proto) + 1;
155 1.6 christos store(fname, line, db, &key, &data, warndup);
156 1.1 lukem
157 1.4 christos /* key `\377port', data = `indirect key' */
158 1.5 christos killproto(&key);
159 1.4 christos store(fname, line, db, &key, &data, 0);
160 1.1 lukem
161 1.3 christos /* build list of aliases */
162 1.3 christos sl = sl_init();
163 1.5 christos (void)sl_add(sl, name);
164 1.3 christos while ((alias = strsep(&cp, " \t")) != NULL) {
165 1.3 christos if (alias[0] == '\0')
166 1.1 lukem continue;
167 1.5 christos (void)sl_add(sl, alias);
168 1.1 lukem }
169 1.1 lukem
170 1.3 christos /* add references for service and all aliases */
171 1.3 christos for (i = 0; i < sl->sl_cur; i++) {
172 1.4 christos /* key `\376service/proto', data = `indirect key' */
173 1.4 christos key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
174 1.3 christos sl->sl_str[i], proto) + 1;
175 1.4 christos store(fname, line, db, &key, &data, 1);
176 1.1 lukem
177 1.4 christos /* key `\376service', data = `indirect key' */
178 1.5 christos killproto(&key);
179 1.4 christos store(fname, line, db, &key, &data, 0);
180 1.1 lukem }
181 1.3 christos sl_free(sl, 0);
182 1.1 lukem }
183 1.1 lukem
184 1.1 lukem if ((db->close)(db))
185 1.1 lukem err(1, "Error closing temporary database `%s'", tname);
186 1.5 christos
187 1.1 lukem if (rename(tname, dbname) == -1)
188 1.1 lukem err(1, "Cannot rename `%s' to `%s'", tname, dbname);
189 1.1 lukem
190 1.3 christos return 0;
191 1.1 lukem }
192 1.1 lukem
193 1.1 lukem /*
194 1.1 lukem * cleanup(): Remove temporary files upon exit
195 1.1 lukem */
196 1.3 christos static void
197 1.3 christos cleanup(void)
198 1.1 lukem {
199 1.3 christos if (tname[0])
200 1.3 christos (void)unlink(tname);
201 1.1 lukem }
202 1.1 lukem
203 1.5 christos static char *
204 1.5 christos getstring(const char *fname, size_t line, char **cp, const char *tag)
205 1.5 christos {
206 1.5 christos char *str;
207 1.5 christos
208 1.5 christos while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
209 1.5 christos continue;
210 1.5 christos
211 1.5 christos if (str == NULL)
212 1.5 christos warnx("%s, %zu: no %s found", fname, line, tag);
213 1.5 christos
214 1.5 christos return str;
215 1.5 christos }
216 1.5 christos
217 1.5 christos static void
218 1.5 christos killproto(DBT *key)
219 1.5 christos {
220 1.5 christos char *p, *d = key->data;
221 1.5 christos
222 1.5 christos if ((p = strchr(d, '/')) == NULL)
223 1.5 christos abort();
224 1.5 christos *p++ = '\0';
225 1.5 christos key->size = p - d;
226 1.5 christos }
227 1.5 christos
228 1.3 christos static void
229 1.4 christos store(const char *fname, size_t line, DB *db, DBT *key, DBT *data, int warndup)
230 1.4 christos {
231 1.4 christos switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
232 1.4 christos case 0:
233 1.4 christos break;
234 1.4 christos case 1:
235 1.4 christos if (warndup)
236 1.4 christos warnx("%s, %zu: duplicate service `%s'",
237 1.4 christos fname, line, &((char *)key->data)[1]);
238 1.4 christos break;
239 1.4 christos case -1:
240 1.4 christos err(1, "put");
241 1.4 christos break;
242 1.4 christos default:
243 1.4 christos abort();
244 1.4 christos break;
245 1.4 christos }
246 1.4 christos }
247 1.4 christos
248 1.4 christos static void
249 1.3 christos usage(void)
250 1.1 lukem {
251 1.6 christos (void)fprintf(stderr, "Usage: %s [-q] [-o <db>] [<servicefile>]\n",
252 1.3 christos getprogname());
253 1.1 lukem exit(1);
254 1.1 lukem }
255