makedbm.c revision 1.2 1 /* $NetBSD: makedbm.c,v 1.2 1997/03/22 03:32:36 lukem 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/param.h>
35 #include <sys/stat.h>
36
37 #include <ctype.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <strings.h>
42 #include <unistd.h>
43
44 #include <rpc/rpc.h>
45 #include <rpc/xdr.h>
46
47 #include "protos.h"
48 #include "ypdb.h"
49 #include "ypdef.h"
50
51 extern char *__progname; /* from crt0.s */
52 extern char *optarg;
53 extern int optind;
54
55 void usage __P((void));
56 void add_record __P((DBM *, char *, char *, int));
57 char *file_date __P((char *));
58 void list_database __P((char *));
59 void create_database __P((char *, char *, char *, char *,
60 char *, char *, int, int, int));
61
62 int
63 main(argc, argv)
64 int argc;
65 char **argv;
66 {
67 int aflag, uflag, bflag, lflag, sflag;
68 char *yp_input_file, *yp_output_file;
69 char *yp_master_name, *yp_domain_name;
70 char *infile, *outfile;
71 int ch;
72
73 yp_input_file = yp_output_file = NULL;
74 yp_master_name = yp_domain_name = NULL;
75 aflag = uflag = bflag = lflag = sflag = 0;
76 infile = outfile = NULL;
77
78 while ((ch = getopt(argc, argv, "blsui:o:m:d:")) != -1) {
79 switch (ch) {
80 case 'b':
81 bflag = aflag = 1;
82 break;
83
84 case 'l':
85 lflag = aflag = 1;
86 break;
87
88 case 's':
89 sflag = aflag = 1;
90 break;
91
92 case 'i':
93 yp_input_file = optarg;
94 aflag = 1;
95 break;
96
97 case 'o':
98 yp_output_file = optarg;
99 aflag = 1;
100 break;
101
102 case 'm':
103 yp_master_name = optarg;
104 aflag = 1;
105 break;
106
107 case 'd':
108 yp_domain_name = optarg;
109 aflag = 1;
110 break;
111
112 case 'u':
113 uflag = 1;
114 break;
115
116 default:
117 usage();
118 }
119 }
120 argc -= optind; argv += optind;
121
122 if ((uflag != 0) && (aflag != 0))
123 usage();
124 else {
125 if (uflag != 0) {
126 if (argc == 1)
127 infile = argv[0];
128 else
129 usage();
130 } else {
131 if (argc == 2) {
132 infile = argv[0];
133 outfile = argv[1];
134 } else
135 usage();
136 }
137 }
138
139 if (uflag != 0)
140 list_database(infile);
141 else
142 create_database(infile, outfile,
143 yp_input_file, yp_output_file, yp_master_name,
144 yp_domain_name, bflag, lflag, sflag);
145
146 exit(0);
147 }
148
149 void
150 add_record(db, str1, str2, check)
151 DBM *db;
152 char *str1, *str2;
153 int check;
154 {
155 datum key, val;
156 int status;
157
158 key.dptr = str1;
159 key.dsize = strlen(str1);
160
161 if (check) {
162 val = ypdb_fetch(db, key);
163
164 if (val.dptr != NULL)
165 return; /* already there */
166 }
167 val.dptr = str2;
168 val.dsize = strlen(str2);
169 status = ypdb_store(db, key, val, YPDB_INSERT);
170
171 if (status != 0)
172 errx(1, "can't store `%s %s'", str1, str2);
173 }
174
175 char *
176 file_date(filename)
177 char *filename;
178 {
179 struct stat finfo;
180 static char datestr[11];
181
182 memset(datestr, 0, sizeof(datestr));
183
184 if (strcmp(filename, "-") == 0)
185 snprintf(datestr, sizeof(datestr), "%010d",
186 (int)time(NULL));
187 else {
188 if (stat(filename, &finfo) != 0)
189 err(1, "can't stat %s", filename);
190 snprintf(datestr, sizeof(datestr), "%010d",
191 (int)finfo.st_mtime);
192 }
193
194 return datestr;
195 }
196
197 void
198 list_database(database)
199 char *database;
200 {
201 DBM *db;
202 datum key, val;
203
204 db = ypdb_open(database, O_RDONLY, 0444);
205 if (db == NULL)
206 errx(1, "can't open database `%s'", database);
207
208 key = ypdb_firstkey(db);
209
210 while (key.dptr != NULL) {
211 val = ypdb_fetch(db, key);
212 printf("%*.*s %*.*s\n",
213 key.dsize, key.dsize, key.dptr,
214 val.dsize, val.dsize, val.dptr);
215 key = ypdb_nextkey(db);
216 }
217
218 ypdb_close(db);
219 }
220
221 void
222 create_database(infile, database, yp_input_file, yp_output_file,
223 yp_master_name, yp_domain_name, bflag, lflag, sflag)
224 char *infile, *database, *yp_input_file, *yp_output_file;
225 char *yp_master_name, *yp_domain_name;
226 int bflag, lflag, sflag;
227 {
228 FILE *data_file;
229 char data_line[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
230 char myname[MAXHOSTNAMELEN];
231 int line_no = 0;
232 int len;
233 char *p, *k, *v, *slash;
234 DBM *new_db;
235 static char mapname[] = "ypdbXXXXXX";
236 char db_mapname[MAXPATHLEN + 1], db_outfile[MAXPATHLEN + 1];
237 char db_tempname[MAXPATHLEN + 1];
238 char empty_str[] = "";
239
240 memset(db_mapname, 0, sizeof(db_mapname));
241 memset(db_outfile, 0, sizeof(db_outfile));
242 memset(db_tempname, 0, sizeof(db_tempname));
243
244 if (strcmp(infile, "-") == 0)
245 data_file = stdin;
246 else {
247 data_file = fopen(infile, "r");
248 if (data_file == NULL)
249 err(1, "can't open `%s'", infile);
250 }
251
252 if (strlen(database) + strlen(YPDB_SUFFIX) > MAXPATHLEN)
253 errx(1, "%s: file name too long\n", database);
254
255 snprintf(db_outfile, sizeof(db_outfile), "%s%s", database, YPDB_SUFFIX);
256
257 slash = strrchr(database, '/');
258 if (slash != NULL)
259 slash[1] = '\0'; /* truncate to dir */
260 else
261 *database = '\0'; /* elminate */
262
263 /* NOTE: database is now directory where map goes ! */
264
265 if (strlen(database) + strlen(mapname) +
266 strlen(YPDB_SUFFIX) > MAXPATHLEN)
267 errx(1, "%s: directory name too long", database);
268
269 snprintf(db_tempname, sizeof(db_tempname), "%s%s",
270 database, mapname);
271 mktemp(db_tempname);
272 snprintf(db_mapname, sizeof(db_mapname), "%s%s",
273 db_tempname, YPDB_SUFFIX);
274
275 new_db = ypdb_open(db_tempname, O_RDWR | O_CREAT | O_EXCL, 0644);
276 if (new_db == NULL)
277 errx(1, "can't create temp database `%s'", db_tempname);
278
279 while (read_line(data_file, data_line, sizeof(data_line))) {
280 line_no++;
281 len = strlen(data_line);
282
283 /* Check if we have the whole line */
284
285 if (data_line[len - 1] != '\n')
286 warnx("%s: line %d too long", infile, line_no);
287 else
288 data_line[len - 1] = '\0';
289
290 p = (char *)&data_line;
291
292 k = p; /* save start of key */
293 while (!isspace(*p)) { /* find first "space" */
294 /*
295 * Convert to lower case if forcing.
296 */
297 if (lflag && isupper(*p))
298 *p = tolower(*p);
299 p++;
300 }
301 while (isspace(*p)) /* replace space with <NUL> */
302 *p++ = '\0';
303
304 v = p; /* save start of value */
305
306 while (*p != '\0') /* find end of string */
307 p++;
308
309 add_record(new_db, k, v, TRUE); /* save record */
310 }
311
312 if (strcmp(infile, "-") != 0)
313 (void) fclose(data_file);
314
315 add_record(new_db, YP_LAST_KEY, file_date(infile), FALSE);
316
317 if (yp_input_file)
318 add_record(new_db, YP_INPUT_KEY, yp_input_file, FALSE);
319
320 if (yp_output_file)
321 add_record(new_db, YP_OUTPUT_KEY, yp_output_file, FALSE);
322
323 if (yp_master_name)
324 add_record(new_db, YP_MASTER_KEY, yp_master_name, FALSE);
325 else {
326 localhostname(myname, sizeof(myname) - 1);
327 add_record(new_db, YP_MASTER_KEY, myname, FALSE);
328 }
329
330 if (yp_domain_name)
331 add_record(new_db, YP_DOMAIN_KEY, yp_domain_name, FALSE);
332
333 if (bflag)
334 add_record(new_db, YP_INTERDOMAIN_KEY, empty_str, FALSE);
335
336 if (sflag)
337 add_record(new_db, YP_SECURE_KEY, empty_str, FALSE);
338
339 ypdb_close(new_db);
340 if (rename(db_mapname, db_outfile) < 0)
341 err(1, "rename `%s' -> `%s'", db_mapname, db_outfile);
342 }
343
344 void
345 usage()
346 {
347
348 fprintf(stderr, "usage: %s -u file", __progname);
349 fprintf(stderr, " %s [-lbs] %s\n", __progname,
350 "[-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]");
351 fprintf(stderr, " %s infile outfile\n", __progname,
352 "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME]");
353 exit(1);
354 }
355