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