Home | History | Annotate | Line # | Download | only in rpcbind
check_bound.c revision 1.6
      1  1.6  christos /*	$NetBSD: check_bound.c,v 1.6 2015/11/08 16:36:28 christos Exp $	*/
      2  1.1      fvdl 
      3  1.1      fvdl /*
      4  1.1      fvdl  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  1.1      fvdl  * unrestricted use provided that this legend is included on all tape
      6  1.1      fvdl  * media and as a part of the software program in whole or part.  Users
      7  1.1      fvdl  * may copy or modify Sun RPC without charge, but are not authorized
      8  1.1      fvdl  * to license or distribute it to anyone else except as part of a product or
      9  1.1      fvdl  * program developed by the user.
     10  1.1      fvdl  *
     11  1.1      fvdl  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  1.1      fvdl  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  1.1      fvdl  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  1.1      fvdl  *
     15  1.1      fvdl  * Sun RPC is provided with no support and without any obligation on the
     16  1.1      fvdl  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  1.1      fvdl  * modification or enhancement.
     18  1.1      fvdl  *
     19  1.1      fvdl  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  1.1      fvdl  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  1.1      fvdl  * OR ANY PART THEREOF.
     22  1.1      fvdl  *
     23  1.1      fvdl  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  1.1      fvdl  * or profits or other special, indirect and consequential damages, even if
     25  1.1      fvdl  * Sun has been advised of the possibility of such damages.
     26  1.1      fvdl  *
     27  1.1      fvdl  * Sun Microsystems, Inc.
     28  1.1      fvdl  * 2550 Garcia Avenue
     29  1.1      fvdl  * Mountain View, California  94043
     30  1.1      fvdl  */
     31  1.1      fvdl /*
     32  1.1      fvdl  * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
     33  1.1      fvdl  */
     34  1.1      fvdl 
     35  1.1      fvdl /* #ident	"@(#)check_bound.c	1.15	93/07/05 SMI" */
     36  1.1      fvdl 
     37  1.1      fvdl #if 0
     38  1.1      fvdl #ifndef lint
     39  1.1      fvdl static	char sccsid[] = "@(#)check_bound.c 1.11 89/04/21 Copyr 1989 Sun Micro";
     40  1.1      fvdl #endif
     41  1.1      fvdl #endif
     42  1.1      fvdl 
     43  1.1      fvdl /*
     44  1.1      fvdl  * check_bound.c
     45  1.1      fvdl  * Checks to see whether the program is still bound to the
     46  1.1      fvdl  * claimed address and returns the univeral merged address
     47  1.1      fvdl  *
     48  1.1      fvdl  */
     49  1.1      fvdl 
     50  1.1      fvdl #include <sys/types.h>
     51  1.1      fvdl #include <sys/socket.h>
     52  1.1      fvdl #include <rpc/rpc.h>
     53  1.1      fvdl #include <stdio.h>
     54  1.1      fvdl #include <netconfig.h>
     55  1.1      fvdl #include <syslog.h>
     56  1.1      fvdl #include <string.h>
     57  1.1      fvdl #include <unistd.h>
     58  1.1      fvdl #include <stdlib.h>
     59  1.1      fvdl 
     60  1.6  christos #ifdef RPCBIND_RUMP
     61  1.6  christos #include <rump/rump.h>
     62  1.6  christos #include <rump/rump_syscalls.h>
     63  1.6  christos #endif
     64  1.6  christos 
     65  1.1      fvdl #include "rpcbind.h"
     66  1.1      fvdl 
     67  1.1      fvdl struct fdlist {
     68  1.1      fvdl 	int fd;
     69  1.1      fvdl 	struct netconfig *nconf;
     70  1.1      fvdl 	struct fdlist *next;
     71  1.1      fvdl 	int check_binding;
     72  1.1      fvdl };
     73  1.1      fvdl 
     74  1.1      fvdl static struct fdlist *fdhead;	/* Link list of the check fd's */
     75  1.1      fvdl static struct fdlist *fdtail;
     76  1.4  christos static const char emptystring[] = "";
     77  1.1      fvdl 
     78  1.4  christos static bool_t check_bound(struct fdlist *, const char *uaddr);
     79  1.1      fvdl 
     80  1.1      fvdl /*
     81  1.1      fvdl  * Returns 1 if the given address is bound for the given addr & transport
     82  1.1      fvdl  * For all error cases, we assume that the address is bound
     83  1.1      fvdl  * Returns 0 for success.
     84  1.1      fvdl  */
     85  1.1      fvdl static bool_t
     86  1.4  christos check_bound(struct fdlist *fdl, const char *uaddr)
     87  1.1      fvdl {
     88  1.1      fvdl 	int fd;
     89  1.1      fvdl 	struct netbuf *na;
     90  1.1      fvdl 	int ans;
     91  1.1      fvdl 
     92  1.1      fvdl 	if (fdl->check_binding == FALSE)
     93  1.1      fvdl 		return (TRUE);
     94  1.1      fvdl 
     95  1.1      fvdl 	na = uaddr2taddr(fdl->nconf, uaddr);
     96  1.1      fvdl 	if (!na)
     97  1.1      fvdl 		return (TRUE); /* punt, should never happen */
     98  1.1      fvdl 
     99  1.1      fvdl 	fd = __rpc_nconf2fd(fdl->nconf);
    100  1.2      fvdl 	if (fd < 0) {
    101  1.2      fvdl 		free(na);
    102  1.1      fvdl 		return (TRUE);
    103  1.2      fvdl 	}
    104  1.1      fvdl 
    105  1.1      fvdl 	ans = bind(fd, (struct sockaddr *)na->buf, na->len);
    106  1.1      fvdl 
    107  1.6  christos #ifdef RPCBIND_RUMP
    108  1.6  christos 	rump_sys_close(fd);
    109  1.6  christos #else
    110  1.1      fvdl 	close(fd);
    111  1.6  christos #endif
    112  1.2      fvdl 	free(na);
    113  1.1      fvdl 
    114  1.1      fvdl 	return (ans == 0 ? FALSE : TRUE);
    115  1.1      fvdl }
    116  1.1      fvdl 
    117  1.1      fvdl int
    118  1.1      fvdl add_bndlist(struct netconfig *nconf, struct netbuf *baddr)
    119  1.1      fvdl {
    120  1.1      fvdl 	struct fdlist *fdl;
    121  1.1      fvdl 	struct netconfig *newnconf;
    122  1.1      fvdl 
    123  1.1      fvdl 	newnconf = getnetconfigent(nconf->nc_netid);
    124  1.1      fvdl 	if (newnconf == NULL)
    125  1.1      fvdl 		return (-1);
    126  1.1      fvdl 	fdl = (struct fdlist *)malloc((u_int)sizeof (struct fdlist));
    127  1.1      fvdl 	if (fdl == NULL) {
    128  1.1      fvdl 		freenetconfigent(newnconf);
    129  1.1      fvdl 		syslog(LOG_ERR, "no memory!");
    130  1.1      fvdl 		return (-1);
    131  1.1      fvdl 	}
    132  1.1      fvdl 	fdl->nconf = newnconf;
    133  1.1      fvdl 	fdl->next = NULL;
    134  1.1      fvdl 	if (fdhead == NULL) {
    135  1.1      fvdl 		fdhead = fdl;
    136  1.1      fvdl 		fdtail = fdl;
    137  1.1      fvdl 	} else {
    138  1.1      fvdl 		fdtail->next = fdl;
    139  1.1      fvdl 		fdtail = fdl;
    140  1.1      fvdl 	}
    141  1.1      fvdl 	/* XXX no bound checking for now */
    142  1.1      fvdl 	fdl->check_binding = FALSE;
    143  1.1      fvdl 
    144  1.1      fvdl 	return 0;
    145  1.1      fvdl }
    146  1.1      fvdl 
    147  1.1      fvdl bool_t
    148  1.4  christos is_bound(const char *netid, const char *uaddr)
    149  1.1      fvdl {
    150  1.1      fvdl 	struct fdlist *fdl;
    151  1.1      fvdl 
    152  1.1      fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    153  1.1      fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    154  1.1      fvdl 			break;
    155  1.1      fvdl 	if (fdl == NULL)
    156  1.1      fvdl 		return (TRUE);
    157  1.1      fvdl 	return (check_bound(fdl, uaddr));
    158  1.1      fvdl }
    159  1.1      fvdl 
    160  1.1      fvdl /*
    161  1.1      fvdl  * Returns NULL if there was some system error.
    162  1.1      fvdl  * Returns "" if the address was not bound, i.e the server crashed.
    163  1.1      fvdl  * Returns the merged address otherwise.
    164  1.1      fvdl  */
    165  1.1      fvdl char *
    166  1.1      fvdl mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
    167  1.1      fvdl {
    168  1.1      fvdl 	struct fdlist *fdl;
    169  1.2      fvdl 	char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
    170  1.1      fvdl 
    171  1.1      fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    172  1.1      fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    173  1.1      fvdl 			break;
    174  1.1      fvdl 	if (fdl == NULL)
    175  1.1      fvdl 		return (NULL);
    176  1.1      fvdl 	if (check_bound(fdl, uaddr) == FALSE)
    177  1.1      fvdl 		/* that server died */
    178  1.4  christos 		return strdup(emptystring);
    179  1.1      fvdl 	/*
    180  1.1      fvdl 	 * If saddr is not NULL, the remote client may have included the
    181  1.1      fvdl 	 * address by which it contacted us.  Use that for the "client" uaddr,
    182  1.1      fvdl 	 * otherwise use the info from the SVCXPRT.
    183  1.1      fvdl 	 */
    184  1.1      fvdl 	if (saddr != NULL) {
    185  1.1      fvdl 		c_uaddr = saddr;
    186  1.1      fvdl 	} else {
    187  1.1      fvdl 		c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
    188  1.1      fvdl 		if (c_uaddr == NULL) {
    189  1.1      fvdl 			syslog(LOG_ERR, "taddr2uaddr failed for %s",
    190  1.1      fvdl 				fdl->nconf->nc_netid);
    191  1.1      fvdl 			return (NULL);
    192  1.1      fvdl 		}
    193  1.2      fvdl 		allocated_uaddr = c_uaddr;
    194  1.1      fvdl 	}
    195  1.1      fvdl 
    196  1.5       dsl #ifdef RPCBIND_DEBUG
    197  1.1      fvdl 	if (debugging) {
    198  1.1      fvdl 		if (saddr == NULL) {
    199  1.1      fvdl 			fprintf(stderr, "mergeaddr: client uaddr = %s\n",
    200  1.1      fvdl 			    c_uaddr);
    201  1.1      fvdl 		} else {
    202  1.1      fvdl 			fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
    203  1.1      fvdl 			    c_uaddr);
    204  1.1      fvdl 		}
    205  1.1      fvdl 	}
    206  1.1      fvdl #endif
    207  1.1      fvdl 	s_uaddr = uaddr;
    208  1.1      fvdl 	/*
    209  1.1      fvdl 	 * This is all we should need for IP 4 and 6
    210  1.1      fvdl 	 */
    211  1.1      fvdl 	m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
    212  1.5       dsl #ifdef RPCBIND_DEBUG
    213  1.1      fvdl 	if (debugging)
    214  1.1      fvdl 		fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
    215  1.1      fvdl 				uaddr, m_uaddr);
    216  1.1      fvdl #endif
    217  1.2      fvdl 	if (allocated_uaddr != NULL)
    218  1.2      fvdl 		free(allocated_uaddr);
    219  1.1      fvdl 	return (m_uaddr);
    220  1.1      fvdl }
    221  1.1      fvdl 
    222  1.1      fvdl /*
    223  1.1      fvdl  * Returns a netconf structure from its internal list.  This
    224  1.1      fvdl  * structure should not be freed.
    225  1.1      fvdl  */
    226  1.1      fvdl struct netconfig *
    227  1.4  christos rpcbind_get_conf(const char *netid)
    228  1.1      fvdl {
    229  1.1      fvdl 	struct fdlist *fdl;
    230  1.1      fvdl 
    231  1.1      fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    232  1.1      fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    233  1.1      fvdl 			break;
    234  1.1      fvdl 	if (fdl == NULL)
    235  1.1      fvdl 		return (NULL);
    236  1.1      fvdl 	return (fdl->nconf);
    237  1.1      fvdl }
    238