Home | History | Annotate | Line # | Download | only in ypbind
ypbind.c revision 1.24
      1  1.24  christos /*	$NetBSD: ypbind.c,v 1.24 1996/06/22 17:59:52 christos Exp $	*/
      2  1.20       cgd 
      3   1.2   deraadt /*
      4   1.7   deraadt  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      5   1.2   deraadt  * All rights reserved.
      6   1.2   deraadt  *
      7   1.2   deraadt  * Redistribution and use in source and binary forms, with or without
      8   1.2   deraadt  * modification, are permitted provided that the following conditions
      9   1.2   deraadt  * are met:
     10   1.2   deraadt  * 1. Redistributions of source code must retain the above copyright
     11   1.2   deraadt  *    notice, this list of conditions and the following disclaimer.
     12   1.2   deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.2   deraadt  *    notice, this list of conditions and the following disclaimer in the
     14   1.2   deraadt  *    documentation and/or other materials provided with the distribution.
     15   1.7   deraadt  * 3. All advertising materials mentioning features or use of this software
     16   1.7   deraadt  *    must display the following acknowledgement:
     17   1.7   deraadt  *	This product includes software developed by Theo de Raadt.
     18   1.7   deraadt  * 4. The name of the author may not be used to endorse or promote
     19   1.2   deraadt  *    products derived from this software without specific prior written
     20   1.2   deraadt  *    permission.
     21   1.2   deraadt  *
     22   1.2   deraadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23   1.2   deraadt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24   1.2   deraadt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.2   deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     26   1.2   deraadt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.2   deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.2   deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.2   deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.2   deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.2   deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.2   deraadt  * SUCH DAMAGE.
     33   1.2   deraadt  */
     34   1.2   deraadt 
     35   1.2   deraadt #ifndef LINT
     36  1.24  christos static char rcsid[] = "$NetBSD: ypbind.c,v 1.24 1996/06/22 17:59:52 christos Exp $";
     37   1.2   deraadt #endif
     38   1.2   deraadt 
     39   1.1   deraadt #include <sys/param.h>
     40   1.1   deraadt #include <sys/types.h>
     41   1.1   deraadt #include <sys/ioctl.h>
     42   1.1   deraadt #include <sys/signal.h>
     43   1.1   deraadt #include <sys/socket.h>
     44   1.1   deraadt #include <sys/file.h>
     45   1.1   deraadt #include <sys/fcntl.h>
     46   1.6   deraadt #include <sys/uio.h>
     47   1.1   deraadt #include <sys/syslog.h>
     48  1.24  christos #include <sys/stat.h>
     49   1.1   deraadt #include <stdio.h>
     50  1.14       cgd #include <stdlib.h>
     51   1.1   deraadt #include <errno.h>
     52   1.1   deraadt #include <ctype.h>
     53   1.1   deraadt #include <dirent.h>
     54   1.1   deraadt #include <netdb.h>
     55   1.1   deraadt #include <string.h>
     56  1.24  christos #include <err.h>
     57   1.1   deraadt #include <rpc/rpc.h>
     58   1.1   deraadt #include <rpc/xdr.h>
     59   1.1   deraadt #include <net/if.h>
     60   1.1   deraadt #include <arpa/inet.h>
     61   1.1   deraadt #include <rpc/pmap_clnt.h>
     62   1.1   deraadt #include <rpc/pmap_prot.h>
     63   1.1   deraadt #include <rpc/pmap_rmt.h>
     64   1.1   deraadt #include <unistd.h>
     65   1.1   deraadt #include <rpcsvc/yp_prot.h>
     66   1.1   deraadt #include <rpcsvc/ypclnt.h>
     67   1.1   deraadt 
     68  1.24  christos #ifndef O_SHLOCK
     69  1.24  christos #define O_SHLOCK 0
     70  1.24  christos #endif
     71  1.24  christos 
     72  1.24  christos #define BUFSIZE		1400
     73  1.13   deraadt #define BINDINGDIR	"/var/yp/binding"
     74  1.13   deraadt #define YPBINDLOCK	"/var/run/ypbind.lock"
     75   1.1   deraadt 
     76   1.1   deraadt struct _dom_binding {
     77   1.1   deraadt 	struct _dom_binding *dom_pnext;
     78   1.1   deraadt 	char dom_domain[YPMAXDOMAIN + 1];
     79   1.1   deraadt 	struct sockaddr_in dom_server_addr;
     80   1.1   deraadt 	unsigned short int dom_server_port;
     81   1.1   deraadt 	int dom_socket;
     82   1.1   deraadt 	CLIENT *dom_client;
     83   1.1   deraadt 	long int dom_vers;
     84   1.1   deraadt 	time_t dom_check_t;
     85   1.9   deraadt 	time_t dom_ask_t;
     86   1.1   deraadt 	int dom_lockfd;
     87   1.1   deraadt 	int dom_alive;
     88  1.20       cgd 	int dom_xid;
     89   1.1   deraadt };
     90   1.1   deraadt 
     91  1.24  christos static char *domainname;
     92   1.1   deraadt 
     93  1.24  christos static struct _dom_binding *ypbindlist;
     94  1.24  christos static int check;
     95   1.1   deraadt 
     96   1.1   deraadt #define YPSET_NO	0
     97   1.1   deraadt #define YPSET_LOCAL	1
     98   1.1   deraadt #define YPSET_ALL	2
     99  1.24  christos static int ypsetmode = YPSET_NO;
    100  1.24  christos #ifdef DEBUG
    101  1.24  christos static int debug;
    102  1.24  christos #endif
    103  1.24  christos 
    104  1.24  christos static int rpcsock, pingsock;
    105  1.24  christos static struct rmtcallargs rmtca;
    106  1.24  christos static struct rmtcallres rmtcr;
    107  1.24  christos static char rmtcr_outval;
    108  1.24  christos static u_long rmtcr_port;
    109  1.24  christos static SVCXPRT *udptransp, *tcptransp;
    110  1.24  christos 
    111  1.24  christos 
    112  1.24  christos static void usage __P((void));
    113  1.24  christos static struct _dom_binding *makebinding __P((const char *));
    114  1.24  christos static int makelock __P((struct _dom_binding *));
    115  1.24  christos static void removelock __P((struct _dom_binding *));
    116  1.24  christos static void *ypbindproc_null_2 __P((SVCXPRT *, void *));
    117  1.24  christos static void *ypbindproc_domain_2 __P((SVCXPRT *, void *));
    118  1.24  christos static void *ypbindproc_setdom_2 __P((SVCXPRT *, void *));
    119  1.24  christos static void ypbindprog_2 __P((struct svc_req *, SVCXPRT *));
    120  1.24  christos static void checkwork __P((void));
    121  1.24  christos static int ping __P((struct _dom_binding *));
    122  1.24  christos static int broadcast __P((struct _dom_binding *));
    123  1.24  christos static enum clnt_stat handle_replies __P((void));
    124  1.24  christos static enum clnt_stat handle_ping __P((void));
    125  1.24  christos static void rpc_received __P((char *, struct sockaddr_in *, int));
    126  1.24  christos static struct _dom_binding *xid2ypdb __P((int));
    127  1.24  christos static int unique_xid __P((struct _dom_binding *));
    128  1.24  christos static struct _dom_binding *xid2ypdb __P((int xid));
    129  1.24  christos static int unique_xid __P((struct _dom_binding *ypdb));
    130  1.24  christos 
    131  1.24  christos static void
    132  1.24  christos usage()
    133  1.24  christos {
    134  1.24  christos 	extern char *__progname;
    135  1.24  christos 	char *opt = "";
    136  1.24  christos #ifdef DEBUG
    137  1.24  christos 	opt = " [-d]";
    138  1.24  christos #endif
    139  1.24  christos 	(void) fprintf(stderr, "Usage: %s [-ypset|-ypsetme]%s\n",
    140  1.24  christos 	    __progname, opt);
    141  1.24  christos 	exit(1);
    142  1.24  christos }
    143  1.24  christos 
    144  1.24  christos static struct _dom_binding *
    145  1.24  christos makebinding(dm)
    146  1.24  christos 	const char *dm;
    147  1.24  christos {
    148  1.24  christos 	struct _dom_binding *ypdb;
    149   1.1   deraadt 
    150  1.24  christos 	if ((ypdb = (struct _dom_binding *)malloc(sizeof *ypdb)) == NULL)
    151  1.24  christos 		err(1, "makebinding");
    152   1.1   deraadt 
    153  1.24  christos 	(void) memset(ypdb, 0, sizeof *ypdb);
    154  1.24  christos 	(void) strncpy(ypdb->dom_domain, dm, sizeof ypdb->dom_domain);
    155  1.24  christos 	ypdb->dom_domain[sizeof(ypdb->dom_domain) - 1] = '\0';
    156  1.24  christos 	return ypdb;
    157  1.24  christos }
    158  1.24  christos 
    159  1.24  christos static int
    160  1.24  christos makelock(ypdb)
    161  1.24  christos 	struct _dom_binding *ypdb;
    162  1.24  christos {
    163  1.24  christos 	int fd;
    164  1.24  christos 	char path[MAXPATHLEN];
    165  1.20       cgd 
    166  1.24  christos 	(void) snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
    167  1.24  christos 	    ypdb->dom_domain, ypdb->dom_vers);
    168  1.24  christos 
    169  1.24  christos 	if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
    170  1.24  christos 		(void) mkdir(BINDINGDIR, 0755);
    171  1.24  christos 		if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
    172  1.24  christos 			return -1;
    173  1.24  christos 	}
    174  1.24  christos 
    175  1.24  christos #if O_SHLOCK == 0
    176  1.24  christos 	(void) flock(fd, LOCK_SH);
    177  1.24  christos #endif
    178  1.24  christos 	return fd;
    179  1.24  christos }
    180  1.24  christos 
    181  1.24  christos static void
    182  1.24  christos removelock(ypdb)
    183  1.24  christos 	struct _dom_binding *ypdb;
    184  1.24  christos {
    185  1.24  christos 	char path[MAXPATHLEN];
    186  1.24  christos 
    187  1.24  christos 	(void) snprintf(path, sizeof(path), "%s/%s.%ld",
    188  1.24  christos 	    BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
    189  1.24  christos 	(void) unlink(path);
    190  1.24  christos }
    191  1.24  christos 
    192  1.24  christos static void *
    193  1.24  christos ypbindproc_null_2(transp, argp)
    194  1.17   mycroft 	SVCXPRT *transp;
    195  1.17   mycroft 	void *argp;
    196   1.1   deraadt {
    197   1.1   deraadt 	static char res;
    198   1.1   deraadt 
    199  1.24  christos #ifdef DEBUG
    200  1.24  christos 	if (debug)
    201  1.24  christos 		printf("ypbindproc_null_2\n");
    202  1.24  christos #endif
    203  1.24  christos 	(void) memset(&res, 0, sizeof(res));
    204   1.1   deraadt 	return (void *)&res;
    205   1.1   deraadt }
    206   1.1   deraadt 
    207  1.24  christos static void *
    208  1.24  christos ypbindproc_domain_2(transp, argp)
    209  1.17   mycroft 	SVCXPRT *transp;
    210  1.24  christos 	void *argp;
    211   1.1   deraadt {
    212   1.1   deraadt 	static struct ypbind_resp res;
    213   1.1   deraadt 	struct _dom_binding *ypdb;
    214  1.24  christos 	char *arg = *(char **) argp;
    215   1.9   deraadt 	time_t now;
    216   1.1   deraadt 
    217  1.24  christos #ifdef DEBUG
    218  1.24  christos 	if (debug)
    219  1.24  christos 		printf("ypbindproc_domain_2 %s\n", arg);
    220  1.24  christos #endif
    221  1.24  christos 	(void) memset(&res, 0, sizeof res);
    222   1.1   deraadt 	res.ypbind_status = YPBIND_FAIL_VAL;
    223   1.1   deraadt 
    224  1.17   mycroft 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
    225  1.22   thorpej 		if (!strcmp(ypdb->dom_domain, arg))
    226   1.1   deraadt 			break;
    227   1.1   deraadt 
    228  1.17   mycroft 	if (ypdb == NULL) {
    229  1.24  christos 		ypdb = makebinding(arg);
    230   1.1   deraadt 		ypdb->dom_vers = YPVERS;
    231   1.1   deraadt 		ypdb->dom_alive = 0;
    232   1.1   deraadt 		ypdb->dom_lockfd = -1;
    233  1.24  christos 		removelock(ypdb);
    234  1.20       cgd 		ypdb->dom_xid = unique_xid(ypdb);
    235   1.1   deraadt 		ypdb->dom_pnext = ypbindlist;
    236   1.1   deraadt 		ypbindlist = ypdb;
    237   1.1   deraadt 		check++;
    238  1.24  christos #ifdef DEBUG
    239  1.24  christos 		if (debug)
    240  1.24  christos 			printf("unknown domain %s\n", arg);
    241  1.24  christos #endif
    242   1.1   deraadt 		return NULL;
    243   1.1   deraadt 	}
    244   1.1   deraadt 
    245  1.24  christos 	if (ypdb->dom_alive == 0) {
    246  1.24  christos #ifdef DEBUG
    247  1.24  christos 		if (debug)
    248  1.24  christos 			printf("dead domain %s\n", arg);
    249  1.24  christos #endif
    250   1.1   deraadt 		return NULL;
    251  1.24  christos 	}
    252   1.1   deraadt 
    253   1.9   deraadt #ifdef HEURISTIC
    254   1.9   deraadt 	time(&now);
    255   1.9   deraadt 	if (now < ypdb->dom_ask_t + 5) {
    256   1.1   deraadt 		/*
    257   1.9   deraadt 		 * Hmm. More than 2 requests in 5 seconds have indicated
    258   1.9   deraadt 		 * that my binding is possibly incorrect.
    259   1.9   deraadt 		 * Ok, do an immediate poll of the server.
    260   1.1   deraadt 		 */
    261   1.9   deraadt 		if (ypdb->dom_check_t >= now) {
    262   1.9   deraadt 			/* don't flood it */
    263   1.9   deraadt 			ypdb->dom_check_t = 0;
    264   1.9   deraadt 			check++;
    265   1.9   deraadt 		}
    266   1.1   deraadt 	}
    267   1.9   deraadt 	ypdb->dom_ask_t = now;
    268   1.1   deraadt #endif
    269   1.1   deraadt 
    270   1.1   deraadt 	res.ypbind_status = YPBIND_SUCC_VAL;
    271   1.1   deraadt 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
    272   1.1   deraadt 		ypdb->dom_server_addr.sin_addr.s_addr;
    273   1.1   deraadt 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    274   1.1   deraadt 		ypdb->dom_server_port;
    275  1.24  christos #ifdef DEBUG
    276  1.24  christos 	if (debug)
    277  1.24  christos 		printf("domain %s at %s/%d\n", ypdb->dom_domain,
    278  1.24  christos 		    inet_ntoa(ypdb->dom_server_addr.sin_addr),
    279  1.24  christos 		    ntohs(ypdb->dom_server_addr.sin_port));
    280  1.24  christos #endif
    281   1.1   deraadt 	return &res;
    282   1.1   deraadt }
    283   1.1   deraadt 
    284  1.24  christos static void *
    285  1.24  christos ypbindproc_setdom_2(transp, argp)
    286  1.17   mycroft 	SVCXPRT *transp;
    287  1.24  christos 	void *argp;
    288   1.1   deraadt {
    289  1.24  christos 	struct ypbind_setdom *sd = argp;
    290   1.1   deraadt 	struct sockaddr_in *fromsin, bindsin;
    291  1.13   deraadt 	static bool_t res;
    292   1.1   deraadt 
    293  1.24  christos #ifdef DEBUG
    294  1.24  christos 	if (debug)
    295  1.24  christos 		printf("ypbindproc_setdom_2 %s\n", inet_ntoa(bindsin.sin_addr));
    296  1.24  christos #endif
    297  1.24  christos 	(void) memset(&res, 0, sizeof(res));
    298   1.1   deraadt 	fromsin = svc_getcaller(transp);
    299   1.1   deraadt 
    300  1.17   mycroft 	switch (ypsetmode) {
    301   1.1   deraadt 	case YPSET_LOCAL:
    302  1.24  christos 		if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
    303  1.24  christos #ifdef DEBUG
    304  1.24  christos 			if (debug)
    305  1.24  christos 				printf("ypset from %s denied\n",
    306  1.24  christos 				    inet_ntoa(fromsin->sin_addr));
    307  1.24  christos #endif
    308  1.24  christos 			return NULL;
    309  1.24  christos 		}
    310   1.1   deraadt 		break;
    311   1.1   deraadt 	case YPSET_ALL:
    312   1.1   deraadt 		break;
    313   1.1   deraadt 	case YPSET_NO:
    314   1.1   deraadt 	default:
    315  1.24  christos #ifdef DEBUG
    316  1.24  christos 		if (debug)
    317  1.24  christos 			printf("ypset denied\n");
    318  1.24  christos #endif
    319  1.24  christos 		return NULL;
    320   1.1   deraadt 	}
    321   1.4   deraadt 
    322  1.24  christos 	if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
    323  1.24  christos #ifdef DEBUG
    324  1.24  christos 		if (debug)
    325  1.24  christos 			printf("ypset from unpriviledged port denied\n");
    326  1.24  christos #endif
    327  1.13   deraadt 		return &res;
    328  1.24  christos 	}
    329   1.1   deraadt 
    330  1.24  christos 	if (sd->ypsetdom_vers != YPVERS) {
    331  1.24  christos #ifdef DEBUG
    332  1.24  christos 		if (debug)
    333  1.24  christos 			printf("ypset with wrong version denied\n");
    334  1.24  christos #endif
    335  1.13   deraadt 		return &res;
    336  1.24  christos 	}
    337   1.1   deraadt 
    338  1.24  christos 	(void) memset(&bindsin, 0, sizeof bindsin);
    339   1.1   deraadt 	bindsin.sin_family = AF_INET;
    340  1.17   mycroft 	bindsin.sin_len = sizeof(bindsin);
    341  1.24  christos 	bindsin.sin_addr = sd->ypsetdom_addr;
    342  1.24  christos 	bindsin.sin_port = sd->ypsetdom_port;
    343  1.24  christos 	rpc_received(sd->ypsetdom_domain, &bindsin, 1);
    344  1.24  christos 
    345  1.24  christos #ifdef DEBUG
    346  1.24  christos 	if (debug)
    347  1.24  christos 		printf("ypset to %s succeeded\n", inet_ntoa(bindsin.sin_addr));
    348  1.24  christos #endif
    349   1.1   deraadt 	res = 1;
    350  1.13   deraadt 	return &res;
    351   1.1   deraadt }
    352   1.1   deraadt 
    353   1.1   deraadt static void
    354   1.1   deraadt ypbindprog_2(rqstp, transp)
    355  1.17   mycroft 	struct svc_req *rqstp;
    356  1.17   mycroft 	register SVCXPRT *transp;
    357   1.1   deraadt {
    358   1.1   deraadt 	union {
    359  1.22   thorpej 		char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
    360   1.1   deraadt 		struct ypbind_setdom ypbindproc_setdom_2_arg;
    361   1.1   deraadt 	} argument;
    362   1.1   deraadt 	struct authunix_parms *creds;
    363   1.1   deraadt 	char *result;
    364  1.24  christos 	xdrproc_t xdr_argument, xdr_result;
    365  1.24  christos 	void *(*local) __P((SVCXPRT *, void *));
    366   1.1   deraadt 
    367   1.1   deraadt 	switch (rqstp->rq_proc) {
    368   1.1   deraadt 	case YPBINDPROC_NULL:
    369   1.1   deraadt 		xdr_argument = xdr_void;
    370   1.1   deraadt 		xdr_result = xdr_void;
    371  1.24  christos 		local = ypbindproc_null_2;
    372   1.1   deraadt 		break;
    373   1.1   deraadt 
    374   1.1   deraadt 	case YPBINDPROC_DOMAIN:
    375  1.21   thorpej 		xdr_argument = xdr_ypdomain_wrap_string;
    376   1.1   deraadt 		xdr_result = xdr_ypbind_resp;
    377  1.24  christos 		local = ypbindproc_domain_2;
    378   1.1   deraadt 		break;
    379   1.1   deraadt 
    380   1.1   deraadt 	case YPBINDPROC_SETDOM:
    381  1.17   mycroft 		switch (rqstp->rq_cred.oa_flavor) {
    382   1.1   deraadt 		case AUTH_UNIX:
    383   1.1   deraadt 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
    384  1.17   mycroft 			if (creds->aup_uid != 0) {
    385   1.1   deraadt 				svcerr_auth(transp, AUTH_BADCRED);
    386   1.1   deraadt 				return;
    387   1.1   deraadt 			}
    388   1.1   deraadt 			break;
    389   1.1   deraadt 		default:
    390   1.1   deraadt 			svcerr_auth(transp, AUTH_TOOWEAK);
    391   1.1   deraadt 			return;
    392   1.1   deraadt 		}
    393   1.1   deraadt 
    394   1.1   deraadt 		xdr_argument = xdr_ypbind_setdom;
    395   1.1   deraadt 		xdr_result = xdr_void;
    396  1.24  christos 		local = ypbindproc_setdom_2;
    397   1.1   deraadt 		break;
    398   1.1   deraadt 
    399   1.1   deraadt 	default:
    400   1.1   deraadt 		svcerr_noproc(transp);
    401   1.1   deraadt 		return;
    402   1.1   deraadt 	}
    403  1.24  christos 	(void) memset(&argument, 0, sizeof(argument));
    404  1.14       cgd 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
    405   1.1   deraadt 		svcerr_decode(transp);
    406   1.1   deraadt 		return;
    407   1.1   deraadt 	}
    408  1.24  christos 	result = (*local)(transp, &argument);
    409   1.1   deraadt 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    410   1.1   deraadt 		svcerr_systemerr(transp);
    411   1.1   deraadt 	}
    412   1.1   deraadt 	return;
    413   1.1   deraadt }
    414   1.1   deraadt 
    415  1.24  christos int
    416   1.1   deraadt main(argc, argv)
    417  1.17   mycroft 	int argc;
    418  1.17   mycroft 	char *argv[];
    419   1.1   deraadt {
    420   1.1   deraadt 	struct timeval tv;
    421   1.1   deraadt 	fd_set fdsr;
    422  1.13   deraadt 	int width, lockfd;
    423  1.18   mycroft 	int evil = 0, one;
    424   1.1   deraadt 
    425   1.1   deraadt 	yp_get_default_domain(&domainname);
    426  1.24  christos 	if (domainname[0] == '\0')
    427  1.24  christos 		errx(1, "Domainname not set. Aborting.");
    428   1.1   deraadt 
    429  1.18   mycroft 	while (--argc) {
    430  1.18   mycroft 		++argv;
    431  1.18   mycroft 		if (!strcmp("-ypset", *argv))
    432   1.1   deraadt 			ypsetmode = YPSET_ALL;
    433  1.18   mycroft 		else if (!strcmp("-ypsetme", *argv))
    434   1.1   deraadt 			ypsetmode = YPSET_LOCAL;
    435  1.24  christos #ifdef DEBUG
    436  1.24  christos 		else if (!strcmp("-d", *argv))
    437  1.24  christos 			debug++;
    438  1.24  christos #endif
    439  1.24  christos 		else
    440  1.24  christos 			usage();
    441   1.1   deraadt 	}
    442   1.1   deraadt 
    443   1.1   deraadt 	/* blow away everything in BINDINGDIR */
    444   1.1   deraadt 
    445  1.24  christos 	lockfd = open(YPBINDLOCK, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644);
    446  1.24  christos 	if (lockfd == -1)
    447  1.24  christos 		err(1, "Cannot create %s", YPBINDLOCK);
    448  1.24  christos 
    449  1.24  christos #if O_SHLOCK == 0
    450  1.24  christos 	(void) flock(lockfd, LOCK_SH);
    451  1.13   deraadt #endif
    452   1.1   deraadt 
    453  1.24  christos 	(void) pmap_unset(YPBINDPROG, YPBINDVERS);
    454   1.1   deraadt 
    455   1.6   deraadt 	udptransp = svcudp_create(RPC_ANYSOCK);
    456  1.24  christos 	if (udptransp == NULL)
    457  1.24  christos 		errx(1, "Cannot create udp service.");
    458  1.24  christos 
    459   1.6   deraadt 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    460  1.24  christos 	    IPPROTO_UDP))
    461  1.24  christos 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, udp).");
    462   1.1   deraadt 
    463   1.6   deraadt 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
    464  1.24  christos 	if (tcptransp == NULL)
    465  1.24  christos 		errx(1, "Cannot create tcp service.");
    466  1.24  christos 
    467   1.6   deraadt 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    468  1.24  christos 	    IPPROTO_TCP))
    469  1.24  christos 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, tcp).");
    470   1.1   deraadt 
    471  1.24  christos 	if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    472  1.24  christos 		err(1, "rpc socket");
    473  1.24  christos 	if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    474  1.24  christos 		err(1, "ping socket");
    475   1.6   deraadt 
    476  1.24  christos 	(void) fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
    477  1.24  christos 	(void) fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY);
    478  1.24  christos 
    479  1.18   mycroft 	one = 1;
    480  1.24  christos 	(void) setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
    481   1.1   deraadt 	rmtca.prog = YPPROG;
    482   1.1   deraadt 	rmtca.vers = YPVERS;
    483   1.1   deraadt 	rmtca.proc = YPPROC_DOMAIN_NONACK;
    484   1.1   deraadt 	rmtca.xdr_args = NULL;		/* set at call time */
    485   1.1   deraadt 	rmtca.args_ptr = NULL;		/* set at call time */
    486   1.1   deraadt 	rmtcr.port_ptr = &rmtcr_port;
    487   1.1   deraadt 	rmtcr.xdr_results = xdr_bool;
    488   1.1   deraadt 	rmtcr.results_ptr = (caddr_t)&rmtcr_outval;
    489   1.1   deraadt 
    490   1.1   deraadt 	/* build initial domain binding, make it "unsuccessful" */
    491  1.24  christos 	ypbindlist = makebinding(domainname);
    492   1.1   deraadt 	ypbindlist->dom_vers = YPVERS;
    493   1.1   deraadt 	ypbindlist->dom_alive = 0;
    494   1.1   deraadt 	ypbindlist->dom_lockfd = -1;
    495  1.24  christos 	removelock(ypbindlist);
    496  1.12   deraadt 
    497  1.12   deraadt 	checkwork();
    498   1.1   deraadt 
    499  1.17   mycroft 	while (1) {
    500  1.16        pk 		width = svc_maxfd;
    501  1.16        pk 		if (rpcsock > width)
    502  1.16        pk 			width = rpcsock;
    503  1.16        pk 		if (pingsock > width)
    504  1.16        pk 			width = pingsock;
    505  1.16        pk 		width++;
    506  1.17   mycroft 
    507   1.1   deraadt 		fdsr = svc_fdset;
    508   1.1   deraadt 		FD_SET(rpcsock, &fdsr);
    509  1.10   deraadt 		FD_SET(pingsock, &fdsr);
    510   1.1   deraadt 		tv.tv_sec = 1;
    511   1.1   deraadt 		tv.tv_usec = 0;
    512   1.1   deraadt 
    513  1.17   mycroft 		switch (select(width, &fdsr, NULL, NULL, &tv)) {
    514   1.1   deraadt 		case 0:
    515   1.1   deraadt 			checkwork();
    516   1.1   deraadt 			break;
    517   1.1   deraadt 		case -1:
    518  1.24  christos 			warn("select");
    519   1.1   deraadt 			break;
    520   1.1   deraadt 		default:
    521  1.15   mycroft 			if (FD_ISSET(rpcsock, &fdsr))
    522   1.1   deraadt 				handle_replies();
    523  1.15   mycroft 			if (FD_ISSET(pingsock, &fdsr))
    524  1.10   deraadt 				handle_ping();
    525   1.1   deraadt 			svc_getreqset(&fdsr);
    526  1.15   mycroft 			if (check)
    527   1.1   deraadt 				checkwork();
    528   1.1   deraadt 			break;
    529  1.18   mycroft 		}
    530  1.18   mycroft 
    531  1.18   mycroft 		if (!evil && ypbindlist->dom_alive) {
    532  1.18   mycroft 			evil = 1;
    533  1.24  christos #ifdef DEBUG
    534  1.24  christos 			if (!debug)
    535  1.24  christos 				daemon(0, 0);
    536  1.24  christos #endif
    537   1.1   deraadt 		}
    538   1.1   deraadt 	}
    539   1.1   deraadt }
    540   1.1   deraadt 
    541   1.1   deraadt /*
    542   1.9   deraadt  * State transition is done like this:
    543   1.9   deraadt  *
    544  1.11        ws  * STATE	EVENT		ACTION			NEWSTATE	TIMEOUT
    545  1.11        ws  * no binding	timeout		broadcast 		no binding	5 sec
    546  1.11        ws  * no binding	answer		--			binding		60 sec
    547  1.11        ws  * binding	timeout		ping server		checking	5 sec
    548  1.11        ws  * checking	timeout		ping server + broadcast	checking	5 sec
    549  1.11        ws  * checking	answer		--			binding		60 sec
    550   1.1   deraadt  */
    551  1.24  christos void
    552   1.1   deraadt checkwork()
    553   1.1   deraadt {
    554   1.1   deraadt 	struct _dom_binding *ypdb;
    555   1.1   deraadt 	time_t t;
    556   1.1   deraadt 
    557   1.1   deraadt 	check = 0;
    558   1.1   deraadt 
    559   1.1   deraadt 	time(&t);
    560  1.17   mycroft 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
    561  1.17   mycroft 		if (ypdb->dom_check_t < t) {
    562  1.10   deraadt 			if (ypdb->dom_alive == 1)
    563  1.10   deraadt 				ping(ypdb);
    564  1.10   deraadt 			else
    565  1.10   deraadt 				broadcast(ypdb);
    566   1.1   deraadt 			time(&t);
    567   1.1   deraadt 			ypdb->dom_check_t = t + 5;
    568   1.1   deraadt 		}
    569   1.1   deraadt 	}
    570   1.1   deraadt }
    571   1.1   deraadt 
    572  1.24  christos int
    573   1.8   deraadt ping(ypdb)
    574   1.8   deraadt 	struct _dom_binding *ypdb;
    575   1.1   deraadt {
    576   1.8   deraadt 	char *dom = ypdb->dom_domain;
    577  1.17   mycroft 	struct rpc_msg msg;
    578  1.24  christos 	char buf[BUFSIZE];
    579  1.10   deraadt 	enum clnt_stat st;
    580  1.10   deraadt 	int outlen;
    581  1.10   deraadt 	AUTH *rpcua;
    582  1.17   mycroft 	XDR xdr;
    583  1.10   deraadt 
    584  1.24  christos 	(void) memset(&xdr, 0, sizeof xdr);
    585  1.24  christos 	(void) memset(&msg, 0, sizeof msg);
    586  1.10   deraadt 
    587  1.10   deraadt 	rpcua = authunix_create_default();
    588  1.24  christos 	if (rpcua == NULL) {
    589  1.24  christos #ifdef DEBUG
    590  1.24  christos 		if (debug)
    591  1.24  christos 			printf("cannot get unix auth\n");
    592  1.24  christos #endif
    593  1.10   deraadt 		return RPC_SYSTEMERROR;
    594  1.10   deraadt 	}
    595  1.24  christos 
    596  1.17   mycroft 	msg.rm_direction = CALL;
    597  1.17   mycroft 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    598  1.17   mycroft 	msg.rm_call.cb_prog = YPPROG;
    599  1.17   mycroft 	msg.rm_call.cb_vers = YPVERS;
    600  1.17   mycroft 	msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
    601  1.17   mycroft 	msg.rm_call.cb_cred = rpcua->ah_cred;
    602  1.17   mycroft 	msg.rm_call.cb_verf = rpcua->ah_verf;
    603  1.17   mycroft 
    604  1.20       cgd 	msg.rm_xid = ypdb->dom_xid;
    605  1.17   mycroft 	xdrmem_create(&xdr, buf, sizeof buf, XDR_ENCODE);
    606  1.17   mycroft 	if (!xdr_callmsg(&xdr, &msg)) {
    607  1.10   deraadt 		st = RPC_CANTENCODEARGS;
    608  1.10   deraadt 		AUTH_DESTROY(rpcua);
    609  1.10   deraadt 		return st;
    610  1.10   deraadt 	}
    611  1.23   thorpej 	if (!xdr_ypdomain_wrap_string(&xdr, &dom)) {
    612  1.10   deraadt 		st = RPC_CANTENCODEARGS;
    613  1.10   deraadt 		AUTH_DESTROY(rpcua);
    614  1.10   deraadt 		return st;
    615  1.10   deraadt 	}
    616  1.17   mycroft 	outlen = (int)xdr_getpos(&xdr);
    617  1.17   mycroft 	xdr_destroy(&xdr);
    618  1.17   mycroft 	if (outlen < 1) {
    619  1.10   deraadt 		st = RPC_CANTENCODEARGS;
    620  1.10   deraadt 		AUTH_DESTROY(rpcua);
    621  1.10   deraadt 		return st;
    622  1.10   deraadt 	}
    623  1.10   deraadt 	AUTH_DESTROY(rpcua);
    624  1.10   deraadt 
    625  1.10   deraadt 	ypdb->dom_alive = 2;
    626  1.10   deraadt 	if (sendto(pingsock, buf, outlen, 0,
    627  1.10   deraadt 		   (struct sockaddr *)&ypdb->dom_server_addr,
    628  1.24  christos 		   sizeof ypdb->dom_server_addr) == -1)
    629  1.24  christos 		warn("ping: sendto");
    630  1.10   deraadt 	return 0;
    631  1.10   deraadt 
    632  1.10   deraadt }
    633  1.10   deraadt 
    634  1.24  christos int
    635  1.10   deraadt broadcast(ypdb)
    636  1.10   deraadt 	struct _dom_binding *ypdb;
    637  1.10   deraadt {
    638  1.10   deraadt 	char *dom = ypdb->dom_domain;
    639  1.17   mycroft 	struct rpc_msg msg;
    640  1.24  christos 	char buf[BUFSIZE], inbuf[8192];
    641   1.1   deraadt 	enum clnt_stat st;
    642   1.1   deraadt 	int outlen, i, sock, len;
    643  1.17   mycroft 	struct sockaddr_in bindsin;
    644   1.1   deraadt 	struct ifconf ifc;
    645   1.1   deraadt 	struct ifreq ifreq, *ifr;
    646   1.1   deraadt 	struct in_addr in;
    647   1.1   deraadt 	AUTH *rpcua;
    648  1.17   mycroft 	XDR xdr;
    649   1.1   deraadt 
    650  1.21   thorpej 	rmtca.xdr_args = xdr_ypdomain_wrap_string;
    651  1.21   thorpej 	rmtca.args_ptr = (char *)&dom;
    652   1.1   deraadt 
    653  1.24  christos 	(void) memset(&xdr, 0, sizeof xdr);
    654  1.24  christos 	(void) memset(&msg, 0, sizeof msg);
    655   1.1   deraadt 
    656   1.1   deraadt 	rpcua = authunix_create_default();
    657  1.24  christos 	if (rpcua == NULL) {
    658  1.24  christos #ifdef DEBUG
    659  1.24  christos 		if (debug)
    660  1.24  christos 			printf("cannot get unix auth\n");
    661  1.24  christos #endif
    662   1.1   deraadt 		return RPC_SYSTEMERROR;
    663   1.1   deraadt 	}
    664  1.17   mycroft 	msg.rm_direction = CALL;
    665  1.17   mycroft 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    666  1.17   mycroft 	msg.rm_call.cb_prog = PMAPPROG;
    667  1.17   mycroft 	msg.rm_call.cb_vers = PMAPVERS;
    668  1.17   mycroft 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
    669  1.17   mycroft 	msg.rm_call.cb_cred = rpcua->ah_cred;
    670  1.17   mycroft 	msg.rm_call.cb_verf = rpcua->ah_verf;
    671  1.17   mycroft 
    672  1.20       cgd 	msg.rm_xid = ypdb->dom_xid;
    673  1.17   mycroft 	xdrmem_create(&xdr, buf, sizeof buf, XDR_ENCODE);
    674  1.17   mycroft 	if (!xdr_callmsg(&xdr, &msg)) {
    675   1.1   deraadt 		st = RPC_CANTENCODEARGS;
    676   1.1   deraadt 		AUTH_DESTROY(rpcua);
    677   1.1   deraadt 		return st;
    678   1.1   deraadt 	}
    679  1.17   mycroft 	if (!xdr_rmtcall_args(&xdr, &rmtca)) {
    680   1.1   deraadt 		st = RPC_CANTENCODEARGS;
    681   1.1   deraadt 		AUTH_DESTROY(rpcua);
    682   1.1   deraadt 		return st;
    683   1.1   deraadt 	}
    684  1.17   mycroft 	outlen = (int)xdr_getpos(&xdr);
    685  1.17   mycroft 	xdr_destroy(&xdr);
    686  1.17   mycroft 	if (outlen < 1) {
    687  1.10   deraadt 		st = RPC_CANTENCODEARGS;
    688   1.1   deraadt 		AUTH_DESTROY(rpcua);
    689   1.1   deraadt 		return st;
    690   1.1   deraadt 	}
    691   1.1   deraadt 	AUTH_DESTROY(rpcua);
    692   1.1   deraadt 
    693  1.17   mycroft 	if (ypdb->dom_lockfd != -1) {
    694  1.24  christos 		(void) close(ypdb->dom_lockfd);
    695   1.9   deraadt 		ypdb->dom_lockfd = -1;
    696  1.24  christos 		removelock(ypdb);
    697   1.9   deraadt 	}
    698   1.8   deraadt 
    699  1.24  christos 	(void) memset(&bindsin, 0, sizeof bindsin);
    700  1.17   mycroft 	bindsin.sin_family = AF_INET;
    701  1.17   mycroft 	bindsin.sin_len = sizeof(bindsin);
    702  1.17   mycroft 	bindsin.sin_port = htons(PMAPPORT);
    703   1.9   deraadt 
    704  1.11        ws 	if (ypdb->dom_alive == 2) {
    705  1.11        ws 		/*
    706  1.11        ws 		 * This resolves the following situation:
    707  1.11        ws 		 * ypserver on other subnet was once bound,
    708  1.11        ws 		 * but rebooted and is now using a different port
    709  1.11        ws 		 */
    710  1.17   mycroft 		bindsin.sin_addr = ypdb->dom_server_addr.sin_addr;
    711  1.17   mycroft 		if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
    712  1.24  christos 			   sizeof bindsin) == -1)
    713  1.24  christos 			warn("broadcast: sendto");
    714  1.11        ws 	}
    715   1.1   deraadt 	/* find all networks and send the RPC packet out them all */
    716  1.24  christos 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    717  1.24  christos 		warn("broadcast: socket");
    718   1.1   deraadt 		return -1;
    719   1.1   deraadt 	}
    720   1.9   deraadt 
    721   1.1   deraadt 	ifc.ifc_len = sizeof inbuf;
    722   1.1   deraadt 	ifc.ifc_buf = inbuf;
    723  1.17   mycroft 	if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
    724  1.24  christos 		(void) close(sock);
    725  1.24  christos 		warn("broadcast: ioctl(SIOCGIFCONF)");
    726   1.1   deraadt 		return -1;
    727   1.1   deraadt 	}
    728   1.1   deraadt 	ifr = ifc.ifc_req;
    729   1.1   deraadt 	ifreq.ifr_name[0] = '\0';
    730  1.17   mycroft 	for (i = 0; i < ifc.ifc_len; i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
    731   1.1   deraadt #if defined(BSD) && BSD >= 199103
    732   1.1   deraadt 		len = sizeof ifr->ifr_name + ifr->ifr_addr.sa_len;
    733   1.1   deraadt #else
    734   1.1   deraadt 		len = sizeof ifc.ifc_len / sizeof(struct ifreq);
    735   1.1   deraadt #endif
    736   1.1   deraadt 		ifreq = *ifr;
    737  1.17   mycroft 		if (ifreq.ifr_addr.sa_family != AF_INET)
    738   1.1   deraadt 			continue;
    739  1.17   mycroft 		if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
    740  1.24  christos 			warn("broadcast: ioctl(SIOCGIFFLAGS)");
    741   1.1   deraadt 			continue;
    742   1.1   deraadt 		}
    743  1.17   mycroft 		if ((ifreq.ifr_flags & IFF_UP) == 0)
    744   1.1   deraadt 			continue;
    745   1.1   deraadt 
    746   1.1   deraadt 		ifreq.ifr_flags &= (IFF_LOOPBACK | IFF_BROADCAST);
    747  1.17   mycroft 		if (ifreq.ifr_flags == IFF_BROADCAST) {
    748  1.17   mycroft 			if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
    749  1.24  christos 				warn("broadcast: ioctl(SIOCGIFBRDADDR)");
    750   1.1   deraadt 				continue;
    751   1.1   deraadt 			}
    752  1.17   mycroft 		} else if (ifreq.ifr_flags == IFF_LOOPBACK) {
    753  1.17   mycroft 			if (ioctl(sock, SIOCGIFADDR, &ifreq) < 0) {
    754  1.24  christos 				warn("broadcast: ioctl(SIOCGIFADDR)");
    755   1.1   deraadt 				continue;
    756   1.1   deraadt 			}
    757   1.1   deraadt 		} else
    758   1.1   deraadt 			continue;
    759   1.1   deraadt 
    760   1.1   deraadt 		in = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
    761  1.17   mycroft 		bindsin.sin_addr = in;
    762  1.17   mycroft 		if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
    763  1.24  christos 			   sizeof bindsin) == -1)
    764  1.24  christos 			warn("broadcast: sendto");
    765   1.1   deraadt 	}
    766  1.24  christos 	(void) close(sock);
    767   1.1   deraadt 	return 0;
    768   1.1   deraadt }
    769   1.1   deraadt 
    770  1.24  christos static enum clnt_stat
    771   1.1   deraadt handle_replies()
    772   1.1   deraadt {
    773  1.24  christos 	char buf[BUFSIZE];
    774   1.1   deraadt 	int fromlen, inlen;
    775  1.20       cgd 	struct _dom_binding *ypdb;
    776   1.1   deraadt 	struct sockaddr_in raddr;
    777   1.1   deraadt 	struct rpc_msg msg;
    778   1.1   deraadt 	XDR xdr;
    779   1.1   deraadt 
    780   1.1   deraadt recv_again:
    781  1.24  christos 	(void) memset(&xdr, 0, sizeof(xdr));
    782  1.24  christos 	(void) memset(&msg, 0, sizeof(msg));
    783   1.1   deraadt 	msg.acpted_rply.ar_verf = _null_auth;
    784   1.1   deraadt 	msg.acpted_rply.ar_results.where = (caddr_t)&rmtcr;
    785   1.1   deraadt 	msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
    786   1.1   deraadt 
    787   1.1   deraadt try_again:
    788   1.1   deraadt 	fromlen = sizeof (struct sockaddr);
    789   1.1   deraadt 	inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
    790   1.1   deraadt 		(struct sockaddr *)&raddr, &fromlen);
    791  1.17   mycroft 	if (inlen < 0) {
    792  1.17   mycroft 		if (errno == EINTR)
    793   1.1   deraadt 			goto try_again;
    794   1.1   deraadt 		return RPC_CANTRECV;
    795   1.1   deraadt 	}
    796  1.17   mycroft 	if (inlen < sizeof(u_int32_t))
    797   1.1   deraadt 		goto recv_again;
    798   1.1   deraadt 
    799   1.1   deraadt 	/*
    800   1.1   deraadt 	 * see if reply transaction id matches sent id.
    801   1.1   deraadt 	 * If so, decode the results.
    802   1.1   deraadt 	 */
    803   1.1   deraadt 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
    804  1.17   mycroft 	if (xdr_replymsg(&xdr, &msg)) {
    805  1.17   mycroft 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    806  1.17   mycroft 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
    807   1.1   deraadt 			raddr.sin_port = htons((u_short)rmtcr_port);
    808  1.20       cgd 			ypdb = xid2ypdb(msg.rm_xid);
    809  1.20       cgd 			if (ypdb != NULL)
    810  1.20       cgd 				rpc_received(ypdb->dom_domain, &raddr, 0);
    811   1.1   deraadt 		}
    812   1.1   deraadt 	}
    813   1.1   deraadt 	xdr.x_op = XDR_FREE;
    814   1.1   deraadt 	msg.acpted_rply.ar_results.proc = xdr_void;
    815   1.1   deraadt 	xdr_destroy(&xdr);
    816   1.1   deraadt 
    817   1.1   deraadt 	return RPC_SUCCESS;
    818   1.1   deraadt }
    819   1.1   deraadt 
    820  1.24  christos static enum clnt_stat
    821  1.10   deraadt handle_ping()
    822  1.10   deraadt {
    823  1.24  christos 	char buf[BUFSIZE];
    824  1.10   deraadt 	int fromlen, inlen;
    825  1.20       cgd 	struct _dom_binding *ypdb;
    826  1.10   deraadt 	struct sockaddr_in raddr;
    827  1.10   deraadt 	struct rpc_msg msg;
    828  1.10   deraadt 	XDR xdr;
    829  1.10   deraadt 	bool_t res;
    830  1.10   deraadt 
    831  1.10   deraadt recv_again:
    832  1.24  christos 	(void) memset(&xdr, 0, sizeof(xdr));
    833  1.24  christos 	(void) memset(&msg, 0, sizeof(msg));
    834  1.10   deraadt 	msg.acpted_rply.ar_verf = _null_auth;
    835  1.10   deraadt 	msg.acpted_rply.ar_results.where = (caddr_t)&res;
    836  1.10   deraadt 	msg.acpted_rply.ar_results.proc = xdr_bool;
    837  1.10   deraadt 
    838  1.10   deraadt try_again:
    839  1.10   deraadt 	fromlen = sizeof (struct sockaddr);
    840  1.10   deraadt 	inlen = recvfrom(pingsock, buf, sizeof buf, 0,
    841  1.10   deraadt 		(struct sockaddr *)&raddr, &fromlen);
    842  1.17   mycroft 	if (inlen < 0) {
    843  1.17   mycroft 		if (errno == EINTR)
    844  1.10   deraadt 			goto try_again;
    845  1.10   deraadt 		return RPC_CANTRECV;
    846  1.10   deraadt 	}
    847  1.17   mycroft 	if (inlen < sizeof(u_int32_t))
    848  1.10   deraadt 		goto recv_again;
    849  1.10   deraadt 
    850  1.10   deraadt 	/*
    851  1.10   deraadt 	 * see if reply transaction id matches sent id.
    852  1.10   deraadt 	 * If so, decode the results.
    853  1.10   deraadt 	 */
    854  1.10   deraadt 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
    855  1.17   mycroft 	if (xdr_replymsg(&xdr, &msg)) {
    856  1.17   mycroft 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    857  1.17   mycroft 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
    858  1.20       cgd 			ypdb = xid2ypdb(msg.rm_xid);
    859  1.20       cgd 			if (ypdb != NULL)
    860  1.20       cgd 				rpc_received(ypdb->dom_domain, &raddr, 0);
    861  1.10   deraadt 		}
    862  1.10   deraadt 	}
    863  1.10   deraadt 	xdr.x_op = XDR_FREE;
    864  1.10   deraadt 	msg.acpted_rply.ar_results.proc = xdr_void;
    865  1.10   deraadt 	xdr_destroy(&xdr);
    866  1.10   deraadt 
    867  1.10   deraadt 	return RPC_SUCCESS;
    868  1.10   deraadt }
    869  1.10   deraadt 
    870   1.1   deraadt /*
    871   1.1   deraadt  * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
    872   1.1   deraadt  */
    873  1.24  christos void
    874   1.1   deraadt rpc_received(dom, raddrp, force)
    875  1.24  christos 	char *dom;
    876  1.24  christos 	struct sockaddr_in *raddrp;
    877  1.24  christos 	int force;
    878   1.1   deraadt {
    879   1.1   deraadt 	struct _dom_binding *ypdb;
    880   1.6   deraadt 	struct iovec iov[2];
    881   1.6   deraadt 	struct ypbind_resp ybr;
    882   1.1   deraadt 	int fd;
    883  1.14       cgd 
    884  1.24  christos #ifdef DEBUG
    885  1.24  christos 	if (debug)
    886  1.24  christos 		printf("returned from %s about %s\n",
    887  1.24  christos 		    inet_ntoa(raddrp->sin_addr), dom);
    888  1.24  christos #endif
    889  1.19       cgd 
    890  1.17   mycroft 	if (dom == NULL)
    891   1.1   deraadt 		return;
    892   1.1   deraadt 
    893  1.17   mycroft 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
    894  1.17   mycroft 		if (!strcmp(ypdb->dom_domain, dom))
    895   1.1   deraadt 			break;
    896   1.1   deraadt 
    897  1.17   mycroft 	if (ypdb == NULL) {
    898  1.17   mycroft 		if (force == 0)
    899   1.1   deraadt 			return;
    900  1.24  christos 		ypdb = makebinding(dom);
    901   1.1   deraadt 		ypdb->dom_lockfd = -1;
    902   1.1   deraadt 		ypdb->dom_pnext = ypbindlist;
    903   1.1   deraadt 		ypbindlist = ypdb;
    904   1.1   deraadt 	}
    905   1.1   deraadt 
    906   1.8   deraadt 	/* soft update, alive */
    907  1.17   mycroft 	if (ypdb->dom_alive == 1 && force == 0) {
    908  1.17   mycroft 		if (!memcmp(&ypdb->dom_server_addr, raddrp,
    909  1.17   mycroft 			    sizeof ypdb->dom_server_addr)) {
    910   1.8   deraadt 			ypdb->dom_alive = 1;
    911  1.24  christos 			/* recheck binding in 60 sec */
    912  1.24  christos 			ypdb->dom_check_t = time(NULL) + 60;
    913   1.8   deraadt 		}
    914   1.1   deraadt 		return;
    915   1.8   deraadt 	}
    916   1.8   deraadt 
    917  1.24  christos 	(void) memcpy(&ypdb->dom_server_addr, raddrp,
    918  1.24  christos 	    sizeof ypdb->dom_server_addr);
    919  1.24  christos 	/* recheck binding in 60 seconds */
    920  1.24  christos 	ypdb->dom_check_t = time(NULL) + 60;
    921   1.1   deraadt 	ypdb->dom_vers = YPVERS;
    922   1.1   deraadt 	ypdb->dom_alive = 1;
    923   1.1   deraadt 
    924  1.17   mycroft 	if (ypdb->dom_lockfd != -1)
    925  1.24  christos 		(void) close(ypdb->dom_lockfd);
    926   1.1   deraadt 
    927  1.24  christos 	if ((fd = makelock(ypdb)) == -1)
    928  1.24  christos 		return;
    929   1.1   deraadt 
    930   1.1   deraadt 	/*
    931   1.1   deraadt 	 * ok, if BINDINGDIR exists, and we can create the binding file,
    932   1.1   deraadt 	 * then write to it..
    933   1.1   deraadt 	 */
    934   1.1   deraadt 	ypdb->dom_lockfd = fd;
    935   1.6   deraadt 
    936   1.6   deraadt 	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
    937   1.6   deraadt 	iov[0].iov_len = sizeof udptransp->xp_port;
    938   1.6   deraadt 	iov[1].iov_base = (caddr_t)&ybr;
    939   1.6   deraadt 	iov[1].iov_len = sizeof ybr;
    940   1.6   deraadt 
    941  1.24  christos 	(void) memset(&ybr, 0, sizeof ybr);
    942   1.6   deraadt 	ybr.ypbind_status = YPBIND_SUCC_VAL;
    943  1.24  christos 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
    944  1.24  christos 	    raddrp->sin_addr;
    945  1.24  christos 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    946  1.24  christos 	    raddrp->sin_port;
    947  1.24  christos 
    948  1.24  christos 	if (writev(ypdb->dom_lockfd, iov, 2) !=
    949  1.24  christos 	    iov[0].iov_len + iov[1].iov_len) {
    950  1.24  christos 		warnx("writev");
    951  1.24  christos 		(void) close(ypdb->dom_lockfd);
    952  1.24  christos 		removelock(ypdb);
    953   1.1   deraadt 		ypdb->dom_lockfd = -1;
    954   1.1   deraadt 	}
    955  1.20       cgd }
    956  1.20       cgd 
    957  1.24  christos static struct _dom_binding *
    958  1.20       cgd xid2ypdb(xid)
    959  1.20       cgd 	int xid;
    960  1.20       cgd {
    961  1.20       cgd 	struct _dom_binding *ypdb;
    962  1.20       cgd 
    963  1.20       cgd 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
    964  1.20       cgd 		if (ypdb->dom_xid == xid)
    965  1.20       cgd 			break;
    966  1.20       cgd 	return (ypdb);
    967  1.20       cgd }
    968  1.20       cgd 
    969  1.24  christos static int
    970  1.20       cgd unique_xid(ypdb)
    971  1.20       cgd 	struct _dom_binding *ypdb;
    972  1.20       cgd {
    973  1.20       cgd 	int tmp_xid;
    974  1.20       cgd 
    975  1.20       cgd 	tmp_xid = (long)ypdb & 0xffffffff;
    976  1.20       cgd 	while (xid2ypdb(tmp_xid) != NULL)
    977  1.20       cgd 		tmp_xid++;
    978  1.20       cgd 
    979  1.20       cgd 	return tmp_xid;
    980   1.1   deraadt }
    981