ypxfr_main.c revision 90298
1/*
2 * Copyright (c) 1995
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
33#ifndef lint
34static const char rcsid[] =
35  "$FreeBSD: head/libexec/ypxfr/ypxfr_main.c 90298 2002-02-06 15:26:07Z des $";
36#endif /* not lint */
37
38#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <syslog.h>
43#include <unistd.h>
44#include <sys/types.h>
45#include <sys/param.h>
46#include <sys/socket.h>
47#include <netinet/in.h>
48#include <arpa/inet.h>
49#include <rpc/rpc.h>
50#include <rpc/clnt.h>
51#include <rpcsvc/yp.h>
52struct dom_binding {};
53#include <rpcsvc/ypclnt.h>
54#include <rpcsvc/ypxfrd.h>
55#include "ypxfr_extern.h"
56
57char *progname = "ypxfr";
58char *yp_dir = _PATH_YP;
59int _rpcpmstart = 0;
60int ypxfr_use_yplib = 0; /* Assume the worst. */
61int ypxfr_clear = 1;
62int ypxfr_prognum = 0;
63struct sockaddr_in ypxfr_callback_addr;
64struct yppushresp_xfr ypxfr_resp;
65DB *dbp;
66
67static void
68ypxfr_exit(ypxfrstat retval, char *temp)
69{
70	CLIENT *clnt;
71	int sock = RPC_ANYSOCK;
72	struct timeval timeout;
73
74	/* Clean up no matter what happened previously. */
75	if (temp != NULL) {
76		if (dbp != NULL)
77			(void)(dbp->close)(dbp);
78		if (unlink(temp) == -1) {
79			yp_error("failed to unlink %s",strerror(errno));
80		}
81	}
82
83	if (ypxfr_prognum) {
84		timeout.tv_sec = 20;
85		timeout.tv_usec = 0;
86
87		if ((clnt = clntudp_create(&ypxfr_callback_addr, ypxfr_prognum,
88					1, timeout, &sock)) == NULL) {
89			yp_error("%s", clnt_spcreateerror("failed to "
90			    "establish callback handle"));
91			exit(1);
92		}
93
94		ypxfr_resp.status = retval;
95
96		if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
97			yp_error("%s", clnt_sperror(clnt, "callback failed"));
98			clnt_destroy(clnt);
99			exit(1);
100		}
101		clnt_destroy(clnt);
102	} else {
103		yp_error("Exiting: %s", ypxfrerr_string(retval));
104	}
105
106	exit(0);
107}
108
109static void
110usage(void)
111{
112	if (_rpcpmstart) {
113		ypxfr_exit(YPXFR_BADARGS,NULL);
114	} else {
115		fprintf(stderr, "%s\n%s\n%s\n",
116	"usage: ypxfr [-f] [-c] [-d target domain] [-h source host]",
117	"             [-s source domain] [-p path]",
118	"             [-C taskid program-number ipaddr port] mapname");
119		exit(1);
120	}
121}
122
123int
124ypxfr_foreach(int status, char *key, int keylen, char *val, int vallen,
125    char *data)
126{
127	DBT dbkey, dbval;
128
129	if (status != YP_TRUE)
130		return (status);
131
132	/*
133	 * XXX Do not attempt to write zero-length keys or
134	 * data into a Berkeley DB hash database. It causes a
135	 * strange failure mode where sequential searches get
136	 * caught in an infinite loop.
137	 */
138	if (keylen) {
139		dbkey.data = key;
140		dbkey.size = keylen;
141	} else {
142		dbkey.data = "";
143		dbkey.size = 1;
144	}
145	if (vallen) {
146		dbval.data = val;
147		dbval.size = vallen;
148	} else {
149		dbval.data = "";
150		dbval.size = 1;
151	}
152
153	if (yp_put_record(dbp, &dbkey, &dbval, 0) != YP_TRUE)
154		return(yp_errno);
155
156	return (0);
157}
158
159int
160main(int argc, char *argv[])
161{
162	int ch;
163	int ypxfr_force = 0;
164	char *ypxfr_dest_domain = NULL;
165	char *ypxfr_source_host = NULL;
166	char *ypxfr_source_domain = NULL;
167	char *ypxfr_local_domain = NULL;
168	char *ypxfr_master = NULL;
169	unsigned long ypxfr_order = -1, ypxfr_skew_check = -1;
170	char *ypxfr_mapname = NULL;
171	int ypxfr_args = 0;
172	char ypxfr_temp_map[MAXPATHLEN + 2];
173	char tempmap[MAXPATHLEN + 2];
174	char buf[MAXPATHLEN + 2];
175	DBT key, data;
176	int remoteport;
177	int interdom = 0;
178	int secure = 0;
179
180	debug = 1;
181
182	if (!isatty(fileno(stderr))) {
183		openlog("ypxfr", LOG_PID, LOG_DAEMON);
184		_rpcpmstart = 1;
185	}
186
187	if (argc < 2)
188		usage();
189
190	while ((ch = getopt(argc, argv, "fcd:h:s:p:C:")) != -1) {
191		int my_optind;
192		switch (ch) {
193		case 'f':
194			ypxfr_force++;
195			ypxfr_args++;
196			break;
197		case 'c':
198			ypxfr_clear = 0;
199			ypxfr_args++;
200			break;
201		case 'd':
202			ypxfr_dest_domain = optarg;
203			ypxfr_args += 2;
204			break;
205		case 'h':
206			ypxfr_source_host = optarg;
207			ypxfr_args += 2;
208			break;
209		case 's':
210			ypxfr_source_domain = optarg;
211			ypxfr_args += 2;
212			break;
213		case 'p':
214			yp_dir = optarg;
215			ypxfr_args += 2;
216			break;
217		case 'C':
218			/*
219			 * Whoever decided that the -C flag should take
220			 * four arguments is a twit.
221			 */
222			my_optind = optind - 1;
223			if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
224				yp_error("transaction ID not specified");
225				usage();
226			}
227			ypxfr_resp.transid = atol(argv[my_optind]);
228			my_optind++;
229			if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
230				yp_error("RPC program number not specified");
231				usage();
232			}
233			ypxfr_prognum = atol(argv[my_optind]);
234			my_optind++;
235			if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
236				yp_error("address not specified");
237				usage();
238			}
239			if (!inet_aton(argv[my_optind], &ypxfr_callback_addr.sin_addr)) {
240				yp_error("failed to convert '%s' to IP addr",
241					argv[my_optind]);
242				exit(1);
243			}
244			my_optind++;
245			if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
246				yp_error("port not specified");
247				usage();
248			}
249			ypxfr_callback_addr.sin_port = htons((u_short)atoi(argv[my_optind]));
250			ypxfr_args += 5;
251			break;
252		default:
253			usage();
254			break;
255		}
256	}
257
258	ypxfr_mapname = argv[ypxfr_args + 1];
259
260	if (ypxfr_mapname == NULL) {
261		yp_error("no map name specified");
262		usage();
263	}
264
265	/* Always the case. */
266	ypxfr_callback_addr.sin_family = AF_INET;
267
268	/* Determine if local NIS client facilities are turned on. */
269	if (!yp_get_default_domain(&ypxfr_local_domain) &&
270	    _yp_check(&ypxfr_local_domain))
271		ypxfr_use_yplib = 1;
272
273	/*
274	 * If no destination domain is specified, assume that the
275	 * local default domain is to be used and try to obtain it.
276	 * Fails if NIS client facilities are turned off.
277	 */
278	if (ypxfr_dest_domain == NULL) {
279		if (ypxfr_use_yplib) {
280			yp_get_default_domain(&ypxfr_dest_domain);
281		} else {
282			yp_error("no destination domain specified and \
283the local domain name isn't set");
284			ypxfr_exit(YPXFR_BADARGS,NULL);
285		}
286	}
287
288	/*
289	 * If a source domain is not specified, assume it to
290	 * be the same as the destination domain.
291	 */
292	if (ypxfr_source_domain == NULL) {
293		ypxfr_source_domain = ypxfr_dest_domain;
294	}
295
296	/*
297	 * If the source host is not specified, assume it to be the
298	 * master for the specified map. If local NIS client facilities
299	 * are turned on, we can figure this out using yp_master().
300	 * If not, we have to see if a local copy of the map exists
301	 * and extract its YP_MASTER_NAME record. If _that_ fails,
302	 * we are stuck and must ask the user for more information.
303	 */
304	if (ypxfr_source_host == NULL) {
305		if (!ypxfr_use_yplib) {
306		/*
307		 * Double whammy: NIS isn't turned on and the user
308		 * didn't specify a source host.
309		 */
310			char *dptr;
311			key.data = "YP_MASTER_NAME";
312			key.size = sizeof("YP_MASTER_NAME") - 1;
313
314			if (yp_get_record(ypxfr_dest_domain, ypxfr_mapname,
315					 &key, &data, 1) != YP_TRUE) {
316				yp_error("no source host specified");
317				ypxfr_exit(YPXFR_BADARGS,NULL);
318			}
319			dptr = data.data;
320			dptr[data.size] = '\0';
321			ypxfr_master = ypxfr_source_host = strdup(dptr);
322		}
323	} else {
324		if (ypxfr_use_yplib)
325			ypxfr_use_yplib = 0;
326	}
327
328	if (ypxfr_master == NULL) {
329		if ((ypxfr_master = ypxfr_get_master(ypxfr_source_domain,
330					    	 ypxfr_mapname,
331					     	ypxfr_source_host,
332					     	ypxfr_use_yplib)) == NULL) {
333			yp_error("failed to find master of %s in domain %s: %s",
334				  ypxfr_mapname, ypxfr_source_domain,
335				  ypxfrerr_string(yp_errno));
336			ypxfr_exit(YPXFR_MADDR,NULL);
337		}
338	}
339
340	/*
341	 * If we got here and ypxfr_source_host is still undefined,
342	 * it means we had to resort to using yp_master() to find the
343	 * master server for the map. The source host and master should
344	 * be identical.
345	 */
346	if (ypxfr_source_host == NULL)
347		ypxfr_source_host = ypxfr_master;
348
349	/*
350	 * Don't talk to ypservs on unprivileged ports.
351	 */
352	remoteport = getrpcport(ypxfr_source_host, YPPROG, YPVERS, IPPROTO_UDP);
353	if (remoteport >= IPPORT_RESERVED) {
354		yp_error("ypserv on %s not running on reserved port",
355						ypxfr_source_host);
356		ypxfr_exit(YPXFR_REFUSED, NULL);
357	}
358
359	if ((ypxfr_order = ypxfr_get_order(ypxfr_source_domain,
360					     ypxfr_mapname,
361					     ypxfr_master, 0)) == 0) {
362		yp_error("failed to get order number of %s: %s",
363				ypxfr_mapname, yp_errno == YPXFR_SUCC ?
364				"map has order 0" : ypxfrerr_string(yp_errno));
365		ypxfr_exit(YPXFR_YPERR,NULL);
366	}
367
368	if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname,
369			"YP_INTERDOMAIN", sizeof("YP_INTERDOMAIN") - 1))
370		interdom++;
371
372	if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname,
373			"YP_SECURE", sizeof("YP_SECURE") - 1))
374		secure++;
375
376	key.data = "YP_LAST_MODIFIED";
377	key.size = sizeof("YP_LAST_MODIFIED") - 1;
378
379	/* The order number is immaterial when the 'force' flag is set. */
380
381	if (!ypxfr_force) {
382		int ignore = 0;
383		if (yp_get_record(ypxfr_dest_domain,ypxfr_mapname,&key,&data,1) != YP_TRUE) {
384			switch (yp_errno) {
385			case YP_NOKEY:
386				ypxfr_exit(YPXFR_FORCE,NULL);
387				break;
388			case YP_NOMAP:
389				/*
390				 * If the map doesn't exist, we're
391				 * creating it. Ignore the error.
392				 */
393				ignore++;
394				break;
395			case YP_BADDB:
396			default:
397				ypxfr_exit(YPXFR_DBM,NULL);
398				break;
399			}
400		}
401		if (!ignore && ypxfr_order <= atoi(data.data))
402			ypxfr_exit(YPXFR_AGE, NULL);
403
404	}
405
406	/* Construct a temporary map file name */
407	snprintf(tempmap, sizeof(tempmap), "%s.%d",ypxfr_mapname, getpid());
408	snprintf(ypxfr_temp_map, sizeof(ypxfr_temp_map), "%s/%s/%s", yp_dir,
409		 ypxfr_dest_domain, tempmap);
410
411	if ((remoteport = getrpcport(ypxfr_source_host, YPXFRD_FREEBSD_PROG,
412					YPXFRD_FREEBSD_VERS, IPPROTO_TCP))) {
413
414		/* Don't talk to rpc.ypxfrds on unprovileged ports. */
415		if (remoteport >= IPPORT_RESERVED) {
416			yp_error("rpc.ypxfrd on %s not using privileged port",
417							ypxfr_source_host);
418			ypxfr_exit(YPXFR_REFUSED, NULL);
419		}
420
421		/* Try to send using ypxfrd. If it fails, use old method. */
422		if (!ypxfrd_get_map(ypxfr_source_host, ypxfr_mapname,
423					ypxfr_source_domain, ypxfr_temp_map))
424			goto leave;
425	}
426
427	/* Open the temporary map read/write. */
428	if ((dbp = yp_open_db_rw(ypxfr_dest_domain, tempmap, 0)) == NULL) {
429		yp_error("failed to open temporary map file");
430		ypxfr_exit(YPXFR_DBM,NULL);
431	}
432
433	/*
434	 * Fill in the keys we already know, such as the order number,
435	 * master name, input file name (we actually make up a bogus
436	 * name for that) and output file name.
437	 */
438	snprintf(buf, sizeof(buf), "%lu", ypxfr_order);
439	data.data = buf;
440	data.size = strlen(buf);
441
442	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
443		yp_error("failed to write order number to database");
444		ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
445	}
446
447	key.data = "YP_MASTER_NAME";
448	key.size = sizeof("YP_MASTER_NAME") - 1;
449	data.data = ypxfr_master;
450	data.size = strlen(ypxfr_master);
451
452	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
453		yp_error("failed to write master name to database");
454		ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
455	}
456
457	key.data = "YP_DOMAIN_NAME";
458	key.size = sizeof("YP_DOMAIN_NAME") - 1;
459	data.data = ypxfr_dest_domain;
460	data.size = strlen(ypxfr_dest_domain);
461
462	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
463		yp_error("failed to write domain name to database");
464		ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
465	}
466
467	snprintf (buf, sizeof(buf), "%s:%s", ypxfr_source_host, ypxfr_mapname);
468
469	key.data = "YP_INPUT_NAME";
470	key.size = sizeof("YP_INPUT_NAME") - 1;
471	data.data = &buf;
472	data.size = strlen(buf);
473
474	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
475		yp_error("failed to write input name to database");
476		ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
477
478	}
479
480	snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain,
481							ypxfr_mapname);
482
483	key.data = "YP_OUTPUT_NAME";
484	key.size = sizeof("YP_OUTPUT_NAME") - 1;
485	data.data = &buf;
486	data.size = strlen(buf);
487
488	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
489		yp_error("failed to write output name to database");
490		ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
491	}
492
493	if (interdom) {
494		key.data = "YP_INTERDOMAIN";
495		key.size = sizeof("YP_INTERDOMAIN") - 1;
496		data.data = "";
497		data.size = 0;
498
499		if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
500			yp_error("failed to add interdomain flag to database");
501			ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
502		}
503	}
504
505	if (secure) {
506		key.data = "YP_SECURE";
507		key.size = sizeof("YP_SECURE") - 1;
508		data.data = "";
509		data.size = 0;
510
511		if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
512			yp_error("failed to add secure flag to database");
513			ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
514		}
515	}
516
517	/* Now suck over the contents of the map from the master. */
518
519	if (ypxfr_get_map(ypxfr_mapname,ypxfr_source_domain,
520			  ypxfr_source_host, ypxfr_foreach)){
521		yp_error("failed to retrieve map from source host");
522		ypxfr_exit(YPXFR_YPERR,&ypxfr_temp_map);
523	}
524
525	(void)(dbp->close)(dbp);
526	dbp = NULL; /* <- yes, it seems this is necessary. */
527
528leave:
529
530	snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain,
531							ypxfr_mapname);
532
533	/* Peek at the order number again and check for skew. */
534	if ((ypxfr_skew_check = ypxfr_get_order(ypxfr_source_domain,
535					     ypxfr_mapname,
536					     ypxfr_master, 0)) == 0) {
537		yp_error("failed to get order number of %s: %s",
538				ypxfr_mapname, yp_errno == YPXFR_SUCC ?
539				"map has order 0" : ypxfrerr_string(yp_errno));
540		ypxfr_exit(YPXFR_YPERR,&ypxfr_temp_map);
541	}
542
543	if (ypxfr_order != ypxfr_skew_check)
544		ypxfr_exit(YPXFR_SKEW,&ypxfr_temp_map);
545
546	/*
547	 * Send a YPPROC_CLEAR to the local ypserv.
548	 */
549	if (ypxfr_clear) {
550		char in = 0;
551		char *out = NULL;
552		int stat;
553		if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR,
554			xdr_void, (void *)&in,
555			xdr_void, (void *)out)) != RPC_SUCCESS) {
556			yp_error("failed to send 'clear' to local ypserv: %s",
557				 clnt_sperrno((enum clnt_stat) stat));
558			ypxfr_exit(YPXFR_CLEAR, &ypxfr_temp_map);
559		}
560	}
561
562	/*
563	 * Put the new map in place immediately. I'm not sure if the
564	 * kernel does an unlink() and rename() atomically in the event
565	 * that we move a new copy of a map over the top of an existing
566	 * one, but there's less chance of a race condition happening
567	 * than if we were to do the unlink() ourselves.
568	 */
569	if (rename(ypxfr_temp_map, buf) == -1) {
570		yp_error("rename(%s,%s) failed: %s", ypxfr_temp_map, buf,
571							strerror(errno));
572		ypxfr_exit(YPXFR_FILE,NULL);
573	}
574
575	ypxfr_exit(YPXFR_SUCC,NULL);
576
577	return(1);
578}
579