Home | History | Annotate | Line # | Download | only in yppush
yppush.c revision 1.2
      1 /*	$NetBSD: yppush.c,v 1.2 1996/08/09 20:18:50 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Mats O Jansson <moj (at) stacken.kth.se>
      5  * 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 Mats O Jansson
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/types.h>
     35 #include <sys/stat.h>
     36 #include <sys/resource.h>
     37 #include <sys/signal.h>
     38 
     39 #include <errno.h>
     40 #include <err.h>
     41 #include <fcntl.h>
     42 #include <netdb.h>
     43 #include <stdio.h>
     44 #include <string.h>
     45 #include <unistd.h>
     46 
     47 #include <rpc/rpc.h>
     48 #include <rpc/xdr.h>
     49 #include <rpcsvc/yp_prot.h>
     50 #include <rpcsvc/ypclnt.h>
     51 
     52 #include "yplib_host.h"
     53 #include "ypdef.h"
     54 #include "ypdb.h"
     55 #include "yppush.h"
     56 
     57 extern	char *__progname;		/* from crt0.o */
     58 extern	char *optarg;
     59 extern	int optind;
     60 
     61 int	pushit __P((int, char *, int, char *, int, char *));
     62 void	push __P((int, char *));
     63 void	req_xfr __P((pid_t, u_int, SVCXPRT *, char *, CLIENT *));
     64 void	_svc_run __P((void));
     65 void	cleanup __P((void));
     66 void	usage __P((void));
     67 
     68 int	Verbose = 0;
     69 u_int	OrderNum;
     70 
     71 char	Domain[YPMAXDOMAIN];
     72 char	Map[YPMAXMAP];
     73 char	LocalHost[YPMAXPEER];
     74 
     75 static	DBM *yp_databas;
     76 
     77 int
     78 main(argc, argv)
     79 	int  argc;
     80 	char **argv;
     81 {
     82 	struct ypall_callback ypcb;
     83 	char *master, *domain, *map, *hostname;
     84 	int c, r, i;
     85 	datum o;
     86 	char *ypmap = "ypservers";
     87 	CLIENT *client;
     88 	struct stat finfo;
     89 	char order_key[YP_LAST_LEN] = YP_LAST_KEY;
     90 	static char map_path[MAXPATHLEN];
     91 
     92 	if (atexit(cleanup))
     93 		err(1, "can't register cleanup function");
     94 
     95         yp_get_default_domain(&domain);
     96 	hostname = NULL;
     97 
     98 	while ((c = getopt(argc, argv, "d:h:v")) != -1) {
     99 		switch(c) {
    100 		case 'd':
    101 			domain = optarg;
    102 			break;
    103 
    104 		case 'h':
    105 			hostname = optarg;
    106 			break;
    107 
    108                 case 'v':
    109 			Verbose = 1;
    110 			break;
    111 
    112 		default:
    113                         usage();
    114                         /*NOTREACHED*/
    115 		}
    116 	}
    117 	argv += optind; argc -= optind;
    118 
    119 	if (argc != 1)
    120 		usage();
    121 
    122 	map = argv[0];
    123 
    124 	memset(Domain, 0, sizeof(Domain));
    125 	snprintf(Domain, sizeof(Domain), "%s", domain);
    126 	memset(Map, 0, sizeof(Map));
    127 	snprintf(Map, sizeof(Map), "%s", map);
    128 
    129 	/* Set up the hostname to be used for the xfr */
    130 	localhostname(LocalHost, sizeof(LocalHost));
    131 
    132 	/* Check domain */
    133 	sprintf(map_path,"%s/%s",YP_DB_PATH,domain);
    134 	if (!((stat(map_path, &finfo) == 0) &&
    135 	      ((finfo.st_mode & S_IFMT) == S_IFDIR))) {
    136 	  	fprintf(stderr,"yppush: Map does not exists.\n");
    137 		exit(1);
    138 	}
    139 
    140 	/* Check map */
    141 	memset(map_path, 0, sizeof(map_path));
    142 	snprintf(map_path, sizeof(map_path), "%s/%s/%s%s", YP_DB_PATH,
    143 	    Domain, Map, YPDB_SUFFIX);
    144 	if (stat(map_path, &finfo) != 0)
    145 		errx(1, "map does not exist");
    146 
    147 	/* Open the database */
    148 	memset(map_path, 0, sizeof(map_path));
    149 	snprintf(map_path, sizeof(map_path), "%s/%s/%s", YP_DB_PATH,
    150 	    Domain, Map);
    151 	yp_databas = ypdb_open(map_path, 0, O_RDONLY);
    152 	OrderNum=0xffffffff;
    153 	if (yp_databas == NULL)
    154 		errx(1, "%s%s: cannot open database", map_path, YPDB_SUFFIX);
    155 	else {
    156 		o.dptr = (char *) &order_key;
    157 		o.dsize = YP_LAST_LEN;
    158 		o = ypdb_fetch(yp_databas, o);
    159 		if (o.dptr == NULL)
    160 			errx(1, "%s: cannot determine order number", Map);
    161 		else {
    162 			OrderNum = 0;
    163 			for (i = 0; i < o.dsize - 1; i++)
    164 				if (!isdigit(o.dptr[i]))
    165 					OrderNum=0xffffffff;
    166 
    167 			if (OrderNum != 0)
    168 				errx(1, "%s: invalid order number `%s'",
    169 				    Map, o.dptr);
    170 			else
    171 				OrderNum = atoi(o.dptr);
    172 		}
    173         }
    174 
    175 	/* No longer need YP map. */
    176 	ypdb_close(yp_databas);
    177 	yp_databas = NULL;
    178 
    179 	r = yp_bind(Domain);
    180 	if (r != 0)
    181 		errx(1, "%s", yperr_string(r));
    182 
    183 	if (hostname != NULL)
    184 		push(strlen(hostname), hostname);
    185 	else {
    186 		r = yp_master(Domain, ypmap, &master);
    187 		if (r != 0)
    188 			errx(1, "%s (map = %s)", yperr_string(r), ypmap);
    189 
    190 		if (Verbose)
    191 			printf("Contacting master for ypservers (%s).\n",
    192 			    master);
    193 
    194 		client = yp_bind_host(master, YPPROG, YPVERS, 0, 1);
    195 
    196 		ypcb.foreach = (int(*)())pushit;
    197 		ypcb.data = NULL;
    198 
    199 		r = yp_all_host(client, Domain, ypmap, &ypcb);
    200 		if (r != 0)
    201 			errx(1, "%s (map = %s)", yperr_string(r), ypmap);
    202 	}
    203 	exit (0);
    204 }
    205 
    206 void
    207 usage()
    208 {
    209 
    210 	fprintf(stderr, "usage: %s [-d domainname] [-h host] [-v] mapname\n",
    211 	    __progname);
    212 	exit(1);
    213 }
    214 
    215 void
    216 _svc_run()
    217 {
    218 	fd_set readfds;
    219 	struct timeval timeout;
    220 
    221 	timeout.tv_sec = 60;
    222 	timeout.tv_usec = 0;
    223 
    224 	for(;;) {
    225 		readfds = svc_fdset;
    226 
    227 		switch (select(_rpc_dtablesize(), &readfds, NULL, NULL,
    228 		    &timeout)) {
    229 
    230 		case -1:
    231 			if (errno == EINTR)
    232 				continue;
    233 
    234 			warnx("_svc_run: select failed");
    235 			return;
    236 
    237 		case 0:
    238 			errx(0, "Callback timed out.");
    239 
    240 		default:
    241 			svc_getreqset(&readfds);
    242 		}
    243 	}
    244 }
    245 
    246 void
    247 req_xfr(pid, prog, transp, host, client)
    248 	pid_t pid;
    249 	u_int prog;
    250 	SVCXPRT *transp;
    251 	char *host;
    252 	CLIENT *client;
    253 {
    254 	struct ypreq_xfr request;
    255 	struct timeval tv;
    256 
    257 	tv.tv_sec = 0;
    258 	tv.tv_usec = 0;
    259 
    260 	request.map_parms.domain = &Domain[0];
    261 	request.map_parms.map = &Map[0];
    262 	request.map_parms.owner = &LocalHost[0];
    263 	request.map_parms.ordernum = OrderNum;
    264 	request.transid = (u_int)pid;
    265 	request.proto = prog;
    266 	request.port = transp->xp_port;
    267 
    268 	if (Verbose)
    269 		printf("%d: %s(%d@%s) -> %s@%s\n", request.transid,
    270 		    request.map_parms.map, request.map_parms.ordernum,
    271 		    host, request.map_parms.owner, request.map_parms.domain);
    272 
    273 	switch (clnt_call(client, YPPROC_XFR, xdr_ypreq_xfr, &request,
    274 	    xdr_void, NULL, tv)) {
    275 	case RPC_SUCCESS:
    276 	case RPC_TIMEDOUT:
    277 		break;
    278 
    279 	default:
    280 		clnt_perror(client, "yppush: Cannot call YPPROC_XFR");
    281 		kill(pid, SIGTERM);
    282 	}
    283 }
    284 
    285 void
    286 push(inlen, indata)
    287 	int inlen;
    288 	char *indata;
    289 {
    290 	char host[YPMAXPEER];
    291 	CLIENT *client;
    292 	SVCXPRT *transp;
    293 	int sock = RPC_ANYSOCK;
    294 	u_int prog;
    295 	bool_t sts;
    296 	pid_t pid;
    297 	int status;
    298 	struct rusage res;
    299 
    300 	memset(host, 0, sizeof(host));
    301 	snprintf(host, sizeof(host), "%*.*s", inlen, inlen, indata);
    302 
    303 	client = clnt_create(host, YPPROG, YPVERS, "tcp");
    304 	if (client == NULL) {
    305 		if (Verbose)
    306 			fprintf(stderr, "Target host: %s\n", host);
    307 		clnt_pcreateerror("yppush: cannot create client");
    308 		return;
    309 	}
    310 
    311 	transp = svcudp_create(sock);
    312 	if (transp == NULL) {
    313 		warnx("cannot create callback transport");
    314 		return;
    315 	}
    316 
    317 	for (prog = 0x40000000; prog < 0x5fffffff; prog++) {
    318 		if (sts = svc_register(transp, prog, 1,
    319 		    yppush_xfrrespprog_1, IPPROTO_UDP))
    320 			break;
    321 	}
    322 
    323 	if (sts == FALSE) {
    324 		warnx("cannot register callback");
    325 		return;
    326 	}
    327 
    328 	switch ((pid = fork())) {
    329 	case -1:
    330 		err(1, "fork failed");
    331 
    332 	case 0:
    333 		_svc_run();
    334 		exit(0);
    335 
    336 	default:
    337 		close(transp->xp_sock);
    338 		req_xfr(pid, prog, transp, host, client);
    339 		wait4(pid, &status, 0, &res);
    340 		svc_unregister(prog, 1);
    341 		if (client != NULL)
    342 		  	clnt_destroy(client);
    343 	}
    344 }
    345 
    346 int
    347 pushit(instatus, inkey, inkeylen, inval, invallen, indata)
    348 	int instatus;
    349 	char *inkey;
    350 	int inkeylen;
    351 	char *inval;
    352 	int invallen;
    353 	char *indata;
    354 {
    355 
    356 	if (instatus != YP_TRUE)
    357 		return instatus;
    358 
    359 	push(inkeylen, inkey);
    360 	return 0;
    361 }
    362 
    363 void
    364 cleanup()
    365 {
    366 
    367 	/* Make sure the map file is closed. */
    368 	if (yp_databas != NULL)
    369 		ypdb_close(yp_databas);
    370 }
    371