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