Home | History | Annotate | Line # | Download | only in yppush
yppush.c revision 1.12.2.1
      1 /*	$NetBSD: yppush.c,v 1.12.2.1 2000/09/19 17:45:12 thorpej Exp $	*/
      2 
      3 /*
      4  *
      5  * Copyright (c) 1997 Charles D. Cranor
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * yppush
     33  * author: Chuck Cranor <chuck (at) ccrc.wustl.edu>
     34  * date: 05-Nov-97
     35  *
     36  * notes: this is a full rewrite of Mats O Jansson <moj (at) stacken.kth.se>'s
     37  * yppush.c.   i have restructured and cleaned up the entire file.
     38  */
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/stat.h>
     42 #include <sys/time.h>
     43 #include <sys/wait.h>
     44 
     45 #include <ctype.h>
     46 #include <err.h>
     47 #include <errno.h>
     48 #include <fcntl.h>
     49 #include <signal.h>
     50 #include <stdio.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 
     54 #include <rpc/rpc.h>
     55 #include <rpcsvc/yp_prot.h>
     56 #include <rpcsvc/ypclnt.h>
     57 
     58 #include "ypdb.h"
     59 #include "ypdef.h"
     60 #include "yplib_host.h"
     61 #include "yppush.h"
     62 
     63 /*
     64  * yppush: push a new YP map out YP servers
     65  *
     66  * usage:
     67  *   yppush [-d domain] [-h host] [-v] mapname
     68  *
     69  *   -d: the domainname the map lives in [if different from default]
     70  *   -h: push only to this host [otherwise, push to all hosts]
     71  *   -v: verbose
     72  */
     73 
     74 /*
     75  * structures
     76  */
     77 
     78 struct yppush_info {
     79 	char   *ourdomain;	/* domain of interest */
     80 	char   *map;		/* map we are pushing */
     81 	char   *owner;		/* owner of map */
     82 	int     order;		/* order number of map (version) */
     83 };
     84 /*
     85  * global vars
     86  */
     87 
     88 extern char *__progname;	/* from crt0.o */
     89 int     verbo = 0;		/* verbose */
     90 
     91 /*
     92  * prototypes
     93  */
     94 
     95 int	main __P((int, char *[]));
     96 int	pushit __P((int, char *, int, char *, int, char *));
     97 void	push __P((char *, int, struct yppush_info *));
     98 void	_svc_run __P((void));
     99 void	usage __P((void));
    100 
    101 
    102 /*
    103  * main
    104  */
    105 
    106 int
    107 main(argc, argv)
    108 	int     argc;
    109 	char   *argv[];
    110 
    111 {
    112 	char   *targhost = NULL;
    113 	struct yppush_info ypi = {NULL, NULL, NULL, 0};
    114 	int     c, rv;
    115 	const char *cp;
    116 	char   *master;
    117 	DBM    *ypdb;
    118 	datum   datum;
    119 	CLIENT *ypserv;
    120 	struct timeval tv;
    121 	enum clnt_stat retval;
    122 	struct ypall_callback ypallcb;
    123 
    124 	/*
    125          * parse command line
    126          */
    127 	while ((c = getopt(argc, argv, "d:h:v")) != -1) {
    128 		switch (c) {
    129 		case 'd':
    130 			ypi.ourdomain = optarg;
    131 			break;
    132 		case 'h':
    133 			targhost = optarg;
    134 			break;
    135 		case 'v':
    136 			verbo = 1;
    137 			break;
    138 		default:
    139 			usage();
    140 			/* NOTREACHED */
    141 		}
    142 	}
    143 	argc -= optind;
    144 	argv += optind;
    145 	if (argc != 1)
    146 		usage();
    147 	ypi.map = argv[0];
    148 	if (strlen(ypi.map) > YPMAXMAP)
    149 		errx(1, "%s: map name too long (limit %d)", ypi.map, YPMAXMAP);
    150 
    151 	/*
    152          * ensure we have a domain
    153          */
    154 	if (ypi.ourdomain == NULL) {
    155 		c = yp_get_default_domain(&ypi.ourdomain);
    156 		if (ypi.ourdomain == NULL)
    157 			errx(1, "unable to get default domain: %s",
    158 			    yperr_string(c));
    159 	}
    160 	/*
    161          * verify that the domain and specified database exsists
    162          *
    163          * XXXCDC: this effectively prevents us from pushing from any
    164          * host but the master.   an alternate plan is to find the master
    165          * host for a map, clear it, ask for the order number, and then
    166          * send xfr requests.   if that was used we would not need local
    167          * file access.
    168          */
    169 	if (chdir(YP_DB_PATH) < 0)
    170 		err(1, "%s", YP_DB_PATH);
    171 	if (chdir(ypi.ourdomain) < 0)
    172 		err(1, "%s/%s", YP_DB_PATH, ypi.ourdomain);
    173 
    174 	/*
    175          * now open the database so we can extract "order number"
    176          * (i.e. timestamp) of the map.
    177          */
    178 	ypdb = ypdb_open(ypi.map, 0, O_RDONLY);
    179 	if (ypdb == NULL)
    180 		err(1, "ypdb_open %s/%s/%s", YP_DB_PATH, ypi.ourdomain,
    181 		    ypi.map);
    182 	datum.dptr = YP_LAST_KEY;
    183 	datum.dsize = YP_LAST_LEN;
    184 	datum = ypdb_fetch(ypdb, datum);
    185 	if (datum.dptr == NULL)
    186 		errx(1,
    187 		    "unable to fetch %s key: check database with 'makedbm -u'",
    188 		    YP_LAST_KEY);
    189 	ypi.order = 0;
    190 	cp = datum.dptr;
    191 	while (cp < datum.dptr + datum.dsize) {
    192 		if (!isdigit(*cp))
    193 			errx(1,
    194 		    "invalid order number: check database with 'makedbm -u'");
    195 		ypi.order = (ypi.order * 10) + *cp - '0';
    196 		cp++;
    197 	}
    198 	ypdb_close(ypdb);
    199 
    200 	if (verbo)
    201 		printf("pushing %s [order=%d] in domain %s\n", ypi.map,
    202 		    ypi.order, ypi.ourdomain);
    203 
    204 	/*
    205          * ok, we are ready to do it.   first we send a clear_2 request
    206          * to the local server [should be the master] to make sure it has
    207          * the correct database open.
    208          *
    209          * XXXCDC: note that yp_bind_local exits on failure so ypserv can't
    210          * be null.   this makes it difficult to print a useful error message.
    211          * [it will print "clntudp_create: no contact with localhost"]
    212          */
    213 	tv.tv_sec = 10;
    214 	tv.tv_usec = 0;
    215 	ypserv = yp_bind_local(YPPROG, YPVERS);
    216 	retval = clnt_call(ypserv, YPPROC_CLEAR, xdr_void, 0, xdr_void, 0, tv);
    217 	if (retval != RPC_SUCCESS)
    218 		errx(1, "clnt_call CLEAR to local ypserv: %s",
    219 		    clnt_sperrno(retval));
    220 	clnt_destroy(ypserv);
    221 
    222 	/*
    223          * now use normal yplib functions to bind to the domain.
    224          */
    225 	rv = yp_bind(ypi.ourdomain);
    226 	if (rv)
    227 		errx(1, "error binding to %s: %s", ypi.ourdomain,
    228 		    yperr_string(rv));
    229 
    230 	/*
    231          * find 'owner' of the map (see pushit for usage)
    232          */
    233 	rv = yp_master(ypi.ourdomain, ypi.map, &ypi.owner);
    234 	if (rv)
    235 		errx(1, "error finding master for %s in %s: %s", ypi.map,
    236 		    ypi.ourdomain, yperr_string(rv));
    237 
    238 	/*
    239          * inform user of our progress
    240          */
    241 	if (verbo) {
    242 		printf("pushing map %s in %s: order=%d, owner=%s\n", ypi.map,
    243 		    ypi.ourdomain, ypi.order, ypi.owner);
    244 		printf("pushing to %s\n",
    245 		    (targhost) ? targhost : "<all ypservs>");
    246 	}
    247 
    248 	/*
    249          * finally, do it.
    250          */
    251 	if (targhost) {
    252 		push(targhost, strlen(targhost), &ypi);
    253 	} else {
    254 
    255 		/*
    256 	         * no host specified, do all hosts the master knows about via
    257 	         * the ypservers map.
    258 	         */
    259 		rv = yp_master(ypi.ourdomain, "ypservers", &master);
    260 		if (rv)
    261 			errx(1, "error finding master for ypservers in %s: %s",
    262 			    ypi.ourdomain, yperr_string(rv));
    263 
    264 		if (verbo)
    265 			printf(
    266 		"contacting ypservers %s master on %s for list of ypservs...\n",
    267 			    ypi.ourdomain, master);
    268 
    269 		ypserv = yp_bind_host(master, YPPROG, YPVERS, 0, 1);
    270 
    271 		ypallcb.foreach = pushit;	/* callback function */
    272 		ypallcb.data = (char *) &ypi;	/* data to pass into callback */
    273 
    274 		rv = yp_all_host(ypserv, ypi.ourdomain, "ypservers", &ypallcb);
    275 		if (rv)
    276 			errx(1, "pushing %s in %s failed: %s", ypi.map,
    277 			    ypi.ourdomain, yperr_string(rv));
    278 	}
    279 	exit(0);
    280 }
    281 
    282 /*
    283  * usage: print usage and exit
    284  */
    285 void
    286 usage()
    287 {
    288 	fprintf(stderr, "usage: %s [-d domain] [-h host] [-v] map\n",
    289 	    __progname);
    290 	exit(1);
    291 }
    292 
    293 /*
    294  * pushit: called from yp_all_host to push a specific host.
    295  * the key/value pairs are from the ypservers map.
    296  */
    297 int
    298 pushit(instatus, inkey, inkeylen, inval, invallen, indata)
    299 	int     instatus, inkeylen, invallen;
    300 	char   *inkey, *inval, *indata;
    301 {
    302 	struct yppush_info *ypi = (struct yppush_info *) indata;
    303 
    304 	push(inkey, inkeylen, ypi);		/* do it! */
    305 	return (0);
    306 }
    307 
    308 /*
    309  * push: push a specific map on a specific host
    310  */
    311 void
    312 push(host, hostlen, ypi)
    313 	char   *host;
    314 	int     hostlen;
    315 	struct yppush_info *ypi;
    316 {
    317 	char    target[YPMAXPEER];
    318 	CLIENT *ypserv;
    319 	SVCXPRT *transp;
    320 	int     prog, pid, rv;
    321 	struct timeval tv;
    322 	struct ypreq_xfr req;
    323 
    324 	/*
    325          * get our target host in a null terminated string
    326          */
    327 	snprintf(target, sizeof(target), "%*.*s", hostlen, hostlen, host);
    328 
    329 	/*
    330          * XXXCDC: arg!  we would like to use yp_bind_host here, except that
    331          * it exits on failure and we don't want to give up just because
    332          * one host fails.  thus, we have to do it the hard way.
    333          */
    334 	ypserv = clnt_create(target, YPPROG, YPVERS, "tcp");
    335 	if (ypserv == NULL) {
    336 		clnt_pcreateerror(target);
    337 		return;
    338 	}
    339 
    340 	/*
    341          * our XFR rpc request to the client just starts the transfer.
    342          * when the client is done, it wants to call a procedure that
    343          * we are serving to tell us that it is done.   so we must create
    344          * and register a procedure for us for it to call.
    345          */
    346 	transp = svcudp_create(RPC_ANYSOCK);
    347 	if (transp == NULL) {
    348 		warnx("callback svcudp_create failed");
    349 		goto error;
    350 	}
    351 
    352 	/* register it with portmap */
    353 	for (prog = 0x40000000; prog < 0x5fffffff; prog++) {
    354 		if (svc_register(transp, prog, 1, yppush_xfrrespprog_1,
    355 		    IPPROTO_UDP))
    356 			break;
    357 	}
    358 	if (prog >= 0x5fffffff) {
    359 		warnx("unable to register callback");
    360 		goto error;
    361 	}
    362 
    363 	/*
    364          * now fork off a server to catch our reply
    365          */
    366 	pid = fork();
    367 	if (pid == -1) {
    368 		svc_unregister(prog, 1);	/* drop our mapping with
    369 						 * portmap */
    370 		warn("fork failed");
    371 		goto error;
    372 	}
    373 
    374 	/*
    375          * child process becomes the server
    376          */
    377 	if (pid == 0) {
    378 		_svc_run();
    379 		exit(0);
    380 	}
    381 
    382 	/*
    383          * we are the parent process: send XFR request to server.
    384          * the "owner" field isn't used by ypserv (and shouldn't be, since
    385          * the ypserv has no idea if we are a legitimate yppush or not).
    386          * instead, the owner of the map is determined by the master value
    387          * currently cached on the slave server.
    388          */
    389 	close(transp->xp_fd);	/* close child's socket, we don't need it */
    390 	/* don't wait for anything here, we will wait for child's exit */
    391 	tv.tv_sec = 0;
    392 	tv.tv_usec = 0;
    393 	req.map_parms.domain = ypi->ourdomain;
    394 	req.map_parms.map = ypi->map;
    395 	req.map_parms.owner = ypi->owner;	/* NOT USED */
    396 	req.map_parms.ordernum = ypi->order;
    397 	req.transid = (u_int) pid;
    398 	req.proto = prog;
    399 	req.port = transp->xp_port;
    400 
    401 	if (verbo)
    402 		printf("asking host %s to transfer map (xid=%d)\n", target,
    403 		    req.transid);
    404 
    405 	rv = clnt_call(ypserv, YPPROC_XFR, xdr_ypreq_xfr, &req,
    406 	    		xdr_void, NULL, tv);			/* do it! */
    407 
    408 	if (rv != RPC_SUCCESS && rv != RPC_TIMEDOUT) {
    409 		warnx("unable to xfr to host %s: %s", target, clnt_sperrno(rv));
    410 		kill(pid, SIGTERM);
    411 	}
    412 
    413 	/*
    414          * now wait for child to get the reply and exit
    415          */
    416 	wait4(pid, NULL, 0, NULL);
    417 	svc_unregister(prog, 1);
    418 
    419 	/*
    420          * ... and we are done.   fall through
    421          */
    422 
    423 error:
    424 	if (transp)
    425 		svc_destroy(transp);
    426 	clnt_destroy(ypserv);
    427 	return;
    428 }
    429 
    430 /*
    431  * _svc_run: this is the main loop for the RPC server that we fork off
    432  * to await the reply from ypxfr.
    433  */
    434 void
    435 _svc_run()
    436 {
    437 	fd_set  readfds;
    438 	struct timeval tv;
    439 	int     rv, nfds;
    440 
    441 	nfds = sysconf(_SC_OPEN_MAX);
    442 	while (1) {
    443 
    444 		readfds = svc_fdset;	/* structure copy from global var */
    445 		tv.tv_sec = 60;
    446 		tv.tv_usec = 0;
    447 
    448 		rv = select(nfds, &readfds, NULL, NULL, &tv);
    449 
    450 		if (rv < 0) {
    451 			if (errno == EINTR)
    452 				continue;
    453 			warn("_svc_run: select failed");
    454 			return;
    455 		}
    456 		if (rv == 0)
    457 			errx(0, "_svc_run: callback timed out");
    458 
    459 		/*
    460 	         * got something
    461 	         */
    462 		svc_getreqset(&readfds);
    463 
    464 	}
    465 }
    466