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