xref: /netbsd-src/usr.sbin/rpcbind/check_bound.c (revision aa97815be0953bb5e277da3a05e0905e1a0955f7)
1*aa97815bSkamil /*	$NetBSD: check_bound.c,v 1.9 2020/06/17 00:16:22 kamil Exp $	*/
234b7ffd9Schristos /*	$FreeBSD: head/usr.sbin/rpcbind/check_bound.c 300942 2016-05-29 06:01:18Z ngie $ */
3d687de29Sfvdl 
434b7ffd9Schristos /*-
534b7ffd9Schristos  * Copyright (c) 2009, Sun Microsystems, Inc.
634b7ffd9Schristos  * All rights reserved.
7d687de29Sfvdl  *
834b7ffd9Schristos  * Redistribution and use in source and binary forms, with or without
934b7ffd9Schristos  * modification, are permitted provided that the following conditions are met:
1034b7ffd9Schristos  * - Redistributions of source code must retain the above copyright notice,
1134b7ffd9Schristos  *   this list of conditions and the following disclaimer.
1234b7ffd9Schristos  * - Redistributions in binary form must reproduce the above copyright notice,
1334b7ffd9Schristos  *   this list of conditions and the following disclaimer in the documentation
1434b7ffd9Schristos  *   and/or other materials provided with the distribution.
1534b7ffd9Schristos  * - Neither the name of Sun Microsystems, Inc. nor the names of its
1634b7ffd9Schristos  *   contributors may be used to endorse or promote products derived
1734b7ffd9Schristos  *   from this software without specific prior written permission.
18d687de29Sfvdl  *
1934b7ffd9Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2034b7ffd9Schristos  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2134b7ffd9Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2234b7ffd9Schristos  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2334b7ffd9Schristos  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2434b7ffd9Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2534b7ffd9Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2634b7ffd9Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2734b7ffd9Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2834b7ffd9Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2934b7ffd9Schristos  * POSSIBILITY OF SUCH DAMAGE.
30d687de29Sfvdl  */
31d687de29Sfvdl /*
32d687de29Sfvdl  * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
33d687de29Sfvdl  */
34d687de29Sfvdl 
35d687de29Sfvdl /* #ident	"@(#)check_bound.c	1.15	93/07/05 SMI" */
36d687de29Sfvdl 
37d687de29Sfvdl #if 0
38d687de29Sfvdl #ifndef lint
39d687de29Sfvdl static	char sccsid[] = "@(#)check_bound.c 1.11 89/04/21 Copyr 1989 Sun Micro";
40d687de29Sfvdl #endif
41d687de29Sfvdl #endif
42d687de29Sfvdl 
43d687de29Sfvdl /*
44d687de29Sfvdl  * check_bound.c
45d687de29Sfvdl  * Checks to see whether the program is still bound to the
4634b7ffd9Schristos  * claimed address and returns the universal merged address
47d687de29Sfvdl  *
48d687de29Sfvdl  */
49d687de29Sfvdl 
50d687de29Sfvdl #include <sys/types.h>
51d687de29Sfvdl #include <sys/socket.h>
52d687de29Sfvdl #include <rpc/rpc.h>
53d687de29Sfvdl #include <stdio.h>
54d687de29Sfvdl #include <netconfig.h>
55d687de29Sfvdl #include <syslog.h>
56d687de29Sfvdl #include <string.h>
57d687de29Sfvdl #include <unistd.h>
58d687de29Sfvdl #include <stdlib.h>
59d687de29Sfvdl 
601e8cf510Schristos #ifdef RPCBIND_RUMP
611e8cf510Schristos #include <rump/rump.h>
62*aa97815bSkamil #include <rump/rump_syscallshotgun.h>
631e8cf510Schristos #include <rump/rump_syscalls.h>
641e8cf510Schristos #endif
651e8cf510Schristos 
66d687de29Sfvdl #include "rpcbind.h"
67d687de29Sfvdl 
68d687de29Sfvdl struct fdlist {
69d687de29Sfvdl 	int fd;
70d687de29Sfvdl 	struct netconfig *nconf;
71d687de29Sfvdl 	struct fdlist *next;
72d687de29Sfvdl 	int check_binding;
73d687de29Sfvdl };
74d687de29Sfvdl 
75d687de29Sfvdl static struct fdlist *fdhead;	/* Link list of the check fd's */
76d687de29Sfvdl static struct fdlist *fdtail;
7734b7ffd9Schristos static char nullstring[] = "";
78d687de29Sfvdl 
79de327a01Schristos static bool_t check_bound(struct fdlist *, const char *uaddr);
80d687de29Sfvdl 
81d687de29Sfvdl /*
82d687de29Sfvdl  * Returns 1 if the given address is bound for the given addr & transport
83d687de29Sfvdl  * For all error cases, we assume that the address is bound
84d687de29Sfvdl  * Returns 0 for success.
85d687de29Sfvdl  */
86d687de29Sfvdl static bool_t
check_bound(struct fdlist * fdl,const char * uaddr)87de327a01Schristos check_bound(struct fdlist *fdl, const char *uaddr)
88d687de29Sfvdl {
89d687de29Sfvdl 	int fd;
90d687de29Sfvdl 	struct netbuf *na;
91d687de29Sfvdl 	int ans;
92d687de29Sfvdl 
93d687de29Sfvdl 	if (fdl->check_binding == FALSE)
94d687de29Sfvdl 		return (TRUE);
95d687de29Sfvdl 
96d687de29Sfvdl 	na = uaddr2taddr(fdl->nconf, uaddr);
97d687de29Sfvdl 	if (!na)
98d687de29Sfvdl 		return (TRUE); /* punt, should never happen */
99d687de29Sfvdl 
100d687de29Sfvdl 	fd = __rpc_nconf2fd(fdl->nconf);
101bcec83b9Sfvdl 	if (fd < 0) {
10234b7ffd9Schristos 		free(na->buf);
103bcec83b9Sfvdl 		free(na);
104d687de29Sfvdl 		return (TRUE);
105bcec83b9Sfvdl 	}
106d687de29Sfvdl 
107d687de29Sfvdl 	ans = bind(fd, (struct sockaddr *)na->buf, na->len);
108d687de29Sfvdl 
1091e8cf510Schristos #ifdef RPCBIND_RUMP
1101e8cf510Schristos 	rump_sys_close(fd);
1111e8cf510Schristos #else
112d687de29Sfvdl 	close(fd);
1131e8cf510Schristos #endif
11434b7ffd9Schristos 	free(na->buf);
115bcec83b9Sfvdl 	free(na);
116d687de29Sfvdl 
117d687de29Sfvdl 	return (ans == 0 ? FALSE : TRUE);
118d687de29Sfvdl }
119d687de29Sfvdl 
120d687de29Sfvdl int
add_bndlist(struct netconfig * nconf,struct netbuf * baddr __unused)12134b7ffd9Schristos add_bndlist(struct netconfig *nconf, struct netbuf *baddr __unused)
122d687de29Sfvdl {
123d687de29Sfvdl 	struct fdlist *fdl;
124d687de29Sfvdl 	struct netconfig *newnconf;
125d687de29Sfvdl 
126d687de29Sfvdl 	newnconf = getnetconfigent(nconf->nc_netid);
127d687de29Sfvdl 	if (newnconf == NULL)
128d687de29Sfvdl 		return (-1);
12905d4f66dSchristos 	fdl = malloc(sizeof(*fdl));
130d687de29Sfvdl 	if (fdl == NULL) {
131d687de29Sfvdl 		freenetconfigent(newnconf);
132d687de29Sfvdl 		syslog(LOG_ERR, "no memory!");
133d687de29Sfvdl 		return (-1);
134d687de29Sfvdl 	}
135d687de29Sfvdl 	fdl->nconf = newnconf;
136d687de29Sfvdl 	fdl->next = NULL;
137d687de29Sfvdl 	if (fdhead == NULL) {
138d687de29Sfvdl 		fdhead = fdl;
139d687de29Sfvdl 		fdtail = fdl;
140d687de29Sfvdl 	} else {
141d687de29Sfvdl 		fdtail->next = fdl;
142d687de29Sfvdl 		fdtail = fdl;
143d687de29Sfvdl 	}
144d687de29Sfvdl 	/* XXX no bound checking for now */
145d687de29Sfvdl 	fdl->check_binding = FALSE;
146d687de29Sfvdl 
147d687de29Sfvdl 	return 0;
148d687de29Sfvdl }
149d687de29Sfvdl 
150d687de29Sfvdl bool_t
is_bound(const char * netid,const char * uaddr)151de327a01Schristos is_bound(const char *netid, const char *uaddr)
152d687de29Sfvdl {
153d687de29Sfvdl 	struct fdlist *fdl;
154d687de29Sfvdl 
155d687de29Sfvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
156d687de29Sfvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
157d687de29Sfvdl 			break;
158d687de29Sfvdl 	if (fdl == NULL)
159d687de29Sfvdl 		return (TRUE);
160d687de29Sfvdl 	return (check_bound(fdl, uaddr));
161d687de29Sfvdl }
162d687de29Sfvdl 
163d687de29Sfvdl /*
164d687de29Sfvdl  * Returns NULL if there was some system error.
165d687de29Sfvdl  * Returns "" if the address was not bound, i.e the server crashed.
166d687de29Sfvdl  * Returns the merged address otherwise.
167d687de29Sfvdl  */
168d687de29Sfvdl char *
mergeaddr(SVCXPRT * xprt,char * netid,char * uaddr,char * saddr)169d687de29Sfvdl mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
170d687de29Sfvdl {
171d687de29Sfvdl 	struct fdlist *fdl;
172bcec83b9Sfvdl 	char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
173d687de29Sfvdl 
174d687de29Sfvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
175d687de29Sfvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
176d687de29Sfvdl 			break;
177d687de29Sfvdl 	if (fdl == NULL)
178d687de29Sfvdl 		return (NULL);
179d687de29Sfvdl 	if (check_bound(fdl, uaddr) == FALSE)
180d687de29Sfvdl 		/* that server died */
18134b7ffd9Schristos 		return nullstring;
182d687de29Sfvdl 	/*
18334b7ffd9Schristos 	 * Try to determine the local address on which the client contacted us,
18434b7ffd9Schristos 	 * so we can send a reply from the same address.  If it's unknown, then
18534b7ffd9Schristos 	 * try to determine which address the client used, and pick a nearby
18634b7ffd9Schristos 	 * local address.
18734b7ffd9Schristos 	 *
188d687de29Sfvdl 	 * If saddr is not NULL, the remote client may have included the
189d687de29Sfvdl 	 * address by which it contacted us.  Use that for the "client" uaddr,
190d687de29Sfvdl 	 * otherwise use the info from the SVCXPRT.
191d687de29Sfvdl 	 */
19234b7ffd9Schristos 	if (xprt->xp_rtaddr.buf != NULL) {
19334b7ffd9Schristos 		c_uaddr = taddr2uaddr(fdl->nconf, &xprt->xp_rtaddr);
19434b7ffd9Schristos 		allocated_uaddr = c_uaddr;
19534b7ffd9Schristos 	} else if (saddr != NULL) {
196d687de29Sfvdl 		c_uaddr = saddr;
197d687de29Sfvdl 	} else {
198d687de29Sfvdl 		c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
19934b7ffd9Schristos 		allocated_uaddr = c_uaddr;
20034b7ffd9Schristos 	}
201d687de29Sfvdl 	if (c_uaddr == NULL) {
202d687de29Sfvdl 		syslog(LOG_ERR, "taddr2uaddr failed for %s",
203d687de29Sfvdl 			fdl->nconf->nc_netid);
204d687de29Sfvdl 		return (NULL);
205d687de29Sfvdl 	}
206d687de29Sfvdl 
207b504fee4Sdsl #ifdef RPCBIND_DEBUG
208d687de29Sfvdl 	if (debugging) {
209d687de29Sfvdl 		if (saddr == NULL) {
210d687de29Sfvdl 			fprintf(stderr, "mergeaddr: client uaddr = %s\n",
211d687de29Sfvdl 			    c_uaddr);
212d687de29Sfvdl 		} else {
213d687de29Sfvdl 			fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
214d687de29Sfvdl 			    c_uaddr);
215d687de29Sfvdl 		}
216d687de29Sfvdl 	}
217d687de29Sfvdl #endif
218d687de29Sfvdl 	s_uaddr = uaddr;
219d687de29Sfvdl 	/*
220d687de29Sfvdl 	 * This is all we should need for IP 4 and 6
221d687de29Sfvdl 	 */
222d687de29Sfvdl 	m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
223b504fee4Sdsl #ifdef RPCBIND_DEBUG
224d687de29Sfvdl 	if (debugging)
225d687de29Sfvdl 		fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
226d687de29Sfvdl 				uaddr, m_uaddr);
227d687de29Sfvdl #endif
228bcec83b9Sfvdl 	free(allocated_uaddr);
229d687de29Sfvdl 	return (m_uaddr);
230d687de29Sfvdl }
231d687de29Sfvdl 
232d687de29Sfvdl /*
233d687de29Sfvdl  * Returns a netconf structure from its internal list.  This
234d687de29Sfvdl  * structure should not be freed.
235d687de29Sfvdl  */
236d687de29Sfvdl struct netconfig *
rpcbind_get_conf(const char * netid)237de327a01Schristos rpcbind_get_conf(const char *netid)
238d687de29Sfvdl {
239d687de29Sfvdl 	struct fdlist *fdl;
240d687de29Sfvdl 
241d687de29Sfvdl 	for (fdl = fdhead; fdl; fdl = fdl->next)
242d687de29Sfvdl 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
243d687de29Sfvdl 			break;
244d687de29Sfvdl 	if (fdl == NULL)
245d687de29Sfvdl 		return (NULL);
246d687de29Sfvdl 	return (fdl->nconf);
247d687de29Sfvdl }
248