]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/fs/nfs/nfsservice/rpcbind/check_bound.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / fs / nfs / nfsservice / rpcbind / check_bound.c
1 /*      $NetBSD: check_bound.c,v 1.1 2010/07/26 15:53:00 pooka Exp $    */
2
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  * 
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  * 
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  * 
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  * 
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  * 
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
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 univeral 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 #include <rump/rump.h>
61 #include <rump/rump_syscalls.h>
62
63 #include "rpcbind.h"
64
65 struct fdlist {
66         int fd;
67         struct netconfig *nconf;
68         struct fdlist *next;
69         int check_binding;
70 };
71
72 static struct fdlist *fdhead;   /* Link list of the check fd's */
73 static struct fdlist *fdtail;
74 static const char emptystring[] = "";
75
76 static bool_t check_bound(struct fdlist *, const char *uaddr);
77
78 /*
79  * Returns 1 if the given address is bound for the given addr & transport
80  * For all error cases, we assume that the address is bound
81  * Returns 0 for success.
82  */
83 static bool_t
84 check_bound(struct fdlist *fdl, const char *uaddr)
85 {
86         int fd;
87         struct netbuf *na;
88         int ans;
89
90         if (fdl->check_binding == FALSE)
91                 return (TRUE);
92
93         na = uaddr2taddr(fdl->nconf, uaddr);
94         if (!na)
95                 return (TRUE); /* punt, should never happen */
96
97         fd = __rpc_nconf2fd(fdl->nconf);
98         if (fd < 0) {
99                 free(na);
100                 return (TRUE);
101         }
102
103         ans = bind(fd, (struct sockaddr *)na->buf, na->len);
104
105         rump_sys_close(fd);
106         free(na);
107
108         return (ans == 0 ? FALSE : TRUE);
109 }
110
111 int
112 add_bndlist(struct netconfig *nconf, struct netbuf *baddr)
113 {
114         struct fdlist *fdl;
115         struct netconfig *newnconf;
116
117         newnconf = getnetconfigent(nconf->nc_netid);
118         if (newnconf == NULL)
119                 return (-1);
120         fdl = (struct fdlist *)malloc((u_int)sizeof (struct fdlist));
121         if (fdl == NULL) {
122                 freenetconfigent(newnconf);
123                 syslog(LOG_ERR, "no memory!");
124                 return (-1);
125         }
126         fdl->nconf = newnconf;
127         fdl->next = NULL;
128         if (fdhead == NULL) {
129                 fdhead = fdl;
130                 fdtail = fdl;
131         } else {
132                 fdtail->next = fdl;
133                 fdtail = fdl;
134         }
135         /* XXX no bound checking for now */
136         fdl->check_binding = FALSE;
137
138         return 0;
139 }
140
141 bool_t
142 is_bound(const char *netid, const char *uaddr)
143 {
144         struct fdlist *fdl;
145
146         for (fdl = fdhead; fdl; fdl = fdl->next)
147                 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
148                         break;
149         if (fdl == NULL)
150                 return (TRUE);
151         return (check_bound(fdl, uaddr));
152 }
153
154 /*
155  * Returns NULL if there was some system error.
156  * Returns "" if the address was not bound, i.e the server crashed.
157  * Returns the merged address otherwise.
158  */
159 char *
160 mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
161 {
162         struct fdlist *fdl;
163         char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
164
165         for (fdl = fdhead; fdl; fdl = fdl->next)
166                 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
167                         break;
168         if (fdl == NULL)
169                 return (NULL);
170         if (check_bound(fdl, uaddr) == FALSE)
171                 /* that server died */
172                 return strdup(emptystring);
173         /*
174          * If saddr is not NULL, the remote client may have included the
175          * address by which it contacted us.  Use that for the "client" uaddr,
176          * otherwise use the info from the SVCXPRT.
177          */
178         if (saddr != NULL) {
179                 c_uaddr = saddr;
180         } else {
181                 c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
182                 if (c_uaddr == NULL) {
183                         syslog(LOG_ERR, "taddr2uaddr failed for %s",
184                                 fdl->nconf->nc_netid);
185                         return (NULL);
186                 }
187                 allocated_uaddr = c_uaddr;
188         }
189
190 #ifdef RPCBIND_DEBUG
191         if (debugging) {
192                 if (saddr == NULL) {
193                         fprintf(stderr, "mergeaddr: client uaddr = %s\n",
194                             c_uaddr);
195                 } else {
196                         fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
197                             c_uaddr);
198                 }
199         }
200 #endif
201         s_uaddr = uaddr;
202         /*
203          * This is all we should need for IP 4 and 6
204          */
205         m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
206 #ifdef RPCBIND_DEBUG
207         if (debugging)
208                 fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
209                                 uaddr, m_uaddr);
210 #endif
211         if (allocated_uaddr != NULL)
212                 free(allocated_uaddr);
213         return (m_uaddr);
214 }
215
216 /*
217  * Returns a netconf structure from its internal list.  This
218  * structure should not be freed.
219  */
220 struct netconfig *
221 rpcbind_get_conf(const char *netid)
222 {
223         struct fdlist *fdl;
224
225         for (fdl = fdhead; fdl; fdl = fdl->next)
226                 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
227                         break;
228         if (fdl == NULL)
229                 return (NULL);
230         return (fdl->nconf);
231 }