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