xref: /dflybsd-src/usr.sbin/rpcbind/check_bound.c (revision bd91f5c31d05935d34ffa5befc63a95350bbf000)
1ce0e08e2SPeter Avalos /*
2*bd91f5c3SJustin C. Sherrill  * Copyright (c) 2009, Sun Microsystems, Inc.
3*bd91f5c3SJustin C. Sherrill  * All rights reserved.
4ce0e08e2SPeter Avalos  *
5*bd91f5c3SJustin C. Sherrill  * Redistribution and use in source and binary forms, with or without
6*bd91f5c3SJustin C. Sherrill  * modification, are permitted provided that the following conditions are met:
7*bd91f5c3SJustin C. Sherrill  * - Redistributions of source code must retain the above copyright notice,
8*bd91f5c3SJustin C. Sherrill  *   this list of conditions and the following disclaimer.
9*bd91f5c3SJustin C. Sherrill  * - Redistributions in binary form must reproduce the above copyright notice,
10*bd91f5c3SJustin C. Sherrill  *   this list of conditions and the following disclaimer in the documentation
11*bd91f5c3SJustin C. Sherrill  *   and/or other materials provided with the distribution.
12*bd91f5c3SJustin C. Sherrill  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*bd91f5c3SJustin C. Sherrill  *   contributors may be used to endorse or promote products derived
14*bd91f5c3SJustin C. Sherrill  *   from this software without specific prior written permission.
15ce0e08e2SPeter Avalos  *
16*bd91f5c3SJustin C. Sherrill  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*bd91f5c3SJustin C. Sherrill  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*bd91f5c3SJustin C. Sherrill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*bd91f5c3SJustin C. Sherrill  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*bd91f5c3SJustin C. Sherrill  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*bd91f5c3SJustin C. Sherrill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*bd91f5c3SJustin C. Sherrill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*bd91f5c3SJustin C. Sherrill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*bd91f5c3SJustin C. Sherrill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*bd91f5c3SJustin C. Sherrill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*bd91f5c3SJustin C. Sherrill  * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos  *
28ce0e08e2SPeter Avalos  * @(#)check_bound.c	1.15	93/07/05 SMI; 1.11 89/04/21 Copyr 1989 Sun Micro
29ce0e08e2SPeter Avalos  * $NetBSD: check_bound.c,v 1.2 2000/06/22 08:09:26 fvdl Exp $
30ce0e08e2SPeter Avalos  * $FreeBSD: src/usr.sbin/rpcbind/check_bound.c,v 1.5 2007/11/07 10:53:39 kevlo Exp $
31ce0e08e2SPeter Avalos  */
32ce0e08e2SPeter Avalos /*
33ce0e08e2SPeter Avalos  * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
34ce0e08e2SPeter Avalos  */
35ce0e08e2SPeter Avalos 
36ce0e08e2SPeter Avalos /*
37ce0e08e2SPeter Avalos  * check_bound.c
38ce0e08e2SPeter Avalos  * Checks to see whether the program is still bound to the
39ce0e08e2SPeter Avalos  * claimed address and returns the univeral merged address
40ce0e08e2SPeter Avalos  *
41ce0e08e2SPeter Avalos  */
42ce0e08e2SPeter Avalos 
43ce0e08e2SPeter Avalos #include <sys/types.h>
44ce0e08e2SPeter Avalos #include <sys/socket.h>
45ce0e08e2SPeter Avalos #include <rpc/rpc.h>
46ce0e08e2SPeter Avalos #include <stdio.h>
47ce0e08e2SPeter Avalos #include <netconfig.h>
48ce0e08e2SPeter Avalos #include <syslog.h>
49ce0e08e2SPeter Avalos #include <string.h>
50ce0e08e2SPeter Avalos #include <unistd.h>
51ce0e08e2SPeter Avalos #include <stdlib.h>
52ce0e08e2SPeter Avalos 
53ce0e08e2SPeter Avalos #include "rpcbind.h"
54ce0e08e2SPeter Avalos 
55ce0e08e2SPeter Avalos struct fdlist {
56ce0e08e2SPeter Avalos 	int fd;
57ce0e08e2SPeter Avalos 	struct netconfig *nconf;
58ce0e08e2SPeter Avalos 	struct fdlist *next;
59ce0e08e2SPeter Avalos 	int check_binding;
60ce0e08e2SPeter Avalos };
61ce0e08e2SPeter Avalos 
62ce0e08e2SPeter Avalos static struct fdlist *fdhead;	/* Link list of the check fd's */
63ce0e08e2SPeter Avalos static struct fdlist *fdtail;
64ce0e08e2SPeter Avalos static char *nullstring = "";
65ce0e08e2SPeter Avalos 
66ce0e08e2SPeter Avalos static bool_t	check_bound(struct fdlist *, char *uaddr);
67ce0e08e2SPeter Avalos 
68ce0e08e2SPeter Avalos /*
69ce0e08e2SPeter Avalos  * Returns 1 if the given address is bound for the given addr & transport
70ce0e08e2SPeter Avalos  * For all error cases, we assume that the address is bound
71ce0e08e2SPeter Avalos  * Returns 0 for success.
72ce0e08e2SPeter Avalos  */
73ce0e08e2SPeter Avalos static bool_t
check_bound(struct fdlist * fdl,char * uaddr)74ce0e08e2SPeter Avalos check_bound(struct fdlist *fdl, char *uaddr)
75ce0e08e2SPeter Avalos {
76ce0e08e2SPeter Avalos 	int fd;
77ce0e08e2SPeter Avalos 	struct netbuf *na;
78ce0e08e2SPeter Avalos 	int ans;
79ce0e08e2SPeter Avalos 
80ce0e08e2SPeter Avalos 	if (fdl->check_binding == FALSE)
81ce0e08e2SPeter Avalos 		return (TRUE);
82ce0e08e2SPeter Avalos 
83ce0e08e2SPeter Avalos 	na = uaddr2taddr(fdl->nconf, uaddr);
84ce0e08e2SPeter Avalos 	if (!na)
85ce0e08e2SPeter Avalos 		return (TRUE); /* punt, should never happen */
86ce0e08e2SPeter Avalos 
87ce0e08e2SPeter Avalos 	fd = __rpc_nconf2fd(fdl->nconf);
88ce0e08e2SPeter Avalos 	if (fd < 0) {
89ce0e08e2SPeter Avalos 		free(na->buf);
90ce0e08e2SPeter Avalos 		free(na);
91ce0e08e2SPeter Avalos 		return (TRUE);
92ce0e08e2SPeter Avalos 	}
93ce0e08e2SPeter Avalos 
94ce0e08e2SPeter Avalos 	ans = bind(fd, (struct sockaddr *)na->buf, na->len);
95ce0e08e2SPeter Avalos 
96ce0e08e2SPeter Avalos 	close(fd);
97ce0e08e2SPeter Avalos 	free(na->buf);
98ce0e08e2SPeter Avalos 	free(na);
99ce0e08e2SPeter Avalos 
100ce0e08e2SPeter Avalos 	return (ans == 0 ? FALSE : TRUE);
101ce0e08e2SPeter Avalos }
102ce0e08e2SPeter Avalos 
103ce0e08e2SPeter Avalos int
add_bndlist(struct netconfig * nconf,struct netbuf * baddr __unused)104ce0e08e2SPeter Avalos add_bndlist(struct netconfig *nconf, struct netbuf *baddr __unused)
105ce0e08e2SPeter Avalos {
106ce0e08e2SPeter Avalos 	struct fdlist *fdl;
107ce0e08e2SPeter Avalos 	struct netconfig *newnconf;
108ce0e08e2SPeter Avalos 
109ce0e08e2SPeter Avalos 	newnconf = getnetconfigent(nconf->nc_netid);
110ce0e08e2SPeter Avalos 	if (newnconf == NULL)
111ce0e08e2SPeter Avalos 		return (-1);
112ce0e08e2SPeter Avalos 	fdl = malloc(sizeof (struct fdlist));
113ce0e08e2SPeter Avalos 	if (fdl == NULL) {
114ce0e08e2SPeter Avalos 		freenetconfigent(newnconf);
115ce0e08e2SPeter Avalos 		syslog(LOG_ERR, "no memory!");
116ce0e08e2SPeter Avalos 		return (-1);
117ce0e08e2SPeter Avalos 	}
118ce0e08e2SPeter Avalos 	fdl->nconf = newnconf;
119ce0e08e2SPeter Avalos 	fdl->next = NULL;
120ce0e08e2SPeter Avalos 	if (fdhead == NULL) {
121ce0e08e2SPeter Avalos 		fdhead = fdl;
122ce0e08e2SPeter Avalos 		fdtail = fdl;
123ce0e08e2SPeter Avalos 	} else {
124ce0e08e2SPeter Avalos 		fdtail->next = fdl;
125ce0e08e2SPeter Avalos 		fdtail = fdl;
126ce0e08e2SPeter Avalos 	}
127ce0e08e2SPeter Avalos 	/* XXX no bound checking for now */
128ce0e08e2SPeter Avalos 	fdl->check_binding = FALSE;
129ce0e08e2SPeter Avalos 
130ce0e08e2SPeter Avalos 	return 0;
131ce0e08e2SPeter Avalos }
132ce0e08e2SPeter Avalos 
133ce0e08e2SPeter Avalos bool_t
is_bound(char * netid,char * uaddr)134ce0e08e2SPeter Avalos is_bound(char *netid, char *uaddr)
135ce0e08e2SPeter Avalos {
136ce0e08e2SPeter Avalos 	struct fdlist *fdl;
137ce0e08e2SPeter Avalos 
138ce0e08e2SPeter Avalos 	for (fdl = fdhead; fdl; fdl = fdl->next)
139ce0e08e2SPeter Avalos 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
140ce0e08e2SPeter Avalos 			break;
141ce0e08e2SPeter Avalos 	if (fdl == NULL)
142ce0e08e2SPeter Avalos 		return (TRUE);
143ce0e08e2SPeter Avalos 	return (check_bound(fdl, uaddr));
144ce0e08e2SPeter Avalos }
145ce0e08e2SPeter Avalos 
146ce0e08e2SPeter Avalos /*
147ce0e08e2SPeter Avalos  * Returns NULL if there was some system error.
148ce0e08e2SPeter Avalos  * Returns "" if the address was not bound, i.e the server crashed.
149ce0e08e2SPeter Avalos  * Returns the merged address otherwise.
150ce0e08e2SPeter Avalos  */
151ce0e08e2SPeter Avalos char *
mergeaddr(SVCXPRT * xprt,char * netid,char * uaddr,char * saddr)152ce0e08e2SPeter Avalos mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
153ce0e08e2SPeter Avalos {
154ce0e08e2SPeter Avalos 	struct fdlist *fdl;
155ce0e08e2SPeter Avalos 	char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
156ce0e08e2SPeter Avalos 
157ce0e08e2SPeter Avalos 	for (fdl = fdhead; fdl; fdl = fdl->next)
158ce0e08e2SPeter Avalos 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
159ce0e08e2SPeter Avalos 			break;
160ce0e08e2SPeter Avalos 	if (fdl == NULL)
161ce0e08e2SPeter Avalos 		return (NULL);
162ce0e08e2SPeter Avalos 	if (check_bound(fdl, uaddr) == FALSE)
163ce0e08e2SPeter Avalos 		/* that server died */
164ce0e08e2SPeter Avalos 		return (nullstring);
165ce0e08e2SPeter Avalos 	/*
166ce0e08e2SPeter Avalos 	 * If saddr is not NULL, the remote client may have included the
167ce0e08e2SPeter Avalos 	 * address by which it contacted us.  Use that for the "client" uaddr,
168ce0e08e2SPeter Avalos 	 * otherwise use the info from the SVCXPRT.
169ce0e08e2SPeter Avalos 	 */
170ce0e08e2SPeter Avalos 	if (saddr != NULL) {
171ce0e08e2SPeter Avalos 		c_uaddr = saddr;
172ce0e08e2SPeter Avalos 	} else {
173ce0e08e2SPeter Avalos 		c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
174ce0e08e2SPeter Avalos 		if (c_uaddr == NULL) {
175ce0e08e2SPeter Avalos 			syslog(LOG_ERR, "taddr2uaddr failed for %s",
176ce0e08e2SPeter Avalos 				fdl->nconf->nc_netid);
177ce0e08e2SPeter Avalos 			return (NULL);
178ce0e08e2SPeter Avalos 		}
179ce0e08e2SPeter Avalos 		allocated_uaddr = c_uaddr;
180ce0e08e2SPeter Avalos 	}
181ce0e08e2SPeter Avalos 
182ce0e08e2SPeter Avalos #ifdef ND_DEBUG
183ce0e08e2SPeter Avalos 	if (debugging) {
184ce0e08e2SPeter Avalos 		if (saddr == NULL) {
185ce0e08e2SPeter Avalos 			fprintf(stderr, "mergeaddr: client uaddr = %s\n",
186ce0e08e2SPeter Avalos 			    c_uaddr);
187ce0e08e2SPeter Avalos 		} else {
188ce0e08e2SPeter Avalos 			fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
189ce0e08e2SPeter Avalos 			    c_uaddr);
190ce0e08e2SPeter Avalos 		}
191ce0e08e2SPeter Avalos 	}
192ce0e08e2SPeter Avalos #endif
193ce0e08e2SPeter Avalos 	s_uaddr = uaddr;
194ce0e08e2SPeter Avalos 	/*
195ce0e08e2SPeter Avalos 	 * This is all we should need for IP 4 and 6
196ce0e08e2SPeter Avalos 	 */
197ce0e08e2SPeter Avalos 	m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
198ce0e08e2SPeter Avalos #ifdef ND_DEBUG
199ce0e08e2SPeter Avalos 	if (debugging)
200ce0e08e2SPeter Avalos 		fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
201ce0e08e2SPeter Avalos 				uaddr, m_uaddr);
202ce0e08e2SPeter Avalos #endif
203ce0e08e2SPeter Avalos 	if (allocated_uaddr != NULL)
204ce0e08e2SPeter Avalos 		free(allocated_uaddr);
205ce0e08e2SPeter Avalos 	return (m_uaddr);
206ce0e08e2SPeter Avalos }
207ce0e08e2SPeter Avalos 
208ce0e08e2SPeter Avalos /*
209ce0e08e2SPeter Avalos  * Returns a netconf structure from its internal list.  This
210ce0e08e2SPeter Avalos  * structure should not be freed.
211ce0e08e2SPeter Avalos  */
212ce0e08e2SPeter Avalos struct netconfig *
rpcbind_get_conf(char * netid)213ce0e08e2SPeter Avalos rpcbind_get_conf(char *netid)
214ce0e08e2SPeter Avalos {
215ce0e08e2SPeter Avalos 	struct fdlist *fdl;
216ce0e08e2SPeter Avalos 
217ce0e08e2SPeter Avalos 	for (fdl = fdhead; fdl; fdl = fdl->next)
218ce0e08e2SPeter Avalos 		if (strcmp(fdl->nconf->nc_netid, netid) == 0)
219ce0e08e2SPeter Avalos 			break;
220ce0e08e2SPeter Avalos 	if (fdl == NULL)
221ce0e08e2SPeter Avalos 		return (NULL);
222ce0e08e2SPeter Avalos 	return (fdl->nconf);
223ce0e08e2SPeter Avalos }
224