1 /* $NetBSD: check_bound.c,v 1.2 2000/06/22 08:09:26 fvdl Exp $ */ 2 /* $FreeBSD$ */ 3 4 /* 5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 6 * unrestricted use provided that this legend is included on all tape 7 * media and as a part of the software program in whole or part. Users 8 * may copy or modify Sun RPC without charge, but are not authorized 9 * to license or distribute it to anyone else except as part of a product or 10 * program developed by the user. 11 * 12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 15 * 16 * Sun RPC is provided with no support and without any obligation on the 17 * part of Sun Microsystems, Inc. to assist in its use, correction, 18 * modification or enhancement. 19 * 20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 22 * OR ANY PART THEREOF. 23 * 24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 25 * or profits or other special, indirect and consequential damages, even if 26 * Sun has been advised of the possibility of such damages. 27 * 28 * Sun Microsystems, Inc. 29 * 2550 Garcia Avenue 30 * Mountain View, California 94043 31 */ 32 /* 33 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc. 34 */ 35 36 /* #ident "@(#)check_bound.c 1.15 93/07/05 SMI" */ 37 38 #if 0 39 #ifndef lint 40 static char sccsid[] = "@(#)check_bound.c 1.11 89/04/21 Copyr 1989 Sun Micro"; 41 #endif 42 #endif 43 44 /* 45 * check_bound.c 46 * Checks to see whether the program is still bound to the 47 * claimed address and returns the univeral merged address 48 * 49 */ 50 51 #include <sys/types.h> 52 #include <sys/socket.h> 53 #include <rpc/rpc.h> 54 #include <stdio.h> 55 #include <netconfig.h> 56 #include <syslog.h> 57 #include <string.h> 58 #include <unistd.h> 59 #include <stdlib.h> 60 61 #include "rpcbind.h" 62 63 struct fdlist { 64 int fd; 65 struct netconfig *nconf; 66 struct fdlist *next; 67 int check_binding; 68 }; 69 70 static struct fdlist *fdhead; /* Link list of the check fd's */ 71 static struct fdlist *fdtail; 72 static char *nullstring = ""; 73 74 static bool_t check_bound __P((struct fdlist *, char *uaddr)); 75 76 /* 77 * Returns 1 if the given address is bound for the given addr & transport 78 * For all error cases, we assume that the address is bound 79 * Returns 0 for success. 80 */ 81 static bool_t 82 check_bound(struct fdlist *fdl, char *uaddr) 83 { 84 int fd; 85 struct netbuf *na; 86 int ans; 87 88 if (fdl->check_binding == FALSE) 89 return (TRUE); 90 91 na = uaddr2taddr(fdl->nconf, uaddr); 92 if (!na) 93 return (TRUE); /* punt, should never happen */ 94 95 fd = __rpc_nconf2fd(fdl->nconf); 96 if (fd < 0) { 97 free(na); 98 return (TRUE); 99 } 100 101 ans = bind(fd, (struct sockaddr *)na->buf, na->len); 102 103 close(fd); 104 free(na); 105 106 return (ans == 0 ? FALSE : TRUE); 107 } 108 109 int 110 add_bndlist(struct netconfig *nconf, struct netbuf *baddr) 111 { 112 struct fdlist *fdl; 113 struct netconfig *newnconf; 114 115 newnconf = getnetconfigent(nconf->nc_netid); 116 if (newnconf == NULL) 117 return (-1); 118 fdl = (struct fdlist *)malloc((u_int)sizeof (struct fdlist)); 119 if (fdl == NULL) { 120 freenetconfigent(newnconf); 121 syslog(LOG_ERR, "no memory!"); 122 return (-1); 123 } 124 fdl->nconf = newnconf; 125 fdl->next = NULL; 126 if (fdhead == NULL) { 127 fdhead = fdl; 128 fdtail = fdl; 129 } else { 130 fdtail->next = fdl; 131 fdtail = fdl; 132 } 133 /* XXX no bound checking for now */ 134 fdl->check_binding = FALSE; 135 136 return 0; 137 } 138 139 bool_t 140 is_bound(char *netid, char *uaddr) 141 { 142 struct fdlist *fdl; 143 144 for (fdl = fdhead; fdl; fdl = fdl->next) 145 if (strcmp(fdl->nconf->nc_netid, netid) == 0) 146 break; 147 if (fdl == NULL) 148 return (TRUE); 149 return (check_bound(fdl, uaddr)); 150 } 151 152 /* 153 * Returns NULL if there was some system error. 154 * Returns "" if the address was not bound, i.e the server crashed. 155 * Returns the merged address otherwise. 156 */ 157 char * 158 mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr) 159 { 160 struct fdlist *fdl; 161 char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL; 162 163 for (fdl = fdhead; fdl; fdl = fdl->next) 164 if (strcmp(fdl->nconf->nc_netid, netid) == 0) 165 break; 166 if (fdl == NULL) 167 return (NULL); 168 if (check_bound(fdl, uaddr) == FALSE) 169 /* that server died */ 170 return (nullstring); 171 /* 172 * If saddr is not NULL, the remote client may have included the 173 * address by which it contacted us. Use that for the "client" uaddr, 174 * otherwise use the info from the SVCXPRT. 175 */ 176 if (saddr != NULL) { 177 c_uaddr = saddr; 178 } else { 179 c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt)); 180 if (c_uaddr == NULL) { 181 syslog(LOG_ERR, "taddr2uaddr failed for %s", 182 fdl->nconf->nc_netid); 183 return (NULL); 184 } 185 allocated_uaddr = c_uaddr; 186 } 187 188 #ifdef ND_DEBUG 189 if (debugging) { 190 if (saddr == NULL) { 191 fprintf(stderr, "mergeaddr: client uaddr = %s\n", 192 c_uaddr); 193 } else { 194 fprintf(stderr, "mergeaddr: contact uaddr = %s\n", 195 c_uaddr); 196 } 197 } 198 #endif 199 s_uaddr = uaddr; 200 /* 201 * This is all we should need for IP 4 and 6 202 */ 203 m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid); 204 #ifdef ND_DEBUG 205 if (debugging) 206 fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n", 207 uaddr, m_uaddr); 208 #endif 209 if (allocated_uaddr != NULL) 210 free(allocated_uaddr); 211 return (m_uaddr); 212 } 213 214 /* 215 * Returns a netconf structure from its internal list. This 216 * structure should not be freed. 217 */ 218 struct netconfig * 219 rpcbind_get_conf(char *netid) 220 { 221 struct fdlist *fdl; 222 223 for (fdl = fdhead; fdl; fdl = fdl->next) 224 if (strcmp(fdl->nconf->nc_netid, netid) == 0) 225 break; 226 if (fdl == NULL) 227 return (NULL); 228 return (fdl->nconf); 229 } 230