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