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