1 /* $NetBSD: rpcb_clnt.c,v 1.17 2005/06/07 09:13:43 he Exp $ */ 2 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 /* 32 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 33 */ 34 35 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */ 36 37 #include <sys/cdefs.h> 38 #if defined(LIBC_SCCS) && !defined(lint) 39 #if 0 40 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro"; 41 #else 42 __RCSID("$NetBSD: rpcb_clnt.c,v 1.17 2005/06/07 09:13:43 he Exp $"); 43 #endif 44 #endif 45 46 /* 47 * rpcb_clnt.c 48 * interface to rpcbind rpc service. 49 * 50 * Copyright (C) 1988, Sun Microsystems, Inc. 51 */ 52 53 #include "namespace.h" 54 #include "reentrant.h" 55 #include <sys/types.h> 56 #include <sys/socket.h> 57 #include <sys/un.h> 58 #include <sys/utsname.h> 59 #include <rpc/rpc.h> 60 #include <rpc/rpcb_prot.h> 61 #include <rpc/nettype.h> 62 #include <netconfig.h> 63 #ifdef PORTMAP 64 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */ 65 #include <rpc/pmap_prot.h> 66 #endif 67 #include <assert.h> 68 #include <errno.h> 69 #include <netdb.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <syslog.h> 74 #include <unistd.h> 75 76 #include "rpc_internal.h" 77 78 #ifdef __weak_alias 79 __weak_alias(rpcb_set,_rpcb_set) 80 __weak_alias(rpcb_unset,_rpcb_unset) 81 __weak_alias(rpcb_getmaps,_rpcb_getmaps) 82 __weak_alias(rpcb_rmtcall,_rpcb_rmtcall) 83 __weak_alias(rpcb_gettime,_rpcb_gettime) 84 __weak_alias(rpcb_taddr2uaddr,_rpcb_taddr2uaddr) 85 __weak_alias(rpcb_uaddr2taddr,_rpcb_uaddr2taddr) 86 #endif 87 88 static struct timeval tottimeout = { 60, 0 }; 89 static const struct timeval rmttimeout = { 3, 0 }; 90 91 static const char nullstring[] = "\000"; 92 93 #define CACHESIZE 6 94 95 struct address_cache { 96 char *ac_host; 97 char *ac_netid; 98 char *ac_uaddr; 99 struct netbuf *ac_taddr; 100 struct address_cache *ac_next; 101 }; 102 103 static struct address_cache *front; 104 static int cachesize; 105 106 #define CLCR_GET_RPCB_TIMEOUT 1 107 #define CLCR_SET_RPCB_TIMEOUT 2 108 109 110 extern int __rpc_lowvers; 111 112 static struct address_cache *check_cache __P((const char *, const char *)); 113 static void delete_cache __P((struct netbuf *)); 114 static void add_cache __P((const char *, const char *, struct netbuf *, 115 char *)); 116 static CLIENT *getclnthandle __P((const char *, const struct netconfig *, 117 char **)); 118 static CLIENT *local_rpcb __P((void)); 119 static struct netbuf *got_entry __P((rpcb_entry_list_ptr, 120 const struct netconfig *)); 121 122 /* 123 * This routine adjusts the timeout used for calls to the remote rpcbind. 124 * Also, this routine can be used to set the use of portmapper version 2 125 * only when doing rpc_broadcasts 126 * These are private routines that may not be provided in future releases. 127 */ 128 bool_t 129 __rpc_control(request, info) 130 int request; 131 void *info; 132 { 133 134 _DIAGASSERT(info != NULL); 135 136 switch (request) { 137 case CLCR_GET_RPCB_TIMEOUT: 138 *(struct timeval *)info = tottimeout; 139 break; 140 case CLCR_SET_RPCB_TIMEOUT: 141 tottimeout = *(struct timeval *)info; 142 break; 143 case CLCR_SET_LOWVERS: 144 __rpc_lowvers = *(int *)info; 145 break; 146 case CLCR_GET_LOWVERS: 147 *(int *)info = __rpc_lowvers; 148 break; 149 default: 150 return (FALSE); 151 } 152 return (TRUE); 153 } 154 155 /* 156 * It might seem that a reader/writer lock would be more reasonable here. 157 * However because getclnthandle(), the only user of the cache functions, 158 * may do a delete_cache() operation if a check_cache() fails to return an 159 * address useful to clnt_tli_create(), we may as well use a mutex. 160 */ 161 /* 162 * As it turns out, if the cache lock is *not* a reader/writer lock, we will 163 * block all clnt_create's if we are trying to connect to a host that's down, 164 * since the lock will be held all during that time. 165 */ 166 #ifdef _REENTRANT 167 extern rwlock_t rpcbaddr_cache_lock; 168 #endif 169 170 /* 171 * The routines check_cache(), add_cache(), delete_cache() manage the 172 * cache of rpcbind addresses for (host, netid). 173 */ 174 175 static struct address_cache * 176 check_cache(host, netid) 177 const char *host, *netid; 178 { 179 struct address_cache *cptr; 180 181 _DIAGASSERT(host != NULL); 182 _DIAGASSERT(netid != NULL); 183 184 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */ 185 186 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) { 187 if (!strcmp(cptr->ac_host, host) && 188 !strcmp(cptr->ac_netid, netid)) { 189 #ifdef ND_DEBUG 190 fprintf(stderr, "Found cache entry for %s: %s\n", 191 host, netid); 192 #endif 193 return (cptr); 194 } 195 } 196 return ((struct address_cache *) NULL); 197 } 198 199 static void 200 delete_cache(addr) 201 struct netbuf *addr; 202 { 203 struct address_cache *cptr, *prevptr = NULL; 204 205 _DIAGASSERT(addr != NULL); 206 207 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */ 208 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) { 209 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) { 210 free(cptr->ac_host); 211 free(cptr->ac_netid); 212 free(cptr->ac_taddr->buf); 213 free(cptr->ac_taddr); 214 if (cptr->ac_uaddr) 215 free(cptr->ac_uaddr); 216 if (prevptr) 217 prevptr->ac_next = cptr->ac_next; 218 else 219 front = cptr->ac_next; 220 free(cptr); 221 cachesize--; 222 break; 223 } 224 prevptr = cptr; 225 } 226 } 227 228 static void 229 add_cache(host, netid, taddr, uaddr) 230 const char *host, *netid; 231 char *uaddr; 232 struct netbuf *taddr; 233 { 234 struct address_cache *ad_cache, *cptr, *prevptr; 235 236 _DIAGASSERT(host != NULL); 237 _DIAGASSERT(netid != NULL); 238 /* uaddr may be NULL */ 239 /* taddr may be NULL ??? */ 240 241 ad_cache = (struct address_cache *) 242 malloc(sizeof (struct address_cache)); 243 if (!ad_cache) { 244 return; 245 } 246 ad_cache->ac_host = strdup(host); 247 ad_cache->ac_netid = strdup(netid); 248 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL; 249 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf)); 250 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr || 251 (uaddr && !ad_cache->ac_uaddr)) { 252 return; 253 } 254 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len; 255 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len); 256 if (ad_cache->ac_taddr->buf == NULL) { 257 return; 258 } 259 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len); 260 #ifdef ND_DEBUG 261 fprintf(stderr, "Added to cache: %s : %s\n", host, netid); 262 #endif 263 264 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */ 265 266 rwlock_wrlock(&rpcbaddr_cache_lock); 267 if (cachesize < CACHESIZE) { 268 ad_cache->ac_next = front; 269 front = ad_cache; 270 cachesize++; 271 } else { 272 /* Free the last entry */ 273 cptr = front; 274 prevptr = NULL; 275 while (cptr->ac_next) { 276 prevptr = cptr; 277 cptr = cptr->ac_next; 278 } 279 280 #ifdef ND_DEBUG 281 fprintf(stderr, "Deleted from cache: %s : %s\n", 282 cptr->ac_host, cptr->ac_netid); 283 #endif 284 free(cptr->ac_host); 285 free(cptr->ac_netid); 286 free(cptr->ac_taddr->buf); 287 free(cptr->ac_taddr); 288 if (cptr->ac_uaddr) 289 free(cptr->ac_uaddr); 290 291 if (prevptr) { 292 prevptr->ac_next = NULL; 293 ad_cache->ac_next = front; 294 front = ad_cache; 295 } else { 296 front = ad_cache; 297 ad_cache->ac_next = NULL; 298 } 299 free(cptr); 300 } 301 rwlock_unlock(&rpcbaddr_cache_lock); 302 } 303 304 /* 305 * This routine will return a client handle that is connected to the 306 * rpcbind. Returns NULL on error and free's everything. 307 */ 308 static CLIENT * 309 getclnthandle(host, nconf, targaddr) 310 const char *host; 311 const struct netconfig *nconf; 312 char **targaddr; 313 { 314 CLIENT *client; 315 struct netbuf *addr, taddr; 316 struct netbuf addr_to_delete; 317 struct __rpc_sockinfo si; 318 struct addrinfo hints, *res, *tres; 319 struct address_cache *ad_cache; 320 char *tmpaddr; 321 322 _DIAGASSERT(host != NULL); 323 _DIAGASSERT(nconf != NULL); 324 /* targaddr may be NULL */ 325 326 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */ 327 328 /* Get the address of the rpcbind. Check cache first */ 329 client = NULL; 330 addr_to_delete.len = 0; 331 rwlock_rdlock(&rpcbaddr_cache_lock); 332 ad_cache = check_cache(host, nconf->nc_netid); 333 if (ad_cache != NULL) { 334 addr = ad_cache->ac_taddr; 335 client = clnt_tli_create(RPC_ANYFD, nconf, addr, 336 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0); 337 if (client != NULL) { 338 if (targaddr) 339 *targaddr = ad_cache->ac_uaddr; 340 rwlock_unlock(&rpcbaddr_cache_lock); 341 return (client); 342 } 343 addr_to_delete.len = addr->len; 344 addr_to_delete.buf = (char *)malloc(addr->len); 345 if (addr_to_delete.buf == NULL) { 346 addr_to_delete.len = 0; 347 } else { 348 memcpy(addr_to_delete.buf, addr->buf, addr->len); 349 } 350 } 351 rwlock_unlock(&rpcbaddr_cache_lock); 352 if (addr_to_delete.len != 0) { 353 /* 354 * Assume this may be due to cache data being 355 * outdated 356 */ 357 rwlock_wrlock(&rpcbaddr_cache_lock); 358 delete_cache(&addr_to_delete); 359 rwlock_unlock(&rpcbaddr_cache_lock); 360 free(addr_to_delete.buf); 361 } 362 if (!__rpc_nconf2sockinfo(nconf, &si)) { 363 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 364 return NULL; 365 } 366 367 memset(&hints, 0, sizeof hints); 368 hints.ai_family = si.si_af; 369 hints.ai_socktype = si.si_socktype; 370 hints.ai_protocol = si.si_proto; 371 372 #ifdef CLNT_DEBUG 373 printf("trying netid %s family %d proto %d socktype %d\n", 374 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype); 375 #endif 376 377 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) { 378 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; 379 return NULL; 380 } 381 382 for (tres = res; tres != NULL; tres = tres->ai_next) { 383 taddr.buf = tres->ai_addr; 384 taddr.len = taddr.maxlen = tres->ai_addrlen; 385 386 #ifdef ND_DEBUG 387 { 388 char *ua; 389 390 ua = taddr2uaddr(nconf, &taddr); 391 fprintf(stderr, "Got it [%s]\n", ua); 392 free(ua); 393 } 394 #endif 395 396 #ifdef ND_DEBUG 397 { 398 int i; 399 400 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n", 401 taddr.len, taddr.maxlen); 402 fprintf(stderr, "\tAddress is "); 403 for (i = 0; i < taddr.len; i++) 404 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]); 405 fprintf(stderr, "\n"); 406 } 407 #endif 408 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr, 409 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0); 410 #ifdef ND_DEBUG 411 if (! client) { 412 clnt_pcreateerror("rpcbind clnt interface"); 413 } 414 #endif 415 416 if (client) { 417 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL; 418 add_cache(host, nconf->nc_netid, &taddr, tmpaddr); 419 if (targaddr) 420 *targaddr = tmpaddr; 421 break; 422 } 423 } 424 freeaddrinfo(res); 425 return (client); 426 } 427 428 /* XXX */ 429 #define IN4_LOCALHOST_STRING "127.0.0.1" 430 #define IN6_LOCALHOST_STRING "::1" 431 432 /* 433 * This routine will return a client handle that is connected to the local 434 * rpcbind. Returns NULL on error and free's everything. 435 */ 436 static CLIENT * 437 local_rpcb() 438 { 439 CLIENT *client; 440 static struct netconfig *loopnconf; 441 static char *hostname; 442 #ifdef _REENTRANT 443 extern mutex_t loopnconf_lock; 444 #endif 445 int sock; 446 size_t tsize; 447 struct netbuf nbuf; 448 struct sockaddr_un sun; 449 450 /* 451 * Try connecting to the local rpcbind through a local socket 452 * first. If this doesn't work, try all transports defined in 453 * the netconfig file. 454 */ 455 memset(&sun, 0, sizeof sun); 456 sock = socket(AF_LOCAL, SOCK_STREAM, 0); 457 if (sock < 0) 458 goto try_nconf; 459 sun.sun_family = AF_LOCAL; 460 strcpy(sun.sun_path, _PATH_RPCBINDSOCK); 461 nbuf.len = sun.sun_len = SUN_LEN(&sun); 462 nbuf.maxlen = sizeof (struct sockaddr_un); 463 nbuf.buf = &sun; 464 465 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0); 466 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG, 467 (rpcvers_t)RPCBVERS, tsize, tsize); 468 469 if (client != NULL) { 470 /* XXX - mark the socket to be closed in destructor */ 471 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL); 472 return client; 473 } 474 475 /* XXX - nobody needs this socket anymore, free the descriptor */ 476 close(sock); 477 478 try_nconf: 479 480 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */ 481 mutex_lock(&loopnconf_lock); 482 if (loopnconf == NULL) { 483 struct netconfig *nconf, *tmpnconf = NULL; 484 void *nc_handle; 485 int fd; 486 487 nc_handle = setnetconfig(); 488 if (nc_handle == NULL) { 489 /* fails to open netconfig file */ 490 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); 491 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 492 mutex_unlock(&loopnconf_lock); 493 return (NULL); 494 } 495 while ((nconf = getnetconfig(nc_handle)) != NULL) { 496 #ifdef INET6 497 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 || 498 #else 499 if (( 500 #endif 501 strcmp(nconf->nc_protofmly, NC_INET) == 0) && 502 (nconf->nc_semantics == NC_TPI_COTS || 503 nconf->nc_semantics == NC_TPI_COTS_ORD)) { 504 fd = __rpc_nconf2fd(nconf); 505 /* 506 * Can't create a socket, assume that 507 * this family isn't configured in the kernel. 508 */ 509 if (fd < 0) 510 continue; 511 close(fd); 512 tmpnconf = nconf; 513 if (!strcmp(nconf->nc_protofmly, NC_INET)) 514 hostname = IN4_LOCALHOST_STRING; 515 else 516 hostname = IN6_LOCALHOST_STRING; 517 } 518 } 519 if (tmpnconf == NULL) { 520 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 521 mutex_unlock(&loopnconf_lock); 522 return (NULL); 523 } 524 loopnconf = getnetconfigent(tmpnconf->nc_netid); 525 /* loopnconf is never freed */ 526 endnetconfig(nc_handle); 527 } 528 mutex_unlock(&loopnconf_lock); 529 client = getclnthandle(hostname, loopnconf, NULL); 530 return (client); 531 } 532 533 /* 534 * Set a mapping between program, version and address. 535 * Calls the rpcbind service to do the mapping. 536 */ 537 bool_t 538 rpcb_set(program, version, nconf, address) 539 rpcprog_t program; 540 rpcvers_t version; 541 const struct netconfig *nconf; /* Network structure of transport */ 542 const struct netbuf *address; /* Services netconfig address */ 543 { 544 CLIENT *client; 545 bool_t rslt = FALSE; 546 RPCB parms; 547 char uidbuf[32]; 548 549 /* parameter checking */ 550 if (nconf == NULL) { 551 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 552 return (FALSE); 553 } 554 if (address == NULL) { 555 rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 556 return (FALSE); 557 } 558 client = local_rpcb(); 559 if (! client) { 560 return (FALSE); 561 } 562 563 /* convert to universal */ 564 /*LINTED const castaway*/ 565 parms.r_addr = taddr2uaddr((struct netconfig *) nconf, 566 (struct netbuf *)address); 567 if (!parms.r_addr) { 568 CLNT_DESTROY(client); 569 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE; 570 return (FALSE); /* no universal address */ 571 } 572 parms.r_prog = program; 573 parms.r_vers = version; 574 parms.r_netid = nconf->nc_netid; 575 /* 576 * Though uid is not being used directly, we still send it for 577 * completeness. For non-unix platforms, perhaps some other 578 * string or an empty string can be sent. 579 */ 580 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid()); 581 parms.r_owner = uidbuf; 582 583 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb, 584 (char *)(void *)&parms, (xdrproc_t) xdr_bool, 585 (char *)(void *)&rslt, tottimeout); 586 587 CLNT_DESTROY(client); 588 free(parms.r_addr); 589 return (rslt); 590 } 591 592 /* 593 * Remove the mapping between program, version and netbuf address. 594 * Calls the rpcbind service to do the un-mapping. 595 * If netbuf is NULL, unset for all the transports, otherwise unset 596 * only for the given transport. 597 */ 598 bool_t 599 rpcb_unset(program, version, nconf) 600 rpcprog_t program; 601 rpcvers_t version; 602 const struct netconfig *nconf; 603 { 604 CLIENT *client; 605 bool_t rslt = FALSE; 606 RPCB parms; 607 char uidbuf[32]; 608 609 client = local_rpcb(); 610 if (! client) { 611 return (FALSE); 612 } 613 614 parms.r_prog = program; 615 parms.r_vers = version; 616 if (nconf) 617 parms.r_netid = nconf->nc_netid; 618 else { 619 /*LINTED const castaway*/ 620 parms.r_netid = (char *) &nullstring[0]; /* unsets all */ 621 } 622 /*LINTED const castaway*/ 623 parms.r_addr = (char *) &nullstring[0]; 624 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid()); 625 parms.r_owner = uidbuf; 626 627 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb, 628 (char *)(void *)&parms, (xdrproc_t) xdr_bool, 629 (char *)(void *)&rslt, tottimeout); 630 631 CLNT_DESTROY(client); 632 return (rslt); 633 } 634 635 /* 636 * From the merged list, find the appropriate entry 637 */ 638 static struct netbuf * 639 got_entry(relp, nconf) 640 rpcb_entry_list_ptr relp; 641 const struct netconfig *nconf; 642 { 643 struct netbuf *na = NULL; 644 rpcb_entry_list_ptr sp; 645 rpcb_entry *rmap; 646 647 _DIAGASSERT(nconf != NULL); 648 649 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) { 650 rmap = &sp->rpcb_entry_map; 651 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) && 652 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) && 653 (nconf->nc_semantics == rmap->r_nc_semantics) && 654 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) { 655 na = uaddr2taddr(nconf, rmap->r_maddr); 656 #ifdef ND_DEBUG 657 fprintf(stderr, "\tRemote address is [%s].\n", 658 rmap->r_maddr); 659 if (!na) 660 fprintf(stderr, 661 "\tCouldn't resolve remote address!\n"); 662 #endif 663 break; 664 } 665 } 666 return (na); 667 } 668 669 /* 670 * An internal function which optimizes rpcb_getaddr function. It also 671 * returns the client handle that it uses to contact the remote rpcbind. 672 * 673 * The algorithm used: If the transports is TCP or UDP, it first tries 674 * version 2 (portmap), 4 and then 3 (svr4). This order should be 675 * changed in the next OS release to 4, 2 and 3. We are assuming that by 676 * that time, version 4 would be available on many machines on the network. 677 * With this algorithm, we get performance as well as a plan for 678 * obsoleting version 2. 679 * 680 * For all other transports, the algorithm remains as 4 and then 3. 681 * 682 * XXX: Due to some problems with t_connect(), we do not reuse the same client 683 * handle for COTS cases and hence in these cases we do not return the 684 * client handle. This code will change if t_connect() ever 685 * starts working properly. Also look under clnt_vc.c. 686 */ 687 struct netbuf * 688 __rpcb_findaddr(program, version, nconf, host, clpp) 689 rpcprog_t program; 690 rpcvers_t version; 691 const struct netconfig *nconf; 692 const char *host; 693 CLIENT **clpp; 694 { 695 CLIENT *client = NULL; 696 RPCB parms; 697 enum clnt_stat clnt_st; 698 char *ua = NULL; 699 rpcvers_t vers; 700 struct netbuf *address = NULL; 701 rpcvers_t start_vers = RPCBVERS4; 702 struct netbuf servaddr; 703 704 /* nconf is handled below */ 705 _DIAGASSERT(host != NULL); 706 /* clpp may be NULL */ 707 708 /* parameter checking */ 709 if (nconf == NULL) { 710 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 711 return (NULL); 712 } 713 714 parms.r_addr = NULL; 715 716 #ifdef PORTMAP 717 /* Try version 2 for TCP or UDP */ 718 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { 719 u_short port = 0; 720 struct netbuf remote; 721 rpcvers_t pmapvers = 2; 722 struct pmap pmapparms; 723 724 /* 725 * Try UDP only - there are some portmappers out 726 * there that use UDP only. 727 */ 728 if (strcmp(nconf->nc_proto, NC_TCP) == 0) { 729 struct netconfig *newnconf; 730 731 if ((newnconf = getnetconfigent("udp")) == NULL) { 732 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 733 return (NULL); 734 } 735 client = getclnthandle(host, newnconf, &parms.r_addr); 736 freenetconfigent(newnconf); 737 } else { 738 client = getclnthandle(host, nconf, &parms.r_addr); 739 } 740 if (client == NULL) { 741 return (NULL); 742 } 743 744 /* Set the version */ 745 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&pmapvers); 746 pmapparms.pm_prog = program; 747 pmapparms.pm_vers = version; 748 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ? 749 IPPROTO_UDP : IPPROTO_TCP; 750 pmapparms.pm_port = 0; /* not needed */ 751 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT, 752 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms, 753 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port, 754 tottimeout); 755 if (clnt_st != RPC_SUCCESS) { 756 if ((clnt_st == RPC_PROGVERSMISMATCH) || 757 (clnt_st == RPC_PROGUNAVAIL)) 758 goto try_rpcbind; /* Try different versions */ 759 rpc_createerr.cf_stat = RPC_PMAPFAILURE; 760 clnt_geterr(client, &rpc_createerr.cf_error); 761 goto error; 762 } else if (port == 0) { 763 address = NULL; 764 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED; 765 goto error; 766 } 767 port = htons(port); 768 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&remote); 769 if (((address = (struct netbuf *) 770 malloc(sizeof (struct netbuf))) == NULL) || 771 ((address->buf = (char *) 772 malloc(remote.len)) == NULL)) { 773 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 774 clnt_geterr(client, &rpc_createerr.cf_error); 775 if (address) { 776 free(address); 777 address = NULL; 778 } 779 goto error; 780 } 781 memcpy(address->buf, remote.buf, remote.len); 782 memcpy(&((char *)address->buf)[sizeof (short)], 783 (char *)(void *)&port, sizeof (short)); 784 address->len = address->maxlen = remote.len; 785 goto done; 786 } 787 #endif 788 789 try_rpcbind: 790 /* 791 * Now we try version 4 and then 3. 792 * We also send the remote system the address we used to 793 * contact it in case it can help to connect back with us 794 */ 795 parms.r_prog = program; 796 parms.r_vers = version; 797 /*LINTED const castaway*/ 798 parms.r_owner = (char *) &nullstring[0]; /* not needed; */ 799 /* just for xdring */ 800 parms.r_netid = nconf->nc_netid; /* not really needed */ 801 802 /* 803 * If a COTS transport is being used, try getting address via CLTS 804 * transport. This works only with version 4. 805 * NOTE: This is being done for all transports EXCEPT LOOPBACK 806 * because with loopback the cost to go to a COTS is same as 807 * the cost to go through CLTS, plus you get the advantage of 808 * finding out immediately if the local rpcbind process is dead. 809 */ 810 #if 1 811 if ((nconf->nc_semantics == NC_TPI_COTS_ORD || 812 nconf->nc_semantics == NC_TPI_COTS) && 813 (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0)) 814 #else 815 if (client != NULL) { 816 CLNT_DESTROY(client); 817 client = NULL; 818 } 819 if (nconf->nc_semantics == NC_TPI_CLTS) 820 #endif 821 { 822 void *handle; 823 struct netconfig *nconf_clts; 824 rpcb_entry_list_ptr relp = NULL; 825 826 if (client == NULL) { 827 /* This did not go through the above PORTMAP/TCP code */ 828 #if 1 829 if ((handle = __rpc_setconf("datagram_v")) != NULL) 830 #else 831 if ((handle = __rpc_setconf("circuit_v")) != NULL) 832 #endif 833 { 834 while ((nconf_clts = __rpc_getconf(handle)) 835 != NULL) { 836 if (strcmp(nconf_clts->nc_protofmly, 837 nconf->nc_protofmly) != 0) { 838 continue; 839 } 840 client = getclnthandle(host, nconf_clts, 841 &parms.r_addr); 842 break; 843 } 844 __rpc_endconf(handle); 845 } 846 if (client == NULL) 847 goto regular_rpcbind; /* Go the regular way */ 848 } else { 849 /* This is a UDP PORTMAP handle. Change to version 4 */ 850 vers = RPCBVERS4; 851 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); 852 } 853 /* 854 * We also send the remote system the address we used to 855 * contact it in case it can help it connect back with us 856 */ 857 if (parms.r_addr == NULL) { 858 /*LINTED const castaway*/ 859 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */ 860 } 861 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST, 862 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms, 863 (xdrproc_t) xdr_rpcb_entry_list_ptr, 864 (char *)(void *)&relp, tottimeout); 865 if (clnt_st == RPC_SUCCESS) { 866 if ((address = got_entry(relp, nconf)) != NULL) { 867 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr, 868 (char *)(void *)&relp); 869 CLNT_CONTROL(client, CLGET_SVC_ADDR, 870 (char *)(void *)&servaddr); 871 __rpc_fixup_addr(address, &servaddr); 872 goto done; 873 } 874 /* Entry not found for this transport */ 875 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr, 876 (char *)(void *)&relp); 877 /* 878 * XXX: should have perhaps returned with error but 879 * since the remote machine might not always be able 880 * to send the address on all transports, we try the 881 * regular way with regular_rpcbind 882 */ 883 goto regular_rpcbind; 884 } else if ((clnt_st == RPC_PROGVERSMISMATCH) || 885 (clnt_st == RPC_PROGUNAVAIL)) { 886 start_vers = RPCBVERS; /* Try version 3 now */ 887 goto regular_rpcbind; /* Try different versions */ 888 } else { 889 rpc_createerr.cf_stat = RPC_PMAPFAILURE; 890 clnt_geterr(client, &rpc_createerr.cf_error); 891 goto error; 892 } 893 } 894 895 regular_rpcbind: 896 897 /* Now the same transport is to be used to get the address */ 898 #if 1 899 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) || 900 (nconf->nc_semantics == NC_TPI_COTS))) 901 #else 902 if (client && nconf->nc_semantics == NC_TPI_CLTS) 903 #endif 904 { 905 /* A CLTS type of client - destroy it */ 906 CLNT_DESTROY(client); 907 client = NULL; 908 } 909 910 if (client == NULL) { 911 client = getclnthandle(host, nconf, &parms.r_addr); 912 if (client == NULL) { 913 goto error; 914 } 915 } 916 if (parms.r_addr == NULL) { 917 /*LINTED const castaway*/ 918 parms.r_addr = (char *) &nullstring[0]; 919 } 920 921 /* First try from start_vers and then version 3 (RPCBVERS) */ 922 for (vers = start_vers; vers >= RPCBVERS; vers--) { 923 /* Set the version */ 924 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); 925 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR, 926 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms, 927 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, 928 tottimeout); 929 if (clnt_st == RPC_SUCCESS) { 930 if ((ua == NULL) || (ua[0] == 0)) { 931 /* address unknown */ 932 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED; 933 goto error; 934 } 935 address = uaddr2taddr(nconf, ua); 936 #ifdef ND_DEBUG 937 fprintf(stderr, "\tRemote address is [%s]\n", ua); 938 if (!address) 939 fprintf(stderr, 940 "\tCouldn't resolve remote address!\n"); 941 #endif 942 xdr_free((xdrproc_t)xdr_wrapstring, 943 (char *)(void *)&ua); 944 945 if (! address) { 946 /* We don't know about your universal address */ 947 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE; 948 goto error; 949 } 950 CLNT_CONTROL(client, CLGET_SVC_ADDR, 951 (char *)(void *)&servaddr); 952 __rpc_fixup_addr(address, &servaddr); 953 goto done; 954 } else if (clnt_st == RPC_PROGVERSMISMATCH) { 955 struct rpc_err rpcerr; 956 957 clnt_geterr(client, &rpcerr); 958 if (rpcerr.re_vers.low > RPCBVERS4) 959 goto error; /* a new version, can't handle */ 960 } else if (clnt_st != RPC_PROGUNAVAIL) { 961 /* Cant handle this error */ 962 rpc_createerr.cf_stat = clnt_st; 963 clnt_geterr(client, &rpc_createerr.cf_error); 964 goto error; 965 } 966 } 967 968 if ((address == NULL) || (address->len == 0)) { 969 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED; 970 clnt_geterr(client, &rpc_createerr.cf_error); 971 } 972 973 error: 974 if (client) { 975 CLNT_DESTROY(client); 976 client = NULL; 977 } 978 done: 979 if (nconf->nc_semantics != NC_TPI_CLTS) { 980 /* This client is the connectionless one */ 981 if (client) { 982 CLNT_DESTROY(client); 983 client = NULL; 984 } 985 } 986 if (clpp) { 987 *clpp = client; 988 } else if (client) { 989 CLNT_DESTROY(client); 990 } 991 return (address); 992 } 993 994 995 /* 996 * Find the mapped address for program, version. 997 * Calls the rpcbind service remotely to do the lookup. 998 * Uses the transport specified in nconf. 999 * Returns FALSE (0) if no map exists, else returns 1. 1000 * 1001 * Assuming that the address is all properly allocated 1002 */ 1003 int 1004 rpcb_getaddr(program, version, nconf, address, host) 1005 rpcprog_t program; 1006 rpcvers_t version; 1007 const struct netconfig *nconf; 1008 struct netbuf *address; 1009 const char *host; 1010 { 1011 struct netbuf *na; 1012 1013 _DIAGASSERT(address != NULL); 1014 1015 if ((na = __rpcb_findaddr(program, version, nconf, 1016 host, (CLIENT **) NULL)) == NULL) 1017 return (FALSE); 1018 1019 if (na->len > address->maxlen) { 1020 /* Too long address */ 1021 free(na->buf); 1022 free(na); 1023 rpc_createerr.cf_stat = RPC_FAILED; 1024 return (FALSE); 1025 } 1026 memcpy(address->buf, na->buf, (size_t)na->len); 1027 address->len = na->len; 1028 free(na->buf); 1029 free(na); 1030 return (TRUE); 1031 } 1032 1033 /* 1034 * Get a copy of the current maps. 1035 * Calls the rpcbind service remotely to get the maps. 1036 * 1037 * It returns only a list of the services 1038 * It returns NULL on failure. 1039 */ 1040 rpcblist * 1041 rpcb_getmaps(nconf, host) 1042 const struct netconfig *nconf; 1043 const char *host; 1044 { 1045 rpcblist_ptr head = NULL; 1046 CLIENT *client; 1047 enum clnt_stat clnt_st; 1048 rpcvers_t vers = 0; 1049 1050 client = getclnthandle(host, nconf, NULL); 1051 if (client == NULL) { 1052 return (head); 1053 } 1054 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP, 1055 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr, 1056 (char *)(void *)&head, tottimeout); 1057 if (clnt_st == RPC_SUCCESS) 1058 goto done; 1059 1060 if ((clnt_st != RPC_PROGVERSMISMATCH) && 1061 (clnt_st != RPC_PROGUNAVAIL)) { 1062 rpc_createerr.cf_stat = RPC_RPCBFAILURE; 1063 clnt_geterr(client, &rpc_createerr.cf_error); 1064 goto done; 1065 } 1066 1067 /* fall back to earlier version */ 1068 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers); 1069 if (vers == RPCBVERS4) { 1070 vers = RPCBVERS; 1071 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); 1072 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP, 1073 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr, 1074 (char *)(void *)&head, tottimeout) == RPC_SUCCESS) 1075 goto done; 1076 } 1077 rpc_createerr.cf_stat = RPC_RPCBFAILURE; 1078 clnt_geterr(client, &rpc_createerr.cf_error); 1079 1080 done: 1081 CLNT_DESTROY(client); 1082 return (head); 1083 } 1084 1085 /* 1086 * rpcbinder remote-call-service interface. 1087 * This routine is used to call the rpcbind remote call service 1088 * which will look up a service program in the address maps, and then 1089 * remotely call that routine with the given parameters. This allows 1090 * programs to do a lookup and call in one step. 1091 */ 1092 enum clnt_stat 1093 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp, 1094 xdrres, resp, tout, addr_ptr) 1095 const struct netconfig *nconf; /* Netconfig structure */ 1096 const char *host; /* Remote host name */ 1097 rpcprog_t prog; 1098 rpcvers_t vers; 1099 rpcproc_t proc; /* Remote proc identifiers */ 1100 xdrproc_t xdrargs, xdrres; /* XDR routines */ 1101 caddr_t argsp, resp; /* Argument and Result */ 1102 struct timeval tout; /* Timeout value for this call */ 1103 const struct netbuf *addr_ptr; /* Preallocated netbuf address */ 1104 { 1105 CLIENT *client; 1106 enum clnt_stat stat; 1107 struct r_rpcb_rmtcallargs a; 1108 struct r_rpcb_rmtcallres r; 1109 rpcvers_t rpcb_vers; 1110 1111 stat = RPC_FAILED; /* XXXGCC -Wuninitialized [dreamcast] */ 1112 1113 client = getclnthandle(host, nconf, NULL); 1114 if (client == NULL) { 1115 return (RPC_FAILED); 1116 } 1117 /*LINTED const castaway*/ 1118 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout); 1119 a.prog = prog; 1120 a.vers = vers; 1121 a.proc = proc; 1122 a.args.args_val = argsp; 1123 a.xdr_args = xdrargs; 1124 r.addr = NULL; 1125 r.results.results_val = resp; 1126 r.xdr_res = xdrres; 1127 1128 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) { 1129 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers); 1130 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT, 1131 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a, 1132 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout); 1133 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) { 1134 struct netbuf *na; 1135 /*LINTED const castaway*/ 1136 na = uaddr2taddr((struct netconfig *) nconf, r.addr); 1137 if (!na) { 1138 stat = RPC_N2AXLATEFAILURE; 1139 /*LINTED const castaway*/ 1140 ((struct netbuf *) addr_ptr)->len = 0; 1141 goto error; 1142 } 1143 if (na->len > addr_ptr->maxlen) { 1144 /* Too long address */ 1145 stat = RPC_FAILED; /* XXX A better error no */ 1146 free(na->buf); 1147 free(na); 1148 /*LINTED const castaway*/ 1149 ((struct netbuf *) addr_ptr)->len = 0; 1150 goto error; 1151 } 1152 memcpy(addr_ptr->buf, na->buf, (size_t)na->len); 1153 /*LINTED const castaway*/ 1154 ((struct netbuf *)addr_ptr)->len = na->len; 1155 free(na->buf); 1156 free(na); 1157 break; 1158 } else if ((stat != RPC_PROGVERSMISMATCH) && 1159 (stat != RPC_PROGUNAVAIL)) { 1160 goto error; 1161 } 1162 } 1163 error: 1164 CLNT_DESTROY(client); 1165 if (r.addr) 1166 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr); 1167 return (stat); 1168 } 1169 1170 /* 1171 * Gets the time on the remote host. 1172 * Returns 1 if succeeds else 0. 1173 */ 1174 bool_t 1175 rpcb_gettime(host, timep) 1176 const char *host; 1177 time_t *timep; 1178 { 1179 CLIENT *client = NULL; 1180 void *handle; 1181 struct netconfig *nconf; 1182 rpcvers_t vers; 1183 enum clnt_stat st; 1184 1185 1186 if ((host == NULL) || (host[0] == 0)) { 1187 time(timep); 1188 return (TRUE); 1189 } 1190 1191 if ((handle = __rpc_setconf("netpath")) == NULL) { 1192 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1193 return (FALSE); 1194 } 1195 rpc_createerr.cf_stat = RPC_SUCCESS; 1196 while (client == NULL) { 1197 if ((nconf = __rpc_getconf(handle)) == NULL) { 1198 if (rpc_createerr.cf_stat == RPC_SUCCESS) 1199 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1200 break; 1201 } 1202 client = getclnthandle(host, nconf, NULL); 1203 if (client) 1204 break; 1205 } 1206 __rpc_endconf(handle); 1207 if (client == (CLIENT *) NULL) { 1208 return (FALSE); 1209 } 1210 1211 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME, 1212 (xdrproc_t) xdr_void, NULL, 1213 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout); 1214 1215 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) { 1216 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers); 1217 if (vers == RPCBVERS4) { 1218 /* fall back to earlier version */ 1219 vers = RPCBVERS; 1220 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); 1221 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME, 1222 (xdrproc_t) xdr_void, NULL, 1223 (xdrproc_t) xdr_int, (char *)(void *)timep, 1224 tottimeout); 1225 } 1226 } 1227 CLNT_DESTROY(client); 1228 return (st == RPC_SUCCESS? TRUE: FALSE); 1229 } 1230 1231 /* 1232 * Converts taddr to universal address. This routine should never 1233 * really be called because local n2a libraries are always provided. 1234 */ 1235 char * 1236 rpcb_taddr2uaddr(nconf, taddr) 1237 struct netconfig *nconf; 1238 struct netbuf *taddr; 1239 { 1240 CLIENT *client; 1241 char *uaddr = NULL; 1242 1243 1244 /* parameter checking */ 1245 if (nconf == NULL) { 1246 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1247 return (NULL); 1248 } 1249 if (taddr == NULL) { 1250 rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 1251 return (NULL); 1252 } 1253 client = local_rpcb(); 1254 if (! client) { 1255 return (NULL); 1256 } 1257 1258 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR, 1259 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr, 1260 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout); 1261 CLNT_DESTROY(client); 1262 return (uaddr); 1263 } 1264 1265 /* 1266 * Converts universal address to netbuf. This routine should never 1267 * really be called because local n2a libraries are always provided. 1268 */ 1269 struct netbuf * 1270 rpcb_uaddr2taddr(nconf, uaddr) 1271 struct netconfig *nconf; 1272 char *uaddr; 1273 { 1274 CLIENT *client; 1275 struct netbuf *taddr; 1276 1277 1278 /* parameter checking */ 1279 if (nconf == NULL) { 1280 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1281 return (NULL); 1282 } 1283 if (uaddr == NULL) { 1284 rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 1285 return (NULL); 1286 } 1287 client = local_rpcb(); 1288 if (! client) { 1289 return (NULL); 1290 } 1291 1292 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf)); 1293 if (taddr == NULL) { 1294 CLNT_DESTROY(client); 1295 return (NULL); 1296 } 1297 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR, 1298 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, 1299 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr, 1300 tottimeout) != RPC_SUCCESS) { 1301 free(taddr); 1302 taddr = NULL; 1303 } 1304 CLNT_DESTROY(client); 1305 return (taddr); 1306 } 1307