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