Home | History | Annotate | Line # | Download | only in ypbind
ypbind.c revision 1.70
      1  1.70  dholland /*	$NetBSD: ypbind.c,v 1.70 2011/05/24 06:57:04 dholland 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.2   deraadt  *
     16   1.2   deraadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17   1.2   deraadt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18   1.2   deraadt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.2   deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20   1.2   deraadt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.2   deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.2   deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.2   deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.2   deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.2   deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.2   deraadt  * SUCH DAMAGE.
     27   1.2   deraadt  */
     28   1.2   deraadt 
     29  1.30     lukem #include <sys/cdefs.h>
     30   1.2   deraadt #ifndef LINT
     31  1.70  dholland __RCSID("$NetBSD: ypbind.c,v 1.70 2011/05/24 06:57:04 dholland Exp $");
     32   1.2   deraadt #endif
     33   1.2   deraadt 
     34  1.67  dholland #include <sys/types.h>
     35   1.1   deraadt #include <sys/param.h>
     36  1.67  dholland #include <sys/file.h>
     37   1.1   deraadt #include <sys/ioctl.h>
     38   1.1   deraadt #include <sys/signal.h>
     39   1.1   deraadt #include <sys/socket.h>
     40  1.67  dholland #include <sys/stat.h>
     41  1.67  dholland #include <sys/syslog.h>
     42   1.6   deraadt #include <sys/uio.h>
     43  1.67  dholland #include <arpa/inet.h>
     44  1.67  dholland #include <net/if.h>
     45  1.67  dholland #include <ctype.h>
     46  1.67  dholland #include <dirent.h>
     47  1.67  dholland #include <err.h>
     48  1.67  dholland #include <errno.h>
     49  1.38    kleink #include <fcntl.h>
     50  1.67  dholland #include <ifaddrs.h>
     51  1.27   thorpej #include <limits.h>
     52  1.67  dholland #include <netdb.h>
     53  1.67  dholland #include <stdarg.h>
     54   1.1   deraadt #include <stdio.h>
     55  1.14       cgd #include <stdlib.h>
     56  1.67  dholland #include <string.h>
     57  1.37    bouyer #include <syslog.h>
     58  1.67  dholland #include <unistd.h>
     59  1.67  dholland #include <util.h>
     60  1.67  dholland 
     61   1.1   deraadt #include <rpc/rpc.h>
     62   1.1   deraadt #include <rpc/xdr.h>
     63   1.1   deraadt #include <rpc/pmap_clnt.h>
     64   1.1   deraadt #include <rpc/pmap_prot.h>
     65   1.1   deraadt #include <rpc/pmap_rmt.h>
     66   1.1   deraadt #include <rpcsvc/yp_prot.h>
     67   1.1   deraadt #include <rpcsvc/ypclnt.h>
     68   1.1   deraadt 
     69  1.27   thorpej #include "pathnames.h"
     70  1.27   thorpej 
     71  1.68  dholland #define YPSERVERSSUFF	".ypservers"
     72  1.68  dholland #define BINDINGDIR	(_PATH_VAR_YP "binding")
     73  1.68  dholland 
     74  1.24  christos #ifndef O_SHLOCK
     75  1.24  christos #define O_SHLOCK 0
     76  1.24  christos #endif
     77  1.24  christos 
     78  1.68  dholland int _yp_invalid_domain(const char *);		/* XXX libc internal */
     79  1.68  dholland 
     80  1.68  dholland ////////////////////////////////////////////////////////////
     81  1.68  dholland // types and globals
     82  1.27   thorpej 
     83  1.68  dholland typedef enum {
     84  1.68  dholland 	YPBIND_DIRECT, YPBIND_BROADCAST, YPBIND_SETLOCAL, YPBIND_SETALL
     85  1.68  dholland } ypbind_mode_t;
     86   1.1   deraadt 
     87   1.1   deraadt struct _dom_binding {
     88   1.1   deraadt 	struct _dom_binding *dom_pnext;
     89   1.1   deraadt 	char dom_domain[YPMAXDOMAIN + 1];
     90   1.1   deraadt 	struct sockaddr_in dom_server_addr;
     91   1.1   deraadt 	int dom_socket;
     92   1.1   deraadt 	CLIENT *dom_client;
     93  1.35     lukem 	long dom_vers;
     94  1.66  dholland 	time_t dom_checktime;
     95  1.66  dholland 	time_t dom_asktime;
     96   1.1   deraadt 	int dom_lockfd;
     97   1.1   deraadt 	int dom_alive;
     98  1.64  dholland 	uint32_t dom_xid;
     99   1.1   deraadt };
    100   1.1   deraadt 
    101  1.68  dholland #define BUFSIZE		1400
    102  1.68  dholland 
    103  1.24  christos static char *domainname;
    104   1.1   deraadt 
    105  1.24  christos static struct _dom_binding *ypbindlist;
    106  1.24  christos static int check;
    107   1.1   deraadt 
    108  1.69  dholland static ypbind_mode_t ypbindmode;
    109  1.27   thorpej 
    110  1.27   thorpej /*
    111  1.27   thorpej  * If ypbindmode is YPBIND_SETLOCAL or YPBIND_SETALL, this indicates
    112  1.27   thorpej  * whether or not we've been "ypset".  If we haven't, we behave like
    113  1.27   thorpej  * YPBIND_BROADCAST.  If we have, we behave like YPBIND_DIRECT.
    114  1.27   thorpej  */
    115  1.69  dholland static int been_ypset;
    116  1.27   thorpej 
    117  1.30     lukem static int insecure;
    118  1.24  christos static int rpcsock, pingsock;
    119  1.24  christos static struct rmtcallargs rmtca;
    120  1.24  christos static struct rmtcallres rmtcr;
    121  1.26        ws static bool_t rmtcr_outval;
    122  1.64  dholland static unsigned long rmtcr_port;
    123  1.24  christos static SVCXPRT *udptransp, *tcptransp;
    124  1.24  christos 
    125  1.68  dholland ////////////////////////////////////////////////////////////
    126  1.68  dholland // logging
    127  1.68  dholland 
    128  1.24  christos #ifdef DEBUG
    129  1.68  dholland #define DPRINTF(...) (debug ? (void)printf(__VA_ARGS__) : (void)0)
    130  1.68  dholland static int debug;
    131  1.68  dholland #else
    132  1.68  dholland #define DPRINTF(...)
    133  1.24  christos #endif
    134  1.36       mrg 
    135  1.69  dholland static void yp_log(int, const char *, ...) __printflike(2, 3);
    136  1.69  dholland 
    137  1.37    bouyer static void
    138  1.37    bouyer yp_log(int pri, const char *fmt, ...)
    139  1.37    bouyer {
    140  1.37    bouyer 	va_list ap;
    141  1.37    bouyer 
    142  1.37    bouyer 	va_start(ap, fmt);
    143  1.37    bouyer 
    144  1.37    bouyer #if defined(DEBUG)
    145  1.37    bouyer 	if (debug)
    146  1.57  christos 		(void)vprintf(fmt, ap);
    147  1.37    bouyer 	else
    148  1.37    bouyer #endif
    149  1.37    bouyer 		vsyslog(pri, fmt, ap);
    150  1.37    bouyer 	va_end(ap);
    151  1.37    bouyer }
    152  1.37    bouyer 
    153  1.68  dholland ////////////////////////////////////////////////////////////
    154  1.68  dholland // struct _dom_binding
    155  1.68  dholland 
    156  1.68  dholland static struct _dom_binding *
    157  1.68  dholland xid2ypdb(uint32_t xid)
    158  1.68  dholland {
    159  1.68  dholland 	struct _dom_binding *ypdb;
    160  1.68  dholland 
    161  1.68  dholland 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
    162  1.68  dholland 		if (ypdb->dom_xid == xid)
    163  1.68  dholland 			break;
    164  1.68  dholland 	return (ypdb);
    165  1.68  dholland }
    166  1.68  dholland 
    167  1.68  dholland static uint32_t
    168  1.68  dholland unique_xid(struct _dom_binding *ypdb)
    169  1.68  dholland {
    170  1.68  dholland 	uint32_t tmp_xid;
    171  1.68  dholland 
    172  1.68  dholland 	tmp_xid = ((uint32_t)(unsigned long)ypdb) & 0xffffffff;
    173  1.68  dholland 	while (xid2ypdb(tmp_xid) != NULL)
    174  1.68  dholland 		tmp_xid++;
    175  1.68  dholland 
    176  1.68  dholland 	return tmp_xid;
    177  1.68  dholland }
    178  1.68  dholland 
    179  1.24  christos static struct _dom_binding *
    180  1.47       wiz makebinding(const char *dm)
    181  1.24  christos {
    182  1.24  christos 	struct _dom_binding *ypdb;
    183   1.1   deraadt 
    184  1.63  dholland 	if ((ypdb = malloc(sizeof *ypdb)) == NULL) {
    185  1.37    bouyer 		yp_log(LOG_ERR, "makebinding");
    186  1.37    bouyer 		exit(1);
    187  1.37    bouyer 	}
    188   1.1   deraadt 
    189  1.36       mrg 	(void)memset(ypdb, 0, sizeof *ypdb);
    190  1.52       jrf 	(void)strlcpy(ypdb->dom_domain, dm, sizeof ypdb->dom_domain);
    191  1.24  christos 	return ypdb;
    192  1.24  christos }
    193  1.24  christos 
    194  1.68  dholland ////////////////////////////////////////////////////////////
    195  1.68  dholland // locks
    196  1.68  dholland 
    197  1.24  christos static int
    198  1.47       wiz makelock(struct _dom_binding *ypdb)
    199  1.24  christos {
    200  1.24  christos 	int fd;
    201  1.24  christos 	char path[MAXPATHLEN];
    202  1.20       cgd 
    203  1.36       mrg 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
    204  1.24  christos 	    ypdb->dom_domain, ypdb->dom_vers);
    205  1.24  christos 
    206  1.24  christos 	if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
    207  1.36       mrg 		(void)mkdir(BINDINGDIR, 0755);
    208  1.24  christos 		if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
    209  1.24  christos 			return -1;
    210  1.24  christos 	}
    211  1.24  christos 
    212  1.24  christos #if O_SHLOCK == 0
    213  1.36       mrg 	(void)flock(fd, LOCK_SH);
    214  1.24  christos #endif
    215  1.24  christos 	return fd;
    216  1.24  christos }
    217  1.24  christos 
    218  1.24  christos static void
    219  1.47       wiz removelock(struct _dom_binding *ypdb)
    220  1.24  christos {
    221  1.24  christos 	char path[MAXPATHLEN];
    222  1.24  christos 
    223  1.36       mrg 	(void)snprintf(path, sizeof(path), "%s/%s.%ld",
    224  1.24  christos 	    BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
    225  1.36       mrg 	(void)unlink(path);
    226  1.24  christos }
    227  1.24  christos 
    228  1.59     chuck /*
    229  1.60  christos  * purge_bindingdir: remove old binding files (i.e. "rm BINDINGDIR\/\*.[0-9]")
    230  1.59     chuck  *
    231  1.59     chuck  * local YP functions [e.g. yp_master()] will fail without even talking
    232  1.59     chuck  * to ypbind if there is a stale (non-flock'd) binding file present.
    233  1.59     chuck  * we have to scan the entire BINDINGDIR for binding files, because
    234  1.59     chuck  * ypbind may bind more than just the yp_get_default_domain() domain.
    235  1.59     chuck  */
    236  1.59     chuck static int
    237  1.60  christos purge_bindingdir(const char *dirpath)
    238  1.61     skrll {
    239  1.59     chuck 	DIR *dirp;
    240  1.59     chuck 	int unlinkedfiles, l;
    241  1.59     chuck 	struct dirent *dp;
    242  1.59     chuck 	char pathname[MAXPATHLEN];
    243  1.59     chuck 
    244  1.59     chuck 	if ((dirp = opendir(dirpath)) == NULL)
    245  1.59     chuck 		return(-1);   /* at this point, shouldn't ever happen */
    246  1.59     chuck 
    247  1.59     chuck 	do {
    248  1.59     chuck 		unlinkedfiles = 0;
    249  1.59     chuck 		while ((dp = readdir(dirp)) != NULL) {
    250  1.59     chuck 			l = dp->d_namlen;
    251  1.59     chuck 			/* 'rm *.[0-9]' */
    252  1.59     chuck 			if (l > 2 && dp->d_name[l-2] == '.' &&
    253  1.59     chuck 			    dp->d_name[l-1] >= '0' && dp->d_name[l-1] <= '9') {
    254  1.59     chuck 				(void)snprintf(pathname, sizeof(pathname),
    255  1.59     chuck 					"%s/%s", dirpath, dp->d_name);
    256  1.59     chuck 				if (unlink(pathname) < 0 && errno != ENOENT)
    257  1.59     chuck 					return(-1);
    258  1.59     chuck 				unlinkedfiles++;
    259  1.59     chuck 			}
    260  1.59     chuck 		}
    261  1.59     chuck 
    262  1.59     chuck 		/* rescan dir if we removed it */
    263  1.59     chuck 		if (unlinkedfiles)
    264  1.59     chuck 			rewinddir(dirp);
    265  1.59     chuck 
    266  1.59     chuck 	} while (unlinkedfiles);
    267  1.59     chuck 
    268  1.59     chuck 	closedir(dirp);
    269  1.59     chuck 	return(0);
    270  1.59     chuck }
    271  1.59     chuck 
    272  1.68  dholland ////////////////////////////////////////////////////////////
    273  1.68  dholland // sunrpc twaddle
    274  1.68  dholland 
    275  1.68  dholland /*
    276  1.68  dholland  * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
    277  1.68  dholland  */
    278  1.69  dholland static void
    279  1.68  dholland rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
    280  1.68  dholland {
    281  1.68  dholland 	struct _dom_binding *ypdb;
    282  1.68  dholland 	struct iovec iov[2];
    283  1.68  dholland 	struct ypbind_resp ybr;
    284  1.68  dholland 	int fd;
    285  1.68  dholland 
    286  1.68  dholland 	DPRINTF("returned from %s about %s\n",
    287  1.68  dholland 		inet_ntoa(raddrp->sin_addr), dom);
    288  1.68  dholland 
    289  1.68  dholland 	if (dom == NULL)
    290  1.68  dholland 		return;
    291  1.68  dholland 
    292  1.68  dholland 	if (_yp_invalid_domain(dom))
    293  1.68  dholland 		return;
    294  1.68  dholland 
    295  1.68  dholland 		/* don't support insecure servers by default */
    296  1.68  dholland 	if (!insecure && ntohs(raddrp->sin_port) >= IPPORT_RESERVED)
    297  1.68  dholland 		return;
    298  1.68  dholland 
    299  1.68  dholland 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
    300  1.68  dholland 		if (!strcmp(ypdb->dom_domain, dom))
    301  1.68  dholland 			break;
    302  1.68  dholland 
    303  1.68  dholland 	if (ypdb == NULL) {
    304  1.68  dholland 		if (force == 0)
    305  1.68  dholland 			return;
    306  1.68  dholland 		ypdb = makebinding(dom);
    307  1.68  dholland 		ypdb->dom_lockfd = -1;
    308  1.68  dholland 		ypdb->dom_pnext = ypbindlist;
    309  1.68  dholland 		ypbindlist = ypdb;
    310  1.68  dholland 	}
    311  1.68  dholland 
    312  1.68  dholland 	/* soft update, alive */
    313  1.68  dholland 	if (ypdb->dom_alive == 1 && force == 0) {
    314  1.68  dholland 		if (!memcmp(&ypdb->dom_server_addr, raddrp,
    315  1.68  dholland 			    sizeof ypdb->dom_server_addr)) {
    316  1.68  dholland 			ypdb->dom_alive = 1;
    317  1.68  dholland 			/* recheck binding in 60 sec */
    318  1.68  dholland 			ypdb->dom_checktime = time(NULL) + 60;
    319  1.68  dholland 		}
    320  1.68  dholland 		return;
    321  1.68  dholland 	}
    322  1.68  dholland 
    323  1.68  dholland 	(void)memcpy(&ypdb->dom_server_addr, raddrp,
    324  1.68  dholland 	    sizeof ypdb->dom_server_addr);
    325  1.68  dholland 	/* recheck binding in 60 seconds */
    326  1.68  dholland 	ypdb->dom_checktime = time(NULL) + 60;
    327  1.68  dholland 	ypdb->dom_vers = YPVERS;
    328  1.68  dholland 	ypdb->dom_alive = 1;
    329  1.68  dholland 
    330  1.68  dholland 	if (ypdb->dom_lockfd != -1)
    331  1.68  dholland 		(void)close(ypdb->dom_lockfd);
    332  1.68  dholland 
    333  1.68  dholland 	if ((fd = makelock(ypdb)) == -1)
    334  1.68  dholland 		return;
    335  1.68  dholland 
    336  1.68  dholland 	/*
    337  1.68  dholland 	 * ok, if BINDINGDIR exists, and we can create the binding file,
    338  1.68  dholland 	 * then write to it..
    339  1.68  dholland 	 */
    340  1.68  dholland 	ypdb->dom_lockfd = fd;
    341  1.68  dholland 
    342  1.68  dholland 	iov[0].iov_base = &(udptransp->xp_port);
    343  1.68  dholland 	iov[0].iov_len = sizeof udptransp->xp_port;
    344  1.68  dholland 	iov[1].iov_base = &ybr;
    345  1.68  dholland 	iov[1].iov_len = sizeof ybr;
    346  1.68  dholland 
    347  1.68  dholland 	(void)memset(&ybr, 0, sizeof ybr);
    348  1.68  dholland 	ybr.ypbind_status = YPBIND_SUCC_VAL;
    349  1.68  dholland 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
    350  1.68  dholland 	    raddrp->sin_addr;
    351  1.68  dholland 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    352  1.68  dholland 	    raddrp->sin_port;
    353  1.68  dholland 
    354  1.68  dholland 	if ((size_t)writev(ypdb->dom_lockfd, iov, 2) !=
    355  1.68  dholland 	    iov[0].iov_len + iov[1].iov_len) {
    356  1.68  dholland 		yp_log(LOG_WARNING, "writev: %m");
    357  1.68  dholland 		(void)close(ypdb->dom_lockfd);
    358  1.68  dholland 		removelock(ypdb);
    359  1.68  dholland 		ypdb->dom_lockfd = -1;
    360  1.68  dholland 	}
    361  1.68  dholland }
    362  1.68  dholland 
    363  1.24  christos static void *
    364  1.57  christos /*ARGSUSED*/
    365  1.47       wiz ypbindproc_null_2(SVCXPRT *transp, void *argp)
    366   1.1   deraadt {
    367   1.1   deraadt 	static char res;
    368   1.1   deraadt 
    369  1.65  dholland 	DPRINTF("ypbindproc_null_2\n");
    370  1.36       mrg 	(void)memset(&res, 0, sizeof(res));
    371   1.1   deraadt 	return (void *)&res;
    372   1.1   deraadt }
    373   1.1   deraadt 
    374  1.24  christos static void *
    375  1.57  christos /*ARGSUSED*/
    376  1.47       wiz ypbindproc_domain_2(SVCXPRT *transp, void *argp)
    377   1.1   deraadt {
    378   1.1   deraadt 	static struct ypbind_resp res;
    379   1.1   deraadt 	struct _dom_binding *ypdb;
    380  1.24  christos 	char *arg = *(char **) argp;
    381   1.9   deraadt 	time_t now;
    382  1.30     lukem 	int count;
    383   1.1   deraadt 
    384  1.65  dholland 	DPRINTF("ypbindproc_domain_2 %s\n", arg);
    385  1.30     lukem 	if (_yp_invalid_domain(arg))
    386  1.30     lukem 		return NULL;
    387  1.30     lukem 
    388  1.36       mrg 	(void)memset(&res, 0, sizeof res);
    389   1.1   deraadt 	res.ypbind_status = YPBIND_FAIL_VAL;
    390   1.1   deraadt 
    391  1.30     lukem 	for (count = 0, ypdb = ypbindlist;
    392  1.30     lukem 	    ypdb != NULL;
    393  1.30     lukem 	    ypdb = ypdb->dom_pnext, count++) {
    394  1.30     lukem 		if (count > 100)
    395  1.30     lukem 			return NULL;		/* prevent denial of service */
    396  1.22   thorpej 		if (!strcmp(ypdb->dom_domain, arg))
    397   1.1   deraadt 			break;
    398  1.30     lukem 	}
    399   1.1   deraadt 
    400  1.17   mycroft 	if (ypdb == NULL) {
    401  1.24  christos 		ypdb = makebinding(arg);
    402   1.1   deraadt 		ypdb->dom_vers = YPVERS;
    403   1.1   deraadt 		ypdb->dom_alive = 0;
    404   1.1   deraadt 		ypdb->dom_lockfd = -1;
    405  1.24  christos 		removelock(ypdb);
    406  1.20       cgd 		ypdb->dom_xid = unique_xid(ypdb);
    407   1.1   deraadt 		ypdb->dom_pnext = ypbindlist;
    408   1.1   deraadt 		ypbindlist = ypdb;
    409   1.1   deraadt 		check++;
    410  1.65  dholland 		DPRINTF("unknown domain %s\n", arg);
    411   1.1   deraadt 		return NULL;
    412   1.1   deraadt 	}
    413   1.1   deraadt 
    414  1.24  christos 	if (ypdb->dom_alive == 0) {
    415  1.65  dholland 		DPRINTF("dead domain %s\n", arg);
    416   1.1   deraadt 		return NULL;
    417  1.24  christos 	}
    418   1.1   deraadt 
    419   1.9   deraadt #ifdef HEURISTIC
    420  1.57  christos 	(void)time(&now);
    421  1.66  dholland 	if (now < ypdb->dom_asktime + 5) {
    422   1.1   deraadt 		/*
    423   1.9   deraadt 		 * Hmm. More than 2 requests in 5 seconds have indicated
    424   1.9   deraadt 		 * that my binding is possibly incorrect.
    425   1.9   deraadt 		 * Ok, do an immediate poll of the server.
    426   1.1   deraadt 		 */
    427  1.66  dholland 		if (ypdb->dom_checktime >= now) {
    428   1.9   deraadt 			/* don't flood it */
    429  1.66  dholland 			ypdb->dom_checktime = 0;
    430   1.9   deraadt 			check++;
    431   1.9   deraadt 		}
    432   1.1   deraadt 	}
    433  1.66  dholland 	ypdb->dom_asktime = now;
    434   1.1   deraadt #endif
    435   1.1   deraadt 
    436   1.1   deraadt 	res.ypbind_status = YPBIND_SUCC_VAL;
    437   1.1   deraadt 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
    438   1.1   deraadt 		ypdb->dom_server_addr.sin_addr.s_addr;
    439   1.1   deraadt 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    440  1.49    bouyer 		ypdb->dom_server_addr.sin_port;
    441  1.65  dholland 	DPRINTF("domain %s at %s/%d\n", ypdb->dom_domain,
    442  1.65  dholland 		inet_ntoa(ypdb->dom_server_addr.sin_addr),
    443  1.65  dholland 		ntohs(ypdb->dom_server_addr.sin_port));
    444   1.1   deraadt 	return &res;
    445   1.1   deraadt }
    446   1.1   deraadt 
    447  1.24  christos static void *
    448  1.47       wiz ypbindproc_setdom_2(SVCXPRT *transp, void *argp)
    449   1.1   deraadt {
    450  1.24  christos 	struct ypbind_setdom *sd = argp;
    451   1.1   deraadt 	struct sockaddr_in *fromsin, bindsin;
    452  1.13   deraadt 	static bool_t res;
    453   1.1   deraadt 
    454  1.65  dholland 	DPRINTF("ypbindproc_setdom_2 %s\n", inet_ntoa(bindsin.sin_addr));
    455  1.36       mrg 	(void)memset(&res, 0, sizeof(res));
    456   1.1   deraadt 	fromsin = svc_getcaller(transp);
    457   1.1   deraadt 
    458  1.27   thorpej 	switch (ypbindmode) {
    459  1.27   thorpej 	case YPBIND_SETLOCAL:
    460  1.24  christos 		if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
    461  1.65  dholland 			DPRINTF("ypset from %s denied\n",
    462  1.65  dholland 				inet_ntoa(fromsin->sin_addr));
    463  1.24  christos 			return NULL;
    464  1.24  christos 		}
    465  1.27   thorpej 		/* FALLTHROUGH */
    466  1.27   thorpej 
    467  1.27   thorpej 	case YPBIND_SETALL:
    468  1.27   thorpej 		been_ypset = 1;
    469   1.1   deraadt 		break;
    470  1.27   thorpej 
    471  1.27   thorpej 	case YPBIND_DIRECT:
    472  1.27   thorpej 	case YPBIND_BROADCAST:
    473   1.1   deraadt 	default:
    474  1.65  dholland 		DPRINTF("ypset denied\n");
    475  1.24  christos 		return NULL;
    476   1.1   deraadt 	}
    477   1.4   deraadt 
    478  1.24  christos 	if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
    479  1.65  dholland 		DPRINTF("ypset from unprivileged port denied\n");
    480  1.13   deraadt 		return &res;
    481  1.24  christos 	}
    482   1.1   deraadt 
    483  1.24  christos 	if (sd->ypsetdom_vers != YPVERS) {
    484  1.65  dholland 		DPRINTF("ypset with wrong version denied\n");
    485  1.13   deraadt 		return &res;
    486  1.24  christos 	}
    487   1.1   deraadt 
    488  1.36       mrg 	(void)memset(&bindsin, 0, sizeof bindsin);
    489   1.1   deraadt 	bindsin.sin_family = AF_INET;
    490  1.17   mycroft 	bindsin.sin_len = sizeof(bindsin);
    491  1.24  christos 	bindsin.sin_addr = sd->ypsetdom_addr;
    492  1.24  christos 	bindsin.sin_port = sd->ypsetdom_port;
    493  1.24  christos 	rpc_received(sd->ypsetdom_domain, &bindsin, 1);
    494  1.24  christos 
    495  1.65  dholland 	DPRINTF("ypset to %s succeeded\n", inet_ntoa(bindsin.sin_addr));
    496   1.1   deraadt 	res = 1;
    497  1.13   deraadt 	return &res;
    498   1.1   deraadt }
    499   1.1   deraadt 
    500   1.1   deraadt static void
    501  1.47       wiz ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
    502   1.1   deraadt {
    503   1.1   deraadt 	union {
    504  1.22   thorpej 		char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
    505   1.1   deraadt 		struct ypbind_setdom ypbindproc_setdom_2_arg;
    506  1.56      tron 		void *alignment;
    507   1.1   deraadt 	} argument;
    508   1.1   deraadt 	struct authunix_parms *creds;
    509   1.1   deraadt 	char *result;
    510  1.24  christos 	xdrproc_t xdr_argument, xdr_result;
    511  1.47       wiz 	void *(*local)(SVCXPRT *, void *);
    512   1.1   deraadt 
    513   1.1   deraadt 	switch (rqstp->rq_proc) {
    514   1.1   deraadt 	case YPBINDPROC_NULL:
    515   1.1   deraadt 		xdr_argument = xdr_void;
    516   1.1   deraadt 		xdr_result = xdr_void;
    517  1.24  christos 		local = ypbindproc_null_2;
    518   1.1   deraadt 		break;
    519   1.1   deraadt 
    520   1.1   deraadt 	case YPBINDPROC_DOMAIN:
    521  1.21   thorpej 		xdr_argument = xdr_ypdomain_wrap_string;
    522   1.1   deraadt 		xdr_result = xdr_ypbind_resp;
    523  1.24  christos 		local = ypbindproc_domain_2;
    524   1.1   deraadt 		break;
    525   1.1   deraadt 
    526   1.1   deraadt 	case YPBINDPROC_SETDOM:
    527  1.17   mycroft 		switch (rqstp->rq_cred.oa_flavor) {
    528   1.1   deraadt 		case AUTH_UNIX:
    529   1.1   deraadt 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
    530  1.17   mycroft 			if (creds->aup_uid != 0) {
    531   1.1   deraadt 				svcerr_auth(transp, AUTH_BADCRED);
    532   1.1   deraadt 				return;
    533   1.1   deraadt 			}
    534   1.1   deraadt 			break;
    535   1.1   deraadt 		default:
    536   1.1   deraadt 			svcerr_auth(transp, AUTH_TOOWEAK);
    537   1.1   deraadt 			return;
    538   1.1   deraadt 		}
    539   1.1   deraadt 
    540   1.1   deraadt 		xdr_argument = xdr_ypbind_setdom;
    541   1.1   deraadt 		xdr_result = xdr_void;
    542  1.24  christos 		local = ypbindproc_setdom_2;
    543   1.1   deraadt 		break;
    544   1.1   deraadt 
    545   1.1   deraadt 	default:
    546   1.1   deraadt 		svcerr_noproc(transp);
    547   1.1   deraadt 		return;
    548   1.1   deraadt 	}
    549  1.36       mrg 	(void)memset(&argument, 0, sizeof(argument));
    550  1.57  christos 	if (!svc_getargs(transp, xdr_argument, (caddr_t)(void *)&argument)) {
    551   1.1   deraadt 		svcerr_decode(transp);
    552   1.1   deraadt 		return;
    553   1.1   deraadt 	}
    554  1.24  christos 	result = (*local)(transp, &argument);
    555   1.1   deraadt 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    556   1.1   deraadt 		svcerr_systemerr(transp);
    557   1.1   deraadt 	}
    558   1.1   deraadt 	return;
    559   1.1   deraadt }
    560   1.1   deraadt 
    561  1.68  dholland ////////////////////////////////////////////////////////////
    562  1.68  dholland // operational logic
    563  1.68  dholland 
    564  1.68  dholland static int
    565  1.68  dholland broadcast(char *buf, int outlen)
    566   1.1   deraadt {
    567  1.68  dholland 	struct ifaddrs *ifap, *ifa;
    568  1.68  dholland 	struct sockaddr_in bindsin;
    569  1.68  dholland 	struct in_addr in;
    570  1.68  dholland 
    571  1.68  dholland 	(void)memset(&bindsin, 0, sizeof bindsin);
    572  1.68  dholland 	bindsin.sin_family = AF_INET;
    573  1.68  dholland 	bindsin.sin_len = sizeof(bindsin);
    574  1.68  dholland 	bindsin.sin_port = htons(PMAPPORT);
    575   1.1   deraadt 
    576  1.68  dholland 	if (getifaddrs(&ifap) != 0) {
    577  1.68  dholland 		yp_log(LOG_WARNING, "broadcast: getifaddrs: %m");
    578  1.68  dholland 		return (-1);
    579  1.68  dholland 	}
    580  1.68  dholland 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    581  1.68  dholland 		if (ifa->ifa_addr->sa_family != AF_INET)
    582  1.68  dholland 			continue;
    583  1.68  dholland 		if ((ifa->ifa_flags & IFF_UP) == 0)
    584  1.68  dholland 			continue;
    585   1.1   deraadt 
    586  1.68  dholland 		switch (ifa->ifa_flags & (IFF_LOOPBACK | IFF_BROADCAST)) {
    587  1.68  dholland 		case IFF_BROADCAST:
    588  1.68  dholland 			if (!ifa->ifa_broadaddr)
    589  1.68  dholland 				continue;
    590  1.68  dholland 			if (ifa->ifa_broadaddr->sa_family != AF_INET)
    591  1.68  dholland 				continue;
    592  1.68  dholland 			in = ((struct sockaddr_in *)(void *)ifa->ifa_broadaddr)->sin_addr;
    593  1.68  dholland 			break;
    594  1.68  dholland 		case IFF_LOOPBACK:
    595  1.68  dholland 			in = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr;
    596  1.68  dholland 			break;
    597  1.68  dholland 		default:
    598  1.68  dholland 			continue;
    599  1.68  dholland 		}
    600  1.27   thorpej 
    601  1.68  dholland 		bindsin.sin_addr = in;
    602  1.68  dholland 		DPRINTF("broadcast %x\n", bindsin.sin_addr.s_addr);
    603  1.68  dholland 		if (sendto(rpcsock, buf, outlen, 0,
    604  1.68  dholland 		    (struct sockaddr *)(void *)&bindsin,
    605  1.68  dholland 		    (socklen_t)bindsin.sin_len) == -1)
    606  1.68  dholland 			yp_log(LOG_WARNING, "broadcast: sendto: %m");
    607   1.1   deraadt 	}
    608  1.68  dholland 	freeifaddrs(ifap);
    609  1.68  dholland 	return (0);
    610  1.68  dholland }
    611   1.1   deraadt 
    612  1.68  dholland static int
    613  1.68  dholland direct(char *buf, int outlen)
    614  1.68  dholland {
    615  1.68  dholland 	static FILE *df;
    616  1.68  dholland 	static char ypservers_path[MAXPATHLEN];
    617  1.68  dholland 	char line[_POSIX2_LINE_MAX];
    618  1.68  dholland 	char *p;
    619  1.68  dholland 	struct hostent *hp;
    620  1.68  dholland 	struct sockaddr_in bindsin;
    621  1.68  dholland 	int i, count = 0;
    622  1.37    bouyer 
    623  1.68  dholland 	if (df)
    624  1.68  dholland 		rewind(df);
    625  1.68  dholland 	else {
    626  1.68  dholland 		(void)snprintf(ypservers_path, sizeof(ypservers_path),
    627  1.68  dholland 		    "%s/%s%s", BINDINGDIR, domainname, YPSERVERSSUFF);
    628  1.27   thorpej 		df = fopen(ypservers_path, "r");
    629  1.37    bouyer 		if (df == NULL) {
    630  1.45     lukem 			yp_log(LOG_ERR, "%s: ", ypservers_path);
    631  1.37    bouyer 			exit(1);
    632  1.37    bouyer 		}
    633  1.27   thorpej 	}
    634  1.27   thorpej 
    635  1.57  christos 	(void)memset(&bindsin, 0, sizeof bindsin);
    636  1.27   thorpej 	bindsin.sin_family = AF_INET;
    637  1.27   thorpej 	bindsin.sin_len = sizeof(bindsin);
    638  1.27   thorpej 	bindsin.sin_port = htons(PMAPPORT);
    639  1.27   thorpej 
    640  1.57  christos 	while(fgets(line, (int)sizeof(line), df) != NULL) {
    641  1.27   thorpej 		/* skip lines that are too big */
    642  1.27   thorpej 		p = strchr(line, '\n');
    643  1.27   thorpej 		if (p == NULL) {
    644  1.27   thorpej 			int c;
    645  1.27   thorpej 
    646  1.27   thorpej 			while ((c = getc(df)) != '\n' && c != EOF)
    647  1.27   thorpej 				;
    648  1.27   thorpej 			continue;
    649  1.27   thorpej 		}
    650  1.27   thorpej 		*p = '\0';
    651  1.27   thorpej 		p = line;
    652  1.53       dsl 		while (isspace((unsigned char)*p))
    653  1.27   thorpej 			p++;
    654  1.27   thorpej 		if (*p == '#')
    655  1.27   thorpej 			continue;
    656  1.27   thorpej 		hp = gethostbyname(p);
    657  1.27   thorpej 		if (!hp) {
    658  1.45     lukem 			yp_log(LOG_WARNING, "%s: %s", p, hstrerror(h_errno));
    659  1.27   thorpej 			continue;
    660  1.27   thorpej 		}
    661  1.27   thorpej 		/* step through all addresses in case first is unavailable */
    662  1.27   thorpej 		for (i = 0; hp->h_addr_list[i]; i++) {
    663  1.57  christos 			(void)memcpy(&bindsin.sin_addr, hp->h_addr_list[0],
    664  1.27   thorpej 			    hp->h_length);
    665  1.27   thorpej 			if (sendto(rpcsock, buf, outlen, 0,
    666  1.57  christos 			    (struct sockaddr *)(void *)&bindsin,
    667  1.57  christos 			    (socklen_t)sizeof(bindsin)) < 0) {
    668  1.37    bouyer 				yp_log(LOG_WARNING, "direct: sendto: %m");
    669  1.27   thorpej 				continue;
    670  1.27   thorpej 			} else
    671  1.27   thorpej 				count++;
    672  1.27   thorpej 		}
    673  1.27   thorpej 	}
    674  1.37    bouyer 	if (!count) {
    675  1.45     lukem 		yp_log(LOG_WARNING, "no contactable servers found in %s",
    676  1.27   thorpej 		    ypservers_path);
    677  1.37    bouyer 		return -1;
    678  1.37    bouyer 	}
    679  1.27   thorpej 	return 0;
    680  1.27   thorpej }
    681  1.27   thorpej 
    682  1.27   thorpej static int
    683  1.47       wiz direct_set(char *buf, int outlen, struct _dom_binding *ypdb)
    684  1.27   thorpej {
    685  1.27   thorpej 	struct sockaddr_in bindsin;
    686  1.27   thorpej 	char path[MAXPATHLEN];
    687  1.27   thorpej 	struct iovec iov[2];
    688  1.27   thorpej 	struct ypbind_resp ybr;
    689  1.27   thorpej 	SVCXPRT dummy_svc;
    690  1.57  christos 	int fd;
    691  1.57  christos 	ssize_t bytes;
    692  1.27   thorpej 
    693  1.27   thorpej 	/*
    694  1.27   thorpej 	 * Gack, we lose if binding file went away.  We reset
    695  1.27   thorpej 	 * "been_set" if this happens, otherwise we'll never
    696  1.27   thorpej 	 * bind again.
    697  1.27   thorpej 	 */
    698  1.57  christos 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
    699  1.27   thorpej 	    ypdb->dom_domain, ypdb->dom_vers);
    700  1.27   thorpej 
    701  1.27   thorpej 	if ((fd = open(path, O_SHLOCK|O_RDONLY, 0644)) == -1) {
    702  1.37    bouyer 		yp_log(LOG_WARNING, "%s: %m", path);
    703  1.27   thorpej 		been_ypset = 0;
    704  1.27   thorpej 		return -1;
    705  1.27   thorpej 	}
    706  1.27   thorpej 
    707  1.27   thorpej #if O_SHLOCK == 0
    708  1.36       mrg 	(void)flock(fd, LOCK_SH);
    709  1.27   thorpej #endif
    710  1.27   thorpej 
    711  1.27   thorpej 	/* Read the binding file... */
    712  1.57  christos 	iov[0].iov_base = &(dummy_svc.xp_port);
    713  1.27   thorpej 	iov[0].iov_len = sizeof(dummy_svc.xp_port);
    714  1.57  christos 	iov[1].iov_base = &ybr;
    715  1.27   thorpej 	iov[1].iov_len = sizeof(ybr);
    716  1.27   thorpej 	bytes = readv(fd, iov, 2);
    717  1.27   thorpej 	(void)close(fd);
    718  1.58     lukem 	if ((size_t)bytes != (iov[0].iov_len + iov[1].iov_len)) {
    719  1.27   thorpej 		/* Binding file corrupt? */
    720  1.37    bouyer 		yp_log(LOG_WARNING, "%s: %m", path);
    721  1.27   thorpej 		been_ypset = 0;
    722  1.27   thorpej 		return -1;
    723  1.68  dholland 	}
    724  1.68  dholland 
    725  1.68  dholland 	bindsin.sin_addr =
    726  1.68  dholland 	    ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
    727  1.68  dholland 
    728  1.68  dholland 	if (sendto(rpcsock, buf, outlen, 0,
    729  1.68  dholland 	    (struct sockaddr *)(void *)&bindsin,
    730  1.68  dholland 	    (socklen_t)sizeof(bindsin)) < 0) {
    731  1.68  dholland 		yp_log(LOG_WARNING, "direct_set: sendto: %m");
    732  1.68  dholland 		return -1;
    733  1.68  dholland 	}
    734  1.68  dholland 
    735  1.68  dholland 	return 0;
    736  1.68  dholland }
    737  1.68  dholland 
    738  1.68  dholland static enum clnt_stat
    739  1.68  dholland handle_replies(void)
    740  1.68  dholland {
    741  1.68  dholland 	char buf[BUFSIZE];
    742  1.68  dholland 	socklen_t fromlen;
    743  1.68  dholland 	ssize_t inlen;
    744  1.68  dholland 	struct _dom_binding *ypdb;
    745  1.68  dholland 	struct sockaddr_in raddr;
    746  1.68  dholland 	struct rpc_msg msg;
    747  1.68  dholland 	XDR xdr;
    748  1.68  dholland 
    749  1.68  dholland recv_again:
    750  1.68  dholland 	DPRINTF("handle_replies receiving\n");
    751  1.68  dholland 	(void)memset(&xdr, 0, sizeof(xdr));
    752  1.68  dholland 	(void)memset(&msg, 0, sizeof(msg));
    753  1.68  dholland 	msg.acpted_rply.ar_verf = _null_auth;
    754  1.68  dholland 	msg.acpted_rply.ar_results.where = (caddr_t)(void *)&rmtcr;
    755  1.68  dholland 	msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
    756  1.68  dholland 
    757  1.68  dholland try_again:
    758  1.68  dholland 	fromlen = sizeof(struct sockaddr);
    759  1.68  dholland 	inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
    760  1.68  dholland 		(struct sockaddr *)(void *)&raddr, &fromlen);
    761  1.68  dholland 	if (inlen < 0) {
    762  1.68  dholland 		if (errno == EINTR)
    763  1.68  dholland 			goto try_again;
    764  1.68  dholland 		DPRINTF("handle_replies: recvfrom failed (%s)\n",
    765  1.68  dholland 			strerror(errno));
    766  1.68  dholland 		return RPC_CANTRECV;
    767  1.68  dholland 	}
    768  1.68  dholland 	if ((size_t)inlen < sizeof(uint32_t))
    769  1.68  dholland 		goto recv_again;
    770  1.27   thorpej 
    771  1.68  dholland 	/*
    772  1.68  dholland 	 * see if reply transaction id matches sent id.
    773  1.68  dholland 	 * If so, decode the results.
    774  1.68  dholland 	 */
    775  1.68  dholland 	xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
    776  1.68  dholland 	if (xdr_replymsg(&xdr, &msg)) {
    777  1.68  dholland 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    778  1.68  dholland 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
    779  1.68  dholland 			raddr.sin_port = htons((uint16_t)rmtcr_port);
    780  1.68  dholland 			ypdb = xid2ypdb(msg.rm_xid);
    781  1.68  dholland 			if (ypdb != NULL)
    782  1.68  dholland 				rpc_received(ypdb->dom_domain, &raddr, 0);
    783  1.68  dholland 		}
    784  1.27   thorpej 	}
    785  1.68  dholland 	xdr.x_op = XDR_FREE;
    786  1.68  dholland 	msg.acpted_rply.ar_results.proc = xdr_void;
    787  1.68  dholland 	xdr_destroy(&xdr);
    788  1.27   thorpej 
    789  1.68  dholland 	return RPC_SUCCESS;
    790   1.1   deraadt }
    791   1.1   deraadt 
    792  1.24  christos static enum clnt_stat
    793  1.68  dholland handle_ping(void)
    794   1.1   deraadt {
    795  1.24  christos 	char buf[BUFSIZE];
    796  1.54       mrg 	socklen_t fromlen;
    797  1.57  christos 	ssize_t inlen;
    798  1.20       cgd 	struct _dom_binding *ypdb;
    799   1.1   deraadt 	struct sockaddr_in raddr;
    800   1.1   deraadt 	struct rpc_msg msg;
    801   1.1   deraadt 	XDR xdr;
    802  1.68  dholland 	bool_t res;
    803   1.1   deraadt 
    804   1.1   deraadt recv_again:
    805  1.68  dholland 	DPRINTF("handle_ping receiving\n");
    806  1.36       mrg 	(void)memset(&xdr, 0, sizeof(xdr));
    807  1.36       mrg 	(void)memset(&msg, 0, sizeof(msg));
    808   1.1   deraadt 	msg.acpted_rply.ar_verf = _null_auth;
    809  1.68  dholland 	msg.acpted_rply.ar_results.where = (caddr_t)(void *)&res;
    810  1.68  dholland 	msg.acpted_rply.ar_results.proc = xdr_bool;
    811   1.1   deraadt 
    812   1.1   deraadt try_again:
    813  1.68  dholland 	fromlen = sizeof (struct sockaddr);
    814  1.68  dholland 	inlen = recvfrom(pingsock, buf, sizeof buf, 0,
    815  1.68  dholland 	    (struct sockaddr *)(void *)&raddr, &fromlen);
    816  1.17   mycroft 	if (inlen < 0) {
    817  1.17   mycroft 		if (errno == EINTR)
    818   1.1   deraadt 			goto try_again;
    819  1.68  dholland 		DPRINTF("handle_ping: recvfrom failed (%s)\n",
    820  1.65  dholland 			strerror(errno));
    821   1.1   deraadt 		return RPC_CANTRECV;
    822   1.1   deraadt 	}
    823  1.64  dholland 	if ((size_t)inlen < sizeof(uint32_t))
    824   1.1   deraadt 		goto recv_again;
    825   1.1   deraadt 
    826   1.1   deraadt 	/*
    827   1.1   deraadt 	 * see if reply transaction id matches sent id.
    828   1.1   deraadt 	 * If so, decode the results.
    829   1.1   deraadt 	 */
    830  1.64  dholland 	xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
    831  1.17   mycroft 	if (xdr_replymsg(&xdr, &msg)) {
    832  1.17   mycroft 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    833  1.17   mycroft 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
    834  1.20       cgd 			ypdb = xid2ypdb(msg.rm_xid);
    835  1.20       cgd 			if (ypdb != NULL)
    836  1.20       cgd 				rpc_received(ypdb->dom_domain, &raddr, 0);
    837   1.1   deraadt 		}
    838   1.1   deraadt 	}
    839   1.1   deraadt 	xdr.x_op = XDR_FREE;
    840   1.1   deraadt 	msg.acpted_rply.ar_results.proc = xdr_void;
    841   1.1   deraadt 	xdr_destroy(&xdr);
    842   1.1   deraadt 
    843   1.1   deraadt 	return RPC_SUCCESS;
    844   1.1   deraadt }
    845   1.1   deraadt 
    846  1.68  dholland static int
    847  1.68  dholland nag_servers(struct _dom_binding *ypdb)
    848  1.68  dholland {
    849  1.68  dholland 	char *dom = ypdb->dom_domain;
    850  1.68  dholland 	struct rpc_msg msg;
    851  1.68  dholland 	char buf[BUFSIZE];
    852  1.68  dholland 	enum clnt_stat st;
    853  1.68  dholland 	int outlen;
    854  1.68  dholland 	AUTH *rpcua;
    855  1.68  dholland 	XDR xdr;
    856  1.68  dholland 
    857  1.68  dholland 	DPRINTF("nag_servers\n");
    858  1.68  dholland 	rmtca.xdr_args = xdr_ypdomain_wrap_string;
    859  1.68  dholland 	rmtca.args_ptr = (caddr_t)(void *)&dom;
    860  1.68  dholland 
    861  1.68  dholland 	(void)memset(&xdr, 0, sizeof xdr);
    862  1.68  dholland 	(void)memset(&msg, 0, sizeof msg);
    863  1.68  dholland 
    864  1.68  dholland 	rpcua = authunix_create_default();
    865  1.68  dholland 	if (rpcua == NULL) {
    866  1.68  dholland 		DPRINTF("cannot get unix auth\n");
    867  1.68  dholland 		return RPC_SYSTEMERROR;
    868  1.68  dholland 	}
    869  1.68  dholland 	msg.rm_direction = CALL;
    870  1.68  dholland 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    871  1.68  dholland 	msg.rm_call.cb_prog = PMAPPROG;
    872  1.68  dholland 	msg.rm_call.cb_vers = PMAPVERS;
    873  1.68  dholland 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
    874  1.68  dholland 	msg.rm_call.cb_cred = rpcua->ah_cred;
    875  1.68  dholland 	msg.rm_call.cb_verf = rpcua->ah_verf;
    876  1.68  dholland 
    877  1.68  dholland 	msg.rm_xid = ypdb->dom_xid;
    878  1.68  dholland 	xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
    879  1.68  dholland 	if (!xdr_callmsg(&xdr, &msg)) {
    880  1.68  dholland 		st = RPC_CANTENCODEARGS;
    881  1.68  dholland 		AUTH_DESTROY(rpcua);
    882  1.68  dholland 		return st;
    883  1.68  dholland 	}
    884  1.68  dholland 	if (!xdr_rmtcall_args(&xdr, &rmtca)) {
    885  1.68  dholland 		st = RPC_CANTENCODEARGS;
    886  1.68  dholland 		AUTH_DESTROY(rpcua);
    887  1.68  dholland 		return st;
    888  1.68  dholland 	}
    889  1.68  dholland 	outlen = (int)xdr_getpos(&xdr);
    890  1.68  dholland 	xdr_destroy(&xdr);
    891  1.68  dholland 	if (outlen < 1) {
    892  1.68  dholland 		st = RPC_CANTENCODEARGS;
    893  1.68  dholland 		AUTH_DESTROY(rpcua);
    894  1.68  dholland 		return st;
    895  1.68  dholland 	}
    896  1.68  dholland 	AUTH_DESTROY(rpcua);
    897  1.68  dholland 
    898  1.68  dholland 	if (ypdb->dom_lockfd != -1) {
    899  1.68  dholland 		(void)close(ypdb->dom_lockfd);
    900  1.68  dholland 		ypdb->dom_lockfd = -1;
    901  1.68  dholland 		removelock(ypdb);
    902  1.68  dholland 	}
    903  1.68  dholland 
    904  1.68  dholland 	if (ypdb->dom_alive == 2) {
    905  1.68  dholland 		/*
    906  1.68  dholland 		 * This resolves the following situation:
    907  1.68  dholland 		 * ypserver on other subnet was once bound,
    908  1.68  dholland 		 * but rebooted and is now using a different port
    909  1.68  dholland 		 */
    910  1.68  dholland 		struct sockaddr_in bindsin;
    911  1.68  dholland 
    912  1.68  dholland 		(void)memset(&bindsin, 0, sizeof bindsin);
    913  1.68  dholland 		bindsin.sin_family = AF_INET;
    914  1.68  dholland 		bindsin.sin_len = sizeof(bindsin);
    915  1.68  dholland 		bindsin.sin_port = htons(PMAPPORT);
    916  1.68  dholland 		bindsin.sin_addr = ypdb->dom_server_addr.sin_addr;
    917  1.68  dholland 
    918  1.68  dholland 		if (sendto(rpcsock, buf, outlen, 0,
    919  1.68  dholland 		    (struct sockaddr *)(void *)&bindsin,
    920  1.68  dholland 		    (socklen_t)sizeof bindsin) == -1)
    921  1.68  dholland 			yp_log(LOG_WARNING, "nag_servers: sendto: %m");
    922  1.68  dholland 	}
    923  1.68  dholland 
    924  1.68  dholland 	switch (ypbindmode) {
    925  1.68  dholland 	case YPBIND_SETALL:
    926  1.68  dholland 	case YPBIND_SETLOCAL:
    927  1.68  dholland 		if (been_ypset)
    928  1.68  dholland 			return direct_set(buf, outlen, ypdb);
    929  1.68  dholland 		/* FALLTHROUGH */
    930  1.68  dholland 
    931  1.68  dholland 	case YPBIND_BROADCAST:
    932  1.68  dholland 		return broadcast(buf, outlen);
    933  1.68  dholland 
    934  1.68  dholland 	case YPBIND_DIRECT:
    935  1.68  dholland 		return direct(buf, outlen);
    936  1.68  dholland 	}
    937  1.68  dholland 	/*NOTREACHED*/
    938  1.68  dholland 	return -1;
    939  1.68  dholland }
    940  1.68  dholland 
    941  1.69  dholland static int
    942  1.68  dholland ping(struct _dom_binding *ypdb)
    943  1.10   deraadt {
    944  1.68  dholland 	char *dom = ypdb->dom_domain;
    945  1.68  dholland 	struct rpc_msg msg;
    946  1.24  christos 	char buf[BUFSIZE];
    947  1.68  dholland 	enum clnt_stat st;
    948  1.68  dholland 	int outlen;
    949  1.68  dholland 	AUTH *rpcua;
    950  1.68  dholland 	XDR xdr;
    951  1.68  dholland 
    952  1.68  dholland 	(void)memset(&xdr, 0, sizeof xdr);
    953  1.68  dholland 	(void)memset(&msg, 0, sizeof msg);
    954  1.68  dholland 
    955  1.68  dholland 	rpcua = authunix_create_default();
    956  1.68  dholland 	if (rpcua == NULL) {
    957  1.68  dholland 		DPRINTF("cannot get unix auth\n");
    958  1.68  dholland 		return RPC_SYSTEMERROR;
    959  1.68  dholland 	}
    960  1.68  dholland 
    961  1.68  dholland 	msg.rm_direction = CALL;
    962  1.68  dholland 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    963  1.68  dholland 	msg.rm_call.cb_prog = YPPROG;
    964  1.68  dholland 	msg.rm_call.cb_vers = YPVERS;
    965  1.68  dholland 	msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
    966  1.68  dholland 	msg.rm_call.cb_cred = rpcua->ah_cred;
    967  1.68  dholland 	msg.rm_call.cb_verf = rpcua->ah_verf;
    968  1.68  dholland 
    969  1.68  dholland 	msg.rm_xid = ypdb->dom_xid;
    970  1.68  dholland 	xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
    971  1.68  dholland 	if (!xdr_callmsg(&xdr, &msg)) {
    972  1.68  dholland 		st = RPC_CANTENCODEARGS;
    973  1.68  dholland 		AUTH_DESTROY(rpcua);
    974  1.68  dholland 		return st;
    975  1.68  dholland 	}
    976  1.68  dholland 	if (!xdr_ypdomain_wrap_string(&xdr, &dom)) {
    977  1.68  dholland 		st = RPC_CANTENCODEARGS;
    978  1.68  dholland 		AUTH_DESTROY(rpcua);
    979  1.68  dholland 		return st;
    980  1.68  dholland 	}
    981  1.68  dholland 	outlen = (int)xdr_getpos(&xdr);
    982  1.68  dholland 	xdr_destroy(&xdr);
    983  1.68  dholland 	if (outlen < 1) {
    984  1.68  dholland 		st = RPC_CANTENCODEARGS;
    985  1.68  dholland 		AUTH_DESTROY(rpcua);
    986  1.68  dholland 		return st;
    987  1.68  dholland 	}
    988  1.68  dholland 	AUTH_DESTROY(rpcua);
    989  1.68  dholland 
    990  1.68  dholland 	ypdb->dom_alive = 2;
    991  1.68  dholland 	DPRINTF("ping %x\n", ypdb->dom_server_addr.sin_addr.s_addr);
    992  1.68  dholland 
    993  1.68  dholland 	if (sendto(pingsock, buf, outlen, 0,
    994  1.68  dholland 	    (struct sockaddr *)(void *)&ypdb->dom_server_addr,
    995  1.68  dholland 	    (socklen_t)sizeof ypdb->dom_server_addr) == -1)
    996  1.68  dholland 		yp_log(LOG_WARNING, "ping: sendto: %m");
    997  1.68  dholland 	return 0;
    998  1.68  dholland 
    999  1.68  dholland }
   1000  1.68  dholland 
   1001  1.68  dholland /*
   1002  1.68  dholland  * State transition is done like this:
   1003  1.68  dholland  *
   1004  1.68  dholland  * STATE	EVENT		ACTION			NEWSTATE	TIMEOUT
   1005  1.68  dholland  * no binding	timeout		broadcast 		no binding	5 sec
   1006  1.68  dholland  * no binding	answer		--			binding		60 sec
   1007  1.68  dholland  * binding	timeout		ping server		checking	5 sec
   1008  1.68  dholland  * checking	timeout		ping server + broadcast	checking	5 sec
   1009  1.68  dholland  * checking	answer		--			binding		60 sec
   1010  1.68  dholland  */
   1011  1.69  dholland static void
   1012  1.68  dholland checkwork(void)
   1013  1.68  dholland {
   1014  1.68  dholland 	struct _dom_binding *ypdb;
   1015  1.68  dholland 	time_t t;
   1016  1.68  dholland 
   1017  1.68  dholland 	check = 0;
   1018  1.68  dholland 
   1019  1.68  dholland 	(void)time(&t);
   1020  1.68  dholland 	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
   1021  1.68  dholland 		if (ypdb->dom_checktime < t) {
   1022  1.68  dholland 			if (ypdb->dom_alive == 1)
   1023  1.68  dholland 				(void)ping(ypdb);
   1024  1.68  dholland 			else
   1025  1.68  dholland 				(void)nag_servers(ypdb);
   1026  1.68  dholland 			(void)time(&t);
   1027  1.68  dholland 			ypdb->dom_checktime = t + 5;
   1028  1.68  dholland 		}
   1029  1.68  dholland 	}
   1030  1.68  dholland }
   1031  1.68  dholland 
   1032  1.68  dholland ////////////////////////////////////////////////////////////
   1033  1.68  dholland // main
   1034  1.68  dholland 
   1035  1.68  dholland static void
   1036  1.68  dholland usage(void)
   1037  1.68  dholland {
   1038  1.68  dholland 	const char *opt = "";
   1039  1.68  dholland #ifdef DEBUG
   1040  1.68  dholland 	opt = " [-d]";
   1041  1.68  dholland #endif
   1042  1.68  dholland 
   1043  1.68  dholland 	(void)fprintf(stderr,
   1044  1.68  dholland 	    "Usage: %s [-broadcast] [-insecure] [-ypset] [-ypsetme]%s\n",
   1045  1.68  dholland 	    getprogname(), opt);
   1046  1.68  dholland 	exit(1);
   1047  1.68  dholland }
   1048  1.68  dholland 
   1049  1.68  dholland int
   1050  1.68  dholland main(int argc, char *argv[])
   1051  1.68  dholland {
   1052  1.68  dholland 	struct timeval tv;
   1053  1.68  dholland 	fd_set fdsr;
   1054  1.68  dholland 	int width, lockfd;
   1055  1.68  dholland 	int evil = 0, one;
   1056  1.68  dholland 	char pathname[MAXPATHLEN];
   1057  1.68  dholland 	struct stat st;
   1058  1.10   deraadt 
   1059  1.68  dholland 	setprogname(argv[0]);
   1060  1.68  dholland 	(void)yp_get_default_domain(&domainname);
   1061  1.68  dholland 	if (domainname[0] == '\0')
   1062  1.68  dholland 		errx(1, "Domainname not set. Aborting.");
   1063  1.70  dholland 	if (_yp_invalid_domain(domainname))
   1064  1.70  dholland 		errx(1, "Invalid domainname: %s", domainname);
   1065  1.10   deraadt 
   1066  1.10   deraadt 	/*
   1067  1.68  dholland 	 * Per traditional ypbind(8) semantics, if a ypservers
   1068  1.68  dholland 	 * file does not exist, we default to broadcast mode.
   1069  1.68  dholland 	 * If the file does exist, we default to direct mode.
   1070  1.68  dholland 	 * Note that we can still override direct mode by passing
   1071  1.68  dholland 	 * the -broadcast flag.
   1072  1.10   deraadt 	 */
   1073  1.68  dholland 	(void)snprintf(pathname, sizeof(pathname), "%s/%s%s", BINDINGDIR,
   1074  1.68  dholland 	    domainname, YPSERVERSSUFF);
   1075  1.68  dholland 	if (stat(pathname, &st) < 0) {
   1076  1.68  dholland 		DPRINTF("%s does not exist, defaulting to broadcast\n",
   1077  1.68  dholland 			pathname);
   1078  1.68  dholland 		ypbindmode = YPBIND_BROADCAST;
   1079  1.68  dholland 	} else
   1080  1.68  dholland 		ypbindmode = YPBIND_DIRECT;
   1081  1.68  dholland 
   1082  1.68  dholland 	while (--argc) {
   1083  1.68  dholland 		++argv;
   1084  1.68  dholland 		if (!strcmp("-insecure", *argv))
   1085  1.68  dholland 			insecure = 1;
   1086  1.68  dholland 		else if (!strcmp("-ypset", *argv))
   1087  1.68  dholland 			ypbindmode = YPBIND_SETALL;
   1088  1.68  dholland 		else if (!strcmp("-ypsetme", *argv))
   1089  1.68  dholland 			ypbindmode = YPBIND_SETLOCAL;
   1090  1.68  dholland 		else if (!strcmp("-broadcast", *argv))
   1091  1.68  dholland 			ypbindmode = YPBIND_BROADCAST;
   1092  1.68  dholland #ifdef DEBUG
   1093  1.68  dholland 		else if (!strcmp("-d", *argv))
   1094  1.68  dholland 			debug++;
   1095  1.68  dholland #endif
   1096  1.68  dholland 		else
   1097  1.68  dholland 			usage();
   1098  1.10   deraadt 	}
   1099  1.10   deraadt 
   1100  1.68  dholland 	/* initialise syslog */
   1101  1.68  dholland 	openlog("ypbind", LOG_PERROR | LOG_PID, LOG_DAEMON);
   1102  1.10   deraadt 
   1103  1.68  dholland 	lockfd = open(_PATH_YPBIND_LOCK, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644);
   1104  1.68  dholland 	if (lockfd == -1)
   1105  1.68  dholland 		err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
   1106  1.14       cgd 
   1107  1.68  dholland #if O_SHLOCK == 0
   1108  1.68  dholland 	(void)flock(lockfd, LOCK_SH);
   1109  1.68  dholland #endif
   1110  1.19       cgd 
   1111  1.68  dholland 	(void)pmap_unset(YPBINDPROG, YPBINDVERS);
   1112   1.1   deraadt 
   1113  1.68  dholland 	udptransp = svcudp_create(RPC_ANYSOCK);
   1114  1.68  dholland 	if (udptransp == NULL)
   1115  1.68  dholland 		errx(1, "Cannot create udp service.");
   1116  1.30     lukem 
   1117  1.68  dholland 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
   1118  1.68  dholland 	    IPPROTO_UDP))
   1119  1.68  dholland 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, udp).");
   1120  1.30     lukem 
   1121  1.68  dholland 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
   1122  1.68  dholland 	if (tcptransp == NULL)
   1123  1.68  dholland 		errx(1, "Cannot create tcp service.");
   1124   1.1   deraadt 
   1125  1.68  dholland 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
   1126  1.68  dholland 	    IPPROTO_TCP))
   1127  1.68  dholland 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, tcp).");
   1128   1.1   deraadt 
   1129  1.68  dholland 	/* XXX use SOCK_STREAM for direct queries? */
   1130  1.68  dholland 	if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
   1131  1.68  dholland 		err(1, "rpc socket");
   1132  1.68  dholland 	if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
   1133  1.68  dholland 		err(1, "ping socket");
   1134   1.8   deraadt 
   1135  1.68  dholland 	(void)fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
   1136  1.68  dholland 	(void)fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY);
   1137   1.1   deraadt 
   1138  1.68  dholland 	one = 1;
   1139  1.68  dholland 	(void)setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one,
   1140  1.68  dholland 	    (socklen_t)sizeof(one));
   1141  1.68  dholland 	rmtca.prog = YPPROG;
   1142  1.68  dholland 	rmtca.vers = YPVERS;
   1143  1.68  dholland 	rmtca.proc = YPPROC_DOMAIN_NONACK;
   1144  1.68  dholland 	rmtca.xdr_args = NULL;		/* set at call time */
   1145  1.68  dholland 	rmtca.args_ptr = NULL;		/* set at call time */
   1146  1.68  dholland 	rmtcr.port_ptr = &rmtcr_port;
   1147  1.68  dholland 	rmtcr.xdr_results = xdr_bool;
   1148  1.68  dholland 	rmtcr.results_ptr = (caddr_t)(void *)&rmtcr_outval;
   1149   1.1   deraadt 
   1150  1.68  dholland 	/* blow away old bindings in BINDINGDIR */
   1151  1.68  dholland 	if (purge_bindingdir(BINDINGDIR) < 0)
   1152  1.68  dholland 		errx(1, "unable to purge old bindings from %s", BINDINGDIR);
   1153   1.6   deraadt 
   1154  1.68  dholland 	/* build initial domain binding, make it "unsuccessful" */
   1155  1.68  dholland 	ypbindlist = makebinding(domainname);
   1156  1.68  dholland 	ypbindlist->dom_vers = YPVERS;
   1157  1.68  dholland 	ypbindlist->dom_alive = 0;
   1158  1.68  dholland 	ypbindlist->dom_lockfd = -1;
   1159  1.68  dholland 	removelock(ypbindlist);
   1160   1.6   deraadt 
   1161  1.68  dholland 	checkwork();
   1162  1.24  christos 
   1163  1.68  dholland 	for (;;) {
   1164  1.68  dholland 		width = svc_maxfd;
   1165  1.68  dholland 		if (rpcsock > width)
   1166  1.68  dholland 			width = rpcsock;
   1167  1.68  dholland 		if (pingsock > width)
   1168  1.68  dholland 			width = pingsock;
   1169  1.68  dholland 		width++;
   1170  1.68  dholland 		fdsr = svc_fdset;
   1171  1.68  dholland 		FD_SET(rpcsock, &fdsr);
   1172  1.68  dholland 		FD_SET(pingsock, &fdsr);
   1173  1.68  dholland 		tv.tv_sec = 1;
   1174  1.68  dholland 		tv.tv_usec = 0;
   1175  1.20       cgd 
   1176  1.68  dholland 		switch (select(width, &fdsr, NULL, NULL, &tv)) {
   1177  1.68  dholland 		case 0:
   1178  1.68  dholland 			checkwork();
   1179  1.68  dholland 			break;
   1180  1.68  dholland 		case -1:
   1181  1.68  dholland 			yp_log(LOG_WARNING, "select: %m");
   1182  1.68  dholland 			break;
   1183  1.68  dholland 		default:
   1184  1.68  dholland 			if (FD_ISSET(rpcsock, &fdsr))
   1185  1.68  dholland 				(void)handle_replies();
   1186  1.68  dholland 			if (FD_ISSET(pingsock, &fdsr))
   1187  1.68  dholland 				(void)handle_ping();
   1188  1.68  dholland 			svc_getreqset(&fdsr);
   1189  1.68  dholland 			if (check)
   1190  1.68  dholland 				checkwork();
   1191  1.20       cgd 			break;
   1192  1.68  dholland 		}
   1193  1.20       cgd 
   1194  1.68  dholland 		if (!evil && ypbindlist->dom_alive) {
   1195  1.68  dholland 			evil = 1;
   1196  1.68  dholland #ifdef DEBUG
   1197  1.68  dholland 			if (!debug)
   1198  1.68  dholland #endif
   1199  1.68  dholland 				(void)daemon(0, 0);
   1200  1.68  dholland 			(void)pidfile(NULL);
   1201  1.68  dholland 		}
   1202  1.68  dholland 	}
   1203   1.1   deraadt }
   1204