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