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