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