xref: /freebsd/usr.sbin/yp_mkdb/yp_mkdb.c (revision 4a0f765fbf09711e612e86fce8bb09ec43f482d9)
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  *	$FreeBSD$
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 <err.h>
47 #include <rpc/rpc.h>
48 #include <rpcsvc/yp.h>
49 #include "yp_extern.h"
50 #include "ypxfr_extern.h"
51 
52 #ifndef lint
53 static const char rcsid[] = "$FreeBSD$";
54 #endif
55 
56 char *yp_dir = "";	/* No particular default needed. */
57 char *progname = "yp_mkdb";
58 int _rpcpmstart = 0;
59 int debug = 1;
60 
61 static 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] [-b] [-s] [-i inputfile] \
66 [-o outputfile]\n", progname);
67 	fprintf(stderr, "               [-d domainname ] [-m mastername] \
68 inputfile dbname\n");
69 	exit(1);
70 }
71 
72 #define PERM_SECURE (S_IRUSR|S_IWUSR)
73 
74 static 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 
83 static 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 
102 main (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 	int interdom = 0;
116 	int secure = 0;
117 	DB *dbp;
118 	DBT key, data;
119 	char buf[10240];
120 	char *keybuf, *datbuf;
121 	FILE *ifp;
122 	char hname[MAXHOSTNAMELEN + 2];
123 
124 	while ((ch = getopt(argc, argv, "uhcbsd:i:o:m:")) != EOF) {
125 		switch(ch) {
126 		case 'u':
127 			un++;
128 			break;
129 		case 'c':
130 			clear++;
131 			break;
132 		case 'b':
133 			interdom++;
134 			break;
135 		case 's':
136 			secure++;
137 			break;
138 		case 'd':
139 			domain = optarg;
140 			break;
141 		case 'i':
142 			infilename = optarg;
143 			break;
144 		case 'o':
145 			outfilename = optarg;
146 			break;
147 		case 'm':
148 			mastername = optarg;
149 			break;
150 		case 'h':
151 		default:
152 			usage();
153 			break;
154 		}
155 	}
156 
157 	argc -= optind;
158 	argv += optind;
159 
160 	if (un) {
161 		map = argv[0];
162 		if (map == NULL)
163 			usage();
164 		unwind(map);
165 		exit(0);
166 
167 	}
168 
169 	infile = argv[0];
170 	map = argv[1];
171 
172 	if (infile == NULL || map == NULL) {
173 		if (clear)
174 			goto doclear;
175 		usage();
176 	}
177 
178 	if (mastername == NULL) {
179 		if (gethostname((char *)&hname, sizeof(hname)) == -1)
180 			err(1, "gethostname() failed");
181 		mastername = (char *)&hname;
182 	}
183 
184 	/*
185 	 * Note that while we can read from stdin, we can't
186 	 * write to stdout; the db library doesn't let you
187 	 * write to a file stream like that.
188 	 */
189 
190 	if (!strcmp(infile, "-")) {
191 		ifp = stdin;
192 	} else {
193 		if ((ifp = fopen(infile, "r")) == NULL)
194 			err(1, "failed to open %s", infile);
195 	}
196 
197 	if ((dbp = open_db(map, O_RDWR|O_EXLOCK|O_EXCL|O_CREAT)) == NULL)
198 		err(1, "open_db(%s) failed", map);
199 
200 	if (interdom) {
201 		key.data = "YP_INTERDOMAIN";
202 		key.size = sizeof("YP_INTERDOMAIN") - 1;
203 		data.data = "";
204 		data.size = 0;
205 		yp_put_record(dbp, &key, &data, 0);
206 	}
207 
208 	if (secure) {
209 		key.data = "YP_SECURE";
210 		key.size = sizeof("YP_SECURE") - 1;
211 		data.data = "";
212 		data.size = 0;
213 		yp_put_record(dbp, &key, &data, 0);
214 	}
215 
216 	key.data = "YP_MASTER_NAME";
217 	key.size = sizeof("YP_MASTER_NAME") - 1;
218 	data.data = mastername;
219 	data.size = strlen(mastername);
220 	yp_put_record(dbp, &key, &data, 0);
221 
222 	key.data = "YP_LAST_MODIFIED";
223 	key.size = sizeof("YP_LAST_MODIFIED") - 1;
224 	snprintf(buf, sizeof(buf), "%lu", time(NULL));
225 	data.data = (char *)&buf;
226 	data.size = strlen(buf);
227 	yp_put_record(dbp, &key, &data, 0);
228 
229 	if (infilename) {
230 		key.data = "YP_INPUT_FILE";
231 		key.size = sizeof("YP_INPUT_FILE") - 1;
232 		data.data = infilename;
233 		data.size = strlen(infilename);
234 		yp_put_record(dbp, &key, &data, 0);
235 	}
236 
237 	if (outfilename) {
238 		key.data = "YP_OUTPUT_FILE";
239 		key.size = sizeof("YP_OUTPUT_FILE") - 1;
240 		data.data = outfilename;
241 		data.size = strlen(outfilename);
242 		yp_put_record(dbp, &key, &data, 0);
243 	}
244 
245 	if (domain) {
246 		key.data = "YP_DOMAIN_NAME";
247 		key.size = sizeof("YP_DOMAIN_NAME") - 1;
248 		data.data = domain;
249 		data.size = strlen(domain);
250 		yp_put_record(dbp, &key, &data, 0);
251 	}
252 
253 	while(fgets((char *)&buf, sizeof(buf), ifp)) {
254 		char *sep = NULL;
255 		int rval;
256 
257 		/* NUL terminate */
258 		if ((sep = strchr(buf, '\n')))
259 			*sep = '\0';
260 
261 		/* handle backslash line continuations */
262 		while(buf[strlen(buf) - 1] == '\\') {
263 			fgets((char *)&buf[strlen(buf) - 1],
264 					sizeof(buf) - strlen(buf), ifp);
265 			if ((sep = strchr(buf, '\n')))
266 				*sep = '\0';
267 		}
268 
269 		/* find the separation between the key and data */
270 		if ((sep = strpbrk(buf, " \t")) == NULL) {
271 			warnx("bad input -- no white space: %s", buf);
272 			continue;
273 		}
274 
275 		/* separate the strings */
276 		keybuf = (char *)&buf;
277 		datbuf = sep + 1;
278 		*sep = '\0';
279 
280 		/* set datbuf to start at first non-whitespace character */
281 		while (*datbuf == ' ' || *datbuf == '\t')
282 			datbuf++;
283 
284 		/* Check for silliness. */
285 		if  (*keybuf == '+' || *keybuf == '-' ||
286 		     *datbuf == '+' || *datbuf == '-') {
287 			warnx("bad character at start of line: %s", buf);
288 			continue;
289 		}
290 
291 		if (strlen(keybuf) > YPMAXRECORD) {
292 			warnx("key too long: %s", keybuf);
293 			continue;
294 		}
295 
296 		if (!strlen(keybuf)) {
297 			warnx("no key -- check source file for blank lines");
298 			continue;
299 		}
300 
301 		if (strlen(datbuf) > YPMAXRECORD) {
302 			warnx("data too long: %s", datbuf);
303 			continue;
304 		}
305 
306 		key.data = keybuf;
307 		key.size = strlen(keybuf);
308 		data.data = datbuf;
309 		data.size = strlen(datbuf);
310 
311 		if ((rval = yp_put_record(dbp, &key, &data, 0)) != YP_TRUE) {
312 			switch(rval) {
313 			case YP_FALSE:
314 				warnx("duplicate key '%s' - skipping", keybuf);
315 				break;
316 			case YP_BADDB:
317 			default:
318 				err(1,"failed to write new record - exiting");
319 				break;
320 			}
321 		}
322 
323 	}
324 
325 	(void)(dbp->close)(dbp);
326 
327 doclear:
328 
329 	if (clear) {
330 		char in = 0;
331 		char *out = NULL;
332 		int stat;
333 		if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR,
334 			xdr_void, (void *)&in,
335 			xdr_void, (void *)out)) != RPC_SUCCESS) {
336 			warnx("failed to send 'clear' to local ypserv: %s",
337 				clnt_sperrno((enum clnt_stat) stat));
338 		}
339 	}
340 
341 	exit(0);
342 }
343