xref: /freebsd/libexec/ypxfr/ypxfr_main.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1df57947fSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
4665823d0SBill Paul  * Copyright (c) 1995
5665823d0SBill Paul  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6665823d0SBill Paul  *
7665823d0SBill Paul  * Redistribution and use in source and binary forms, with or without
8665823d0SBill Paul  * modification, are permitted provided that the following conditions
9665823d0SBill Paul  * are met:
10665823d0SBill Paul  * 1. Redistributions of source code must retain the above copyright
11665823d0SBill Paul  *    notice, this list of conditions and the following disclaimer.
12665823d0SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
13665823d0SBill Paul  *    notice, this list of conditions and the following disclaimer in the
14665823d0SBill Paul  *    documentation and/or other materials provided with the distribution.
15665823d0SBill Paul  * 3. All advertising materials mentioning features or use of this software
16665823d0SBill Paul  *    must display the following acknowledgement:
17665823d0SBill Paul  *	This product includes software developed by Bill Paul.
18665823d0SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
19665823d0SBill Paul  *    may be used to endorse or promote products derived from this software
20665823d0SBill Paul  *    without specific prior written permission.
21665823d0SBill Paul  *
22665823d0SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23665823d0SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24665823d0SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25665823d0SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26665823d0SBill Paul  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27665823d0SBill Paul  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28665823d0SBill Paul  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29665823d0SBill Paul  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30665823d0SBill Paul  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31665823d0SBill Paul  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32665823d0SBill Paul  * SUCH DAMAGE.
33665823d0SBill Paul  */
3427eed7e3SPhilippe Charnier 
3522e9bc15SDavid E. O'Brien #include <sys/cdefs.h>
3627eed7e3SPhilippe Charnier #include <errno.h>
37665823d0SBill Paul #include <stdio.h>
38665823d0SBill Paul #include <stdlib.h>
39665823d0SBill Paul #include <string.h>
40665823d0SBill Paul #include <syslog.h>
4127eed7e3SPhilippe Charnier #include <unistd.h>
42665823d0SBill Paul #include <sys/types.h>
43665823d0SBill Paul #include <sys/param.h>
44665823d0SBill Paul #include <sys/socket.h>
45665823d0SBill Paul #include <netinet/in.h>
46665823d0SBill Paul #include <arpa/inet.h>
47665823d0SBill Paul #include <rpc/rpc.h>
4834a042e8SBill Paul #include <rpc/clnt.h>
49665823d0SBill Paul #include <rpcsvc/yp.h>
50665823d0SBill Paul #include <rpcsvc/ypclnt.h>
5116deb43aSBill Paul #include <rpcsvc/ypxfrd.h>
52665823d0SBill Paul #include "ypxfr_extern.h"
53665823d0SBill Paul 
54*3a166b33SKyle Evans int debug = 1;
55*3a166b33SKyle Evans 
56665823d0SBill Paul char *progname = "ypxfr";
57665823d0SBill Paul char *yp_dir = _PATH_YP;
58665823d0SBill Paul int _rpcpmstart = 0;
59033af09dSMarcelo Araujo static int ypxfr_use_yplib = 0; /* Assume the worst. */
60033af09dSMarcelo Araujo static int ypxfr_clear = 1;
61033af09dSMarcelo Araujo static int ypxfr_prognum = 0;
62033af09dSMarcelo Araujo static struct sockaddr_in ypxfr_callback_addr;
63033af09dSMarcelo Araujo static struct yppushresp_xfr ypxfr_resp;
64033af09dSMarcelo Araujo static DB *dbp;
65665823d0SBill Paul 
66dc584ddbSDag-Erling Smørgrav static void
ypxfr_exit(ypxfrstat retval,char * temp)67dc584ddbSDag-Erling Smørgrav ypxfr_exit(ypxfrstat retval, char *temp)
68665823d0SBill Paul {
69665823d0SBill Paul 	CLIENT *clnt;
70665823d0SBill Paul 	int sock = RPC_ANYSOCK;
71665823d0SBill Paul 	struct timeval timeout;
72665823d0SBill Paul 
73665823d0SBill Paul 	/* Clean up no matter what happened previously. */
74665823d0SBill Paul 	if (temp != NULL) {
75d6cab902SBill Paul 		if (dbp != NULL)
76665823d0SBill Paul 			(void)(dbp->close)(dbp);
77665823d0SBill Paul 		if (unlink(temp) == -1) {
78665823d0SBill Paul 			yp_error("failed to unlink %s",strerror(errno));
79665823d0SBill Paul 		}
80665823d0SBill Paul 	}
81665823d0SBill Paul 
82f9d553adSBill Paul 	if (ypxfr_prognum) {
83665823d0SBill Paul 		timeout.tv_sec = 20;
84665823d0SBill Paul 		timeout.tv_usec = 0;
85665823d0SBill Paul 
86665823d0SBill Paul 		if ((clnt = clntudp_create(&ypxfr_callback_addr, ypxfr_prognum,
87665823d0SBill Paul 					1, timeout, &sock)) == NULL) {
88f9d553adSBill Paul 			yp_error("%s", clnt_spcreateerror("failed to "
89f9d553adSBill Paul 			    "establish callback handle"));
90665823d0SBill Paul 			exit(1);
91665823d0SBill Paul 		}
92665823d0SBill Paul 
933eb9425cSDimitry Andric 		ypxfr_resp.status = (yppush_status)retval;
94665823d0SBill Paul 
95665823d0SBill Paul 		if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
96665823d0SBill Paul 			yp_error("%s", clnt_sperror(clnt, "callback failed"));
97665823d0SBill Paul 			clnt_destroy(clnt);
98665823d0SBill Paul 			exit(1);
99665823d0SBill Paul 		}
100665823d0SBill Paul 		clnt_destroy(clnt);
101665823d0SBill Paul 	} else {
102665823d0SBill Paul 		yp_error("Exiting: %s", ypxfrerr_string(retval));
103665823d0SBill Paul 	}
104665823d0SBill Paul 
105665823d0SBill Paul 	exit(0);
106665823d0SBill Paul }
107665823d0SBill Paul 
108dc584ddbSDag-Erling Smørgrav static void
usage(void)109dc584ddbSDag-Erling Smørgrav usage(void)
110665823d0SBill Paul {
111665823d0SBill Paul 	if (_rpcpmstart) {
112665823d0SBill Paul 		ypxfr_exit(YPXFR_BADARGS,NULL);
113665823d0SBill Paul 	} else {
11427eed7e3SPhilippe Charnier 		fprintf(stderr, "%s\n%s\n%s\n",
11527eed7e3SPhilippe Charnier 	"usage: ypxfr [-f] [-c] [-d target domain] [-h source host]",
11627eed7e3SPhilippe Charnier 	"             [-s source domain] [-p path]",
11727eed7e3SPhilippe Charnier 	"             [-C taskid program-number ipaddr port] mapname");
118665823d0SBill Paul 		exit(1);
119665823d0SBill Paul 	}
120665823d0SBill Paul }
121665823d0SBill Paul 
122dc584ddbSDag-Erling Smørgrav int
ypxfr_foreach(int status,char * key,int keylen,char * val,int vallen,char * data)123dc584ddbSDag-Erling Smørgrav ypxfr_foreach(int status, char *key, int keylen, char *val, int vallen,
124dc584ddbSDag-Erling Smørgrav     char *data)
125665823d0SBill Paul {
126665823d0SBill Paul 	DBT dbkey, dbval;
127665823d0SBill Paul 
128665823d0SBill Paul 	if (status != YP_TRUE)
129665823d0SBill Paul 		return (status);
130665823d0SBill Paul 
13123677e98SBill Paul 	/*
13223677e98SBill Paul 	 * XXX Do not attempt to write zero-length keys or
13323677e98SBill Paul 	 * data into a Berkeley DB hash database. It causes a
13423677e98SBill Paul 	 * strange failure mode where sequential searches get
13523677e98SBill Paul 	 * caught in an infinite loop.
13623677e98SBill Paul 	 */
13723677e98SBill Paul 	if (keylen) {
138665823d0SBill Paul 		dbkey.data = key;
139665823d0SBill Paul 		dbkey.size = keylen;
14023677e98SBill Paul 	} else {
14123677e98SBill Paul 		dbkey.data = "";
14223677e98SBill Paul 		dbkey.size = 1;
14323677e98SBill Paul 	}
14423677e98SBill Paul 	if (vallen) {
145665823d0SBill Paul 		dbval.data = val;
146665823d0SBill Paul 		dbval.size = vallen;
14723677e98SBill Paul 	} else {
14823677e98SBill Paul 		dbval.data = "";
14923677e98SBill Paul 		dbval.size = 1;
15023677e98SBill Paul 	}
151665823d0SBill Paul 
15216deb43aSBill Paul 	if (yp_put_record(dbp, &dbkey, &dbval, 0) != YP_TRUE)
153665823d0SBill Paul 		return(yp_errno);
154665823d0SBill Paul 
155665823d0SBill Paul 	return (0);
156665823d0SBill Paul }
157665823d0SBill Paul 
15827eed7e3SPhilippe Charnier int
main(int argc,char * argv[])159dc584ddbSDag-Erling Smørgrav main(int argc, char *argv[])
160665823d0SBill Paul {
161665823d0SBill Paul 	int ch;
162665823d0SBill Paul 	int ypxfr_force = 0;
163665823d0SBill Paul 	char *ypxfr_dest_domain = NULL;
164665823d0SBill Paul 	char *ypxfr_source_host = NULL;
165665823d0SBill Paul 	char *ypxfr_source_domain = NULL;
166665823d0SBill Paul 	char *ypxfr_local_domain = NULL;
167665823d0SBill Paul 	char *ypxfr_master = NULL;
168665823d0SBill Paul 	unsigned long ypxfr_order = -1, ypxfr_skew_check = -1;
169665823d0SBill Paul 	char *ypxfr_mapname = NULL;
170665823d0SBill Paul 	int ypxfr_args = 0;
171665823d0SBill Paul 	char ypxfr_temp_map[MAXPATHLEN + 2];
172665823d0SBill Paul 	char tempmap[MAXPATHLEN + 2];
173665823d0SBill Paul 	char buf[MAXPATHLEN + 2];
174665823d0SBill Paul 	DBT key, data;
1752c33b20aSBill Paul 	int remoteport;
17681a82d4dSBill Paul 	int interdom = 0;
17781a82d4dSBill Paul 	int secure = 0;
178665823d0SBill Paul 
179665823d0SBill Paul 	if (!isatty(fileno(stderr))) {
18027eed7e3SPhilippe Charnier 		openlog("ypxfr", LOG_PID, LOG_DAEMON);
181665823d0SBill Paul 		_rpcpmstart = 1;
182665823d0SBill Paul 	}
183665823d0SBill Paul 
184665823d0SBill Paul 	if (argc < 2)
185665823d0SBill Paul 		usage();
186665823d0SBill Paul 
18791477cc4SWarner Losh 	while ((ch = getopt(argc, argv, "fcd:h:s:p:C:")) != -1) {
188665823d0SBill Paul 		int my_optind;
189665823d0SBill Paul 		switch (ch) {
190665823d0SBill Paul 		case 'f':
191665823d0SBill Paul 			ypxfr_force++;
192665823d0SBill Paul 			ypxfr_args++;
193665823d0SBill Paul 			break;
194665823d0SBill Paul 		case 'c':
195665823d0SBill Paul 			ypxfr_clear = 0;
196665823d0SBill Paul 			ypxfr_args++;
197665823d0SBill Paul 			break;
198665823d0SBill Paul 		case 'd':
199665823d0SBill Paul 			ypxfr_dest_domain = optarg;
200665823d0SBill Paul 			ypxfr_args += 2;
201665823d0SBill Paul 			break;
202665823d0SBill Paul 		case 'h':
203665823d0SBill Paul 			ypxfr_source_host = optarg;
204665823d0SBill Paul 			ypxfr_args += 2;
205665823d0SBill Paul 			break;
206665823d0SBill Paul 		case 's':
207665823d0SBill Paul 			ypxfr_source_domain = optarg;
208665823d0SBill Paul 			ypxfr_args += 2;
209665823d0SBill Paul 			break;
210665823d0SBill Paul 		case 'p':
211665823d0SBill Paul 			yp_dir = optarg;
212665823d0SBill Paul 			ypxfr_args += 2;
213665823d0SBill Paul 			break;
214665823d0SBill Paul 		case 'C':
215665823d0SBill Paul 			/*
216665823d0SBill Paul 			 * Whoever decided that the -C flag should take
217665823d0SBill Paul 			 * four arguments is a twit.
218665823d0SBill Paul 			 */
219665823d0SBill Paul 			my_optind = optind - 1;
220665823d0SBill Paul 			if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
221665823d0SBill Paul 				yp_error("transaction ID not specified");
222665823d0SBill Paul 				usage();
223665823d0SBill Paul 			}
224665823d0SBill Paul 			ypxfr_resp.transid = atol(argv[my_optind]);
225665823d0SBill Paul 			my_optind++;
226665823d0SBill Paul 			if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
227665823d0SBill Paul 				yp_error("RPC program number not specified");
228665823d0SBill Paul 				usage();
229665823d0SBill Paul 			}
230665823d0SBill Paul 			ypxfr_prognum = atol(argv[my_optind]);
231665823d0SBill Paul 			my_optind++;
232665823d0SBill Paul 			if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
233665823d0SBill Paul 				yp_error("address not specified");
234665823d0SBill Paul 				usage();
235665823d0SBill Paul 			}
236665823d0SBill Paul 			if (!inet_aton(argv[my_optind], &ypxfr_callback_addr.sin_addr)) {
237665823d0SBill Paul 				yp_error("failed to convert '%s' to IP addr",
238665823d0SBill Paul 					argv[my_optind]);
239665823d0SBill Paul 				exit(1);
240665823d0SBill Paul 			}
241665823d0SBill Paul 			my_optind++;
242665823d0SBill Paul 			if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
243665823d0SBill Paul 				yp_error("port not specified");
244665823d0SBill Paul 				usage();
245665823d0SBill Paul 			}
246665823d0SBill Paul 			ypxfr_callback_addr.sin_port = htons((u_short)atoi(argv[my_optind]));
247665823d0SBill Paul 			ypxfr_args += 5;
248665823d0SBill Paul 			break;
249665823d0SBill Paul 		default:
250665823d0SBill Paul 			usage();
251665823d0SBill Paul 			break;
252665823d0SBill Paul 		}
253665823d0SBill Paul 	}
254665823d0SBill Paul 
255665823d0SBill Paul 	ypxfr_mapname = argv[ypxfr_args + 1];
256665823d0SBill Paul 
257665823d0SBill Paul 	if (ypxfr_mapname == NULL) {
258665823d0SBill Paul 		yp_error("no map name specified");
259665823d0SBill Paul 		usage();
260665823d0SBill Paul 	}
261665823d0SBill Paul 
262665823d0SBill Paul 	/* Always the case. */
263665823d0SBill Paul 	ypxfr_callback_addr.sin_family = AF_INET;
264665823d0SBill Paul 
265665823d0SBill Paul 	/* Determine if local NIS client facilities are turned on. */
266665823d0SBill Paul 	if (!yp_get_default_domain(&ypxfr_local_domain) &&
267665823d0SBill Paul 	    _yp_check(&ypxfr_local_domain))
268665823d0SBill Paul 		ypxfr_use_yplib = 1;
269665823d0SBill Paul 
270665823d0SBill Paul 	/*
271665823d0SBill Paul 	 * If no destination domain is specified, assume that the
272665823d0SBill Paul 	 * local default domain is to be used and try to obtain it.
273665823d0SBill Paul 	 * Fails if NIS client facilities are turned off.
274665823d0SBill Paul 	 */
275665823d0SBill Paul 	if (ypxfr_dest_domain == NULL) {
276665823d0SBill Paul 		if (ypxfr_use_yplib) {
277665823d0SBill Paul 			yp_get_default_domain(&ypxfr_dest_domain);
278665823d0SBill Paul 		} else {
279665823d0SBill Paul 			yp_error("no destination domain specified and \
280665823d0SBill Paul the local domain name isn't set");
281665823d0SBill Paul 			ypxfr_exit(YPXFR_BADARGS,NULL);
282665823d0SBill Paul 		}
283665823d0SBill Paul 	}
284665823d0SBill Paul 
285665823d0SBill Paul 	/*
286665823d0SBill Paul 	 * If a source domain is not specified, assume it to
287665823d0SBill Paul 	 * be the same as the destination domain.
288665823d0SBill Paul 	 */
289665823d0SBill Paul 	if (ypxfr_source_domain == NULL) {
290665823d0SBill Paul 		ypxfr_source_domain = ypxfr_dest_domain;
291665823d0SBill Paul 	}
292665823d0SBill Paul 
293665823d0SBill Paul 	/*
294665823d0SBill Paul 	 * If the source host is not specified, assume it to be the
295665823d0SBill Paul 	 * master for the specified map. If local NIS client facilities
296665823d0SBill Paul 	 * are turned on, we can figure this out using yp_master().
297665823d0SBill Paul 	 * If not, we have to see if a local copy of the map exists
298665823d0SBill Paul 	 * and extract its YP_MASTER_NAME record. If _that_ fails,
299665823d0SBill Paul 	 * we are stuck and must ask the user for more information.
300665823d0SBill Paul 	 */
301665823d0SBill Paul 	if (ypxfr_source_host == NULL) {
302665823d0SBill Paul 		if (!ypxfr_use_yplib) {
303665823d0SBill Paul 		/*
304665823d0SBill Paul 		 * Double whammy: NIS isn't turned on and the user
305665823d0SBill Paul 		 * didn't specify a source host.
306665823d0SBill Paul 		 */
307665823d0SBill Paul 			char *dptr;
308665823d0SBill Paul 			key.data = "YP_MASTER_NAME";
309665823d0SBill Paul 			key.size = sizeof("YP_MASTER_NAME") - 1;
310665823d0SBill Paul 
311665823d0SBill Paul 			if (yp_get_record(ypxfr_dest_domain, ypxfr_mapname,
312665823d0SBill Paul 					 &key, &data, 1) != YP_TRUE) {
313665823d0SBill Paul 				yp_error("no source host specified");
314665823d0SBill Paul 				ypxfr_exit(YPXFR_BADARGS,NULL);
315665823d0SBill Paul 			}
316665823d0SBill Paul 			dptr = data.data;
317665823d0SBill Paul 			dptr[data.size] = '\0';
318665823d0SBill Paul 			ypxfr_master = ypxfr_source_host = strdup(dptr);
319665823d0SBill Paul 		}
320665823d0SBill Paul 	} else {
321665823d0SBill Paul 		if (ypxfr_use_yplib)
322665823d0SBill Paul 			ypxfr_use_yplib = 0;
323665823d0SBill Paul 	}
324665823d0SBill Paul 
325665823d0SBill Paul 	if (ypxfr_master == NULL) {
326665823d0SBill Paul 		if ((ypxfr_master = ypxfr_get_master(ypxfr_source_domain,
327665823d0SBill Paul 					    	 ypxfr_mapname,
328665823d0SBill Paul 					     	ypxfr_source_host,
329665823d0SBill Paul 					     	ypxfr_use_yplib)) == NULL) {
330d6cab902SBill Paul 			yp_error("failed to find master of %s in domain %s: %s",
331d6cab902SBill Paul 				  ypxfr_mapname, ypxfr_source_domain,
3323eb9425cSDimitry Andric 				  ypxfrerr_string((ypxfrstat)yp_errno));
333665823d0SBill Paul 			ypxfr_exit(YPXFR_MADDR,NULL);
334665823d0SBill Paul 		}
335665823d0SBill Paul 	}
336665823d0SBill Paul 
337665823d0SBill Paul 	/*
338665823d0SBill Paul 	 * If we got here and ypxfr_source_host is still undefined,
339665823d0SBill Paul 	 * it means we had to resort to using yp_master() to find the
340665823d0SBill Paul 	 * master server for the map. The source host and master should
341665823d0SBill Paul 	 * be identical.
342665823d0SBill Paul 	 */
343665823d0SBill Paul 	if (ypxfr_source_host == NULL)
344665823d0SBill Paul 		ypxfr_source_host = ypxfr_master;
345665823d0SBill Paul 
3462c33b20aSBill Paul 	/*
3472c33b20aSBill Paul 	 * Don't talk to ypservs on unprivileged ports.
3482c33b20aSBill Paul 	 */
3492c33b20aSBill Paul 	remoteport = getrpcport(ypxfr_source_host, YPPROG, YPVERS, IPPROTO_UDP);
3502c33b20aSBill Paul 	if (remoteport >= IPPORT_RESERVED) {
3512c33b20aSBill Paul 		yp_error("ypserv on %s not running on reserved port",
3522c33b20aSBill Paul 						ypxfr_source_host);
3532c33b20aSBill Paul 		ypxfr_exit(YPXFR_REFUSED, NULL);
3542c33b20aSBill Paul 	}
3552c33b20aSBill Paul 
356665823d0SBill Paul 	if ((ypxfr_order = ypxfr_get_order(ypxfr_source_domain,
357665823d0SBill Paul 					     ypxfr_mapname,
358665823d0SBill Paul 					     ypxfr_master, 0)) == 0) {
359d6cab902SBill Paul 		yp_error("failed to get order number of %s: %s",
36089482baaSCraig Rodrigues 				ypxfr_mapname, yp_errno == YP_TRUE ?
3613eb9425cSDimitry Andric 				"map has order 0" :
3623eb9425cSDimitry Andric 				ypxfrerr_string((ypxfrstat)yp_errno));
363665823d0SBill Paul 		ypxfr_exit(YPXFR_YPERR,NULL);
364665823d0SBill Paul 	}
365665823d0SBill Paul 
36681a82d4dSBill Paul 	if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname,
36781a82d4dSBill Paul 			"YP_INTERDOMAIN", sizeof("YP_INTERDOMAIN") - 1))
36881a82d4dSBill Paul 		interdom++;
36981a82d4dSBill Paul 
37081a82d4dSBill Paul 	if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname,
37181a82d4dSBill Paul 			"YP_SECURE", sizeof("YP_SECURE") - 1))
37281a82d4dSBill Paul 		secure++;
37381a82d4dSBill Paul 
374665823d0SBill Paul 	key.data = "YP_LAST_MODIFIED";
375665823d0SBill Paul 	key.size = sizeof("YP_LAST_MODIFIED") - 1;
376665823d0SBill Paul 
377665823d0SBill Paul 	/* The order number is immaterial when the 'force' flag is set. */
378665823d0SBill Paul 
379665823d0SBill Paul 	if (!ypxfr_force) {
380665823d0SBill Paul 		int ignore = 0;
381665823d0SBill Paul 		if (yp_get_record(ypxfr_dest_domain,ypxfr_mapname,&key,&data,1) != YP_TRUE) {
382665823d0SBill Paul 			switch (yp_errno) {
383665823d0SBill Paul 			case YP_NOKEY:
384665823d0SBill Paul 				ypxfr_exit(YPXFR_FORCE,NULL);
385665823d0SBill Paul 				break;
386665823d0SBill Paul 			case YP_NOMAP:
387665823d0SBill Paul 				/*
388665823d0SBill Paul 				 * If the map doesn't exist, we're
389665823d0SBill Paul 				 * creating it. Ignore the error.
390665823d0SBill Paul 				 */
391665823d0SBill Paul 				ignore++;
392665823d0SBill Paul 				break;
393665823d0SBill Paul 			case YP_BADDB:
394665823d0SBill Paul 			default:
395665823d0SBill Paul 				ypxfr_exit(YPXFR_DBM,NULL);
396665823d0SBill Paul 				break;
397665823d0SBill Paul 			}
398665823d0SBill Paul 		}
399665823d0SBill Paul 		if (!ignore && ypxfr_order <= atoi(data.data))
400665823d0SBill Paul 			ypxfr_exit(YPXFR_AGE, NULL);
401665823d0SBill Paul 
402665823d0SBill Paul 	}
403665823d0SBill Paul 
404665823d0SBill Paul 	/* Construct a temporary map file name */
405665823d0SBill Paul 	snprintf(tempmap, sizeof(tempmap), "%s.%d",ypxfr_mapname, getpid());
406665823d0SBill Paul 	snprintf(ypxfr_temp_map, sizeof(ypxfr_temp_map), "%s/%s/%s", yp_dir,
407665823d0SBill Paul 		 ypxfr_dest_domain, tempmap);
408665823d0SBill Paul 
4092c33b20aSBill Paul 	if ((remoteport = getrpcport(ypxfr_source_host, YPXFRD_FREEBSD_PROG,
4102c33b20aSBill Paul 					YPXFRD_FREEBSD_VERS, IPPROTO_TCP))) {
4112c33b20aSBill Paul 
4122c33b20aSBill Paul 		/* Don't talk to rpc.ypxfrds on unprovileged ports. */
4132c33b20aSBill Paul 		if (remoteport >= IPPORT_RESERVED) {
4142c33b20aSBill Paul 			yp_error("rpc.ypxfrd on %s not using privileged port",
4152c33b20aSBill Paul 							ypxfr_source_host);
4162c33b20aSBill Paul 			ypxfr_exit(YPXFR_REFUSED, NULL);
4172c33b20aSBill Paul 		}
4182c33b20aSBill Paul 
41916deb43aSBill Paul 		/* Try to send using ypxfrd. If it fails, use old method. */
4202c33b20aSBill Paul 		if (!ypxfrd_get_map(ypxfr_source_host, ypxfr_mapname,
42116deb43aSBill Paul 					ypxfr_source_domain, ypxfr_temp_map))
42216deb43aSBill Paul 			goto leave;
42316deb43aSBill Paul 	}
42416deb43aSBill Paul 
425665823d0SBill Paul 	/* Open the temporary map read/write. */
42616deb43aSBill Paul 	if ((dbp = yp_open_db_rw(ypxfr_dest_domain, tempmap, 0)) == NULL) {
427665823d0SBill Paul 		yp_error("failed to open temporary map file");
428665823d0SBill Paul 		ypxfr_exit(YPXFR_DBM,NULL);
429665823d0SBill Paul 	}
430665823d0SBill Paul 
431665823d0SBill Paul 	/*
432665823d0SBill Paul 	 * Fill in the keys we already know, such as the order number,
433665823d0SBill Paul 	 * master name, input file name (we actually make up a bogus
434665823d0SBill Paul 	 * name for that) and output file name.
435665823d0SBill Paul 	 */
436bb6ae0a4SBruce Evans 	snprintf(buf, sizeof(buf), "%lu", ypxfr_order);
437665823d0SBill Paul 	data.data = buf;
438665823d0SBill Paul 	data.size = strlen(buf);
439665823d0SBill Paul 
44016deb43aSBill Paul 	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
441665823d0SBill Paul 		yp_error("failed to write order number to database");
442425dd8acSDag-Erling Smørgrav 		ypxfr_exit(YPXFR_DBM,ypxfr_temp_map);
443665823d0SBill Paul 	}
444665823d0SBill Paul 
445665823d0SBill Paul 	key.data = "YP_MASTER_NAME";
446665823d0SBill Paul 	key.size = sizeof("YP_MASTER_NAME") - 1;
447665823d0SBill Paul 	data.data = ypxfr_master;
448665823d0SBill Paul 	data.size = strlen(ypxfr_master);
449665823d0SBill Paul 
45016deb43aSBill Paul 	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
451665823d0SBill Paul 		yp_error("failed to write master name to database");
452425dd8acSDag-Erling Smørgrav 		ypxfr_exit(YPXFR_DBM,ypxfr_temp_map);
453665823d0SBill Paul 	}
454665823d0SBill Paul 
455665823d0SBill Paul 	key.data = "YP_DOMAIN_NAME";
456665823d0SBill Paul 	key.size = sizeof("YP_DOMAIN_NAME") - 1;
457665823d0SBill Paul 	data.data = ypxfr_dest_domain;
458665823d0SBill Paul 	data.size = strlen(ypxfr_dest_domain);
459665823d0SBill Paul 
46016deb43aSBill Paul 	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
461665823d0SBill Paul 		yp_error("failed to write domain name to database");
462425dd8acSDag-Erling Smørgrav 		ypxfr_exit(YPXFR_DBM,ypxfr_temp_map);
463665823d0SBill Paul 	}
464665823d0SBill Paul 
465665823d0SBill Paul 	snprintf (buf, sizeof(buf), "%s:%s", ypxfr_source_host, ypxfr_mapname);
466665823d0SBill Paul 
467665823d0SBill Paul 	key.data = "YP_INPUT_NAME";
468665823d0SBill Paul 	key.size = sizeof("YP_INPUT_NAME") - 1;
469665823d0SBill Paul 	data.data = &buf;
470665823d0SBill Paul 	data.size = strlen(buf);
471665823d0SBill Paul 
47216deb43aSBill Paul 	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
473665823d0SBill Paul 		yp_error("failed to write input name to database");
474425dd8acSDag-Erling Smørgrav 		ypxfr_exit(YPXFR_DBM,ypxfr_temp_map);
475665823d0SBill Paul 
476665823d0SBill Paul 	}
477665823d0SBill Paul 
478665823d0SBill Paul 	snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain,
479665823d0SBill Paul 							ypxfr_mapname);
480665823d0SBill Paul 
481665823d0SBill Paul 	key.data = "YP_OUTPUT_NAME";
482665823d0SBill Paul 	key.size = sizeof("YP_OUTPUT_NAME") - 1;
483665823d0SBill Paul 	data.data = &buf;
484665823d0SBill Paul 	data.size = strlen(buf);
485665823d0SBill Paul 
48616deb43aSBill Paul 	if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
487665823d0SBill Paul 		yp_error("failed to write output name to database");
488425dd8acSDag-Erling Smørgrav 		ypxfr_exit(YPXFR_DBM,ypxfr_temp_map);
489665823d0SBill Paul 	}
490665823d0SBill Paul 
49181a82d4dSBill Paul 	if (interdom) {
49281a82d4dSBill Paul 		key.data = "YP_INTERDOMAIN";
49381a82d4dSBill Paul 		key.size = sizeof("YP_INTERDOMAIN") - 1;
49481a82d4dSBill Paul 		data.data = "";
49581a82d4dSBill Paul 		data.size = 0;
49681a82d4dSBill Paul 
49781a82d4dSBill Paul 		if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
49881a82d4dSBill Paul 			yp_error("failed to add interdomain flag to database");
499425dd8acSDag-Erling Smørgrav 			ypxfr_exit(YPXFR_DBM,ypxfr_temp_map);
50081a82d4dSBill Paul 		}
50181a82d4dSBill Paul 	}
50281a82d4dSBill Paul 
50381a82d4dSBill Paul 	if (secure) {
50481a82d4dSBill Paul 		key.data = "YP_SECURE";
50581a82d4dSBill Paul 		key.size = sizeof("YP_SECURE") - 1;
50681a82d4dSBill Paul 		data.data = "";
50781a82d4dSBill Paul 		data.size = 0;
50881a82d4dSBill Paul 
50981a82d4dSBill Paul 		if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
51081a82d4dSBill Paul 			yp_error("failed to add secure flag to database");
511425dd8acSDag-Erling Smørgrav 			ypxfr_exit(YPXFR_DBM,ypxfr_temp_map);
51281a82d4dSBill Paul 		}
51381a82d4dSBill Paul 	}
51481a82d4dSBill Paul 
515665823d0SBill Paul 	/* Now suck over the contents of the map from the master. */
516665823d0SBill Paul 
517665823d0SBill Paul 	if (ypxfr_get_map(ypxfr_mapname,ypxfr_source_domain,
518665823d0SBill Paul 			  ypxfr_source_host, ypxfr_foreach)){
519665823d0SBill Paul 		yp_error("failed to retrieve map from source host");
520425dd8acSDag-Erling Smørgrav 		ypxfr_exit(YPXFR_YPERR,ypxfr_temp_map);
521665823d0SBill Paul 	}
522665823d0SBill Paul 
523665823d0SBill Paul 	(void)(dbp->close)(dbp);
524d6cab902SBill Paul 	dbp = NULL; /* <- yes, it seems this is necessary. */
525665823d0SBill Paul 
52616deb43aSBill Paul leave:
52716deb43aSBill Paul 
52816deb43aSBill Paul 	snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain,
52916deb43aSBill Paul 							ypxfr_mapname);
53016deb43aSBill Paul 
531665823d0SBill Paul 	/* Peek at the order number again and check for skew. */
532665823d0SBill Paul 	if ((ypxfr_skew_check = ypxfr_get_order(ypxfr_source_domain,
533665823d0SBill Paul 					     ypxfr_mapname,
534665823d0SBill Paul 					     ypxfr_master, 0)) == 0) {
535d6cab902SBill Paul 		yp_error("failed to get order number of %s: %s",
53689482baaSCraig Rodrigues 				ypxfr_mapname, yp_errno == YP_TRUE ?
5373eb9425cSDimitry Andric 				"map has order 0" :
5383eb9425cSDimitry Andric 				ypxfrerr_string((ypxfrstat)yp_errno));
539425dd8acSDag-Erling Smørgrav 		ypxfr_exit(YPXFR_YPERR,ypxfr_temp_map);
540665823d0SBill Paul 	}
541665823d0SBill Paul 
542665823d0SBill Paul 	if (ypxfr_order != ypxfr_skew_check)
543425dd8acSDag-Erling Smørgrav 		ypxfr_exit(YPXFR_SKEW,ypxfr_temp_map);
544665823d0SBill Paul 
545665823d0SBill Paul 	/*
546665823d0SBill Paul 	 * Send a YPPROC_CLEAR to the local ypserv.
547665823d0SBill Paul 	 */
54834a042e8SBill Paul 	if (ypxfr_clear) {
549665823d0SBill Paul 		char in = 0;
550665823d0SBill Paul 		char *out = NULL;
55134a042e8SBill Paul 		int stat;
55234a042e8SBill Paul 		if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR,
553d988f6deSPeter Wemm 			(xdrproc_t)xdr_void, (void *)&in,
554d988f6deSPeter Wemm 			(xdrproc_t)xdr_void, (void *)out)) != RPC_SUCCESS) {
55534a042e8SBill Paul 			yp_error("failed to send 'clear' to local ypserv: %s",
55634a042e8SBill Paul 				 clnt_sperrno((enum clnt_stat) stat));
557425dd8acSDag-Erling Smørgrav 			ypxfr_exit(YPXFR_CLEAR, ypxfr_temp_map);
558665823d0SBill Paul 		}
559665823d0SBill Paul 	}
560665823d0SBill Paul 
56134a042e8SBill Paul 	/*
56234a042e8SBill Paul 	 * Put the new map in place immediately. I'm not sure if the
56334a042e8SBill Paul 	 * kernel does an unlink() and rename() atomically in the event
56434a042e8SBill Paul 	 * that we move a new copy of a map over the top of an existing
56534a042e8SBill Paul 	 * one, but there's less chance of a race condition happening
56634a042e8SBill Paul 	 * than if we were to do the unlink() ourselves.
56734a042e8SBill Paul 	 */
568665823d0SBill Paul 	if (rename(ypxfr_temp_map, buf) == -1) {
569665823d0SBill Paul 		yp_error("rename(%s,%s) failed: %s", ypxfr_temp_map, buf,
570665823d0SBill Paul 							strerror(errno));
571665823d0SBill Paul 		ypxfr_exit(YPXFR_FILE,NULL);
572665823d0SBill Paul 	}
573665823d0SBill Paul 
574665823d0SBill Paul 	ypxfr_exit(YPXFR_SUCC,NULL);
575665823d0SBill Paul 
576665823d0SBill Paul 	return(1);
577665823d0SBill Paul }
578