Home | History | Annotate | Line # | Download | only in rpcbind
check_bound.c revision 1.2
      1  1.2  fvdl /*	$NetBSD: check_bound.c,v 1.2 2000/06/22 08:09:26 fvdl 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.1  fvdl #include "rpcbind.h"
     61  1.1  fvdl 
     62  1.1  fvdl struct fdlist {
     63  1.1  fvdl 	int fd;
     64  1.1  fvdl 	struct netconfig *nconf;
     65  1.1  fvdl 	struct fdlist *next;
     66  1.1  fvdl 	int check_binding;
     67  1.1  fvdl };
     68  1.1  fvdl 
     69  1.1  fvdl static struct fdlist *fdhead;	/* Link list of the check fd's */
     70  1.1  fvdl static struct fdlist *fdtail;
     71  1.1  fvdl static char *nullstring = "";
     72  1.1  fvdl 
     73  1.1  fvdl static bool_t check_bound __P((struct fdlist *, char *uaddr));
     74  1.1  fvdl 
     75  1.1  fvdl /*
     76  1.1  fvdl  * Returns 1 if the given address is bound for the given addr & transport
     77  1.1  fvdl  * For all error cases, we assume that the address is bound
     78  1.1  fvdl  * Returns 0 for success.
     79  1.1  fvdl  */
     80  1.1  fvdl static bool_t
     81  1.1  fvdl check_bound(struct fdlist *fdl, char *uaddr)
     82  1.1  fvdl {
     83  1.1  fvdl 	int fd;
     84  1.1  fvdl 	struct netbuf *na;
     85  1.1  fvdl 	int ans;
     86  1.1  fvdl 
     87  1.1  fvdl 	if (fdl->check_binding == FALSE)
     88  1.1  fvdl 		return (TRUE);
     89  1.1  fvdl 
     90  1.1  fvdl 	na = uaddr2taddr(fdl->nconf, uaddr);
     91  1.1  fvdl 	if (!na)
     92  1.1  fvdl 		return (TRUE); /* punt, should never happen */
     93  1.1  fvdl 
     94  1.1  fvdl 	fd = __rpc_nconf2fd(fdl->nconf);
     95  1.2  fvdl 	if (fd < 0) {
     96  1.2  fvdl 		free(na);
     97  1.1  fvdl 		return (TRUE);
     98  1.2  fvdl 	}
     99  1.1  fvdl 
    100  1.1  fvdl 	ans = bind(fd, (struct sockaddr *)na->buf, na->len);
    101  1.1  fvdl 
    102  1.1  fvdl 	close(fd);
    103  1.2  fvdl 	free(na);
    104  1.1  fvdl 
    105  1.1  fvdl 	return (ans == 0 ? FALSE : TRUE);
    106  1.1  fvdl }
    107  1.1  fvdl 
    108  1.1  fvdl int
    109  1.1  fvdl add_bndlist(struct netconfig *nconf, struct netbuf *baddr)
    110  1.1  fvdl {
    111  1.1  fvdl 	struct fdlist *fdl;
    112  1.1  fvdl 	struct netconfig *newnconf;
    113  1.1  fvdl 
    114  1.1  fvdl 	newnconf = getnetconfigent(nconf->nc_netid);
    115  1.1  fvdl 	if (newnconf == NULL)
    116  1.1  fvdl 		return (-1);
    117  1.1  fvdl 	fdl = (struct fdlist *)malloc((u_int)sizeof (struct fdlist));
    118  1.1  fvdl 	if (fdl == NULL) {
    119  1.1  fvdl 		freenetconfigent(newnconf);
    120  1.1  fvdl 		syslog(LOG_ERR, "no memory!");
    121  1.1  fvdl 		return (-1);
    122  1.1  fvdl 	}
    123  1.1  fvdl 	fdl->nconf = newnconf;
    124  1.1  fvdl 	fdl->next = NULL;
    125  1.1  fvdl 	if (fdhead == NULL) {
    126  1.1  fvdl 		fdhead = fdl;
    127  1.1  fvdl 		fdtail = fdl;
    128  1.1  fvdl 	} else {
    129  1.1  fvdl 		fdtail->next = fdl;
    130  1.1  fvdl 		fdtail = fdl;
    131  1.1  fvdl 	}
    132  1.1  fvdl 	/* XXX no bound checking for now */
    133  1.1  fvdl 	fdl->check_binding = FALSE;
    134  1.1  fvdl 
    135  1.1  fvdl 	return 0;
    136  1.1  fvdl }
    137  1.1  fvdl 
    138  1.1  fvdl bool_t
    139  1.1  fvdl is_bound(char *netid, char *uaddr)
    140  1.1  fvdl {
    141  1.1  fvdl 	struct fdlist *fdl;
    142  1.1  fvdl 
    143  1.1  fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    144  1.1  fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    145  1.1  fvdl 			break;
    146  1.1  fvdl 	if (fdl == NULL)
    147  1.1  fvdl 		return (TRUE);
    148  1.1  fvdl 	return (check_bound(fdl, uaddr));
    149  1.1  fvdl }
    150  1.1  fvdl 
    151  1.1  fvdl /*
    152  1.1  fvdl  * Returns NULL if there was some system error.
    153  1.1  fvdl  * Returns "" if the address was not bound, i.e the server crashed.
    154  1.1  fvdl  * Returns the merged address otherwise.
    155  1.1  fvdl  */
    156  1.1  fvdl char *
    157  1.1  fvdl mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
    158  1.1  fvdl {
    159  1.1  fvdl 	struct fdlist *fdl;
    160  1.2  fvdl 	char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
    161  1.1  fvdl 
    162  1.1  fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    163  1.1  fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    164  1.1  fvdl 			break;
    165  1.1  fvdl 	if (fdl == NULL)
    166  1.1  fvdl 		return (NULL);
    167  1.1  fvdl 	if (check_bound(fdl, uaddr) == FALSE)
    168  1.1  fvdl 		/* that server died */
    169  1.1  fvdl 		return (nullstring);
    170  1.1  fvdl 	/*
    171  1.1  fvdl 	 * If saddr is not NULL, the remote client may have included the
    172  1.1  fvdl 	 * address by which it contacted us.  Use that for the "client" uaddr,
    173  1.1  fvdl 	 * otherwise use the info from the SVCXPRT.
    174  1.1  fvdl 	 */
    175  1.1  fvdl 	if (saddr != NULL) {
    176  1.1  fvdl 		c_uaddr = saddr;
    177  1.1  fvdl 	} else {
    178  1.1  fvdl 		c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
    179  1.1  fvdl 		if (c_uaddr == NULL) {
    180  1.1  fvdl 			syslog(LOG_ERR, "taddr2uaddr failed for %s",
    181  1.1  fvdl 				fdl->nconf->nc_netid);
    182  1.1  fvdl 			return (NULL);
    183  1.1  fvdl 		}
    184  1.2  fvdl 		allocated_uaddr = c_uaddr;
    185  1.1  fvdl 	}
    186  1.1  fvdl 
    187  1.1  fvdl #ifdef ND_DEBUG
    188  1.1  fvdl 	if (debugging) {
    189  1.1  fvdl 		if (saddr == NULL) {
    190  1.1  fvdl 			fprintf(stderr, "mergeaddr: client uaddr = %s\n",
    191  1.1  fvdl 			    c_uaddr);
    192  1.1  fvdl 		} else {
    193  1.1  fvdl 			fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
    194  1.1  fvdl 			    c_uaddr);
    195  1.1  fvdl 		}
    196  1.1  fvdl 	}
    197  1.1  fvdl #endif
    198  1.1  fvdl 	s_uaddr = uaddr;
    199  1.1  fvdl 	/*
    200  1.1  fvdl 	 * This is all we should need for IP 4 and 6
    201  1.1  fvdl 	 */
    202  1.1  fvdl 	m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
    203  1.1  fvdl #ifdef ND_DEBUG
    204  1.1  fvdl 	if (debugging)
    205  1.1  fvdl 		fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
    206  1.1  fvdl 				uaddr, m_uaddr);
    207  1.1  fvdl #endif
    208  1.2  fvdl 	if (allocated_uaddr != NULL)
    209  1.2  fvdl 		free(allocated_uaddr);
    210  1.1  fvdl 	return (m_uaddr);
    211  1.1  fvdl }
    212  1.1  fvdl 
    213  1.1  fvdl /*
    214  1.1  fvdl  * Returns a netconf structure from its internal list.  This
    215  1.1  fvdl  * structure should not be freed.
    216  1.1  fvdl  */
    217  1.1  fvdl struct netconfig *
    218  1.1  fvdl rpcbind_get_conf(char *netid)
    219  1.1  fvdl {
    220  1.1  fvdl 	struct fdlist *fdl;
    221  1.1  fvdl 
    222  1.1  fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    223  1.1  fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    224  1.1  fvdl 			break;
    225  1.1  fvdl 	if (fdl == NULL)
    226  1.1  fvdl 		return (NULL);
    227  1.1  fvdl 	return (fdl->nconf);
    228  1.1  fvdl }
    229