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