Home | History | Annotate | Line # | Download | only in rpcbind
check_bound.c revision 1.7.2.1
      1  1.7.2.1  pgoyette /*	$NetBSD: check_bound.c,v 1.7.2.1 2019/01/18 08:51:02 pgoyette Exp $	*/
      2      1.7  christos /*	$FreeBSD: head/usr.sbin/rpcbind/check_bound.c 300942 2016-05-29 06:01:18Z ngie $ */
      3      1.1      fvdl 
      4      1.7  christos /*-
      5      1.7  christos  * Copyright (c) 2009, Sun Microsystems, Inc.
      6      1.7  christos  * All rights reserved.
      7      1.7  christos  *
      8      1.7  christos  * Redistribution and use in source and binary forms, with or without
      9      1.7  christos  * modification, are permitted provided that the following conditions are met:
     10      1.7  christos  * - Redistributions of source code must retain the above copyright notice,
     11      1.7  christos  *   this list of conditions and the following disclaimer.
     12      1.7  christos  * - Redistributions in binary form must reproduce the above copyright notice,
     13      1.7  christos  *   this list of conditions and the following disclaimer in the documentation
     14      1.7  christos  *   and/or other materials provided with the distribution.
     15      1.7  christos  * - Neither the name of Sun Microsystems, Inc. nor the names of its
     16      1.7  christos  *   contributors may be used to endorse or promote products derived
     17      1.7  christos  *   from this software without specific prior written permission.
     18      1.7  christos  *
     19      1.7  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     20      1.7  christos  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21      1.7  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22      1.7  christos  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
     23      1.7  christos  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.7  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.7  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.7  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.7  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.7  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.7  christos  * POSSIBILITY OF SUCH DAMAGE.
     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.7  christos  * claimed address and returns the universal 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.7  christos static char nullstring[] = "";
     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.7  christos 		free(na->buf);
    102      1.2      fvdl 		free(na);
    103      1.1      fvdl 		return (TRUE);
    104      1.2      fvdl 	}
    105      1.1      fvdl 
    106      1.1      fvdl 	ans = bind(fd, (struct sockaddr *)na->buf, na->len);
    107      1.1      fvdl 
    108      1.6  christos #ifdef RPCBIND_RUMP
    109      1.6  christos 	rump_sys_close(fd);
    110      1.6  christos #else
    111      1.1      fvdl 	close(fd);
    112      1.6  christos #endif
    113      1.7  christos 	free(na->buf);
    114      1.2      fvdl 	free(na);
    115      1.1      fvdl 
    116      1.1      fvdl 	return (ans == 0 ? FALSE : TRUE);
    117      1.1      fvdl }
    118      1.1      fvdl 
    119      1.1      fvdl int
    120      1.7  christos add_bndlist(struct netconfig *nconf, struct netbuf *baddr __unused)
    121      1.1      fvdl {
    122      1.1      fvdl 	struct fdlist *fdl;
    123      1.1      fvdl 	struct netconfig *newnconf;
    124      1.1      fvdl 
    125      1.1      fvdl 	newnconf = getnetconfigent(nconf->nc_netid);
    126      1.1      fvdl 	if (newnconf == NULL)
    127      1.1      fvdl 		return (-1);
    128  1.7.2.1  pgoyette 	fdl = malloc(sizeof(*fdl));
    129      1.1      fvdl 	if (fdl == NULL) {
    130      1.1      fvdl 		freenetconfigent(newnconf);
    131      1.1      fvdl 		syslog(LOG_ERR, "no memory!");
    132      1.1      fvdl 		return (-1);
    133      1.1      fvdl 	}
    134      1.1      fvdl 	fdl->nconf = newnconf;
    135      1.1      fvdl 	fdl->next = NULL;
    136      1.1      fvdl 	if (fdhead == NULL) {
    137      1.1      fvdl 		fdhead = fdl;
    138      1.1      fvdl 		fdtail = fdl;
    139      1.1      fvdl 	} else {
    140      1.1      fvdl 		fdtail->next = fdl;
    141      1.1      fvdl 		fdtail = fdl;
    142      1.1      fvdl 	}
    143      1.1      fvdl 	/* XXX no bound checking for now */
    144      1.1      fvdl 	fdl->check_binding = FALSE;
    145      1.1      fvdl 
    146      1.1      fvdl 	return 0;
    147      1.1      fvdl }
    148      1.1      fvdl 
    149      1.1      fvdl bool_t
    150      1.4  christos is_bound(const char *netid, const char *uaddr)
    151      1.1      fvdl {
    152      1.1      fvdl 	struct fdlist *fdl;
    153      1.1      fvdl 
    154      1.1      fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    155      1.1      fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    156      1.1      fvdl 			break;
    157      1.1      fvdl 	if (fdl == NULL)
    158      1.1      fvdl 		return (TRUE);
    159      1.1      fvdl 	return (check_bound(fdl, uaddr));
    160      1.1      fvdl }
    161      1.1      fvdl 
    162      1.1      fvdl /*
    163      1.1      fvdl  * Returns NULL if there was some system error.
    164      1.1      fvdl  * Returns "" if the address was not bound, i.e the server crashed.
    165      1.1      fvdl  * Returns the merged address otherwise.
    166      1.1      fvdl  */
    167      1.1      fvdl char *
    168      1.1      fvdl mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
    169      1.1      fvdl {
    170      1.1      fvdl 	struct fdlist *fdl;
    171      1.2      fvdl 	char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
    172      1.1      fvdl 
    173      1.1      fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    174      1.1      fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    175      1.1      fvdl 			break;
    176      1.1      fvdl 	if (fdl == NULL)
    177      1.1      fvdl 		return (NULL);
    178      1.1      fvdl 	if (check_bound(fdl, uaddr) == FALSE)
    179      1.1      fvdl 		/* that server died */
    180      1.7  christos 		return nullstring;
    181      1.1      fvdl 	/*
    182      1.7  christos 	 * Try to determine the local address on which the client contacted us,
    183      1.7  christos 	 * so we can send a reply from the same address.  If it's unknown, then
    184      1.7  christos 	 * try to determine which address the client used, and pick a nearby
    185      1.7  christos 	 * local address.
    186      1.7  christos 	 *
    187      1.1      fvdl 	 * If saddr is not NULL, the remote client may have included the
    188      1.1      fvdl 	 * address by which it contacted us.  Use that for the "client" uaddr,
    189      1.1      fvdl 	 * otherwise use the info from the SVCXPRT.
    190      1.1      fvdl 	 */
    191      1.7  christos 	if (xprt->xp_rtaddr.buf != NULL) {
    192      1.7  christos 		c_uaddr = taddr2uaddr(fdl->nconf, &xprt->xp_rtaddr);
    193      1.7  christos 		allocated_uaddr = c_uaddr;
    194      1.7  christos 	} else if (saddr != NULL) {
    195      1.1      fvdl 		c_uaddr = saddr;
    196      1.1      fvdl 	} else {
    197      1.1      fvdl 		c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
    198      1.2      fvdl 		allocated_uaddr = c_uaddr;
    199      1.1      fvdl 	}
    200      1.7  christos 	if (c_uaddr == NULL) {
    201      1.7  christos 		syslog(LOG_ERR, "taddr2uaddr failed for %s",
    202      1.7  christos 			fdl->nconf->nc_netid);
    203      1.7  christos 		return (NULL);
    204      1.7  christos 	}
    205      1.1      fvdl 
    206      1.5       dsl #ifdef RPCBIND_DEBUG
    207      1.1      fvdl 	if (debugging) {
    208      1.1      fvdl 		if (saddr == NULL) {
    209      1.1      fvdl 			fprintf(stderr, "mergeaddr: client uaddr = %s\n",
    210      1.1      fvdl 			    c_uaddr);
    211      1.1      fvdl 		} else {
    212      1.1      fvdl 			fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
    213      1.1      fvdl 			    c_uaddr);
    214      1.1      fvdl 		}
    215      1.1      fvdl 	}
    216      1.1      fvdl #endif
    217      1.1      fvdl 	s_uaddr = uaddr;
    218      1.1      fvdl 	/*
    219      1.1      fvdl 	 * This is all we should need for IP 4 and 6
    220      1.1      fvdl 	 */
    221      1.1      fvdl 	m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
    222      1.5       dsl #ifdef RPCBIND_DEBUG
    223      1.1      fvdl 	if (debugging)
    224      1.1      fvdl 		fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
    225      1.1      fvdl 				uaddr, m_uaddr);
    226      1.1      fvdl #endif
    227      1.7  christos 	free(allocated_uaddr);
    228      1.1      fvdl 	return (m_uaddr);
    229      1.1      fvdl }
    230      1.1      fvdl 
    231      1.1      fvdl /*
    232      1.1      fvdl  * Returns a netconf structure from its internal list.  This
    233      1.1      fvdl  * structure should not be freed.
    234      1.1      fvdl  */
    235      1.1      fvdl struct netconfig *
    236      1.4  christos rpcbind_get_conf(const char *netid)
    237      1.1      fvdl {
    238      1.1      fvdl 	struct fdlist *fdl;
    239      1.1      fvdl 
    240      1.1      fvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
    241      1.1      fvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
    242      1.1      fvdl 			break;
    243      1.1      fvdl 	if (fdl == NULL)
    244      1.1      fvdl 		return (NULL);
    245      1.1      fvdl 	return (fdl->nconf);
    246      1.1      fvdl }
    247