2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)check_bound.c 1.15 93/07/05 SMI; 1.11 89/04/21 Copyr 1989 Sun Micro
29 * $NetBSD: check_bound.c,v 1.2 2000/06/22 08:09:26 fvdl Exp $
30 * $FreeBSD: src/usr.sbin/rpcbind/check_bound.c,v 1.5 2007/11/07 10:53:39 kevlo Exp $
33 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
38 * Checks to see whether the program is still bound to the
39 * claimed address and returns the univeral merged address
43 #include <sys/types.h>
44 #include <sys/socket.h>
47 #include <netconfig.h>
57 struct netconfig
*nconf
;
62 static struct fdlist
*fdhead
; /* Link list of the check fd's */
63 static struct fdlist
*fdtail
;
64 static char *nullstring
= "";
66 static bool_t
check_bound(struct fdlist
*, char *uaddr
);
69 * Returns 1 if the given address is bound for the given addr & transport
70 * For all error cases, we assume that the address is bound
71 * Returns 0 for success.
74 check_bound(struct fdlist
*fdl
, char *uaddr
)
80 if (fdl
->check_binding
== FALSE
)
83 na
= uaddr2taddr(fdl
->nconf
, uaddr
);
85 return (TRUE
); /* punt, should never happen */
87 fd
= __rpc_nconf2fd(fdl
->nconf
);
94 ans
= bind(fd
, (struct sockaddr
*)na
->buf
, na
->len
);
100 return (ans
== 0 ? FALSE
: TRUE
);
104 add_bndlist(struct netconfig
*nconf
, struct netbuf
*baddr __unused
)
107 struct netconfig
*newnconf
;
109 newnconf
= getnetconfigent(nconf
->nc_netid
);
110 if (newnconf
== NULL
)
112 fdl
= malloc(sizeof (struct fdlist
));
114 freenetconfigent(newnconf
);
115 syslog(LOG_ERR
, "no memory!");
118 fdl
->nconf
= newnconf
;
120 if (fdhead
== NULL
) {
127 /* XXX no bound checking for now */
128 fdl
->check_binding
= FALSE
;
134 is_bound(char *netid
, char *uaddr
)
138 for (fdl
= fdhead
; fdl
; fdl
= fdl
->next
)
139 if (strcmp(fdl
->nconf
->nc_netid
, netid
) == 0)
143 return (check_bound(fdl
, uaddr
));
147 * Returns NULL if there was some system error.
148 * Returns "" if the address was not bound, i.e the server crashed.
149 * Returns the merged address otherwise.
152 mergeaddr(SVCXPRT
*xprt
, char *netid
, char *uaddr
, char *saddr
)
155 char *c_uaddr
, *s_uaddr
, *m_uaddr
, *allocated_uaddr
= NULL
;
157 for (fdl
= fdhead
; fdl
; fdl
= fdl
->next
)
158 if (strcmp(fdl
->nconf
->nc_netid
, netid
) == 0)
162 if (check_bound(fdl
, uaddr
) == FALSE
)
163 /* that server died */
166 * If saddr is not NULL, the remote client may have included the
167 * address by which it contacted us. Use that for the "client" uaddr,
168 * otherwise use the info from the SVCXPRT.
173 c_uaddr
= taddr2uaddr(fdl
->nconf
, svc_getrpccaller(xprt
));
174 if (c_uaddr
== NULL
) {
175 syslog(LOG_ERR
, "taddr2uaddr failed for %s",
176 fdl
->nconf
->nc_netid
);
179 allocated_uaddr
= c_uaddr
;
185 fprintf(stderr
, "mergeaddr: client uaddr = %s\n",
188 fprintf(stderr
, "mergeaddr: contact uaddr = %s\n",
195 * This is all we should need for IP 4 and 6
197 m_uaddr
= addrmerge(svc_getrpccaller(xprt
), s_uaddr
, c_uaddr
, netid
);
200 fprintf(stderr
, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
203 if (allocated_uaddr
!= NULL
)
204 free(allocated_uaddr
);
209 * Returns a netconf structure from its internal list. This
210 * structure should not be freed.
213 rpcbind_get_conf(char *netid
)
217 for (fdl
= fdhead
; fdl
; fdl
= fdl
->next
)
218 if (strcmp(fdl
->nconf
->nc_netid
, netid
) == 0)