services_mkdb.c revision 1.1 1 1.1 lukem /* $NetBSD: services_mkdb.c,v 1.1 1999/02/08 03:57:05 lukem 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.1 lukem * by Luke Mewburn.
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.1 lukem __RCSID("$NetBSD: services_mkdb.c,v 1.1 1999/02/08 03:57:05 lukem 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.1 lukem
57 1.1 lukem #ifndef _PATH_SERVICES_DB
58 1.1 lukem #define _PATH_SERVICES_DB "/etc/services.db";
59 1.1 lukem #endif
60 1.1 lukem
61 1.1 lukem static char *dbname = _PATH_SERVICES_DB;
62 1.1 lukem
63 1.1 lukem void cleanup __P((void));
64 1.1 lukem int main __P((int, char *[]));
65 1.1 lukem void usage __P((void));
66 1.1 lukem
67 1.1 lukem int
68 1.1 lukem main(argc, argv)
69 1.1 lukem int argc;
70 1.1 lukem char *argv[];
71 1.1 lukem {
72 1.1 lukem DB *db;
73 1.1 lukem FILE *fp;
74 1.1 lukem int ch, len, line;
75 1.1 lukem char tname[MAXPATHLEN], *p;
76 1.1 lukem char keyb[BUFSIZ], datab[BUFSIZ];
77 1.1 lukem char *fname = _PATH_SERVICES;
78 1.1 lukem DBT data, key;
79 1.1 lukem
80 1.1 lukem while ((ch = getopt(argc, argv, "o:")) != -1)
81 1.1 lukem switch (ch) {
82 1.1 lukem case 'o':
83 1.1 lukem dbname = optarg;
84 1.1 lukem break;
85 1.1 lukem case '?':
86 1.1 lukem default:
87 1.1 lukem usage();
88 1.1 lukem }
89 1.1 lukem
90 1.1 lukem argc -= optind;
91 1.1 lukem argv += optind;
92 1.1 lukem
93 1.1 lukem if (argc == 1)
94 1.1 lukem fname = argv[0];
95 1.1 lukem else if (argc > 1)
96 1.1 lukem usage();
97 1.1 lukem if ((fp = fopen(fname, "r")) == NULL)
98 1.1 lukem err(1, "Cannot open `%s'", fname);
99 1.1 lukem if (atexit(cleanup))
100 1.1 lukem err(1, "Cannot install exit handler");
101 1.1 lukem (void) snprintf(tname, sizeof(tname), "%s.tmp", dbname);
102 1.1 lukem db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
103 1.1 lukem (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, NULL);
104 1.1 lukem if (!db)
105 1.1 lukem err(1, "Error opening temporary database `%s'", tname);
106 1.1 lukem
107 1.1 lukem key.data = (u_char *)keyb;
108 1.1 lukem data.data = (u_char *)datab;
109 1.1 lukem
110 1.1 lukem /* XXX: change NULL to "\0\0#" when fparseln fixed */
111 1.1 lukem for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
112 1.1 lukem char *name, *port, *proto, *aliases, *cp;
113 1.1 lukem #define MAXALIASES 35
114 1.1 lukem char *aliasv[MAXALIASES];
115 1.1 lukem int aliasc, i;
116 1.1 lukem
117 1.1 lukem if (len == 0)
118 1.1 lukem continue;
119 1.1 lukem cp = p;
120 1.1 lukem while ((name = strsep(&cp, " \t")) != NULL && *name == '\0')
121 1.1 lukem ;
122 1.1 lukem if (name == NULL) {
123 1.1 lukem warnx("%s line %d: no name found", fname, line);
124 1.1 lukem continue;
125 1.1 lukem }
126 1.1 lukem while ((port = strsep(&cp, " \t")) != NULL && *port == '\0')
127 1.1 lukem ;
128 1.1 lukem if (port == NULL) {
129 1.1 lukem warnx("%s line %d: no port found", fname, line);
130 1.1 lukem continue;
131 1.1 lukem }
132 1.1 lukem proto = strchr(port, '/');
133 1.1 lukem if (proto == NULL || proto[1] == '\0') {
134 1.1 lukem warnx("%s line %d: no protocol found", fname, line);
135 1.1 lukem continue;
136 1.1 lukem }
137 1.1 lukem *proto++ = '\0';
138 1.1 lukem while ((aliases = strsep(&cp, " \t")) != NULL &&
139 1.1 lukem *aliases == '\0')
140 1.1 lukem ;
141 1.1 lukem
142 1.1 lukem /* key `port/proto', data = full line */
143 1.1 lukem key.size = snprintf(keyb, sizeof(keyb), "%s/%s",
144 1.1 lukem port, proto) + 1;
145 1.1 lukem data.size = snprintf(datab, sizeof(datab), "%s %s/%s %s",
146 1.1 lukem name, port, proto, aliases == NULL ? "" : aliases) + 1;
147 1.1 lukem switch ((db->put)(db, &key, &data, R_NOOVERWRITE)) {
148 1.1 lukem case 0:
149 1.1 lukem break;
150 1.1 lukem case 1:
151 1.1 lukem warnx("%s line %d: duplicate service `%s'",
152 1.1 lukem fname, line, keyb);
153 1.1 lukem break;
154 1.1 lukem case -1:
155 1.1 lukem err(1, "put");
156 1.1 lukem break;
157 1.1 lukem default:
158 1.1 lukem abort();
159 1.1 lukem break;
160 1.1 lukem }
161 1.1 lukem
162 1.1 lukem /* key `port/.', data = full line */
163 1.1 lukem key.size = snprintf(keyb, sizeof(keyb), "%s/.", port) + 1;
164 1.1 lukem if ((db->put)(db, &key, &data, R_NOOVERWRITE) == -1)
165 1.1 lukem err(1, "put");
166 1.1 lukem
167 1.1 lukem
168 1.1 lukem /* build list of aliases */
169 1.1 lukem cp = aliases;
170 1.1 lukem aliasv[0] = name;
171 1.1 lukem for (aliasc = 1;
172 1.1 lukem (aliasv[aliasc] = strsep(&cp, " \t")) != NULL;) {
173 1.1 lukem if (aliasv[aliasc][0] == '\0')
174 1.1 lukem continue;
175 1.1 lukem if (++aliasc > MAXALIASES)
176 1.1 lukem break;
177 1.1 lukem }
178 1.1 lukem aliasv[aliasc] = NULL;
179 1.1 lukem
180 1.1 lukem /* add references for service and all aliases */
181 1.1 lukem for (i = 0; i < aliasc; i++) {
182 1.1 lukem
183 1.1 lukem /* key `service/proto', data = `.port/proto' */
184 1.1 lukem key.size = snprintf(keyb, sizeof(keyb), "%s/%s",
185 1.1 lukem aliasv[i], proto) + 1;
186 1.1 lukem data.size = snprintf(datab, sizeof(datab), ".%s/%s",
187 1.1 lukem port, proto) + 1;
188 1.1 lukem switch ((db->put)(db, &key, &data, R_NOOVERWRITE)) {
189 1.1 lukem case 0:
190 1.1 lukem break;
191 1.1 lukem case 1:
192 1.1 lukem warnx("%s line %d: duplicate service `%s'",
193 1.1 lukem fname, line, keyb);
194 1.1 lukem break;
195 1.1 lukem case -1:
196 1.1 lukem err(1, "put");
197 1.1 lukem break;
198 1.1 lukem default:
199 1.1 lukem abort();
200 1.1 lukem break;
201 1.1 lukem }
202 1.1 lukem
203 1.1 lukem /* key `service/.', data = `.port/.' */
204 1.1 lukem key.size = snprintf(keyb, sizeof(keyb), "%s/.",
205 1.1 lukem aliasv[i]) + 1;
206 1.1 lukem data.size = snprintf(datab, sizeof(datab), ".%s/.",
207 1.1 lukem port) + 1;
208 1.1 lukem if ((db->put)(db, &key, &data, R_NOOVERWRITE) == -1)
209 1.1 lukem err(1, "put");
210 1.1 lukem }
211 1.1 lukem }
212 1.1 lukem
213 1.1 lukem if ((db->close)(db))
214 1.1 lukem err(1, "Error closing temporary database `%s'", tname);
215 1.1 lukem if (rename(tname, dbname) == -1)
216 1.1 lukem err(1, "Cannot rename `%s' to `%s'", tname, dbname);
217 1.1 lukem
218 1.1 lukem return (0);
219 1.1 lukem }
220 1.1 lukem
221 1.1 lukem /*
222 1.1 lukem * cleanup(): Remove temporary files upon exit
223 1.1 lukem */
224 1.1 lukem void
225 1.1 lukem cleanup()
226 1.1 lukem {
227 1.1 lukem char tname[MAXPATHLEN];
228 1.1 lukem (void) snprintf(tname, sizeof(tname), "%s.tmp", dbname);
229 1.1 lukem (void) unlink(tname);
230 1.1 lukem }
231 1.1 lukem
232 1.1 lukem void
233 1.1 lukem usage()
234 1.1 lukem {
235 1.1 lukem extern const char *__progname;
236 1.1 lukem
237 1.1 lukem fprintf(stderr, "usage: %s [-o db]\n", __progname);
238 1.1 lukem exit(1);
239 1.1 lukem }
240