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