makedbm.c revision 1.16 1 1.16 lukem /* $NetBSD: makedbm.c,v 1.16 1999/07/25 07:59:48 lukem Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Mats O Jansson
18 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.6 lukem #include <sys/cdefs.h>
35 1.6 lukem #ifndef lint
36 1.16 lukem __RCSID("$NetBSD: makedbm.c,v 1.16 1999/07/25 07:59:48 lukem Exp $");
37 1.6 lukem #endif
38 1.6 lukem
39 1.1 thorpej #include <sys/param.h>
40 1.1 thorpej #include <sys/stat.h>
41 1.1 thorpej
42 1.1 thorpej #include <ctype.h>
43 1.3 thorpej #include <err.h>
44 1.1 thorpej #include <errno.h>
45 1.1 thorpej #include <fcntl.h>
46 1.1 thorpej #include <stdio.h>
47 1.11 kleink #include <stdlib.h>
48 1.7 lukem #include <string.h>
49 1.14 kleink #include <time.h>
50 1.1 thorpej #include <unistd.h>
51 1.12 lukem #include <util.h>
52 1.1 thorpej
53 1.1 thorpej #include <rpc/rpc.h>
54 1.1 thorpej #include <rpc/xdr.h>
55 1.1 thorpej
56 1.1 thorpej #include "protos.h"
57 1.1 thorpej #include "ypdb.h"
58 1.1 thorpej #include "ypdef.h"
59 1.1 thorpej
60 1.4 mikel extern char *__progname; /* from crt0.o */
61 1.1 thorpej
62 1.3 thorpej int main __P((int, char *[]));
63 1.1 thorpej void usage __P((void));
64 1.8 lukem int add_record __P((DBM *, char *, char *, int));
65 1.1 thorpej char *file_date __P((char *));
66 1.1 thorpej void list_database __P((char *));
67 1.1 thorpej void create_database __P((char *, char *, char *, char *,
68 1.1 thorpej char *, char *, int, int, int));
69 1.1 thorpej
70 1.1 thorpej int
71 1.1 thorpej main(argc, argv)
72 1.1 thorpej int argc;
73 1.3 thorpej char *argv[];
74 1.1 thorpej {
75 1.1 thorpej int aflag, uflag, bflag, lflag, sflag;
76 1.1 thorpej char *yp_input_file, *yp_output_file;
77 1.1 thorpej char *yp_master_name, *yp_domain_name;
78 1.1 thorpej char *infile, *outfile;
79 1.1 thorpej int ch;
80 1.1 thorpej
81 1.1 thorpej yp_input_file = yp_output_file = NULL;
82 1.1 thorpej yp_master_name = yp_domain_name = NULL;
83 1.1 thorpej aflag = uflag = bflag = lflag = sflag = 0;
84 1.1 thorpej infile = outfile = NULL;
85 1.1 thorpej
86 1.1 thorpej while ((ch = getopt(argc, argv, "blsui:o:m:d:")) != -1) {
87 1.1 thorpej switch (ch) {
88 1.1 thorpej case 'b':
89 1.1 thorpej bflag = aflag = 1;
90 1.1 thorpej break;
91 1.1 thorpej
92 1.1 thorpej case 'l':
93 1.1 thorpej lflag = aflag = 1;
94 1.1 thorpej break;
95 1.1 thorpej
96 1.1 thorpej case 's':
97 1.1 thorpej sflag = aflag = 1;
98 1.1 thorpej break;
99 1.1 thorpej
100 1.1 thorpej case 'i':
101 1.1 thorpej yp_input_file = optarg;
102 1.1 thorpej aflag = 1;
103 1.1 thorpej break;
104 1.1 thorpej
105 1.1 thorpej case 'o':
106 1.1 thorpej yp_output_file = optarg;
107 1.1 thorpej aflag = 1;
108 1.1 thorpej break;
109 1.1 thorpej
110 1.1 thorpej case 'm':
111 1.1 thorpej yp_master_name = optarg;
112 1.1 thorpej aflag = 1;
113 1.1 thorpej break;
114 1.1 thorpej
115 1.1 thorpej case 'd':
116 1.1 thorpej yp_domain_name = optarg;
117 1.1 thorpej aflag = 1;
118 1.1 thorpej break;
119 1.1 thorpej
120 1.1 thorpej case 'u':
121 1.1 thorpej uflag = 1;
122 1.1 thorpej break;
123 1.1 thorpej
124 1.1 thorpej default:
125 1.1 thorpej usage();
126 1.1 thorpej }
127 1.1 thorpej }
128 1.1 thorpej argc -= optind; argv += optind;
129 1.1 thorpej
130 1.1 thorpej if ((uflag != 0) && (aflag != 0))
131 1.1 thorpej usage();
132 1.1 thorpej else {
133 1.1 thorpej if (uflag != 0) {
134 1.1 thorpej if (argc == 1)
135 1.1 thorpej infile = argv[0];
136 1.1 thorpej else
137 1.1 thorpej usage();
138 1.1 thorpej } else {
139 1.1 thorpej if (argc == 2) {
140 1.1 thorpej infile = argv[0];
141 1.1 thorpej outfile = argv[1];
142 1.1 thorpej } else
143 1.1 thorpej usage();
144 1.1 thorpej }
145 1.1 thorpej }
146 1.1 thorpej
147 1.1 thorpej if (uflag != 0)
148 1.1 thorpej list_database(infile);
149 1.1 thorpej else
150 1.1 thorpej create_database(infile, outfile,
151 1.1 thorpej yp_input_file, yp_output_file, yp_master_name,
152 1.1 thorpej yp_domain_name, bflag, lflag, sflag);
153 1.1 thorpej
154 1.1 thorpej exit(0);
155 1.1 thorpej }
156 1.1 thorpej
157 1.8 lukem int
158 1.1 thorpej add_record(db, str1, str2, check)
159 1.1 thorpej DBM *db;
160 1.1 thorpej char *str1, *str2;
161 1.1 thorpej int check;
162 1.1 thorpej {
163 1.1 thorpej datum key, val;
164 1.1 thorpej int status;
165 1.1 thorpej
166 1.1 thorpej key.dptr = str1;
167 1.1 thorpej key.dsize = strlen(str1);
168 1.1 thorpej
169 1.1 thorpej if (check) {
170 1.1 thorpej val = ypdb_fetch(db, key);
171 1.1 thorpej
172 1.1 thorpej if (val.dptr != NULL)
173 1.8 lukem return 0; /* already there */
174 1.1 thorpej }
175 1.1 thorpej val.dptr = str2;
176 1.1 thorpej val.dsize = strlen(str2);
177 1.1 thorpej status = ypdb_store(db, key, val, YPDB_INSERT);
178 1.1 thorpej
179 1.8 lukem if (status != 0) {
180 1.8 lukem warnx("can't store `%s %s'", str1, str2);
181 1.8 lukem return -1;
182 1.8 lukem }
183 1.8 lukem return 0;
184 1.1 thorpej }
185 1.1 thorpej
186 1.1 thorpej char *
187 1.1 thorpej file_date(filename)
188 1.1 thorpej char *filename;
189 1.1 thorpej {
190 1.1 thorpej struct stat finfo;
191 1.1 thorpej static char datestr[11];
192 1.1 thorpej
193 1.1 thorpej memset(datestr, 0, sizeof(datestr));
194 1.1 thorpej
195 1.1 thorpej if (strcmp(filename, "-") == 0)
196 1.1 thorpej snprintf(datestr, sizeof(datestr), "%010d",
197 1.1 thorpej (int)time(NULL));
198 1.1 thorpej else {
199 1.1 thorpej if (stat(filename, &finfo) != 0)
200 1.1 thorpej err(1, "can't stat %s", filename);
201 1.1 thorpej snprintf(datestr, sizeof(datestr), "%010d",
202 1.1 thorpej (int)finfo.st_mtime);
203 1.1 thorpej }
204 1.1 thorpej
205 1.1 thorpej return datestr;
206 1.1 thorpej }
207 1.1 thorpej
208 1.1 thorpej void
209 1.1 thorpej list_database(database)
210 1.1 thorpej char *database;
211 1.1 thorpej {
212 1.1 thorpej DBM *db;
213 1.1 thorpej datum key, val;
214 1.1 thorpej
215 1.1 thorpej db = ypdb_open(database, O_RDONLY, 0444);
216 1.1 thorpej if (db == NULL)
217 1.5 lukem err(1, "can't open database `%s'", database);
218 1.1 thorpej
219 1.1 thorpej key = ypdb_firstkey(db);
220 1.1 thorpej
221 1.1 thorpej while (key.dptr != NULL) {
222 1.1 thorpej val = ypdb_fetch(db, key);
223 1.6 lukem /* workround trailing \0 in aliases.db */
224 1.6 lukem if (key.dptr[key.dsize - 1] == '\0')
225 1.6 lukem key.dsize--;
226 1.8 lukem printf("%*.*s", key.dsize, key.dsize, key.dptr);
227 1.8 lukem if (val.dsize > 0) {
228 1.8 lukem if (val.dptr[val.dsize - 1] == '\0')
229 1.8 lukem val.dsize--;
230 1.8 lukem printf(" %*.*s", val.dsize, val.dsize, val.dptr);
231 1.8 lukem }
232 1.8 lukem printf("\n");
233 1.1 thorpej key = ypdb_nextkey(db);
234 1.1 thorpej }
235 1.1 thorpej
236 1.1 thorpej ypdb_close(db);
237 1.1 thorpej }
238 1.1 thorpej
239 1.1 thorpej void
240 1.1 thorpej create_database(infile, database, yp_input_file, yp_output_file,
241 1.1 thorpej yp_master_name, yp_domain_name, bflag, lflag, sflag)
242 1.1 thorpej char *infile, *database, *yp_input_file, *yp_output_file;
243 1.1 thorpej char *yp_master_name, *yp_domain_name;
244 1.1 thorpej int bflag, lflag, sflag;
245 1.1 thorpej {
246 1.1 thorpej FILE *data_file;
247 1.1 thorpej char myname[MAXHOSTNAMELEN];
248 1.13 kleink size_t line_no = 0;
249 1.10 thorpej size_t len;
250 1.1 thorpej char *p, *k, *v, *slash;
251 1.1 thorpej DBM *new_db;
252 1.1 thorpej static char mapname[] = "ypdbXXXXXX";
253 1.1 thorpej char db_mapname[MAXPATHLEN + 1], db_outfile[MAXPATHLEN + 1];
254 1.1 thorpej char db_tempname[MAXPATHLEN + 1];
255 1.1 thorpej char empty_str[] = "";
256 1.1 thorpej
257 1.1 thorpej memset(db_mapname, 0, sizeof(db_mapname));
258 1.1 thorpej memset(db_outfile, 0, sizeof(db_outfile));
259 1.1 thorpej memset(db_tempname, 0, sizeof(db_tempname));
260 1.1 thorpej
261 1.1 thorpej if (strcmp(infile, "-") == 0)
262 1.1 thorpej data_file = stdin;
263 1.1 thorpej else {
264 1.1 thorpej data_file = fopen(infile, "r");
265 1.1 thorpej if (data_file == NULL)
266 1.1 thorpej err(1, "can't open `%s'", infile);
267 1.1 thorpej }
268 1.1 thorpej
269 1.1 thorpej if (strlen(database) + strlen(YPDB_SUFFIX) > MAXPATHLEN)
270 1.8 lukem errx(1, "file name `%s' too long", database);
271 1.1 thorpej
272 1.1 thorpej snprintf(db_outfile, sizeof(db_outfile), "%s%s", database, YPDB_SUFFIX);
273 1.1 thorpej
274 1.1 thorpej slash = strrchr(database, '/');
275 1.1 thorpej if (slash != NULL)
276 1.1 thorpej slash[1] = '\0'; /* truncate to dir */
277 1.1 thorpej else
278 1.8 lukem *database = '\0'; /* eliminate */
279 1.1 thorpej
280 1.1 thorpej /* NOTE: database is now directory where map goes ! */
281 1.1 thorpej
282 1.1 thorpej if (strlen(database) + strlen(mapname) +
283 1.1 thorpej strlen(YPDB_SUFFIX) > MAXPATHLEN)
284 1.5 lukem errx(1, "directory name `%s' too long", database);
285 1.1 thorpej
286 1.1 thorpej snprintf(db_tempname, sizeof(db_tempname), "%s%s",
287 1.1 thorpej database, mapname);
288 1.15 mrg mktemp(db_tempname); /* OK */
289 1.1 thorpej snprintf(db_mapname, sizeof(db_mapname), "%s%s",
290 1.1 thorpej db_tempname, YPDB_SUFFIX);
291 1.1 thorpej
292 1.2 lukem new_db = ypdb_open(db_tempname, O_RDWR | O_CREAT | O_EXCL, 0644);
293 1.1 thorpej if (new_db == NULL)
294 1.5 lukem err(1, "can't create temp database `%s'", db_tempname);
295 1.1 thorpej
296 1.12 lukem for (;
297 1.12 lukem (p = fparseln(data_file, &len, &line_no, NULL, FPARSELN_UNESCALL));
298 1.12 lukem free(p)) {
299 1.16 lukem k = p; /* set start of key */
300 1.16 lukem while (*k && isspace(*k)) /* skip leading whitespace */
301 1.16 lukem k++;
302 1.8 lukem
303 1.16 lukem if (! *k)
304 1.8 lukem continue;
305 1.8 lukem
306 1.16 lukem v = k;
307 1.16 lukem while (*v && !isspace(*v)) { /* find leading whitespace */
308 1.16 lukem /* convert key to lower case if forcing. */
309 1.16 lukem if (lflag && isupper(*v))
310 1.16 lukem *v = tolower(*v);
311 1.16 lukem v++;
312 1.8 lukem }
313 1.16 lukem while (*v && isspace(*v)) /* replace space with <NUL> */
314 1.16 lukem *v++ = '\0';
315 1.1 thorpej
316 1.8 lukem if (add_record(new_db, k, v, TRUE)) { /* save record */
317 1.8 lukem bad_record:
318 1.8 lukem ypdb_close(new_db);
319 1.8 lukem unlink(db_mapname);
320 1.13 kleink errx(1, "error adding record for line %lu",
321 1.13 kleink (unsigned long)line_no);
322 1.8 lukem }
323 1.1 thorpej }
324 1.1 thorpej
325 1.1 thorpej if (strcmp(infile, "-") != 0)
326 1.1 thorpej (void) fclose(data_file);
327 1.1 thorpej
328 1.8 lukem if (add_record(new_db, YP_LAST_KEY, file_date(infile), FALSE))
329 1.8 lukem goto bad_record;
330 1.1 thorpej
331 1.9 lukem if (yp_input_file) {
332 1.8 lukem if (add_record(new_db, YP_INPUT_KEY, yp_input_file, FALSE))
333 1.8 lukem goto bad_record;
334 1.9 lukem }
335 1.1 thorpej
336 1.9 lukem if (yp_output_file) {
337 1.8 lukem if (add_record(new_db, YP_OUTPUT_KEY, yp_output_file, FALSE))
338 1.8 lukem goto bad_record;
339 1.9 lukem }
340 1.1 thorpej
341 1.9 lukem if (yp_master_name) {
342 1.8 lukem if (add_record(new_db, YP_MASTER_KEY, yp_master_name, FALSE))
343 1.8 lukem goto bad_record;
344 1.9 lukem } else {
345 1.1 thorpej localhostname(myname, sizeof(myname) - 1);
346 1.8 lukem if (add_record(new_db, YP_MASTER_KEY, myname, FALSE))
347 1.8 lukem goto bad_record;
348 1.1 thorpej }
349 1.1 thorpej
350 1.9 lukem if (yp_domain_name) {
351 1.8 lukem if (add_record(new_db, YP_DOMAIN_KEY, yp_domain_name, FALSE))
352 1.8 lukem goto bad_record;
353 1.9 lukem }
354 1.1 thorpej
355 1.9 lukem if (bflag) {
356 1.8 lukem if (add_record(new_db, YP_INTERDOMAIN_KEY, empty_str, FALSE))
357 1.8 lukem goto bad_record;
358 1.9 lukem }
359 1.1 thorpej
360 1.9 lukem if (sflag) {
361 1.8 lukem if (add_record(new_db, YP_SECURE_KEY, empty_str, FALSE))
362 1.8 lukem goto bad_record;
363 1.9 lukem }
364 1.1 thorpej
365 1.1 thorpej ypdb_close(new_db);
366 1.8 lukem if (rename(db_mapname, db_outfile) < 0) {
367 1.8 lukem unlink(db_mapname);
368 1.1 thorpej err(1, "rename `%s' -> `%s'", db_mapname, db_outfile);
369 1.8 lukem }
370 1.1 thorpej }
371 1.1 thorpej
372 1.1 thorpej void
373 1.1 thorpej usage()
374 1.1 thorpej {
375 1.1 thorpej
376 1.4 mikel fprintf(stderr, "usage: %s -u file\n", __progname);
377 1.1 thorpej fprintf(stderr, " %s [-lbs] %s\n", __progname,
378 1.1 thorpej "[-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]");
379 1.3 thorpej fprintf(stderr, " %s infile outfile\n",
380 1.1 thorpej "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME]");
381 1.1 thorpej exit(1);
382 1.1 thorpej }
383