2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)check_bound.c 1.15 93/07/05 SMI; 1.11 89/04/21 Copyr 1989 Sun Micro
30 * $NetBSD: check_bound.c,v 1.2 2000/06/22 08:09:26 fvdl Exp $
31 * $FreeBSD: src/usr.sbin/rpcbind/check_bound.c,v 1.5 2007/11/07 10:53:39 kevlo Exp $
35 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
40 * Checks to see whether the program is still bound to the
41 * claimed address and returns the univeral merged address
45 #include <sys/types.h>
46 #include <sys/socket.h>
49 #include <netconfig.h>
59 struct netconfig *nconf;
64 static struct fdlist *fdhead; /* Link list of the check fd's */
65 static struct fdlist *fdtail;
66 static char *nullstring = "";
68 static bool_t check_bound(struct fdlist *, char *uaddr);
71 * Returns 1 if the given address is bound for the given addr & transport
72 * For all error cases, we assume that the address is bound
73 * Returns 0 for success.
76 check_bound(struct fdlist *fdl, char *uaddr)
82 if (fdl->check_binding == FALSE)
85 na = uaddr2taddr(fdl->nconf, uaddr);
87 return (TRUE); /* punt, should never happen */
89 fd = __rpc_nconf2fd(fdl->nconf);
96 ans = bind(fd, (struct sockaddr *)na->buf, na->len);
102 return (ans == 0 ? FALSE : TRUE);
106 add_bndlist(struct netconfig *nconf, struct netbuf *baddr __unused)
109 struct netconfig *newnconf;
111 newnconf = getnetconfigent(nconf->nc_netid);
112 if (newnconf == NULL)
114 fdl = malloc(sizeof (struct fdlist));
116 freenetconfigent(newnconf);
117 syslog(LOG_ERR, "no memory!");
120 fdl->nconf = newnconf;
122 if (fdhead == NULL) {
129 /* XXX no bound checking for now */
130 fdl->check_binding = FALSE;
136 is_bound(char *netid, char *uaddr)
140 for (fdl = fdhead; fdl; fdl = fdl->next)
141 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
145 return (check_bound(fdl, uaddr));
149 * Returns NULL if there was some system error.
150 * Returns "" if the address was not bound, i.e the server crashed.
151 * Returns the merged address otherwise.
154 mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
157 char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
159 for (fdl = fdhead; fdl; fdl = fdl->next)
160 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
164 if (check_bound(fdl, uaddr) == FALSE)
165 /* that server died */
168 * If saddr is not NULL, the remote client may have included the
169 * address by which it contacted us. Use that for the "client" uaddr,
170 * otherwise use the info from the SVCXPRT.
175 c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
176 if (c_uaddr == NULL) {
177 syslog(LOG_ERR, "taddr2uaddr failed for %s",
178 fdl->nconf->nc_netid);
181 allocated_uaddr = c_uaddr;
187 fprintf(stderr, "mergeaddr: client uaddr = %s\n",
190 fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
197 * This is all we should need for IP 4 and 6
199 m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
202 fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
205 if (allocated_uaddr != NULL)
206 free(allocated_uaddr);
211 * Returns a netconf structure from its internal list. This
212 * structure should not be freed.
215 rpcbind_get_conf(char *netid)
219 for (fdl = fdhead; fdl; fdl = fdl->next)
220 if (strcmp(fdl->nconf->nc_netid, netid) == 0)