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