xref: /freebsd/usr.sbin/yppush/yppush_main.c (revision 8c2f6c3be0125142d3c1782e4b0ee0634c584b9e)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995
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 #include <errno.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <sys/socket.h>
45 #include <sys/fcntl.h>
46 #include <sys/wait.h>
47 #include <sys/param.h>
48 #include <rpc/rpc.h>
49 #include <rpc/clnt.h>
50 #include <rpc/pmap_clnt.h>
51 #include <rpcsvc/yp.h>
52 #include <rpcsvc/ypclnt.h>
53 #include "ypxfr_extern.h"
54 #include "yppush_extern.h"
55 
56 char *progname = "yppush";
57 int debug = 1;
58 int _rpcpmstart = 0;
59 char *yp_dir = _PATH_YP;
60 
61 static char *yppush_mapname = NULL;	/* Map to transfer. */
62 static char *yppush_domain = NULL;	/* Domain in which map resides. */
63 static char *yppush_master = NULL;	/* Master NIS server for said domain. */
64 static int skip_master = 0;		/* Do not attempt to push map to master. */
65 static int verbose = 0;		/* Toggle verbose mode. */
66 static unsigned long yppush_transid = 0;
67 static int yppush_timeout = 80;	/* Default timeout. */
68 static int yppush_jobs = 1;		/* Number of allowed concurrent jobs. */
69 static int yppush_running_jobs = 0;	/* Number of currently running jobs. */
70 
71 /* Structure for holding information about a running job. */
72 struct jobs {
73 	unsigned long tid;
74 	int port;
75 	ypxfrstat stat;
76 	unsigned long prognum;
77 	char *server;
78 	char *map;
79 	int polled;
80 	struct jobs *next;
81 };
82 
83 static struct jobs *yppush_joblist;	/* Linked list of running jobs. */
84 static int yppush_svc_run(int);
85 
86 /*
87  * Local error messages.
88  */
89 static const char *
90 yppusherr_string(int err)
91 {
92 	switch (err) {
93 	case YPPUSH_TIMEDOUT:
94 		return("transfer or callback timed out");
95 	case YPPUSH_YPSERV:
96 		return("failed to contact ypserv");
97 	case YPPUSH_NOHOST:
98 		return("no such host");
99 	case YPPUSH_PMAP:
100 		return("portmapper failure");
101 	default:
102 		return("unknown error code");
103 	}
104 }
105 
106 /*
107  * Report state of a job.
108  */
109 static int
110 yppush_show_status(ypxfrstat status, unsigned long tid)
111 {
112 	struct jobs *job;
113 
114 	job = yppush_joblist;
115 
116 	while (job != NULL) {
117 		if (job->tid == tid)
118 			break;
119 		job = job->next;
120 	}
121 
122 	if (job == NULL) {
123 		yp_error("warning: received callback with invalid transaction ID: %lu",
124 			 tid);
125 		return (0);
126 	}
127 
128 	if (job->polled) {
129 		yp_error("warning: received callback with duplicate transaction ID: %lu",
130 			 tid);
131 		return (0);
132 	}
133 
134 	if (verbose > 1) {
135 		yp_error("checking return status: transaction ID: %lu",
136 								job->tid);
137 	}
138 
139 	if (status != YPXFR_SUCC || verbose) {
140 		yp_error("transfer of map %s to server %s %s",
141 		 	job->map, job->server, status == YPXFR_SUCC ?
142 		 	"succeeded" : "failed");
143 		yp_error("status returned by ypxfr: %s", status > YPXFR_AGE ?
144 			yppusherr_string(status) :
145 			ypxfrerr_string(status));
146 	}
147 
148 	job->polled = 1;
149 
150 	svc_unregister(job->prognum, 1);
151 
152 	yppush_running_jobs--;
153 	return(0);
154 }
155 
156 /* Exit routine. */
157 static void
158 yppush_exit(int now)
159 {
160 	struct jobs *jptr;
161 	int still_pending = 1;
162 
163 	/* Let all the information trickle in. */
164 	while (!now && still_pending) {
165 		jptr = yppush_joblist;
166 		still_pending = 0;
167 		while (jptr) {
168 			if (jptr->polled == 0) {
169 				still_pending++;
170 				if (verbose > 1)
171 					yp_error("%s has not responded",
172 						  jptr->server);
173 			} else {
174 				if (verbose > 1)
175 					yp_error("%s has responded",
176 						  jptr->server);
177 			}
178 			jptr = jptr->next;
179 		}
180 		if (still_pending) {
181 			if (verbose > 1)
182 				yp_error("%d transfer%sstill pending",
183 					still_pending,
184 					still_pending > 1 ? "s " : " ");
185 			if (yppush_svc_run (YPPUSH_RESPONSE_TIMEOUT) == 0) {
186 				yp_error("timed out");
187 				now = 1;
188 			}
189 		} else {
190 			if (verbose)
191 				yp_error("all transfers complete");
192 			break;
193 		}
194 	}
195 
196 
197 	/* All stats collected and reported -- kill all the stragglers. */
198 	jptr = yppush_joblist;
199 	while (jptr) {
200 		if (!jptr->polled)
201 			yp_error("warning: exiting with transfer \
202 to %s (transid = %lu) still pending", jptr->server, jptr->tid);
203 		svc_unregister(jptr->prognum, 1);
204 		jptr = jptr->next;
205 	}
206 
207 	exit(0);
208 }
209 
210 /*
211  * Handler for 'normal' signals.
212  */
213 
214 static void
215 handler(int sig)
216 {
217 	yppush_exit (1);
218 }
219 
220 /*
221  * Dispatch loop for callback RPC services.
222  * Return value:
223  *   -1 error
224  *    0 timeout
225  *   >0 request serviced
226  */
227 static int
228 yppush_svc_run(int timeout_secs)
229 {
230 	int rc;
231 	fd_set readfds;
232 	struct timeval timeout;
233 
234 	timeout.tv_usec = 0;
235 	timeout.tv_sec = timeout_secs;
236 
237 retry:
238 	readfds = svc_fdset;
239 	rc = select(svc_maxfd + 1, &readfds, NULL, NULL, &timeout);
240 	switch (rc) {
241 	case -1:
242 		if (errno == EINTR)
243 			goto retry;
244 		yp_error("select failed: %s", strerror(errno));
245 		break;
246 	case 0:
247 		yp_error("select() timed out");
248 		break;
249 	default:
250 		svc_getreqset(&readfds);
251 		break;
252 	}
253 	return rc;
254 }
255 
256 /*
257  * RPC service routines for callbacks.
258  */
259 void *
260 yppushproc_null_1_svc(void *argp, struct svc_req *rqstp)
261 {
262 	static char * result;
263 	/* Do nothing -- RPC conventions call for all a null proc. */
264 	return((void *) &result);
265 }
266 
267 void *
268 yppushproc_xfrresp_1_svc(yppushresp_xfr *argp, struct svc_req *rqstp)
269 {
270 	static char * result;
271 	yppush_show_status(argp->status, argp->transid);
272 	return((void *) &result);
273 }
274 
275 /*
276  * Transmit a YPPROC_XFR request to ypserv.
277  */
278 static int
279 yppush_send_xfr(struct jobs *job)
280 {
281 	ypreq_xfr req;
282 /*	ypresp_xfr *resp; */
283 	DBT key, data;
284 	CLIENT *clnt;
285 	struct rpc_err err;
286 	struct timeval timeout;
287 
288 	timeout.tv_usec = 0;
289 	timeout.tv_sec = 0;
290 
291 	/*
292 	 * The ypreq_xfr structure has a member of type map_parms,
293 	 * which seems to require the order number of the map.
294 	 * It isn't actually used at the other end (at least the
295 	 * FreeBSD ypserv doesn't use it) but we fill it in here
296 	 * for the sake of completeness.
297 	 */
298 	key.data = "YP_LAST_MODIFIED";
299 	key.size = sizeof ("YP_LAST_MODIFIED") - 1;
300 
301 	if (yp_get_record(yppush_domain, yppush_mapname, &key, &data,
302 			  1) != YP_TRUE) {
303 		yp_error("failed to read order number from %s: %s: %s",
304 			  yppush_mapname, yperr_string(yp_errno),
305 			  strerror(errno));
306 		return(1);
307 	}
308 
309 	/* Fill in the request arguments */
310 	req.map_parms.ordernum = atoi(data.data);
311 	req.map_parms.domain = yppush_domain;
312 	req.map_parms.peer = yppush_master;
313 	req.map_parms.map = job->map;
314 	req.transid = job->tid;
315 	req.prog = job->prognum;
316 	req.port = job->port;
317 
318 	/* Get a handle to the remote ypserv. */
319 	if ((clnt = clnt_create(job->server, YPPROG, YPVERS, "udp")) == NULL) {
320 		yp_error("%s: %s",job->server,clnt_spcreateerror("couldn't \
321 create udp handle to NIS server"));
322 		switch (rpc_createerr.cf_stat) {
323 			case RPC_UNKNOWNHOST:
324 				job->stat = YPPUSH_NOHOST;
325 				break;
326 			case RPC_PMAPFAILURE:
327 				job->stat = YPPUSH_PMAP;
328 				break;
329 			default:
330 				job->stat = YPPUSH_RPC;
331 				break;
332 			}
333 		return(1);
334 	}
335 
336 	/*
337 	 * Reduce timeout to nothing since we may not
338 	 * get a response from ypserv and we don't want to block.
339 	 */
340 	if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE)
341 		yp_error("failed to set timeout on ypproc_xfr call");
342 
343 	/* Invoke the ypproc_xfr service. */
344 	if (ypproc_xfr_2(&req, clnt) == NULL) {
345 		clnt_geterr(clnt, &err);
346 		if (err.re_status != RPC_SUCCESS &&
347 		    err.re_status != RPC_TIMEDOUT) {
348 			yp_error("%s: %s", job->server, clnt_sperror(clnt,
349 							"yp_xfr failed"));
350 			job->stat = YPPUSH_YPSERV;
351 			clnt_destroy(clnt);
352 			return(1);
353 		}
354 	}
355 
356 	clnt_destroy(clnt);
357 
358 	return(0);
359 }
360 
361 /*
362  * Main driver function. Register the callback service, add the transfer
363  * request to the internal list, send the YPPROC_XFR request to ypserv
364  * do other magic things.
365  */
366 static int
367 yp_push(char *server, char *map, unsigned long tid)
368 {
369 	unsigned long prognum;
370 	int sock = RPC_ANYSOCK;
371 	SVCXPRT *xprt;
372 	struct jobs *job;
373 
374 	/* Register the job in our linked list of jobs. */
375 
376 	/* First allocate job structure */
377 	if ((job = (struct jobs *)malloc(sizeof (struct jobs))) == NULL) {
378 		yp_error("malloc failed");
379 		yppush_exit (1);
380 	}
381 
382 	/*
383 	 * Register the callback service on the first free transient
384 	 * program number.
385 	 */
386 	xprt = svcudp_create(sock);
387 	for (prognum = 0x40000000; prognum < 0x5FFFFFFF; prognum++) {
388 		if (svc_register(xprt, prognum, 1,
389 		    yppush_xfrrespprog_1, IPPROTO_UDP) == TRUE)
390 			break;
391 	}
392 	if (prognum == 0x5FFFFFFF) {
393 		yp_error ("can't register yppush_xfrrespprog_1");
394 		yppush_exit (1);
395 	}
396 
397 	/* Initialize the info for this job. */
398 	job->stat = 0;
399 	job->tid = tid;
400 	job->port = xprt->xp_port;
401 	job->server = strdup(server);
402 	job->map = strdup(map);
403 	job->prognum = prognum;
404 	job->polled = 0;
405 	job->next = yppush_joblist;
406 	yppush_joblist = job;
407 
408 	if (verbose) {
409 		yp_error("initiating transfer: %s -> %s (transid = %lu)",
410 			yppush_mapname, server, tid);
411 	}
412 
413 	/*
414 	 * Send the XFR request to ypserv. We don't have to wait for
415 	 * a response here since we handle them asynchronously.
416 	 */
417 
418 	if (yppush_send_xfr(job)){
419 		/* Transfer request blew up. */
420 		yppush_show_status(job->stat ? job->stat :
421 			YPPUSH_YPSERV,job->tid);
422 	} else {
423 		if (verbose > 1)
424 			yp_error("%s has been called", server);
425 	}
426 
427 	return(0);
428 }
429 
430 /*
431  * Called for each entry in the ypservers map from yp_get_map(), which
432  * is our private yp_all() routine.
433  */
434 static int
435 yppush_foreach(int status, char *key, int keylen, char *val, int vallen,
436     char *data)
437 {
438 	char *server;
439 
440 	if (status != YP_TRUE)
441 		return (status);
442 
443 	asprintf(&server, "%.*s", vallen, val);
444 
445 	/*
446 	 * Do not stop the iteration on the allocation failure.  We
447 	 * cannot usefully react on low memory condition anyway, and
448 	 * the failure is more likely due to insane val.
449 	 */
450 	if (server == NULL)
451 		return (0);
452 
453 	if (skip_master && strcasecmp(server, yppush_master) == 0) {
454 		free(server);
455 		return (0);
456 	}
457 
458 	/*
459 	 * Restrict the number of concurrent jobs: if yppush_jobs number
460 	 * of jobs have already been dispatched and are still pending,
461 	 * wait for one of them to finish so we can reuse its slot.
462 	 */
463 	while (yppush_running_jobs >= yppush_jobs && (yppush_svc_run (yppush_timeout) > 0))
464 		;
465 
466 	/* Cleared for takeoff: set everything in motion. */
467 	if (yp_push(server, yppush_mapname, yppush_transid)) {
468 		free(server);
469 		return(yp_errno);
470 	}
471 
472 	/* Bump the job counter and transaction ID. */
473 	yppush_running_jobs++;
474 	yppush_transid++;
475 	free(server);
476 	return (0);
477 }
478 
479 static void
480 usage()
481 {
482 	fprintf (stderr, "%s\n%s\n",
483 	"usage: yppush [-d domain] [-t timeout] [-j #parallel jobs] [-h host]",
484 	"              [-p path] mapname");
485 	exit(1);
486 }
487 
488 /*
489  * Entry point. (About time!)
490  */
491 int
492 main(int argc, char *argv[])
493 {
494 	int ch;
495 	DBT key, data;
496 	char myname[MAXHOSTNAMELEN];
497 	struct hostlist {
498 		char *name;
499 		struct hostlist *next;
500 	};
501 	struct hostlist *yppush_hostlist = NULL;
502 	struct hostlist *tmp;
503 
504 	while ((ch = getopt(argc, argv, "d:j:p:h:t:v")) != -1) {
505 		switch (ch) {
506 		case 'd':
507 			yppush_domain = optarg;
508 			break;
509 		case 'j':
510 			yppush_jobs = atoi(optarg);
511 			if (yppush_jobs <= 0)
512 				yppush_jobs = 1;
513 			break;
514 		case 'p':
515 			yp_dir = optarg;
516 			break;
517 		case 'h': /* we can handle multiple hosts */
518 			if ((tmp = (struct hostlist *)malloc(sizeof(struct hostlist))) == NULL) {
519 				yp_error("malloc failed");
520 				yppush_exit(1);
521 			}
522 			tmp->name = strdup(optarg);
523 			tmp->next = yppush_hostlist;
524 			yppush_hostlist = tmp;
525 			break;
526 		case 't':
527 			yppush_timeout = atoi(optarg);
528 			break;
529 		case 'v':
530 			verbose++;
531 			break;
532 		default:
533 			usage();
534 			break;
535 		}
536 	}
537 
538 	argc -= optind;
539 	argv += optind;
540 
541 	yppush_mapname = argv[0];
542 
543 	if (yppush_mapname == NULL) {
544 	/* "No guts, no glory." */
545 		usage();
546 	}
547 
548 	/*
549 	 * If no domain was specified, try to find the default
550 	 * domain. If we can't find that, we're doomed and must bail.
551 	 */
552 	if (yppush_domain == NULL) {
553 		char *yppush_check_domain;
554 		if (!yp_get_default_domain(&yppush_check_domain) &&
555 			!_yp_check(&yppush_check_domain)) {
556 			yp_error("no domain specified and NIS not running");
557 			usage();
558 		} else
559 			yp_get_default_domain(&yppush_domain);
560 	}
561 
562 	/* Check to see that we are the master for this map. */
563 
564 	if (gethostname ((char *)&myname, sizeof(myname))) {
565 		yp_error("failed to get name of local host: %s",
566 			strerror(errno));
567 		yppush_exit(1);
568 	}
569 
570 	key.data = "YP_MASTER_NAME";
571 	key.size = sizeof("YP_MASTER_NAME") - 1;
572 
573 	if (yp_get_record(yppush_domain, yppush_mapname,
574 			  &key, &data, 1) != YP_TRUE) {
575 		yp_error("couldn't open %s map: %s", yppush_mapname,
576 			 strerror(errno));
577 		yppush_exit(1);
578 	}
579 
580 	if (strncasecmp(myname, data.data, data.size) == 0) {
581 		/* I am master server, and no explicit host list was
582 		   specified: do not push map to myself -- this will
583 		   fail with YPPUSH_AGE anyway. */
584 		if (yppush_hostlist == NULL)
585 			skip_master = 1;
586 	} else {
587 		yp_error("warning: this host is not the master for %s",
588 							yppush_mapname);
589 #ifdef NITPICKY
590 		yppush_exit(1);
591 #endif
592 	}
593 
594 	yppush_master = malloc(data.size + 1);
595 	strncpy(yppush_master, data.data, data.size);
596 	yppush_master[data.size] = '\0';
597 
598 	/* Install some handy handlers. */
599 	signal(SIGTERM, handler);
600 	signal(SIGINT, handler);
601 
602 	/* set initial transaction ID */
603 	yppush_transid = time((time_t *)NULL);
604 
605 	if (yppush_hostlist) {
606 	/*
607 	 * Host list was specified on the command line:
608 	 * kick off the transfers by hand.
609 	 */
610 		tmp = yppush_hostlist;
611 		while (tmp) {
612 			yppush_foreach(YP_TRUE, NULL, 0, tmp->name,
613 			    strlen(tmp->name), NULL);
614 			tmp = tmp->next;
615 		}
616 	} else {
617 	/*
618 	 * Do a yp_all() on the ypservers map and initiate a ypxfr
619 	 * for each one.
620 	 */
621 		ypxfr_get_map("ypservers", yppush_domain,
622 			      "localhost", yppush_foreach);
623 	}
624 
625 	if (verbose > 1)
626 		yp_error("all jobs dispatched");
627 
628 	/* All done -- normal exit. */
629 	yppush_exit(0);
630 
631 	/* Just in case. */
632 	exit(0);
633 }
634