Home | History | Annotate | Line # | Download | only in yppush
yppush.c revision 1.9
      1 /*	$NetBSD: yppush.c,v 1.9 1997/11/18 22:58:00 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/errno.h>
     41 #include <sys/param.h>
     42 #include <sys/stat.h>
     43 #include <sys/time.h>
     44 #include <sys/wait.h>
     45 
     46 #include <ctype.h>
     47 #include <err.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 	ypdb_close(ypdb);
    186 	if (datum.dptr == NULL)
    187 		errx(1,
    188 		    "unable to fetch %s key: check database with 'makedbm -u'",
    189 		    YP_LAST_KEY);
    190 	ypi.order = 0;
    191 	cp = datum.dptr;
    192 	while (cp < datum.dptr + datum.dsize) {
    193 		if (!isdigit(*cp))
    194 			errx(1,
    195 		    "invalid order number: check database with 'makedbm -u'");
    196 		ypi.order = (ypi.order * 10) + *cp - '0';
    197 		cp++;
    198 	}
    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 	if (instatus != YP_TRUE)		/* failure? */
    305 		return (instatus);
    306 
    307 	push(inkey, inkeylen, ypi);		/* do it! */
    308 	return (0);
    309 }
    310 
    311 /*
    312  * push: push a specific map on a specific host
    313  */
    314 void
    315 push(host, hostlen, ypi)
    316 	char   *host;
    317 	int     hostlen;
    318 	struct yppush_info *ypi;
    319 {
    320 	char    target[YPMAXPEER];
    321 	CLIENT *ypserv;
    322 	SVCXPRT *transp;
    323 	int     prog, pid, rv;
    324 	struct timeval tv;
    325 	struct ypreq_xfr req;
    326 
    327 	/*
    328          * get our target host in a null terminated string
    329          */
    330 	snprintf(target, sizeof(target), "%*.*s", hostlen, hostlen, host);
    331 
    332 	/*
    333          * XXXCDC: arg!  we would like to use yp_bind_host here, except that
    334          * it exits on failure and we don't want to give up just because
    335          * one host fails.  thus, we have to do it the hard way.
    336          */
    337 	ypserv = clnt_create(target, YPPROG, YPVERS, "tcp");
    338 	if (ypserv == NULL) {
    339 		clnt_pcreateerror(target);
    340 		return;
    341 	}
    342 
    343 	/*
    344          * our XFR rpc request to the client just starts the transfer.
    345          * when the client is done, it wants to call a procedure that
    346          * we are serving to tell us that it is done.   so we must create
    347          * and register a procedure for us for it to call.
    348          */
    349 	transp = svcudp_create(RPC_ANYSOCK);
    350 	if (transp == NULL) {
    351 		warnx("callback svcudp_create failed");
    352 		goto error;
    353 	}
    354 
    355 	/* register it with portmap */
    356 	for (prog = 0x40000000; prog < 0x5fffffff; prog++) {
    357 		if (svc_register(transp, prog, 1, yppush_xfrrespprog_1,
    358 		    IPPROTO_UDP))
    359 			break;
    360 	}
    361 	if (prog >= 0x5fffffff) {
    362 		warnx("unable to register callback");
    363 		goto error;
    364 	}
    365 
    366 	/*
    367          * now fork off a server to catch our reply
    368          */
    369 	pid = fork();
    370 	if (pid == -1) {
    371 		svc_unregister(prog, 1);	/* drop our mapping with
    372 						 * portmap */
    373 		warn("fork failed");
    374 		goto error;
    375 	}
    376 
    377 	/*
    378          * child process becomes the server
    379          */
    380 	if (pid == 0) {
    381 		_svc_run();
    382 		exit(0);
    383 	}
    384 
    385 	/*
    386          * we are the parent process: send XFR request to server.
    387          * the "owner" field isn't used by ypserv (and shouldn't be, since
    388          * the ypserv has no idea if we are a legitimate yppush or not).
    389          * instead, the owner of the map is determined by the master value
    390          * currently cached on the slave server.
    391          */
    392 	close(transp->xp_sock);	/* close child's socket, we don't need it */
    393 	/* don't wait for anything here, we will wait for child's exit */
    394 	tv.tv_sec = 0;
    395 	tv.tv_usec = 0;
    396 	req.map_parms.domain = ypi->ourdomain;
    397 	req.map_parms.map = ypi->map;
    398 	req.map_parms.owner = ypi->owner;	/* NOT USED */
    399 	req.map_parms.ordernum = ypi->order;
    400 	req.transid = (u_int) pid;
    401 	req.proto = prog;
    402 	req.port = transp->xp_port;
    403 
    404 	if (verbo)
    405 		printf("asking host %s to transfer map (xid=%d)\n", target,
    406 		    req.transid);
    407 
    408 	rv = clnt_call(ypserv, YPPROC_XFR, xdr_ypreq_xfr, &req,
    409 	    		xdr_void, NULL, tv);			/* do it! */
    410 
    411 	if (rv != RPC_SUCCESS && rv != RPC_TIMEDOUT) {
    412 		warnx("unable to xfr to host %s: %s", target, clnt_sperrno(rv));
    413 		kill(pid, SIGTERM);
    414 	}
    415 
    416 	/*
    417          * now wait for child to get the reply and exit
    418          */
    419 	wait4(pid, NULL, 0, NULL);
    420 	svc_unregister(prog, 1);
    421 
    422 	/*
    423          * ... and we are done.   fall through
    424          */
    425 
    426 error:
    427 	if (transp)
    428 		svc_destroy(transp);
    429 	clnt_destroy(ypserv);
    430 	return;
    431 }
    432 
    433 /*
    434  * _svc_run: this is the main loop for the RPC server that we fork off
    435  * to await the reply from ypxfr.
    436  */
    437 void
    438 _svc_run()
    439 {
    440 	fd_set  readfds;
    441 	struct timeval tv;
    442 	int     rv, nfds;
    443 
    444 	nfds = sysconf(_SC_OPEN_MAX);
    445 	while (1) {
    446 
    447 		readfds = svc_fdset;	/* structure copy from global var */
    448 		tv.tv_sec = 60;
    449 		tv.tv_usec = 0;
    450 
    451 		rv = select(nfds, &readfds, NULL, NULL, &tv);
    452 
    453 		if (rv < 0) {
    454 			if (errno == EINTR)
    455 				continue;
    456 			warn("_svc_run: select failed");
    457 			return;
    458 		}
    459 		if (rv == 0)
    460 			errx(0, "_svc_run: callback timed out");
    461 
    462 		/*
    463 	         * got something
    464 	         */
    465 		svc_getreqset(&readfds);
    466 
    467 	}
    468 }
    469