Home | History | Annotate | Line # | Download | only in ypbind
ypbind.c revision 1.95
      1  1.95  dholland /*	$NetBSD: ypbind.c,v 1.95 2014/06/10 17:19:12 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.95  dholland __RCSID("$NetBSD: ypbind.c,v 1.95 2014/06/10 17:19:12 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.81  dholland 	YPBIND_DIRECT, YPBIND_BROADCAST,
     85  1.68  dholland } ypbind_mode_t;
     86   1.1   deraadt 
     87  1.75  dholland struct domain {
     88  1.78  dholland 	struct domain *dom_next;
     89  1.75  dholland 
     90  1.79  dholland 	char dom_name[YPMAXDOMAIN + 1];
     91   1.1   deraadt 	struct sockaddr_in dom_server_addr;
     92  1.35     lukem 	long dom_vers;
     93  1.66  dholland 	time_t dom_checktime;
     94  1.66  dholland 	time_t dom_asktime;
     95   1.1   deraadt 	int dom_lockfd;
     96   1.1   deraadt 	int dom_alive;
     97  1.64  dholland 	uint32_t dom_xid;
     98  1.84  dholland 	FILE *dom_serversfile;		/* /var/yp/binding/foo.ypservers */
     99  1.84  dholland 	int dom_been_ypset;		/* ypset been done on this domain? */
    100  1.83  dholland 	ypbind_mode_t dom_ypbindmode;	/* broadcast or direct */
    101   1.1   deraadt };
    102   1.1   deraadt 
    103  1.68  dholland #define BUFSIZE		1400
    104  1.68  dholland 
    105  1.95  dholland /* the list of all domains */
    106  1.77  dholland static struct domain *domains;
    107  1.24  christos static int check;
    108   1.1   deraadt 
    109  1.95  dholland /* option settings */
    110  1.82  dholland static ypbind_mode_t default_ypbindmode;
    111  1.81  dholland static int allow_local_ypset = 0, allow_any_ypset = 0;
    112  1.30     lukem static int insecure;
    113  1.86  dholland 
    114  1.95  dholland /* the sockets we use to interact with servers */
    115  1.24  christos static int rpcsock, pingsock;
    116  1.95  dholland 
    117  1.95  dholland /* stuff used for manually interacting with servers */
    118  1.24  christos static struct rmtcallargs rmtca;
    119  1.24  christos static struct rmtcallres rmtcr;
    120  1.26        ws static bool_t rmtcr_outval;
    121  1.64  dholland static unsigned long rmtcr_port;
    122  1.95  dholland 
    123  1.95  dholland /* The ypbind service transports */
    124  1.24  christos static SVCXPRT *udptransp, *tcptransp;
    125  1.24  christos 
    126  1.68  dholland ////////////////////////////////////////////////////////////
    127  1.73  dholland // utilities
    128  1.73  dholland 
    129  1.95  dholland /*
    130  1.95  dholland  * Combo of open() and flock().
    131  1.95  dholland  */
    132  1.73  dholland static int
    133  1.73  dholland open_locked(const char *path, int flags, mode_t mode)
    134  1.73  dholland {
    135  1.73  dholland 	int fd;
    136  1.73  dholland 
    137  1.73  dholland 	fd = open(path, flags|O_SHLOCK, mode);
    138  1.73  dholland 	if (fd < 0) {
    139  1.73  dholland 		return -1;
    140  1.73  dholland 	}
    141  1.73  dholland #if O_SHLOCK == 0
    142  1.73  dholland 	/* dholland 20110522 wouldn't it be better to check this for error? */
    143  1.73  dholland 	(void)flock(fd, LOCK_SH);
    144  1.73  dholland #endif
    145  1.73  dholland 	return fd;
    146  1.73  dholland }
    147  1.73  dholland 
    148  1.73  dholland ////////////////////////////////////////////////////////////
    149  1.68  dholland // logging
    150  1.68  dholland 
    151  1.24  christos #ifdef DEBUG
    152  1.68  dholland #define DPRINTF(...) (debug ? (void)printf(__VA_ARGS__) : (void)0)
    153  1.68  dholland static int debug;
    154  1.68  dholland #else
    155  1.68  dholland #define DPRINTF(...)
    156  1.24  christos #endif
    157  1.36       mrg 
    158  1.69  dholland static void yp_log(int, const char *, ...) __printflike(2, 3);
    159  1.69  dholland 
    160  1.95  dholland /*
    161  1.95  dholland  * Log some stuff, to syslog or stderr depending on the debug setting.
    162  1.95  dholland  */
    163  1.37    bouyer static void
    164  1.37    bouyer yp_log(int pri, const char *fmt, ...)
    165  1.37    bouyer {
    166  1.37    bouyer 	va_list ap;
    167  1.37    bouyer 
    168  1.37    bouyer 	va_start(ap, fmt);
    169  1.37    bouyer 
    170  1.37    bouyer #if defined(DEBUG)
    171  1.72  dholland 	if (debug) {
    172  1.57  christos 		(void)vprintf(fmt, ap);
    173  1.72  dholland 		(void)printf("\n");
    174  1.72  dholland 	} else
    175  1.37    bouyer #endif
    176  1.37    bouyer 		vsyslog(pri, fmt, ap);
    177  1.37    bouyer 	va_end(ap);
    178  1.37    bouyer }
    179  1.37    bouyer 
    180  1.68  dholland ////////////////////////////////////////////////////////////
    181  1.71  dholland // ypservers file
    182  1.71  dholland 
    183  1.71  dholland /*
    184  1.71  dholland  * Get pathname for the ypservers file for a given domain
    185  1.71  dholland  * (/var/yp/binding/DOMAIN.ypservers)
    186  1.71  dholland  */
    187  1.71  dholland static const char *
    188  1.71  dholland ypservers_filename(const char *domain)
    189  1.71  dholland {
    190  1.71  dholland 	static char ret[PATH_MAX];
    191  1.71  dholland 
    192  1.71  dholland 	(void)snprintf(ret, sizeof(ret), "%s/%s%s",
    193  1.71  dholland 			BINDINGDIR, domain, YPSERVERSSUFF);
    194  1.71  dholland 	return ret;
    195  1.71  dholland }
    196  1.71  dholland 
    197  1.71  dholland ////////////////////////////////////////////////////////////
    198  1.75  dholland // struct domain
    199  1.68  dholland 
    200  1.95  dholland /*
    201  1.95  dholland  * State transition is done like this:
    202  1.95  dholland  *
    203  1.95  dholland  * STATE	EVENT		ACTION			NEWSTATE	TIMEOUT
    204  1.95  dholland  * no binding	timeout		broadcast 		no binding	5 sec
    205  1.95  dholland  * no binding	answer		--			binding		60 sec
    206  1.95  dholland  * binding	timeout		ping server		checking	5 sec
    207  1.95  dholland  * checking	timeout		ping server + broadcast	checking	5 sec
    208  1.95  dholland  * checking	answer		--			binding		60 sec
    209  1.95  dholland  */
    210  1.95  dholland 
    211  1.95  dholland /*
    212  1.95  dholland  * Look up a domain by the XID we assigned it.
    213  1.95  dholland  */
    214  1.75  dholland static struct domain *
    215  1.76  dholland domain_find(uint32_t xid)
    216  1.68  dholland {
    217  1.78  dholland 	struct domain *dom;
    218  1.68  dholland 
    219  1.78  dholland 	for (dom = domains; dom != NULL; dom = dom->dom_next)
    220  1.78  dholland 		if (dom->dom_xid == xid)
    221  1.68  dholland 			break;
    222  1.78  dholland 	return dom;
    223  1.68  dholland }
    224  1.68  dholland 
    225  1.95  dholland /*
    226  1.95  dholland  * Pick an XID for a domain.
    227  1.95  dholland  *
    228  1.95  dholland  * XXX: this should just generate a random number.
    229  1.95  dholland  */
    230  1.68  dholland static uint32_t
    231  1.78  dholland unique_xid(struct domain *dom)
    232  1.68  dholland {
    233  1.68  dholland 	uint32_t tmp_xid;
    234  1.68  dholland 
    235  1.78  dholland 	tmp_xid = ((uint32_t)(unsigned long)dom) & 0xffffffff;
    236  1.76  dholland 	while (domain_find(tmp_xid) != NULL)
    237  1.68  dholland 		tmp_xid++;
    238  1.68  dholland 
    239  1.68  dholland 	return tmp_xid;
    240  1.68  dholland }
    241  1.68  dholland 
    242  1.95  dholland /*
    243  1.95  dholland  * Construct a new domain. Adds it to the global linked list of all
    244  1.95  dholland  * domains.
    245  1.95  dholland  */
    246  1.75  dholland static struct domain *
    247  1.78  dholland domain_create(const char *name)
    248  1.24  christos {
    249  1.78  dholland 	struct domain *dom;
    250  1.82  dholland 	const char *pathname;
    251  1.82  dholland 	struct stat st;
    252   1.1   deraadt 
    253  1.78  dholland 	dom = malloc(sizeof *dom);
    254  1.78  dholland 	if (dom == NULL) {
    255  1.75  dholland 		yp_log(LOG_ERR, "domain_create: Out of memory");
    256  1.37    bouyer 		exit(1);
    257  1.37    bouyer 	}
    258   1.1   deraadt 
    259  1.80  dholland 	dom->dom_next = NULL;
    260  1.80  dholland 
    261  1.79  dholland 	(void)strlcpy(dom->dom_name, name, sizeof(dom->dom_name));
    262  1.80  dholland 	(void)memset(&dom->dom_server_addr, 0, sizeof(dom->dom_server_addr));
    263  1.80  dholland 	dom->dom_vers = YPVERS;
    264  1.80  dholland 	dom->dom_checktime = 0;
    265  1.80  dholland 	dom->dom_asktime = 0;
    266  1.80  dholland 	dom->dom_lockfd = -1;
    267  1.80  dholland 	dom->dom_alive = 0;
    268  1.80  dholland 	dom->dom_xid = unique_xid(dom);
    269  1.84  dholland 	dom->dom_been_ypset = 0;
    270  1.84  dholland 	dom->dom_serversfile = NULL;
    271  1.80  dholland 
    272  1.82  dholland 	/*
    273  1.82  dholland 	 * Per traditional ypbind(8) semantics, if a ypservers
    274  1.82  dholland 	 * file does not exist, we revert to broadcast mode.
    275  1.82  dholland 	 *
    276  1.82  dholland 	 * The sysadmin can force broadcast mode by passing the
    277  1.82  dholland 	 * -broadcast flag. There is currently no way to fail and
    278  1.82  dholland 	 * reject domains for which there is no ypservers file.
    279  1.82  dholland 	 */
    280  1.82  dholland 	dom->dom_ypbindmode = default_ypbindmode;
    281  1.82  dholland 	if (dom->dom_ypbindmode == YPBIND_DIRECT) {
    282  1.87  dholland 		pathname = ypservers_filename(dom->dom_name);
    283  1.82  dholland 		if (stat(pathname, &st) < 0) {
    284  1.82  dholland 			/* XXX syslog a warning here? */
    285  1.82  dholland 			DPRINTF("%s does not exist, defaulting to broadcast\n",
    286  1.82  dholland 				pathname);
    287  1.82  dholland 			dom->dom_ypbindmode = YPBIND_BROADCAST;
    288  1.82  dholland 		}
    289  1.82  dholland 	}
    290  1.82  dholland 
    291  1.80  dholland 	/* add to global list */
    292  1.80  dholland 	dom->dom_next = domains;
    293  1.80  dholland 	domains = dom;
    294  1.80  dholland 
    295  1.78  dholland 	return dom;
    296  1.24  christos }
    297  1.24  christos 
    298  1.68  dholland ////////////////////////////////////////////////////////////
    299  1.68  dholland // locks
    300  1.68  dholland 
    301  1.95  dholland /*
    302  1.95  dholland  * Open a new binding file. Does not write the contents out; the
    303  1.95  dholland  * caller (there's only one) does that.
    304  1.95  dholland  */
    305  1.24  christos static int
    306  1.78  dholland makelock(struct domain *dom)
    307  1.24  christos {
    308  1.24  christos 	int fd;
    309  1.24  christos 	char path[MAXPATHLEN];
    310  1.20       cgd 
    311  1.36       mrg 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
    312  1.79  dholland 	    dom->dom_name, dom->dom_vers);
    313  1.24  christos 
    314  1.73  dholland 	fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
    315  1.73  dholland 	if (fd == -1) {
    316  1.36       mrg 		(void)mkdir(BINDINGDIR, 0755);
    317  1.73  dholland 		fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
    318  1.73  dholland 		if (fd == -1) {
    319  1.24  christos 			return -1;
    320  1.73  dholland 		}
    321  1.24  christos 	}
    322  1.24  christos 
    323  1.24  christos 	return fd;
    324  1.24  christos }
    325  1.24  christos 
    326  1.95  dholland /*
    327  1.95  dholland  * Remove a binding file.
    328  1.95  dholland  */
    329  1.24  christos static void
    330  1.78  dholland removelock(struct domain *dom)
    331  1.24  christos {
    332  1.24  christos 	char path[MAXPATHLEN];
    333  1.24  christos 
    334  1.36       mrg 	(void)snprintf(path, sizeof(path), "%s/%s.%ld",
    335  1.79  dholland 	    BINDINGDIR, dom->dom_name, dom->dom_vers);
    336  1.36       mrg 	(void)unlink(path);
    337  1.24  christos }
    338  1.24  christos 
    339  1.59     chuck /*
    340  1.95  dholland  * purge_bindingdir: remove old binding files (i.e. "rm *.[0-9]" in BINDINGDIR)
    341  1.95  dholland  *
    342  1.95  dholland  * The local YP functions [e.g. yp_master()] will fail without even
    343  1.95  dholland  * talking to ypbind if there is a stale (non-flock'd) binding file
    344  1.95  dholland  * present.
    345  1.59     chuck  *
    346  1.95  dholland  * We have to remove all binding files in BINDINGDIR, not just the one
    347  1.95  dholland  * for the default domain.
    348  1.59     chuck  */
    349  1.59     chuck static int
    350  1.60  christos purge_bindingdir(const char *dirpath)
    351  1.61     skrll {
    352  1.59     chuck 	DIR *dirp;
    353  1.59     chuck 	int unlinkedfiles, l;
    354  1.59     chuck 	struct dirent *dp;
    355  1.59     chuck 	char pathname[MAXPATHLEN];
    356  1.59     chuck 
    357  1.59     chuck 	if ((dirp = opendir(dirpath)) == NULL)
    358  1.59     chuck 		return(-1);   /* at this point, shouldn't ever happen */
    359  1.59     chuck 
    360  1.59     chuck 	do {
    361  1.59     chuck 		unlinkedfiles = 0;
    362  1.59     chuck 		while ((dp = readdir(dirp)) != NULL) {
    363  1.59     chuck 			l = dp->d_namlen;
    364  1.59     chuck 			/* 'rm *.[0-9]' */
    365  1.59     chuck 			if (l > 2 && dp->d_name[l-2] == '.' &&
    366  1.59     chuck 			    dp->d_name[l-1] >= '0' && dp->d_name[l-1] <= '9') {
    367  1.59     chuck 				(void)snprintf(pathname, sizeof(pathname),
    368  1.59     chuck 					"%s/%s", dirpath, dp->d_name);
    369  1.59     chuck 				if (unlink(pathname) < 0 && errno != ENOENT)
    370  1.59     chuck 					return(-1);
    371  1.59     chuck 				unlinkedfiles++;
    372  1.59     chuck 			}
    373  1.59     chuck 		}
    374  1.59     chuck 
    375  1.59     chuck 		/* rescan dir if we removed it */
    376  1.59     chuck 		if (unlinkedfiles)
    377  1.59     chuck 			rewinddir(dirp);
    378  1.59     chuck 
    379  1.59     chuck 	} while (unlinkedfiles);
    380  1.59     chuck 
    381  1.59     chuck 	closedir(dirp);
    382  1.59     chuck 	return(0);
    383  1.59     chuck }
    384  1.59     chuck 
    385  1.68  dholland ////////////////////////////////////////////////////////////
    386  1.68  dholland // sunrpc twaddle
    387  1.68  dholland 
    388  1.68  dholland /*
    389  1.92  dholland  * Check if the info coming in is (at least somewhat) valid.
    390  1.92  dholland  */
    391  1.92  dholland static int
    392  1.92  dholland rpc_is_valid_response(char *name, struct sockaddr_in *addr)
    393  1.92  dholland {
    394  1.92  dholland 	if (name == NULL) {
    395  1.92  dholland 		return 0;
    396  1.92  dholland 	}
    397  1.92  dholland 
    398  1.92  dholland 	if (_yp_invalid_domain(name)) {
    399  1.92  dholland 		return 0;
    400  1.92  dholland 	}
    401  1.92  dholland 
    402  1.92  dholland 	/* don't support insecure servers by default */
    403  1.92  dholland 	if (!insecure && ntohs(addr->sin_port) >= IPPORT_RESERVED) {
    404  1.92  dholland 		return 0;
    405  1.92  dholland 	}
    406  1.92  dholland 
    407  1.92  dholland 	return 1;
    408  1.92  dholland }
    409  1.92  dholland 
    410  1.92  dholland /*
    411  1.95  dholland  * Take note of the fact that we've received a reply from a ypserver.
    412  1.95  dholland  * Or, in the case of being ypset, that we've been ypset, which
    413  1.95  dholland  * functions much the same.
    414  1.95  dholland  *
    415  1.95  dholland  * Note that FORCE is set if and only if IS_YPSET is set.
    416  1.95  dholland  *
    417  1.95  dholland  * This function has also for the past 20+ years carried the annotation
    418  1.95  dholland  *
    419  1.95  dholland  *      LOOPBACK IS MORE IMPORTANT: PUT IN HACK
    420  1.95  dholland  *
    421  1.95  dholland  * whose meaning isn't entirely clear.
    422  1.68  dholland  */
    423  1.69  dholland static void
    424  1.83  dholland rpc_received(char *dom_name, struct sockaddr_in *raddrp, int force,
    425  1.83  dholland 	     int is_ypset)
    426  1.68  dholland {
    427  1.78  dholland 	struct domain *dom;
    428  1.68  dholland 	struct iovec iov[2];
    429  1.68  dholland 	struct ypbind_resp ybr;
    430  1.72  dholland 	ssize_t result;
    431  1.68  dholland 	int fd;
    432  1.68  dholland 
    433  1.68  dholland 	DPRINTF("returned from %s about %s\n",
    434  1.78  dholland 		inet_ntoa(raddrp->sin_addr), dom_name);
    435  1.68  dholland 
    436  1.95  dholland 	/* validate some stuff */
    437  1.92  dholland 	if (!rpc_is_valid_response(dom_name, raddrp)) {
    438  1.68  dholland 		return;
    439  1.92  dholland 	}
    440  1.68  dholland 
    441  1.95  dholland 	/* look for the domain */
    442  1.78  dholland 	for (dom = domains; dom != NULL; dom = dom->dom_next)
    443  1.79  dholland 		if (!strcmp(dom->dom_name, dom_name))
    444  1.68  dholland 			break;
    445  1.68  dholland 
    446  1.95  dholland 	/* if not found, create it, but only if FORCE; otherwise ignore */
    447  1.78  dholland 	if (dom == NULL) {
    448  1.68  dholland 		if (force == 0)
    449  1.68  dholland 			return;
    450  1.78  dholland 		dom = domain_create(dom_name);
    451  1.68  dholland 	}
    452  1.68  dholland 
    453  1.95  dholland 	/* the domain needs to know if it's been explicitly ypset */
    454  1.83  dholland 	if (is_ypset) {
    455  1.83  dholland 		dom->dom_been_ypset = 1;
    456  1.83  dholland 	}
    457  1.83  dholland 
    458  1.95  dholland 	/*
    459  1.95  dholland 	 * If the domain is alive and we aren't ypset, we don't need
    460  1.95  dholland 	 * to do anything.
    461  1.95  dholland 	 *
    462  1.95  dholland 	 * This code is unreachable (AFAIK) unless we receive an
    463  1.95  dholland 	 * unsolicited ping reply from the ypserver: because dom_alive
    464  1.95  dholland 	 * is 0 until we have a binding, but set from 1 to 2 when we
    465  1.95  dholland 	 * ping, it will never normally be 1 when a reply comes in,
    466  1.95  dholland 	 * even a reply to a ping. In the case where we lost the
    467  1.95  dholland 	 * binding and are getting a reply arising from nag_servers,
    468  1.95  dholland 	 * we lost the binding because we never got a ping response so
    469  1.95  dholland 	 * in that case dom_alive will also be 2.
    470  1.95  dholland 	 *
    471  1.95  dholland 	 * This logic is clearly wrong. XXX.
    472  1.95  dholland 	 */
    473  1.78  dholland 	if (dom->dom_alive == 1 && force == 0) {
    474  1.95  dholland 		/*
    475  1.95  dholland 		 * If the reply came from the server we expect, set
    476  1.95  dholland 		 * dom_alive back to 1 and ping again in 60 seconds.
    477  1.95  dholland 		 *
    478  1.95  dholland 		 * If it came from somewhere else, ignore it.
    479  1.95  dholland 		 */
    480  1.78  dholland 		if (!memcmp(&dom->dom_server_addr, raddrp,
    481  1.78  dholland 			    sizeof(dom->dom_server_addr))) {
    482  1.78  dholland 			dom->dom_alive = 1;
    483  1.68  dholland 			/* recheck binding in 60 sec */
    484  1.78  dholland 			dom->dom_checktime = time(NULL) + 60;
    485  1.68  dholland 		}
    486  1.68  dholland 		return;
    487  1.68  dholland 	}
    488  1.95  dholland 
    489  1.95  dholland 	/*
    490  1.95  dholland 	 * Take the address we got the message from (or in the case of
    491  1.95  dholland 	 * ypset, the explicit address we were given) as the server
    492  1.95  dholland 	 * address for this domain, mark the domain alive, and we'll
    493  1.95  dholland 	 * check it again in 60 seconds.
    494  1.95  dholland 	 *
    495  1.95  dholland 	 * XXX: it looks like if we get a random unsolicited reply
    496  1.95  dholland 	 * from somewhere, we'll silently switch to that server
    497  1.95  dholland 	 * address, regardless of merit.
    498  1.95  dholland 	 *
    499  1.95  dholland 	 * 1. If we have a foo.ypservers file the address should be
    500  1.95  dholland 	 * checked against it and rejected if it's not one of the
    501  1.95  dholland 	 * addresses of one of the listed hostnames. Note that it
    502  1.95  dholland 	 * might not be the same address we sent to; even fairly smart
    503  1.95  dholland 	 * UDP daemons don't always handle multihomed hosts correctly
    504  1.95  dholland 	 * and we can't expect sunrpc code to do anything intelligent
    505  1.95  dholland 	 * at all.
    506  1.95  dholland 	 *
    507  1.95  dholland 	 * 2. If we're in broadcast mode the address should be
    508  1.95  dholland 	 * checked against the local addresses and netmasks so we
    509  1.95  dholland 	 * don't accept responses from Mars.
    510  1.95  dholland 	 *
    511  1.95  dholland 	 * 2a. If we're in broadcast mode and we've been ypset, we
    512  1.95  dholland 	 * should not accept anything else until we drop the ypset
    513  1.95  dholland 	 * state for not responding.
    514  1.95  dholland 	 *
    515  1.95  dholland 	 * 3. Either way we should not accept a response from an
    516  1.95  dholland 	 * arbitrary host unless we don't currently have a binding.
    517  1.95  dholland 	 * (The logic in the previous if statement is probably
    518  1.95  dholland 	 * supposed to handle this, but it doesn't currently work.)
    519  1.95  dholland 	 *
    520  1.95  dholland 	 * Note that for a random unsolicited reply to work it has to
    521  1.95  dholland 	 * carry the XID of one of the domains we know about; but
    522  1.95  dholland 	 * those values are predictable.
    523  1.95  dholland 	 */
    524  1.78  dholland 	(void)memcpy(&dom->dom_server_addr, raddrp,
    525  1.78  dholland 	    sizeof(dom->dom_server_addr));
    526  1.68  dholland 	/* recheck binding in 60 seconds */
    527  1.78  dholland 	dom->dom_checktime = time(NULL) + 60;
    528  1.78  dholland 	dom->dom_alive = 1;
    529  1.68  dholland 
    530  1.95  dholland 	/*
    531  1.95  dholland 	 * Generate a new binding file. If this fails, forget about it.
    532  1.95  dholland 	 * (But we keep the binding and we'll report it to anyone who
    533  1.95  dholland 	 * asks via the ypbind service.) XXX: this will interact badly,
    534  1.95  dholland 	 * maybe very badly, with the code in HEURISTIC.
    535  1.95  dholland 	 *
    536  1.95  dholland 	 * Note that makelock() doesn't log on failure.
    537  1.95  dholland 	 */
    538  1.95  dholland 
    539  1.78  dholland 	if (dom->dom_lockfd != -1)
    540  1.78  dholland 		(void)close(dom->dom_lockfd);
    541  1.68  dholland 
    542  1.78  dholland 	if ((fd = makelock(dom)) == -1)
    543  1.68  dholland 		return;
    544  1.68  dholland 
    545  1.78  dholland 	dom->dom_lockfd = fd;
    546  1.68  dholland 
    547  1.68  dholland 	iov[0].iov_base = &(udptransp->xp_port);
    548  1.68  dholland 	iov[0].iov_len = sizeof udptransp->xp_port;
    549  1.68  dholland 	iov[1].iov_base = &ybr;
    550  1.68  dholland 	iov[1].iov_len = sizeof ybr;
    551  1.68  dholland 
    552  1.68  dholland 	(void)memset(&ybr, 0, sizeof ybr);
    553  1.68  dholland 	ybr.ypbind_status = YPBIND_SUCC_VAL;
    554  1.68  dholland 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
    555  1.68  dholland 	    raddrp->sin_addr;
    556  1.68  dholland 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    557  1.68  dholland 	    raddrp->sin_port;
    558  1.68  dholland 
    559  1.78  dholland 	result = writev(dom->dom_lockfd, iov, 2);
    560  1.72  dholland 	if (result < 0 || (size_t)result != iov[0].iov_len + iov[1].iov_len) {
    561  1.72  dholland 		if (result < 0)
    562  1.72  dholland 			yp_log(LOG_WARNING, "writev: %s", strerror(errno));
    563  1.72  dholland 		else
    564  1.72  dholland 			yp_log(LOG_WARNING, "writev: short count");
    565  1.78  dholland 		(void)close(dom->dom_lockfd);
    566  1.78  dholland 		removelock(dom);
    567  1.78  dholland 		dom->dom_lockfd = -1;
    568  1.68  dholland 	}
    569  1.68  dholland }
    570  1.68  dholland 
    571  1.95  dholland /*
    572  1.95  dholland  * The NULL call: do nothing. This is obliged to exist because of
    573  1.95  dholland  * sunrpc silliness.
    574  1.95  dholland  */
    575  1.24  christos static void *
    576  1.57  christos /*ARGSUSED*/
    577  1.47       wiz ypbindproc_null_2(SVCXPRT *transp, void *argp)
    578   1.1   deraadt {
    579   1.1   deraadt 	static char res;
    580   1.1   deraadt 
    581  1.65  dholland 	DPRINTF("ypbindproc_null_2\n");
    582  1.36       mrg 	(void)memset(&res, 0, sizeof(res));
    583   1.1   deraadt 	return (void *)&res;
    584   1.1   deraadt }
    585   1.1   deraadt 
    586  1.95  dholland /*
    587  1.95  dholland  * The DOMAIN call: look up the ypserver for a specified domain.
    588  1.95  dholland  */
    589  1.24  christos static void *
    590  1.57  christos /*ARGSUSED*/
    591  1.47       wiz ypbindproc_domain_2(SVCXPRT *transp, void *argp)
    592   1.1   deraadt {
    593   1.1   deraadt 	static struct ypbind_resp res;
    594  1.78  dholland 	struct domain *dom;
    595  1.24  christos 	char *arg = *(char **) argp;
    596   1.9   deraadt 	time_t now;
    597  1.30     lukem 	int count;
    598   1.1   deraadt 
    599  1.65  dholland 	DPRINTF("ypbindproc_domain_2 %s\n", arg);
    600  1.95  dholland 
    601  1.95  dholland 	/* Reject invalid domains. */
    602  1.30     lukem 	if (_yp_invalid_domain(arg))
    603  1.30     lukem 		return NULL;
    604  1.30     lukem 
    605  1.36       mrg 	(void)memset(&res, 0, sizeof res);
    606   1.1   deraadt 	res.ypbind_status = YPBIND_FAIL_VAL;
    607   1.1   deraadt 
    608  1.95  dholland 	/*
    609  1.95  dholland 	 * Look for the domain. XXX: Behave erratically if we have
    610  1.95  dholland 	 * more than 100 domains. The intent here is to avoid allowing
    611  1.95  dholland 	 * arbitrary incoming requests to create more than 100
    612  1.95  dholland 	 * domains; but this logic means that if we legitimately have
    613  1.95  dholland 	 * more than 100 (e.g. via ypset) we'll only actually bind the
    614  1.95  dholland 	 * first 100 and the rest will fail. The test on 'count' should
    615  1.95  dholland 	 * be moved further down.
    616  1.95  dholland 	 */
    617  1.78  dholland 	for (count = 0, dom = domains;
    618  1.78  dholland 	    dom != NULL;
    619  1.78  dholland 	    dom = dom->dom_next, count++) {
    620  1.30     lukem 		if (count > 100)
    621  1.30     lukem 			return NULL;		/* prevent denial of service */
    622  1.79  dholland 		if (!strcmp(dom->dom_name, arg))
    623   1.1   deraadt 			break;
    624  1.30     lukem 	}
    625   1.1   deraadt 
    626  1.95  dholland 	/*
    627  1.95  dholland 	 * If the domain doesn't exist, create it, then fail the call
    628  1.95  dholland 	 * because we have no information yet.
    629  1.95  dholland 	 *
    630  1.95  dholland 	 * Set "check" so that checkwork() will run and look for a
    631  1.95  dholland 	 * server.
    632  1.95  dholland 	 *
    633  1.95  dholland 	 * XXX: like during startup there's a spurious call to
    634  1.95  dholland 	 * removelock() after domain_create().
    635  1.95  dholland 	 */
    636  1.78  dholland 	if (dom == NULL) {
    637  1.78  dholland 		dom = domain_create(arg);
    638  1.78  dholland 		removelock(dom);
    639   1.1   deraadt 		check++;
    640  1.65  dholland 		DPRINTF("unknown domain %s\n", arg);
    641   1.1   deraadt 		return NULL;
    642   1.1   deraadt 	}
    643   1.1   deraadt 
    644  1.78  dholland 	if (dom->dom_alive == 0) {
    645  1.65  dholland 		DPRINTF("dead domain %s\n", arg);
    646   1.1   deraadt 		return NULL;
    647  1.24  christos 	}
    648   1.1   deraadt 
    649   1.9   deraadt #ifdef HEURISTIC
    650  1.95  dholland 	/*
    651  1.95  dholland 	 * Keep track of the last time we were explicitly asked about
    652  1.95  dholland 	 * this domain. If it happens a lot, force a ping. This works
    653  1.95  dholland 	 * (or "works") because we only get asked specifically when
    654  1.95  dholland 	 * things aren't going; otherwise the client code in libc and
    655  1.95  dholland 	 * elsewhere uses the binding file.
    656  1.95  dholland 	 *
    657  1.95  dholland 	 * Note: HEURISTIC is enabled by default.
    658  1.95  dholland 	 */
    659  1.57  christos 	(void)time(&now);
    660  1.78  dholland 	if (now < dom->dom_asktime + 5) {
    661   1.1   deraadt 		/*
    662   1.9   deraadt 		 * Hmm. More than 2 requests in 5 seconds have indicated
    663   1.9   deraadt 		 * that my binding is possibly incorrect.
    664   1.9   deraadt 		 * Ok, do an immediate poll of the server.
    665   1.1   deraadt 		 */
    666  1.78  dholland 		if (dom->dom_checktime >= now) {
    667   1.9   deraadt 			/* don't flood it */
    668  1.78  dholland 			dom->dom_checktime = 0;
    669   1.9   deraadt 			check++;
    670   1.9   deraadt 		}
    671   1.1   deraadt 	}
    672  1.78  dholland 	dom->dom_asktime = now;
    673   1.1   deraadt #endif
    674   1.1   deraadt 
    675   1.1   deraadt 	res.ypbind_status = YPBIND_SUCC_VAL;
    676   1.1   deraadt 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
    677  1.78  dholland 		dom->dom_server_addr.sin_addr.s_addr;
    678   1.1   deraadt 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    679  1.78  dholland 		dom->dom_server_addr.sin_port;
    680  1.79  dholland 	DPRINTF("domain %s at %s/%d\n", dom->dom_name,
    681  1.78  dholland 		inet_ntoa(dom->dom_server_addr.sin_addr),
    682  1.78  dholland 		ntohs(dom->dom_server_addr.sin_port));
    683   1.1   deraadt 	return &res;
    684   1.1   deraadt }
    685   1.1   deraadt 
    686  1.95  dholland /*
    687  1.95  dholland  * The SETDOM call: ypset.
    688  1.95  dholland  *
    689  1.95  dholland  * Unless -ypsetme was given on the command line, this is rejected;
    690  1.95  dholland  * even then it's only allowed from localhost unless -ypset was
    691  1.95  dholland  * given on the command line.
    692  1.95  dholland  *
    693  1.95  dholland  * Allowing anyone anywhere to ypset you (and therefore provide your
    694  1.95  dholland  * password file and such) is a horrible thing and it isn't clear to
    695  1.95  dholland  * me why this functionality even exists.
    696  1.95  dholland  *
    697  1.95  dholland  * ypset from localhost has some but limited utility.
    698  1.95  dholland  */
    699  1.24  christos static void *
    700  1.47       wiz ypbindproc_setdom_2(SVCXPRT *transp, void *argp)
    701   1.1   deraadt {
    702  1.24  christos 	struct ypbind_setdom *sd = argp;
    703   1.1   deraadt 	struct sockaddr_in *fromsin, bindsin;
    704  1.13   deraadt 	static bool_t res;
    705   1.1   deraadt 
    706  1.36       mrg 	(void)memset(&res, 0, sizeof(res));
    707   1.1   deraadt 	fromsin = svc_getcaller(transp);
    708  1.88  dholland 	DPRINTF("ypbindproc_setdom_2 from %s\n", inet_ntoa(fromsin->sin_addr));
    709   1.1   deraadt 
    710  1.95  dholland 	/*
    711  1.95  dholland 	 * Reject unless enabled.
    712  1.95  dholland 	 */
    713  1.95  dholland 
    714  1.81  dholland 	if (allow_any_ypset) {
    715  1.81  dholland 		/* nothing */
    716  1.81  dholland 	} else if (allow_local_ypset) {
    717  1.24  christos 		if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
    718  1.81  dholland 			DPRINTF("ypset denied from %s\n",
    719  1.65  dholland 				inet_ntoa(fromsin->sin_addr));
    720  1.24  christos 			return NULL;
    721  1.24  christos 		}
    722  1.81  dholland 	} else {
    723  1.65  dholland 		DPRINTF("ypset denied\n");
    724  1.24  christos 		return NULL;
    725   1.1   deraadt 	}
    726   1.4   deraadt 
    727  1.95  dholland 	/* Make a "security" check. */
    728  1.24  christos 	if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
    729  1.65  dholland 		DPRINTF("ypset from unprivileged port denied\n");
    730  1.13   deraadt 		return &res;
    731  1.24  christos 	}
    732   1.1   deraadt 
    733  1.95  dholland 	/* Ignore requests we don't understand. */
    734  1.24  christos 	if (sd->ypsetdom_vers != YPVERS) {
    735  1.65  dholland 		DPRINTF("ypset with wrong version denied\n");
    736  1.13   deraadt 		return &res;
    737  1.24  christos 	}
    738   1.1   deraadt 
    739  1.95  dholland 	/*
    740  1.95  dholland 	 * Fetch the arguments out of the xdr-decoded blob and call
    741  1.95  dholland 	 * rpc_received(), setting FORCE so that the domain will be
    742  1.95  dholland 	 * created if we don't already know about it, and also saying
    743  1.95  dholland 	 * that it's actually a ypset.
    744  1.95  dholland 	 *
    745  1.95  dholland 	 * Effectively we're telilng rpc_received() that we got an
    746  1.95  dholland 	 * RPC response from the server specified by ypset.
    747  1.95  dholland 	 */
    748  1.36       mrg 	(void)memset(&bindsin, 0, sizeof bindsin);
    749   1.1   deraadt 	bindsin.sin_family = AF_INET;
    750  1.17   mycroft 	bindsin.sin_len = sizeof(bindsin);
    751  1.24  christos 	bindsin.sin_addr = sd->ypsetdom_addr;
    752  1.24  christos 	bindsin.sin_port = sd->ypsetdom_port;
    753  1.83  dholland 	rpc_received(sd->ypsetdom_domain, &bindsin, 1, 1);
    754  1.24  christos 
    755  1.85  dholland 	DPRINTF("ypset to %s for domain %s succeeded\n",
    756  1.85  dholland 		inet_ntoa(bindsin.sin_addr), sd->ypsetdom_domain);
    757   1.1   deraadt 	res = 1;
    758  1.13   deraadt 	return &res;
    759   1.1   deraadt }
    760   1.1   deraadt 
    761  1.95  dholland /*
    762  1.95  dholland  * Dispatcher for the ypbind service.
    763  1.95  dholland  *
    764  1.95  dholland  * There are three calls: NULL, which does nothing, DOMAIN, which
    765  1.95  dholland  * gets the binding for a particular domain, and SETDOM, which
    766  1.95  dholland  * does ypset.
    767  1.95  dholland  */
    768   1.1   deraadt static void
    769  1.47       wiz ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
    770   1.1   deraadt {
    771   1.1   deraadt 	union {
    772  1.22   thorpej 		char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
    773   1.1   deraadt 		struct ypbind_setdom ypbindproc_setdom_2_arg;
    774  1.56      tron 		void *alignment;
    775   1.1   deraadt 	} argument;
    776   1.1   deraadt 	struct authunix_parms *creds;
    777   1.1   deraadt 	char *result;
    778  1.24  christos 	xdrproc_t xdr_argument, xdr_result;
    779  1.47       wiz 	void *(*local)(SVCXPRT *, void *);
    780   1.1   deraadt 
    781   1.1   deraadt 	switch (rqstp->rq_proc) {
    782   1.1   deraadt 	case YPBINDPROC_NULL:
    783  1.90    plunky 		xdr_argument = (xdrproc_t)xdr_void;
    784  1.90    plunky 		xdr_result = (xdrproc_t)xdr_void;
    785  1.24  christos 		local = ypbindproc_null_2;
    786   1.1   deraadt 		break;
    787   1.1   deraadt 
    788   1.1   deraadt 	case YPBINDPROC_DOMAIN:
    789  1.90    plunky 		xdr_argument = (xdrproc_t)xdr_ypdomain_wrap_string;
    790  1.90    plunky 		xdr_result = (xdrproc_t)xdr_ypbind_resp;
    791  1.24  christos 		local = ypbindproc_domain_2;
    792   1.1   deraadt 		break;
    793   1.1   deraadt 
    794   1.1   deraadt 	case YPBINDPROC_SETDOM:
    795  1.17   mycroft 		switch (rqstp->rq_cred.oa_flavor) {
    796   1.1   deraadt 		case AUTH_UNIX:
    797   1.1   deraadt 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
    798  1.17   mycroft 			if (creds->aup_uid != 0) {
    799   1.1   deraadt 				svcerr_auth(transp, AUTH_BADCRED);
    800   1.1   deraadt 				return;
    801   1.1   deraadt 			}
    802   1.1   deraadt 			break;
    803   1.1   deraadt 		default:
    804   1.1   deraadt 			svcerr_auth(transp, AUTH_TOOWEAK);
    805   1.1   deraadt 			return;
    806   1.1   deraadt 		}
    807   1.1   deraadt 
    808  1.90    plunky 		xdr_argument = (xdrproc_t)xdr_ypbind_setdom;
    809  1.90    plunky 		xdr_result = (xdrproc_t)xdr_void;
    810  1.24  christos 		local = ypbindproc_setdom_2;
    811   1.1   deraadt 		break;
    812   1.1   deraadt 
    813   1.1   deraadt 	default:
    814   1.1   deraadt 		svcerr_noproc(transp);
    815   1.1   deraadt 		return;
    816   1.1   deraadt 	}
    817  1.36       mrg 	(void)memset(&argument, 0, sizeof(argument));
    818  1.57  christos 	if (!svc_getargs(transp, xdr_argument, (caddr_t)(void *)&argument)) {
    819   1.1   deraadt 		svcerr_decode(transp);
    820   1.1   deraadt 		return;
    821   1.1   deraadt 	}
    822  1.24  christos 	result = (*local)(transp, &argument);
    823   1.1   deraadt 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    824   1.1   deraadt 		svcerr_systemerr(transp);
    825   1.1   deraadt 	}
    826   1.1   deraadt 	return;
    827   1.1   deraadt }
    828   1.1   deraadt 
    829  1.95  dholland /*
    830  1.95  dholland  * Set up sunrpc stuff.
    831  1.95  dholland  *
    832  1.95  dholland  * This sets up the ypbind service (both TCP and UDP) and also opens
    833  1.95  dholland  * the sockets we use for talking to ypservers.
    834  1.95  dholland  */
    835  1.74  dholland static void
    836  1.74  dholland sunrpc_setup(void)
    837  1.74  dholland {
    838  1.74  dholland 	int one;
    839  1.74  dholland 
    840  1.74  dholland 	(void)pmap_unset(YPBINDPROG, YPBINDVERS);
    841  1.74  dholland 
    842  1.74  dholland 	udptransp = svcudp_create(RPC_ANYSOCK);
    843  1.74  dholland 	if (udptransp == NULL)
    844  1.74  dholland 		errx(1, "Cannot create udp service.");
    845  1.74  dholland 
    846  1.74  dholland 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    847  1.74  dholland 	    IPPROTO_UDP))
    848  1.74  dholland 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, udp).");
    849  1.74  dholland 
    850  1.74  dholland 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
    851  1.74  dholland 	if (tcptransp == NULL)
    852  1.74  dholland 		errx(1, "Cannot create tcp service.");
    853  1.74  dholland 
    854  1.74  dholland 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    855  1.74  dholland 	    IPPROTO_TCP))
    856  1.74  dholland 		errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, tcp).");
    857  1.74  dholland 
    858  1.74  dholland 	/* XXX use SOCK_STREAM for direct queries? */
    859  1.74  dholland 	if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    860  1.74  dholland 		err(1, "rpc socket");
    861  1.74  dholland 	if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    862  1.74  dholland 		err(1, "ping socket");
    863  1.74  dholland 
    864  1.74  dholland 	(void)fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
    865  1.74  dholland 	(void)fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY);
    866  1.74  dholland 
    867  1.74  dholland 	one = 1;
    868  1.74  dholland 	(void)setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one,
    869  1.74  dholland 	    (socklen_t)sizeof(one));
    870  1.74  dholland 	rmtca.prog = YPPROG;
    871  1.74  dholland 	rmtca.vers = YPVERS;
    872  1.74  dholland 	rmtca.proc = YPPROC_DOMAIN_NONACK;
    873  1.74  dholland 	rmtca.xdr_args = NULL;		/* set at call time */
    874  1.74  dholland 	rmtca.args_ptr = NULL;		/* set at call time */
    875  1.74  dholland 	rmtcr.port_ptr = &rmtcr_port;
    876  1.90    plunky 	rmtcr.xdr_results = (xdrproc_t)xdr_bool;
    877  1.74  dholland 	rmtcr.results_ptr = (caddr_t)(void *)&rmtcr_outval;
    878  1.74  dholland }
    879  1.74  dholland 
    880  1.68  dholland ////////////////////////////////////////////////////////////
    881  1.68  dholland // operational logic
    882  1.68  dholland 
    883  1.95  dholland /*
    884  1.95  dholland  * Broadcast an RPC packet to hopefully contact some servers for a
    885  1.95  dholland  * domain.
    886  1.95  dholland  */
    887  1.68  dholland static int
    888  1.68  dholland broadcast(char *buf, int outlen)
    889   1.1   deraadt {
    890  1.68  dholland 	struct ifaddrs *ifap, *ifa;
    891  1.68  dholland 	struct sockaddr_in bindsin;
    892  1.68  dholland 	struct in_addr in;
    893  1.68  dholland 
    894  1.68  dholland 	(void)memset(&bindsin, 0, sizeof bindsin);
    895  1.68  dholland 	bindsin.sin_family = AF_INET;
    896  1.68  dholland 	bindsin.sin_len = sizeof(bindsin);
    897  1.68  dholland 	bindsin.sin_port = htons(PMAPPORT);
    898   1.1   deraadt 
    899  1.68  dholland 	if (getifaddrs(&ifap) != 0) {
    900  1.72  dholland 		yp_log(LOG_WARNING, "broadcast: getifaddrs: %s",
    901  1.72  dholland 		       strerror(errno));
    902  1.68  dholland 		return (-1);
    903  1.68  dholland 	}
    904  1.68  dholland 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    905  1.68  dholland 		if (ifa->ifa_addr->sa_family != AF_INET)
    906  1.68  dholland 			continue;
    907  1.68  dholland 		if ((ifa->ifa_flags & IFF_UP) == 0)
    908  1.68  dholland 			continue;
    909   1.1   deraadt 
    910  1.68  dholland 		switch (ifa->ifa_flags & (IFF_LOOPBACK | IFF_BROADCAST)) {
    911  1.68  dholland 		case IFF_BROADCAST:
    912  1.68  dholland 			if (!ifa->ifa_broadaddr)
    913  1.68  dholland 				continue;
    914  1.68  dholland 			if (ifa->ifa_broadaddr->sa_family != AF_INET)
    915  1.68  dholland 				continue;
    916  1.68  dholland 			in = ((struct sockaddr_in *)(void *)ifa->ifa_broadaddr)->sin_addr;
    917  1.68  dholland 			break;
    918  1.68  dholland 		case IFF_LOOPBACK:
    919  1.68  dholland 			in = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr;
    920  1.68  dholland 			break;
    921  1.68  dholland 		default:
    922  1.68  dholland 			continue;
    923  1.68  dholland 		}
    924  1.27   thorpej 
    925  1.68  dholland 		bindsin.sin_addr = in;
    926  1.68  dholland 		DPRINTF("broadcast %x\n", bindsin.sin_addr.s_addr);
    927  1.68  dholland 		if (sendto(rpcsock, buf, outlen, 0,
    928  1.68  dholland 		    (struct sockaddr *)(void *)&bindsin,
    929  1.68  dholland 		    (socklen_t)bindsin.sin_len) == -1)
    930  1.72  dholland 			yp_log(LOG_WARNING, "broadcast: sendto: %s",
    931  1.72  dholland 			       strerror(errno));
    932   1.1   deraadt 	}
    933  1.68  dholland 	freeifaddrs(ifap);
    934  1.68  dholland 	return (0);
    935  1.68  dholland }
    936   1.1   deraadt 
    937  1.95  dholland /*
    938  1.95  dholland  * Send an RPC packet to all the configured (in /var/yp/foo.ypservers)
    939  1.95  dholland  * servers for a domain.
    940  1.95  dholland  *
    941  1.95  dholland  * XXX: we should read and parse the file up front and reread it only
    942  1.95  dholland  * if it changes.
    943  1.95  dholland  */
    944  1.68  dholland static int
    945  1.84  dholland direct(char *buf, int outlen, struct domain *dom)
    946  1.68  dholland {
    947  1.71  dholland 	const char *path;
    948  1.68  dholland 	char line[_POSIX2_LINE_MAX];
    949  1.68  dholland 	char *p;
    950  1.68  dholland 	struct hostent *hp;
    951  1.68  dholland 	struct sockaddr_in bindsin;
    952  1.68  dholland 	int i, count = 0;
    953  1.37    bouyer 
    954  1.84  dholland 	/*
    955  1.84  dholland 	 * XXX what happens if someone's editor unlinks and replaces
    956  1.84  dholland 	 * the servers file?
    957  1.84  dholland 	 */
    958  1.84  dholland 
    959  1.84  dholland 	if (dom->dom_serversfile != NULL) {
    960  1.84  dholland 		rewind(dom->dom_serversfile);
    961  1.84  dholland 	} else {
    962  1.84  dholland 		path = ypservers_filename(dom->dom_name);
    963  1.84  dholland 		dom->dom_serversfile = fopen(path, "r");
    964  1.84  dholland 		if (dom->dom_serversfile == NULL) {
    965  1.84  dholland 			/*
    966  1.84  dholland 			 * XXX there should be a time restriction on
    967  1.84  dholland 			 * this (and/or on trying the open) so we
    968  1.84  dholland 			 * don't flood the log. Or should we fall back
    969  1.84  dholland 			 * to broadcast mode?
    970  1.84  dholland 			 */
    971  1.84  dholland 			yp_log(LOG_ERR, "%s: %s", path,
    972  1.72  dholland 			       strerror(errno));
    973  1.84  dholland 			return -1;
    974  1.37    bouyer 		}
    975  1.27   thorpej 	}
    976  1.27   thorpej 
    977  1.57  christos 	(void)memset(&bindsin, 0, sizeof bindsin);
    978  1.27   thorpej 	bindsin.sin_family = AF_INET;
    979  1.27   thorpej 	bindsin.sin_len = sizeof(bindsin);
    980  1.27   thorpej 	bindsin.sin_port = htons(PMAPPORT);
    981  1.27   thorpej 
    982  1.84  dholland 	while (fgets(line, (int)sizeof(line), dom->dom_serversfile) != NULL) {
    983  1.27   thorpej 		/* skip lines that are too big */
    984  1.27   thorpej 		p = strchr(line, '\n');
    985  1.27   thorpej 		if (p == NULL) {
    986  1.27   thorpej 			int c;
    987  1.27   thorpej 
    988  1.84  dholland 			while ((c = getc(dom->dom_serversfile)) != '\n' && c != EOF)
    989  1.27   thorpej 				;
    990  1.27   thorpej 			continue;
    991  1.27   thorpej 		}
    992  1.27   thorpej 		*p = '\0';
    993  1.27   thorpej 		p = line;
    994  1.53       dsl 		while (isspace((unsigned char)*p))
    995  1.27   thorpej 			p++;
    996  1.27   thorpej 		if (*p == '#')
    997  1.27   thorpej 			continue;
    998  1.27   thorpej 		hp = gethostbyname(p);
    999  1.27   thorpej 		if (!hp) {
   1000  1.45     lukem 			yp_log(LOG_WARNING, "%s: %s", p, hstrerror(h_errno));
   1001  1.27   thorpej 			continue;
   1002  1.27   thorpej 		}
   1003  1.27   thorpej 		/* step through all addresses in case first is unavailable */
   1004  1.27   thorpej 		for (i = 0; hp->h_addr_list[i]; i++) {
   1005  1.57  christos 			(void)memcpy(&bindsin.sin_addr, hp->h_addr_list[0],
   1006  1.27   thorpej 			    hp->h_length);
   1007  1.27   thorpej 			if (sendto(rpcsock, buf, outlen, 0,
   1008  1.57  christos 			    (struct sockaddr *)(void *)&bindsin,
   1009  1.57  christos 			    (socklen_t)sizeof(bindsin)) < 0) {
   1010  1.72  dholland 				yp_log(LOG_WARNING, "direct: sendto: %s",
   1011  1.72  dholland 				       strerror(errno));
   1012  1.27   thorpej 				continue;
   1013  1.27   thorpej 			} else
   1014  1.27   thorpej 				count++;
   1015  1.27   thorpej 		}
   1016  1.27   thorpej 	}
   1017  1.37    bouyer 	if (!count) {
   1018  1.72  dholland 		yp_log(LOG_WARNING, "No contactable servers found in %s",
   1019  1.84  dholland 		    ypservers_filename(dom->dom_name));
   1020  1.37    bouyer 		return -1;
   1021  1.37    bouyer 	}
   1022  1.27   thorpej 	return 0;
   1023  1.27   thorpej }
   1024  1.27   thorpej 
   1025  1.95  dholland /*
   1026  1.95  dholland  * Send an RPC packet to the server that's been selected with ypset.
   1027  1.95  dholland  * (This is only used when in broadcast mode and when ypset is
   1028  1.95  dholland  * allowed.)
   1029  1.95  dholland  */
   1030  1.27   thorpej static int
   1031  1.78  dholland direct_set(char *buf, int outlen, struct domain *dom)
   1032  1.27   thorpej {
   1033  1.27   thorpej 	struct sockaddr_in bindsin;
   1034  1.27   thorpej 	char path[MAXPATHLEN];
   1035  1.27   thorpej 	struct iovec iov[2];
   1036  1.27   thorpej 	struct ypbind_resp ybr;
   1037  1.27   thorpej 	SVCXPRT dummy_svc;
   1038  1.57  christos 	int fd;
   1039  1.57  christos 	ssize_t bytes;
   1040  1.27   thorpej 
   1041  1.27   thorpej 	/*
   1042  1.27   thorpej 	 * Gack, we lose if binding file went away.  We reset
   1043  1.27   thorpej 	 * "been_set" if this happens, otherwise we'll never
   1044  1.27   thorpej 	 * bind again.
   1045  1.27   thorpej 	 */
   1046  1.57  christos 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
   1047  1.79  dholland 	    dom->dom_name, dom->dom_vers);
   1048  1.27   thorpej 
   1049  1.73  dholland 	fd = open_locked(path, O_RDONLY, 0644);
   1050  1.73  dholland 	if (fd == -1) {
   1051  1.72  dholland 		yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
   1052  1.83  dholland 		dom->dom_been_ypset = 0;
   1053  1.27   thorpej 		return -1;
   1054  1.27   thorpej 	}
   1055  1.27   thorpej 
   1056  1.27   thorpej 	/* Read the binding file... */
   1057  1.57  christos 	iov[0].iov_base = &(dummy_svc.xp_port);
   1058  1.27   thorpej 	iov[0].iov_len = sizeof(dummy_svc.xp_port);
   1059  1.57  christos 	iov[1].iov_base = &ybr;
   1060  1.27   thorpej 	iov[1].iov_len = sizeof(ybr);
   1061  1.27   thorpej 	bytes = readv(fd, iov, 2);
   1062  1.27   thorpej 	(void)close(fd);
   1063  1.72  dholland 	if (bytes <0 || (size_t)bytes != (iov[0].iov_len + iov[1].iov_len)) {
   1064  1.27   thorpej 		/* Binding file corrupt? */
   1065  1.72  dholland 		if (bytes < 0)
   1066  1.72  dholland 			yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
   1067  1.72  dholland 		else
   1068  1.72  dholland 			yp_log(LOG_WARNING, "%s: short read", path);
   1069  1.83  dholland 		dom->dom_been_ypset = 0;
   1070  1.27   thorpej 		return -1;
   1071  1.68  dholland 	}
   1072  1.68  dholland 
   1073  1.68  dholland 	bindsin.sin_addr =
   1074  1.68  dholland 	    ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
   1075  1.68  dholland 
   1076  1.68  dholland 	if (sendto(rpcsock, buf, outlen, 0,
   1077  1.68  dholland 	    (struct sockaddr *)(void *)&bindsin,
   1078  1.68  dholland 	    (socklen_t)sizeof(bindsin)) < 0) {
   1079  1.72  dholland 		yp_log(LOG_WARNING, "direct_set: sendto: %s", strerror(errno));
   1080  1.68  dholland 		return -1;
   1081  1.68  dholland 	}
   1082  1.68  dholland 
   1083  1.68  dholland 	return 0;
   1084  1.68  dholland }
   1085  1.68  dholland 
   1086  1.95  dholland /*
   1087  1.95  dholland  * Receive and dispatch packets on the general RPC socket.
   1088  1.95  dholland  */
   1089  1.68  dholland static enum clnt_stat
   1090  1.68  dholland handle_replies(void)
   1091  1.68  dholland {
   1092  1.68  dholland 	char buf[BUFSIZE];
   1093  1.68  dholland 	socklen_t fromlen;
   1094  1.68  dholland 	ssize_t inlen;
   1095  1.78  dholland 	struct domain *dom;
   1096  1.68  dholland 	struct sockaddr_in raddr;
   1097  1.68  dholland 	struct rpc_msg msg;
   1098  1.68  dholland 	XDR xdr;
   1099  1.68  dholland 
   1100  1.68  dholland recv_again:
   1101  1.68  dholland 	DPRINTF("handle_replies receiving\n");
   1102  1.68  dholland 	(void)memset(&xdr, 0, sizeof(xdr));
   1103  1.68  dholland 	(void)memset(&msg, 0, sizeof(msg));
   1104  1.68  dholland 	msg.acpted_rply.ar_verf = _null_auth;
   1105  1.68  dholland 	msg.acpted_rply.ar_results.where = (caddr_t)(void *)&rmtcr;
   1106  1.90    plunky 	msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
   1107  1.68  dholland 
   1108  1.68  dholland try_again:
   1109  1.68  dholland 	fromlen = sizeof(struct sockaddr);
   1110  1.68  dholland 	inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
   1111  1.68  dholland 		(struct sockaddr *)(void *)&raddr, &fromlen);
   1112  1.68  dholland 	if (inlen < 0) {
   1113  1.68  dholland 		if (errno == EINTR)
   1114  1.68  dholland 			goto try_again;
   1115  1.68  dholland 		DPRINTF("handle_replies: recvfrom failed (%s)\n",
   1116  1.68  dholland 			strerror(errno));
   1117  1.68  dholland 		return RPC_CANTRECV;
   1118  1.68  dholland 	}
   1119  1.68  dholland 	if ((size_t)inlen < sizeof(uint32_t))
   1120  1.68  dholland 		goto recv_again;
   1121  1.27   thorpej 
   1122  1.68  dholland 	/*
   1123  1.68  dholland 	 * see if reply transaction id matches sent id.
   1124  1.68  dholland 	 * If so, decode the results.
   1125  1.68  dholland 	 */
   1126  1.68  dholland 	xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
   1127  1.68  dholland 	if (xdr_replymsg(&xdr, &msg)) {
   1128  1.68  dholland 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
   1129  1.68  dholland 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
   1130  1.68  dholland 			raddr.sin_port = htons((uint16_t)rmtcr_port);
   1131  1.78  dholland 			dom = domain_find(msg.rm_xid);
   1132  1.78  dholland 			if (dom != NULL)
   1133  1.83  dholland 				rpc_received(dom->dom_name, &raddr, 0, 0);
   1134  1.68  dholland 		}
   1135  1.27   thorpej 	}
   1136  1.68  dholland 	xdr.x_op = XDR_FREE;
   1137  1.90    plunky 	msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
   1138  1.68  dholland 	xdr_destroy(&xdr);
   1139  1.27   thorpej 
   1140  1.68  dholland 	return RPC_SUCCESS;
   1141   1.1   deraadt }
   1142   1.1   deraadt 
   1143  1.95  dholland /*
   1144  1.95  dholland  * Receive and dispatch packets on the ping socket.
   1145  1.95  dholland  */
   1146  1.24  christos static enum clnt_stat
   1147  1.68  dholland handle_ping(void)
   1148   1.1   deraadt {
   1149  1.24  christos 	char buf[BUFSIZE];
   1150  1.54       mrg 	socklen_t fromlen;
   1151  1.57  christos 	ssize_t inlen;
   1152  1.78  dholland 	struct domain *dom;
   1153   1.1   deraadt 	struct sockaddr_in raddr;
   1154   1.1   deraadt 	struct rpc_msg msg;
   1155   1.1   deraadt 	XDR xdr;
   1156  1.68  dholland 	bool_t res;
   1157   1.1   deraadt 
   1158   1.1   deraadt recv_again:
   1159  1.68  dholland 	DPRINTF("handle_ping receiving\n");
   1160  1.36       mrg 	(void)memset(&xdr, 0, sizeof(xdr));
   1161  1.36       mrg 	(void)memset(&msg, 0, sizeof(msg));
   1162   1.1   deraadt 	msg.acpted_rply.ar_verf = _null_auth;
   1163  1.68  dholland 	msg.acpted_rply.ar_results.where = (caddr_t)(void *)&res;
   1164  1.90    plunky 	msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_bool;
   1165   1.1   deraadt 
   1166   1.1   deraadt try_again:
   1167  1.68  dholland 	fromlen = sizeof (struct sockaddr);
   1168  1.68  dholland 	inlen = recvfrom(pingsock, buf, sizeof buf, 0,
   1169  1.68  dholland 	    (struct sockaddr *)(void *)&raddr, &fromlen);
   1170  1.17   mycroft 	if (inlen < 0) {
   1171  1.17   mycroft 		if (errno == EINTR)
   1172   1.1   deraadt 			goto try_again;
   1173  1.68  dholland 		DPRINTF("handle_ping: recvfrom failed (%s)\n",
   1174  1.65  dholland 			strerror(errno));
   1175   1.1   deraadt 		return RPC_CANTRECV;
   1176   1.1   deraadt 	}
   1177  1.64  dholland 	if ((size_t)inlen < sizeof(uint32_t))
   1178   1.1   deraadt 		goto recv_again;
   1179   1.1   deraadt 
   1180   1.1   deraadt 	/*
   1181   1.1   deraadt 	 * see if reply transaction id matches sent id.
   1182   1.1   deraadt 	 * If so, decode the results.
   1183   1.1   deraadt 	 */
   1184  1.64  dholland 	xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
   1185  1.17   mycroft 	if (xdr_replymsg(&xdr, &msg)) {
   1186  1.17   mycroft 		if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
   1187  1.17   mycroft 		    (msg.acpted_rply.ar_stat == SUCCESS)) {
   1188  1.78  dholland 			dom = domain_find(msg.rm_xid);
   1189  1.78  dholland 			if (dom != NULL)
   1190  1.83  dholland 				rpc_received(dom->dom_name, &raddr, 0, 0);
   1191   1.1   deraadt 		}
   1192   1.1   deraadt 	}
   1193   1.1   deraadt 	xdr.x_op = XDR_FREE;
   1194  1.90    plunky 	msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
   1195   1.1   deraadt 	xdr_destroy(&xdr);
   1196   1.1   deraadt 
   1197   1.1   deraadt 	return RPC_SUCCESS;
   1198   1.1   deraadt }
   1199   1.1   deraadt 
   1200  1.95  dholland /*
   1201  1.95  dholland  * Contact all known servers for a domain in the hopes that one of
   1202  1.95  dholland  * them's awake. Also, if we previously had a binding but it timed
   1203  1.95  dholland  * out, try the portmapper on that host in case ypserv moved ports for
   1204  1.95  dholland  * some reason.
   1205  1.95  dholland  *
   1206  1.95  dholland  * As a side effect, wipe out any existing binding file.
   1207  1.95  dholland  */
   1208  1.68  dholland static int
   1209  1.78  dholland nag_servers(struct domain *dom)
   1210  1.68  dholland {
   1211  1.79  dholland 	char *dom_name = dom->dom_name;
   1212  1.68  dholland 	struct rpc_msg msg;
   1213  1.68  dholland 	char buf[BUFSIZE];
   1214  1.68  dholland 	enum clnt_stat st;
   1215  1.68  dholland 	int outlen;
   1216  1.68  dholland 	AUTH *rpcua;
   1217  1.68  dholland 	XDR xdr;
   1218  1.68  dholland 
   1219  1.68  dholland 	DPRINTF("nag_servers\n");
   1220  1.90    plunky 	rmtca.xdr_args = (xdrproc_t)xdr_ypdomain_wrap_string;
   1221  1.78  dholland 	rmtca.args_ptr = (caddr_t)(void *)&dom_name;
   1222  1.68  dholland 
   1223  1.68  dholland 	(void)memset(&xdr, 0, sizeof xdr);
   1224  1.68  dholland 	(void)memset(&msg, 0, sizeof msg);
   1225  1.68  dholland 
   1226  1.68  dholland 	rpcua = authunix_create_default();
   1227  1.68  dholland 	if (rpcua == NULL) {
   1228  1.68  dholland 		DPRINTF("cannot get unix auth\n");
   1229  1.68  dholland 		return RPC_SYSTEMERROR;
   1230  1.68  dholland 	}
   1231  1.68  dholland 	msg.rm_direction = CALL;
   1232  1.68  dholland 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
   1233  1.68  dholland 	msg.rm_call.cb_prog = PMAPPROG;
   1234  1.68  dholland 	msg.rm_call.cb_vers = PMAPVERS;
   1235  1.68  dholland 	msg.rm_call.cb_proc = PMAPPROC_CALLIT;
   1236  1.68  dholland 	msg.rm_call.cb_cred = rpcua->ah_cred;
   1237  1.68  dholland 	msg.rm_call.cb_verf = rpcua->ah_verf;
   1238  1.68  dholland 
   1239  1.78  dholland 	msg.rm_xid = dom->dom_xid;
   1240  1.68  dholland 	xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
   1241  1.68  dholland 	if (!xdr_callmsg(&xdr, &msg)) {
   1242  1.68  dholland 		st = RPC_CANTENCODEARGS;
   1243  1.68  dholland 		AUTH_DESTROY(rpcua);
   1244  1.68  dholland 		return st;
   1245  1.68  dholland 	}
   1246  1.68  dholland 	if (!xdr_rmtcall_args(&xdr, &rmtca)) {
   1247  1.68  dholland 		st = RPC_CANTENCODEARGS;
   1248  1.68  dholland 		AUTH_DESTROY(rpcua);
   1249  1.68  dholland 		return st;
   1250  1.68  dholland 	}
   1251  1.68  dholland 	outlen = (int)xdr_getpos(&xdr);
   1252  1.68  dholland 	xdr_destroy(&xdr);
   1253  1.68  dholland 	if (outlen < 1) {
   1254  1.68  dholland 		st = RPC_CANTENCODEARGS;
   1255  1.68  dholland 		AUTH_DESTROY(rpcua);
   1256  1.68  dholland 		return st;
   1257  1.68  dholland 	}
   1258  1.68  dholland 	AUTH_DESTROY(rpcua);
   1259  1.68  dholland 
   1260  1.78  dholland 	if (dom->dom_lockfd != -1) {
   1261  1.78  dholland 		(void)close(dom->dom_lockfd);
   1262  1.78  dholland 		dom->dom_lockfd = -1;
   1263  1.78  dholland 		removelock(dom);
   1264  1.68  dholland 	}
   1265  1.68  dholland 
   1266  1.78  dholland 	if (dom->dom_alive == 2) {
   1267  1.68  dholland 		/*
   1268  1.68  dholland 		 * This resolves the following situation:
   1269  1.68  dholland 		 * ypserver on other subnet was once bound,
   1270  1.68  dholland 		 * but rebooted and is now using a different port
   1271  1.68  dholland 		 */
   1272  1.68  dholland 		struct sockaddr_in bindsin;
   1273  1.68  dholland 
   1274  1.68  dholland 		(void)memset(&bindsin, 0, sizeof bindsin);
   1275  1.68  dholland 		bindsin.sin_family = AF_INET;
   1276  1.68  dholland 		bindsin.sin_len = sizeof(bindsin);
   1277  1.68  dholland 		bindsin.sin_port = htons(PMAPPORT);
   1278  1.78  dholland 		bindsin.sin_addr = dom->dom_server_addr.sin_addr;
   1279  1.68  dholland 
   1280  1.68  dholland 		if (sendto(rpcsock, buf, outlen, 0,
   1281  1.68  dholland 		    (struct sockaddr *)(void *)&bindsin,
   1282  1.68  dholland 		    (socklen_t)sizeof bindsin) == -1)
   1283  1.72  dholland 			yp_log(LOG_WARNING, "nag_servers: sendto: %s",
   1284  1.72  dholland 			       strerror(errno));
   1285  1.68  dholland 	}
   1286  1.68  dholland 
   1287  1.82  dholland 	switch (dom->dom_ypbindmode) {
   1288  1.81  dholland 	case YPBIND_BROADCAST:
   1289  1.83  dholland 		if (dom->dom_been_ypset) {
   1290  1.78  dholland 			return direct_set(buf, outlen, dom);
   1291  1.81  dholland 		}
   1292  1.68  dholland 		return broadcast(buf, outlen);
   1293  1.68  dholland 
   1294  1.68  dholland 	case YPBIND_DIRECT:
   1295  1.84  dholland 		return direct(buf, outlen, dom);
   1296  1.68  dholland 	}
   1297  1.68  dholland 	/*NOTREACHED*/
   1298  1.68  dholland 	return -1;
   1299  1.68  dholland }
   1300  1.68  dholland 
   1301  1.95  dholland /*
   1302  1.95  dholland  * Send a ping message to a domain's current ypserver.
   1303  1.95  dholland  *
   1304  1.95  dholland  * Note that dom_alive is 2 while waiting for the response.
   1305  1.95  dholland  */
   1306  1.69  dholland static int
   1307  1.78  dholland ping(struct domain *dom)
   1308  1.10   deraadt {
   1309  1.79  dholland 	char *dom_name = dom->dom_name;
   1310  1.68  dholland 	struct rpc_msg msg;
   1311  1.24  christos 	char buf[BUFSIZE];
   1312  1.68  dholland 	enum clnt_stat st;
   1313  1.68  dholland 	int outlen;
   1314  1.68  dholland 	AUTH *rpcua;
   1315  1.68  dholland 	XDR xdr;
   1316  1.68  dholland 
   1317  1.68  dholland 	(void)memset(&xdr, 0, sizeof xdr);
   1318  1.68  dholland 	(void)memset(&msg, 0, sizeof msg);
   1319  1.68  dholland 
   1320  1.68  dholland 	rpcua = authunix_create_default();
   1321  1.68  dholland 	if (rpcua == NULL) {
   1322  1.68  dholland 		DPRINTF("cannot get unix auth\n");
   1323  1.68  dholland 		return RPC_SYSTEMERROR;
   1324  1.68  dholland 	}
   1325  1.68  dholland 
   1326  1.68  dholland 	msg.rm_direction = CALL;
   1327  1.68  dholland 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
   1328  1.68  dholland 	msg.rm_call.cb_prog = YPPROG;
   1329  1.68  dholland 	msg.rm_call.cb_vers = YPVERS;
   1330  1.68  dholland 	msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
   1331  1.68  dholland 	msg.rm_call.cb_cred = rpcua->ah_cred;
   1332  1.68  dholland 	msg.rm_call.cb_verf = rpcua->ah_verf;
   1333  1.68  dholland 
   1334  1.78  dholland 	msg.rm_xid = dom->dom_xid;
   1335  1.68  dholland 	xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
   1336  1.68  dholland 	if (!xdr_callmsg(&xdr, &msg)) {
   1337  1.68  dholland 		st = RPC_CANTENCODEARGS;
   1338  1.68  dholland 		AUTH_DESTROY(rpcua);
   1339  1.68  dholland 		return st;
   1340  1.68  dholland 	}
   1341  1.78  dholland 	if (!xdr_ypdomain_wrap_string(&xdr, &dom_name)) {
   1342  1.68  dholland 		st = RPC_CANTENCODEARGS;
   1343  1.68  dholland 		AUTH_DESTROY(rpcua);
   1344  1.68  dholland 		return st;
   1345  1.68  dholland 	}
   1346  1.68  dholland 	outlen = (int)xdr_getpos(&xdr);
   1347  1.68  dholland 	xdr_destroy(&xdr);
   1348  1.68  dholland 	if (outlen < 1) {
   1349  1.68  dholland 		st = RPC_CANTENCODEARGS;
   1350  1.68  dholland 		AUTH_DESTROY(rpcua);
   1351  1.68  dholland 		return st;
   1352  1.68  dholland 	}
   1353  1.68  dholland 	AUTH_DESTROY(rpcua);
   1354  1.68  dholland 
   1355  1.78  dholland 	dom->dom_alive = 2;
   1356  1.78  dholland 	DPRINTF("ping %x\n", dom->dom_server_addr.sin_addr.s_addr);
   1357  1.68  dholland 
   1358  1.68  dholland 	if (sendto(pingsock, buf, outlen, 0,
   1359  1.78  dholland 	    (struct sockaddr *)(void *)&dom->dom_server_addr,
   1360  1.78  dholland 	    (socklen_t)(sizeof dom->dom_server_addr)) == -1)
   1361  1.72  dholland 		yp_log(LOG_WARNING, "ping: sendto: %s", strerror(errno));
   1362  1.68  dholland 	return 0;
   1363  1.68  dholland 
   1364  1.68  dholland }
   1365  1.68  dholland 
   1366  1.68  dholland /*
   1367  1.95  dholland  * Scan for timer-based work to do.
   1368  1.95  dholland  *
   1369  1.95  dholland  * If the domain is currently alive, ping the server we're currently
   1370  1.95  dholland  * bound to. Otherwise, try all known servers and/or broadcast for a
   1371  1.95  dholland  * server via nag_servers.
   1372  1.68  dholland  *
   1373  1.95  dholland  * Try again in five seconds.
   1374  1.68  dholland  */
   1375  1.69  dholland static void
   1376  1.68  dholland checkwork(void)
   1377  1.68  dholland {
   1378  1.78  dholland 	struct domain *dom;
   1379  1.68  dholland 	time_t t;
   1380  1.68  dholland 
   1381  1.68  dholland 	check = 0;
   1382  1.68  dholland 
   1383  1.68  dholland 	(void)time(&t);
   1384  1.78  dholland 	for (dom = domains; dom != NULL; dom = dom->dom_next) {
   1385  1.78  dholland 		if (dom->dom_checktime < t) {
   1386  1.78  dholland 			if (dom->dom_alive == 1)
   1387  1.78  dholland 				(void)ping(dom);
   1388  1.68  dholland 			else
   1389  1.78  dholland 				(void)nag_servers(dom);
   1390  1.68  dholland 			(void)time(&t);
   1391  1.78  dholland 			dom->dom_checktime = t + 5;
   1392  1.68  dholland 		}
   1393  1.68  dholland 	}
   1394  1.68  dholland }
   1395  1.68  dholland 
   1396  1.68  dholland ////////////////////////////////////////////////////////////
   1397  1.68  dholland // main
   1398  1.68  dholland 
   1399  1.95  dholland /*
   1400  1.95  dholland  * Usage message.
   1401  1.95  dholland  */
   1402  1.89     joerg __dead static void
   1403  1.68  dholland usage(void)
   1404  1.68  dholland {
   1405  1.68  dholland 	const char *opt = "";
   1406  1.68  dholland #ifdef DEBUG
   1407  1.68  dholland 	opt = " [-d]";
   1408  1.68  dholland #endif
   1409  1.68  dholland 
   1410  1.68  dholland 	(void)fprintf(stderr,
   1411  1.68  dholland 	    "Usage: %s [-broadcast] [-insecure] [-ypset] [-ypsetme]%s\n",
   1412  1.68  dholland 	    getprogname(), opt);
   1413  1.68  dholland 	exit(1);
   1414  1.68  dholland }
   1415  1.68  dholland 
   1416  1.95  dholland /*
   1417  1.95  dholland  * Main.
   1418  1.95  dholland  */
   1419  1.68  dholland int
   1420  1.68  dholland main(int argc, char *argv[])
   1421  1.68  dholland {
   1422  1.68  dholland 	struct timeval tv;
   1423  1.68  dholland 	fd_set fdsr;
   1424  1.68  dholland 	int width, lockfd;
   1425  1.93  dholland 	int started = 0;
   1426  1.91  dholland 	char *domainname;
   1427  1.10   deraadt 
   1428  1.68  dholland 	setprogname(argv[0]);
   1429  1.10   deraadt 
   1430  1.95  dholland 	/*
   1431  1.95  dholland 	 * Process arguments.
   1432  1.95  dholland 	 */
   1433  1.95  dholland 
   1434  1.82  dholland 	default_ypbindmode = YPBIND_DIRECT;
   1435  1.68  dholland 	while (--argc) {
   1436  1.68  dholland 		++argv;
   1437  1.81  dholland 		if (!strcmp("-insecure", *argv)) {
   1438  1.68  dholland 			insecure = 1;
   1439  1.81  dholland 		} else if (!strcmp("-ypset", *argv)) {
   1440  1.81  dholland 			allow_any_ypset = 1;
   1441  1.81  dholland 			allow_local_ypset = 1;
   1442  1.81  dholland 		} else if (!strcmp("-ypsetme", *argv)) {
   1443  1.81  dholland 			allow_any_ypset = 0;
   1444  1.81  dholland 			allow_local_ypset = 1;
   1445  1.81  dholland 		} else if (!strcmp("-broadcast", *argv)) {
   1446  1.82  dholland 			default_ypbindmode = YPBIND_BROADCAST;
   1447  1.68  dholland #ifdef DEBUG
   1448  1.81  dholland 		} else if (!strcmp("-d", *argv)) {
   1449  1.81  dholland 			debug = 1;
   1450  1.68  dholland #endif
   1451  1.81  dholland 		} else {
   1452  1.68  dholland 			usage();
   1453  1.81  dholland 		}
   1454  1.10   deraadt 	}
   1455  1.10   deraadt 
   1456  1.95  dholland 	/*
   1457  1.95  dholland 	 * Look up the name of the default domain.
   1458  1.95  dholland 	 */
   1459  1.95  dholland 
   1460  1.94  dholland 	(void)yp_get_default_domain(&domainname);
   1461  1.94  dholland 	if (domainname[0] == '\0')
   1462  1.94  dholland 		errx(1, "Domainname not set. Aborting.");
   1463  1.94  dholland 	if (_yp_invalid_domain(domainname))
   1464  1.94  dholland 		errx(1, "Invalid domainname: %s", domainname);
   1465  1.94  dholland 
   1466  1.95  dholland 	/*
   1467  1.95  dholland 	 * Start things up.
   1468  1.95  dholland 	 */
   1469  1.95  dholland 
   1470  1.95  dholland 	/* Open the system log. */
   1471  1.68  dholland 	openlog("ypbind", LOG_PERROR | LOG_PID, LOG_DAEMON);
   1472  1.10   deraadt 
   1473  1.95  dholland 	/* Acquire /var/run/ypbind.lock. */
   1474  1.73  dholland 	lockfd = open_locked(_PATH_YPBIND_LOCK, O_CREAT|O_RDWR|O_TRUNC, 0644);
   1475  1.68  dholland 	if (lockfd == -1)
   1476  1.68  dholland 		err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
   1477  1.14       cgd 
   1478  1.95  dholland 	/* Initialize sunrpc stuff. */
   1479  1.74  dholland 	sunrpc_setup();
   1480   1.1   deraadt 
   1481  1.95  dholland 	/* Clean out BINDINGDIR, deleting all existing (now stale) bindings */
   1482  1.68  dholland 	if (purge_bindingdir(BINDINGDIR) < 0)
   1483  1.95  dholland 		errx(1, "Unable to purge old bindings from %s", BINDINGDIR);
   1484  1.95  dholland 
   1485  1.95  dholland 	/*
   1486  1.95  dholland 	 * We start with one binding, for the default domain. It starts
   1487  1.95  dholland 	 * out "unsuccessful".
   1488  1.95  dholland 	 *
   1489  1.95  dholland 	 * XXX: domain_create adds the new domain to 'domains' (the
   1490  1.95  dholland 	 * global linked list) and therefore we shouldn't assign
   1491  1.95  dholland 	 * 'domains' again on return.
   1492  1.95  dholland 	 */
   1493   1.6   deraadt 
   1494  1.77  dholland 	domains = domain_create(domainname);
   1495  1.95  dholland 
   1496  1.95  dholland 	/*
   1497  1.95  dholland 	 * Delete the lock for the default domain again, just in case something
   1498  1.95  dholland 	 * magically caused it to appear since purge_bindingdir() was called.
   1499  1.95  dholland 	 * XXX: this is useless and redundant; remove it.
   1500  1.95  dholland 	 */
   1501  1.77  dholland 	removelock(domains);
   1502   1.6   deraadt 
   1503  1.95  dholland 	/*
   1504  1.95  dholland 	 * Main loop. Wake up at least once a second and check for
   1505  1.95  dholland 	 * timer-based work to do (checkwork) and also handle incoming
   1506  1.95  dholland 	 * responses from ypservers and any RPCs made to the ypbind
   1507  1.95  dholland 	 * service.
   1508  1.95  dholland 	 *
   1509  1.95  dholland 	 * There are two sockets used for ypserver traffic: one for
   1510  1.95  dholland 	 * pings and one for everything else. These call XDR manually
   1511  1.95  dholland 	 * for encoding and are *not* dispatched via the sunrpc
   1512  1.95  dholland 	 * libraries.
   1513  1.95  dholland 	 *
   1514  1.95  dholland 	 * The ypbind serivce *is* dispatched via the sunrpc libraries.
   1515  1.95  dholland 	 * svc_getreqset() does whatever internal muck and ultimately
   1516  1.95  dholland 	 * ypbind service calls arrive at ypbindprog_2().
   1517  1.95  dholland 	 */
   1518  1.68  dholland 	checkwork();
   1519  1.68  dholland 	for (;;) {
   1520  1.68  dholland 		width = svc_maxfd;
   1521  1.68  dholland 		if (rpcsock > width)
   1522  1.68  dholland 			width = rpcsock;
   1523  1.68  dholland 		if (pingsock > width)
   1524  1.68  dholland 			width = pingsock;
   1525  1.68  dholland 		width++;
   1526  1.68  dholland 		fdsr = svc_fdset;
   1527  1.68  dholland 		FD_SET(rpcsock, &fdsr);
   1528  1.68  dholland 		FD_SET(pingsock, &fdsr);
   1529  1.68  dholland 		tv.tv_sec = 1;
   1530  1.68  dholland 		tv.tv_usec = 0;
   1531  1.20       cgd 
   1532  1.68  dholland 		switch (select(width, &fdsr, NULL, NULL, &tv)) {
   1533  1.68  dholland 		case 0:
   1534  1.95  dholland 			/* select timed out - check for timer-based work */
   1535  1.68  dholland 			checkwork();
   1536  1.68  dholland 			break;
   1537  1.68  dholland 		case -1:
   1538  1.72  dholland 			yp_log(LOG_WARNING, "select: %s", strerror(errno));
   1539  1.68  dholland 			break;
   1540  1.68  dholland 		default:
   1541  1.95  dholland 			/* incoming of our own; read it */
   1542  1.68  dholland 			if (FD_ISSET(rpcsock, &fdsr))
   1543  1.68  dholland 				(void)handle_replies();
   1544  1.68  dholland 			if (FD_ISSET(pingsock, &fdsr))
   1545  1.68  dholland 				(void)handle_ping();
   1546  1.95  dholland 
   1547  1.95  dholland 			/* read any incoming packets for the ypbind service */
   1548  1.68  dholland 			svc_getreqset(&fdsr);
   1549  1.95  dholland 
   1550  1.95  dholland 			/*
   1551  1.95  dholland 			 * Only check for timer-based work if
   1552  1.95  dholland 			 * something in the incoming RPC logic said
   1553  1.95  dholland 			 * to. This might be just a hack to avoid
   1554  1.95  dholland 			 * scanning the list unnecessarily, but I
   1555  1.95  dholland 			 * suspect it's also a hack to cover wrong
   1556  1.95  dholland 			 * state logic. - dholland 20140609
   1557  1.95  dholland 			 */
   1558  1.68  dholland 			if (check)
   1559  1.68  dholland 				checkwork();
   1560  1.20       cgd 			break;
   1561  1.68  dholland 		}
   1562  1.20       cgd 
   1563  1.95  dholland 		/*
   1564  1.95  dholland 		 * Defer daemonizing until the default domain binds
   1565  1.95  dholland 		 * successfully. XXX: there seems to be no timeout
   1566  1.95  dholland 		 * on this, which means that if the default domain
   1567  1.95  dholland 		 * is dead upstream boot will hang indefinitely.
   1568  1.95  dholland 		 */
   1569  1.93  dholland 		if (!started && domains->dom_alive) {
   1570  1.93  dholland 			started = 1;
   1571  1.68  dholland #ifdef DEBUG
   1572  1.68  dholland 			if (!debug)
   1573  1.68  dholland #endif
   1574  1.68  dholland 				(void)daemon(0, 0);
   1575  1.68  dholland 			(void)pidfile(NULL);
   1576  1.68  dholland 		}
   1577  1.68  dholland 	}
   1578   1.1   deraadt }
   1579  1.95  dholland 
   1580