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