1 /* $NetBSD: check_bound.c,v 1.9 2020/06/17 00:16:22 kamil Exp $ */ 2 /* $FreeBSD: head/usr.sbin/rpcbind/check_bound.c 300942 2016-05-29 06:01:18Z ngie $ */ 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 39 static 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 <stdio.h> 54 #include <netconfig.h> 55 #include <syslog.h> 56 #include <string.h> 57 #include <unistd.h> 58 #include <stdlib.h> 59 60 #ifdef RPCBIND_RUMP 61 #include <rump/rump.h> 62 #include <rump/rump_syscallshotgun.h> 63 #include <rump/rump_syscalls.h> 64 #endif 65 66 #include "rpcbind.h" 67 68 struct fdlist { 69 int fd; 70 struct netconfig *nconf; 71 struct fdlist *next; 72 int check_binding; 73 }; 74 75 static struct fdlist *fdhead; /* Link list of the check fd's */ 76 static struct fdlist *fdtail; 77 static char nullstring[] = ""; 78 79 static bool_t check_bound(struct fdlist *, const char *uaddr); 80 81 /* 82 * Returns 1 if the given address is bound for the given addr & transport 83 * For all error cases, we assume that the address is bound 84 * Returns 0 for success. 85 */ 86 static bool_t 87 check_bound(struct fdlist *fdl, const char *uaddr) 88 { 89 int fd; 90 struct netbuf *na; 91 int ans; 92 93 if (fdl->check_binding == FALSE) 94 return (TRUE); 95 96 na = uaddr2taddr(fdl->nconf, uaddr); 97 if (!na) 98 return (TRUE); /* punt, should never happen */ 99 100 fd = __rpc_nconf2fd(fdl->nconf); 101 if (fd < 0) { 102 free(na->buf); 103 free(na); 104 return (TRUE); 105 } 106 107 ans = bind(fd, (struct sockaddr *)na->buf, na->len); 108 109 #ifdef RPCBIND_RUMP 110 rump_sys_close(fd); 111 #else 112 close(fd); 113 #endif 114 free(na->buf); 115 free(na); 116 117 return (ans == 0 ? FALSE : TRUE); 118 } 119 120 int 121 add_bndlist(struct netconfig *nconf, struct netbuf *baddr __unused) 122 { 123 struct fdlist *fdl; 124 struct netconfig *newnconf; 125 126 newnconf = getnetconfigent(nconf->nc_netid); 127 if (newnconf == NULL) 128 return (-1); 129 fdl = malloc(sizeof(*fdl)); 130 if (fdl == NULL) { 131 freenetconfigent(newnconf); 132 syslog(LOG_ERR, "no memory!"); 133 return (-1); 134 } 135 fdl->nconf = newnconf; 136 fdl->next = NULL; 137 if (fdhead == NULL) { 138 fdhead = fdl; 139 fdtail = fdl; 140 } else { 141 fdtail->next = fdl; 142 fdtail = fdl; 143 } 144 /* XXX no bound checking for now */ 145 fdl->check_binding = FALSE; 146 147 return 0; 148 } 149 150 bool_t 151 is_bound(const char *netid, const char *uaddr) 152 { 153 struct fdlist *fdl; 154 155 for (fdl = fdhead; fdl; fdl = fdl->next) 156 if (strcmp(fdl->nconf->nc_netid, netid) == 0) 157 break; 158 if (fdl == NULL) 159 return (TRUE); 160 return (check_bound(fdl, uaddr)); 161 } 162 163 /* 164 * Returns NULL if there was some system error. 165 * Returns "" if the address was not bound, i.e the server crashed. 166 * Returns the merged address otherwise. 167 */ 168 char * 169 mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr) 170 { 171 struct fdlist *fdl; 172 char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL; 173 174 for (fdl = fdhead; fdl; fdl = fdl->next) 175 if (strcmp(fdl->nconf->nc_netid, netid) == 0) 176 break; 177 if (fdl == NULL) 178 return (NULL); 179 if (check_bound(fdl, uaddr) == FALSE) 180 /* that server died */ 181 return nullstring; 182 /* 183 * Try to determine the local address on which the client contacted us, 184 * so we can send a reply from the same address. If it's unknown, then 185 * try to determine which address the client used, and pick a nearby 186 * local address. 187 * 188 * If saddr is not NULL, the remote client may have included the 189 * address by which it contacted us. Use that for the "client" uaddr, 190 * otherwise use the info from the SVCXPRT. 191 */ 192 if (xprt->xp_rtaddr.buf != NULL) { 193 c_uaddr = taddr2uaddr(fdl->nconf, &xprt->xp_rtaddr); 194 allocated_uaddr = c_uaddr; 195 } else if (saddr != NULL) { 196 c_uaddr = saddr; 197 } else { 198 c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt)); 199 allocated_uaddr = c_uaddr; 200 } 201 if (c_uaddr == NULL) { 202 syslog(LOG_ERR, "taddr2uaddr failed for %s", 203 fdl->nconf->nc_netid); 204 return (NULL); 205 } 206 207 #ifdef RPCBIND_DEBUG 208 if (debugging) { 209 if (saddr == NULL) { 210 fprintf(stderr, "mergeaddr: client uaddr = %s\n", 211 c_uaddr); 212 } else { 213 fprintf(stderr, "mergeaddr: contact uaddr = %s\n", 214 c_uaddr); 215 } 216 } 217 #endif 218 s_uaddr = uaddr; 219 /* 220 * This is all we should need for IP 4 and 6 221 */ 222 m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid); 223 #ifdef RPCBIND_DEBUG 224 if (debugging) 225 fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n", 226 uaddr, m_uaddr); 227 #endif 228 free(allocated_uaddr); 229 return (m_uaddr); 230 } 231 232 /* 233 * Returns a netconf structure from its internal list. This 234 * structure should not be freed. 235 */ 236 struct netconfig * 237 rpcbind_get_conf(const char *netid) 238 { 239 struct fdlist *fdl; 240 241 for (fdl = fdhead; fdl; fdl = fdl->next) 242 if (strcmp(fdl->nconf->nc_netid, netid) == 0) 243 break; 244 if (fdl == NULL) 245 return (NULL); 246 return (fdl->nconf); 247 } 248