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