yp_mkdb.c revision 15424
1/*
2 * Copyright (c) 1995, 1996
3 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	$Id: yp_mkdb.c,v 1.4 1996/03/26 05:32:14 wpaul Exp wpaul $
33 */
34
35#include <stdio.h>
36#include <string.h>
37#include <fcntl.h>
38#include <sys/param.h>
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <limits.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <errno.h>
45#include <time.h>
46#include <db.h>
47#include <err.h>
48#include <rpc/rpc.h>
49#include <rpcsvc/yp.h>
50#include "yp_extern.h"
51
52#ifndef lint
53static const char rcsid[] = "$Id: yp_mkdb.c,v 1.4 1996/03/26 05:32:14 wpaul Exp wpaul $";
54#endif
55
56char *yp_dir = "";	/* No particular default needed. */
57char *progname = "yp_mkdb";
58int _rpcpmstart = 0;
59int debug = 1;
60
61static void usage()
62{
63	fprintf(stderr, "usage: %s -c\n", progname);
64	fprintf(stderr, "usage: %s -u dbname\n", progname);
65	fprintf(stderr, "usage: %s [-c] [-i inputfile] [-o outputfile]\n",
66								progname);
67	fprintf(stderr, "               [-d domainname ] [-m mastername] \
68inputfile dbname\n");
69	exit(1);
70}
71
72#define PERM_SECURE (S_IRUSR|S_IWUSR)
73
74static DB *open_db(path, flags)
75	char *path;
76	int flags;
77{
78	extern HASHINFO openinfo;
79
80	return(dbopen(path, flags, PERM_SECURE, DB_HASH, &openinfo));
81}
82
83static void unwind(map)
84	char *map;
85{
86	DB *dbp;
87	DBT key, data;
88
89	dbp = open_db(map, O_RDONLY);
90
91	if (dbp == NULL)
92		err(1, "open_db(%s) failed", map);
93
94	key.data = NULL;
95	while(yp_next_record(dbp, &key, &data, 1, 1) == YP_TRUE)
96		printf("%.*s %.*s\n", key.size,key.data,data.size,data.data);
97
98	(void)(dbp->close)(dbp);
99	return;
100}
101
102main (argc, argv)
103	int argc;
104	char *argv[];
105{
106	int ch;
107	int un = 0;
108	int clear = 0;
109	char *infile = NULL;
110	char *map = NULL;
111	char *domain = NULL;
112	char *infilename = NULL;
113	char *outfilename = NULL;
114	char *mastername = NULL;
115	DB *dbp;
116	DBT key, data;
117	char buf[10240];
118	char *keybuf, *datbuf;
119	FILE *ifp;
120	char hname[MAXHOSTNAMELEN + 2];
121
122	while ((ch = getopt(argc, argv, "uhcd:i:o:m:")) != EOF) {
123		switch(ch) {
124		case 'u':
125			un++;
126			break;
127		case 'c':
128			clear++;
129			break;
130		case 'd':
131			domain = optarg;
132			break;
133		case 'i':
134			infilename = optarg;
135			break;
136		case 'o':
137			outfilename = optarg;
138			break;
139		case 'm':
140			mastername = optarg;
141			break;
142		case 'h':
143		default:
144			usage();
145			break;
146		}
147	}
148
149	argc -= optind;
150	argv += optind;
151
152	if (un) {
153		map = argv[0];
154		if (map == NULL)
155			usage();
156		unwind(map);
157		exit(0);
158
159	}
160
161	infile = argv[0];
162	map = argv[1];
163
164	if (infile == NULL || map == NULL) {
165		if (clear)
166			goto doclear;
167		usage();
168	}
169
170	if (mastername == NULL) {
171		if (gethostname((char *)&hname, sizeof(hname)) == -1)
172			err(1, "gethostname() failed");
173		mastername = (char *)&hname;
174	}
175
176	/*
177	 * Note that while we can read from stdin, we can't
178	 * write to stdout; the db library doesn't let you
179	 * write to a file stream like that.
180	 */
181
182	if (!strcmp(infile, "-")) {
183		ifp = stdin;
184	} else {
185		if ((ifp = fopen(infile, "r")) == NULL)
186			err(1, "failed to open %s", infile);
187	}
188
189	if ((dbp = open_db(map, O_RDWR|O_EXLOCK|O_EXCL|O_CREAT)) == NULL)
190		err(1, "open_db(%s) failed", map);
191
192	key.data = "YP_MASTER_NAME";
193	key.size = sizeof("YP_MASTER_NAME") - 1;
194	data.data = mastername;
195	data.size = strlen(mastername);
196	yp_put_record(dbp, &key, &data);
197
198	key.data = "YP_LAST_MODIFIED";
199	key.size = sizeof("YP_LAST_MODIFIED") - 1;
200	snprintf(buf, sizeof(buf), "%lu", time(NULL));
201	data.data = &buf;
202	data.size = strlen(buf);
203	yp_put_record(dbp, &key, &data);
204
205	if (infilename) {
206		key.data = "YP_INPUT_FILE";
207		key.size = sizeof("YP_INPUT_FILE") - 1;
208		data.data = infilename;
209		data.size = strlen(infilename);
210		yp_put_record(dbp, &key, &data);
211	}
212
213	if (outfilename) {
214		key.data = "YP_OUTPUT_FILE";
215		key.size = sizeof("YP_OUTPUT_FILE") - 1;
216		data.data = outfilename;
217		data.size = strlen(outfilename);
218		yp_put_record(dbp, &key, &data);
219	}
220
221	if (domain) {
222		key.data = "YP_DOMAIN_NAME";
223		key.size = sizeof("YP_DOMAIN_NAME") - 1;
224		data.data = domain;
225		data.size = strlen(domain);
226		yp_put_record(dbp, &key, &data);
227	}
228
229	while(fgets((char *)&buf, sizeof(buf), ifp)) {
230		char *sep = NULL;
231		int rval;
232
233		/* NUL terminate */
234		if ((sep = strchr(buf, '\n')))
235			*sep = '\0';
236
237		/* handle backslash line continuations */
238		while(buf[strlen(buf) - 1] == '\\') {
239			fgets((char *)&buf[strlen(buf) - 1],
240					sizeof(buf) - strlen(buf), ifp);
241			if ((sep = strchr(buf, '\n')))
242				*sep = '\0';
243		}
244
245		/* find the separation between the key and data */
246		if ((sep = strpbrk(buf, " \t")) == NULL) {
247			warnx("bad input -- no white space: %s", buf);
248			continue;
249		}
250
251		/* separate the strings */
252		keybuf = (char *)&buf;
253		datbuf = sep + 1;
254		*sep = '\0';
255
256		/* set datbuf to start at first non-whitespace character */
257		while (*datbuf == ' ' || *datbuf == '\t')
258			datbuf++;
259
260		/* Check for silliness. */
261		if  (*keybuf == '+' || *keybuf == '-' ||
262		     *datbuf == '+' || *datbuf == '-') {
263			warnx("bad character at start of line: %s", buf);
264			continue;
265		}
266
267		if (strlen(keybuf) > YPMAXRECORD) {
268			warnx("key too long: %s", keybuf);
269			continue;
270		}
271
272		if (strlen(datbuf) > YPMAXRECORD) {
273			warnx("data too long: %s", datbuf);
274			continue;
275		}
276
277		key.data = keybuf;
278		key.size = strlen(keybuf);
279		data.data = datbuf;
280		data.size = strlen(datbuf);
281
282		if ((rval = yp_put_record(dbp, &key, &data)) != YP_TRUE) {
283			switch(rval) {
284			case YP_FALSE:
285				warnx("duplicate key '%s' - skipping", keybuf);
286				break;
287			case YP_BADDB:
288			default:
289				err(1,"failed to write new record - exiting");
290				break;
291			}
292		}
293
294	}
295
296	(void)(dbp->close)(dbp);
297
298doclear:
299
300	if (clear) {
301		char in = 0;
302		char *out = NULL;
303		int stat;
304		if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR,
305			xdr_void, (void *)&in,
306			xdr_void, (void *)out)) != RPC_SUCCESS) {
307			warnx("failed to send 'clear' to local ypserv: %s",
308				clnt_sperrno((enum clnt_stat) stat));
309		}
310	}
311
312	exit(0);
313}
314