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