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