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