1 /* $NetBSD: rpcb_clnt.c,v 1.15 2003/10/21 00:07:17 fvdl 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.15 2003/10/21 00:07:17 fvdl 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 addr_to_delete.len = 0; 330 rwlock_rdlock(&rpcbaddr_cache_lock); 331 ad_cache = check_cache(host, nconf->nc_netid); 332 if (ad_cache != NULL) { 333 addr = ad_cache->ac_taddr; 334 client = clnt_tli_create(RPC_ANYFD, nconf, addr, 335 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0); 336 if (client != NULL) { 337 if (targaddr) 338 *targaddr = ad_cache->ac_uaddr; 339 rwlock_unlock(&rpcbaddr_cache_lock); 340 return (client); 341 } 342 addr_to_delete.len = addr->len; 343 addr_to_delete.buf = (char *)malloc(addr->len); 344 if (addr_to_delete.buf == NULL) { 345 addr_to_delete.len = 0; 346 } else { 347 memcpy(addr_to_delete.buf, addr->buf, addr->len); 348 } 349 } 350 rwlock_unlock(&rpcbaddr_cache_lock); 351 if (addr_to_delete.len != 0) { 352 /* 353 * Assume this may be due to cache data being 354 * outdated 355 */ 356 rwlock_wrlock(&rpcbaddr_cache_lock); 357 delete_cache(&addr_to_delete); 358 rwlock_unlock(&rpcbaddr_cache_lock); 359 free(addr_to_delete.buf); 360 } 361 if (!__rpc_nconf2sockinfo(nconf, &si)) { 362 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 363 return NULL; 364 } 365 366 memset(&hints, 0, sizeof hints); 367 hints.ai_family = si.si_af; 368 hints.ai_socktype = si.si_socktype; 369 hints.ai_protocol = si.si_proto; 370 371 #ifdef CLNT_DEBUG 372 printf("trying netid %s family %d proto %d socktype %d\n", 373 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype); 374 #endif 375 376 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) { 377 rpc_createerr.cf_stat = RPC_UNKNOWNHOST; 378 return NULL; 379 } 380 381 for (tres = res; tres != NULL; tres = tres->ai_next) { 382 taddr.buf = tres->ai_addr; 383 taddr.len = taddr.maxlen = tres->ai_addrlen; 384 385 #ifdef ND_DEBUG 386 { 387 char *ua; 388 389 ua = taddr2uaddr(nconf, &taddr); 390 fprintf(stderr, "Got it [%s]\n", ua); 391 free(ua); 392 } 393 #endif 394 395 #ifdef ND_DEBUG 396 { 397 int i; 398 399 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n", 400 taddr.len, taddr.maxlen); 401 fprintf(stderr, "\tAddress is "); 402 for (i = 0; i < taddr.len; i++) 403 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]); 404 fprintf(stderr, "\n"); 405 } 406 #endif 407 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr, 408 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0); 409 #ifdef ND_DEBUG 410 if (! client) { 411 clnt_pcreateerror("rpcbind clnt interface"); 412 } 413 #endif 414 415 if (client) { 416 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL; 417 add_cache(host, nconf->nc_netid, &taddr, tmpaddr); 418 if (targaddr) 419 *targaddr = tmpaddr; 420 break; 421 } 422 } 423 freeaddrinfo(res); 424 return (client); 425 } 426 427 /* XXX */ 428 #define IN4_LOCALHOST_STRING "127.0.0.1" 429 #define IN6_LOCALHOST_STRING "::1" 430 431 /* 432 * This routine will return a client handle that is connected to the local 433 * rpcbind. Returns NULL on error and free's everything. 434 */ 435 static CLIENT * 436 local_rpcb() 437 { 438 CLIENT *client; 439 static struct netconfig *loopnconf; 440 static char *hostname; 441 #ifdef _REENTRANT 442 extern mutex_t loopnconf_lock; 443 #endif 444 int sock; 445 size_t tsize; 446 struct netbuf nbuf; 447 struct sockaddr_un sun; 448 449 /* 450 * Try connecting to the local rpcbind through a local socket 451 * first. If this doesn't work, try all transports defined in 452 * the netconfig file. 453 */ 454 memset(&sun, 0, sizeof sun); 455 sock = socket(AF_LOCAL, SOCK_STREAM, 0); 456 if (sock < 0) 457 goto try_nconf; 458 sun.sun_family = AF_LOCAL; 459 strcpy(sun.sun_path, _PATH_RPCBINDSOCK); 460 nbuf.len = sun.sun_len = SUN_LEN(&sun); 461 nbuf.maxlen = sizeof (struct sockaddr_un); 462 nbuf.buf = &sun; 463 464 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0); 465 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG, 466 (rpcvers_t)RPCBVERS, tsize, tsize); 467 468 if (client != NULL) { 469 /* XXX - mark the socket to be closed in destructor */ 470 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL); 471 return client; 472 } 473 474 /* XXX - nobody needs this socket anymore, free the descriptor */ 475 close(sock); 476 477 try_nconf: 478 479 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */ 480 mutex_lock(&loopnconf_lock); 481 if (loopnconf == NULL) { 482 struct netconfig *nconf, *tmpnconf = NULL; 483 void *nc_handle; 484 int fd; 485 486 nc_handle = setnetconfig(); 487 if (nc_handle == NULL) { 488 /* fails to open netconfig file */ 489 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); 490 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 491 mutex_unlock(&loopnconf_lock); 492 return (NULL); 493 } 494 while ((nconf = getnetconfig(nc_handle)) != NULL) { 495 #ifdef INET6 496 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 || 497 #else 498 if (( 499 #endif 500 strcmp(nconf->nc_protofmly, NC_INET) == 0) && 501 (nconf->nc_semantics == NC_TPI_COTS || 502 nconf->nc_semantics == NC_TPI_COTS_ORD)) { 503 fd = __rpc_nconf2fd(nconf); 504 /* 505 * Can't create a socket, assume that 506 * this family isn't configured in the kernel. 507 */ 508 if (fd < 0) 509 continue; 510 close(fd); 511 tmpnconf = nconf; 512 if (!strcmp(nconf->nc_protofmly, NC_INET)) 513 hostname = IN4_LOCALHOST_STRING; 514 else 515 hostname = IN6_LOCALHOST_STRING; 516 } 517 } 518 if (tmpnconf == NULL) { 519 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 520 mutex_unlock(&loopnconf_lock); 521 return (NULL); 522 } 523 loopnconf = getnetconfigent(tmpnconf->nc_netid); 524 /* loopnconf is never freed */ 525 endnetconfig(nc_handle); 526 } 527 mutex_unlock(&loopnconf_lock); 528 client = getclnthandle(hostname, loopnconf, NULL); 529 return (client); 530 } 531 532 /* 533 * Set a mapping between program, version and address. 534 * Calls the rpcbind service to do the mapping. 535 */ 536 bool_t 537 rpcb_set(program, version, nconf, address) 538 rpcprog_t program; 539 rpcvers_t version; 540 const struct netconfig *nconf; /* Network structure of transport */ 541 const struct netbuf *address; /* Services netconfig address */ 542 { 543 CLIENT *client; 544 bool_t rslt = FALSE; 545 RPCB parms; 546 char uidbuf[32]; 547 548 /* parameter checking */ 549 if (nconf == NULL) { 550 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 551 return (FALSE); 552 } 553 if (address == NULL) { 554 rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 555 return (FALSE); 556 } 557 client = local_rpcb(); 558 if (! client) { 559 return (FALSE); 560 } 561 562 /* convert to universal */ 563 /*LINTED const castaway*/ 564 parms.r_addr = taddr2uaddr((struct netconfig *) nconf, 565 (struct netbuf *)address); 566 if (!parms.r_addr) { 567 CLNT_DESTROY(client); 568 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE; 569 return (FALSE); /* no universal address */ 570 } 571 parms.r_prog = program; 572 parms.r_vers = version; 573 parms.r_netid = nconf->nc_netid; 574 /* 575 * Though uid is not being used directly, we still send it for 576 * completeness. For non-unix platforms, perhaps some other 577 * string or an empty string can be sent. 578 */ 579 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid()); 580 parms.r_owner = uidbuf; 581 582 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb, 583 (char *)(void *)&parms, (xdrproc_t) xdr_bool, 584 (char *)(void *)&rslt, tottimeout); 585 586 CLNT_DESTROY(client); 587 free(parms.r_addr); 588 return (rslt); 589 } 590 591 /* 592 * Remove the mapping between program, version and netbuf address. 593 * Calls the rpcbind service to do the un-mapping. 594 * If netbuf is NULL, unset for all the transports, otherwise unset 595 * only for the given transport. 596 */ 597 bool_t 598 rpcb_unset(program, version, nconf) 599 rpcprog_t program; 600 rpcvers_t version; 601 const struct netconfig *nconf; 602 { 603 CLIENT *client; 604 bool_t rslt = FALSE; 605 RPCB parms; 606 char uidbuf[32]; 607 608 client = local_rpcb(); 609 if (! client) { 610 return (FALSE); 611 } 612 613 parms.r_prog = program; 614 parms.r_vers = version; 615 if (nconf) 616 parms.r_netid = nconf->nc_netid; 617 else { 618 /*LINTED const castaway*/ 619 parms.r_netid = (char *) &nullstring[0]; /* unsets all */ 620 } 621 /*LINTED const castaway*/ 622 parms.r_addr = (char *) &nullstring[0]; 623 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid()); 624 parms.r_owner = uidbuf; 625 626 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb, 627 (char *)(void *)&parms, (xdrproc_t) xdr_bool, 628 (char *)(void *)&rslt, tottimeout); 629 630 CLNT_DESTROY(client); 631 return (rslt); 632 } 633 634 /* 635 * From the merged list, find the appropriate entry 636 */ 637 static struct netbuf * 638 got_entry(relp, nconf) 639 rpcb_entry_list_ptr relp; 640 const struct netconfig *nconf; 641 { 642 struct netbuf *na = NULL; 643 rpcb_entry_list_ptr sp; 644 rpcb_entry *rmap; 645 646 _DIAGASSERT(nconf != NULL); 647 648 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) { 649 rmap = &sp->rpcb_entry_map; 650 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) && 651 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) && 652 (nconf->nc_semantics == rmap->r_nc_semantics) && 653 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) { 654 na = uaddr2taddr(nconf, rmap->r_maddr); 655 #ifdef ND_DEBUG 656 fprintf(stderr, "\tRemote address is [%s].\n", 657 rmap->r_maddr); 658 if (!na) 659 fprintf(stderr, 660 "\tCouldn't resolve remote address!\n"); 661 #endif 662 break; 663 } 664 } 665 return (na); 666 } 667 668 /* 669 * An internal function which optimizes rpcb_getaddr function. It also 670 * returns the client handle that it uses to contact the remote rpcbind. 671 * 672 * The algorithm used: If the transports is TCP or UDP, it first tries 673 * version 2 (portmap), 4 and then 3 (svr4). This order should be 674 * changed in the next OS release to 4, 2 and 3. We are assuming that by 675 * that time, version 4 would be available on many machines on the network. 676 * With this algorithm, we get performance as well as a plan for 677 * obsoleting version 2. 678 * 679 * For all other transports, the algorithm remains as 4 and then 3. 680 * 681 * XXX: Due to some problems with t_connect(), we do not reuse the same client 682 * handle for COTS cases and hence in these cases we do not return the 683 * client handle. This code will change if t_connect() ever 684 * starts working properly. Also look under clnt_vc.c. 685 */ 686 struct netbuf * 687 __rpcb_findaddr(program, version, nconf, host, clpp) 688 rpcprog_t program; 689 rpcvers_t version; 690 const struct netconfig *nconf; 691 const char *host; 692 CLIENT **clpp; 693 { 694 CLIENT *client = NULL; 695 RPCB parms; 696 enum clnt_stat clnt_st; 697 char *ua = NULL; 698 rpcvers_t vers; 699 struct netbuf *address = NULL; 700 rpcvers_t start_vers = RPCBVERS4; 701 struct netbuf servaddr; 702 703 /* nconf is handled below */ 704 _DIAGASSERT(host != NULL); 705 /* clpp may be NULL */ 706 707 /* parameter checking */ 708 if (nconf == NULL) { 709 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 710 return (NULL); 711 } 712 713 parms.r_addr = NULL; 714 715 #ifdef PORTMAP 716 /* Try version 2 for TCP or UDP */ 717 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { 718 u_short port = 0; 719 struct netbuf remote; 720 rpcvers_t pmapvers = 2; 721 struct pmap pmapparms; 722 723 /* 724 * Try UDP only - there are some portmappers out 725 * there that use UDP only. 726 */ 727 if (strcmp(nconf->nc_proto, NC_TCP) == 0) { 728 struct netconfig *newnconf; 729 730 if ((newnconf = getnetconfigent("udp")) == NULL) { 731 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 732 return (NULL); 733 } 734 client = getclnthandle(host, newnconf, &parms.r_addr); 735 freenetconfigent(newnconf); 736 } else { 737 client = getclnthandle(host, nconf, &parms.r_addr); 738 } 739 if (client == NULL) { 740 return (NULL); 741 } 742 743 /* Set the version */ 744 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&pmapvers); 745 pmapparms.pm_prog = program; 746 pmapparms.pm_vers = version; 747 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ? 748 IPPROTO_UDP : IPPROTO_TCP; 749 pmapparms.pm_port = 0; /* not needed */ 750 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT, 751 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms, 752 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port, 753 tottimeout); 754 if (clnt_st != RPC_SUCCESS) { 755 if ((clnt_st == RPC_PROGVERSMISMATCH) || 756 (clnt_st == RPC_PROGUNAVAIL)) 757 goto try_rpcbind; /* Try different versions */ 758 rpc_createerr.cf_stat = RPC_PMAPFAILURE; 759 clnt_geterr(client, &rpc_createerr.cf_error); 760 goto error; 761 } else if (port == 0) { 762 address = NULL; 763 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED; 764 goto error; 765 } 766 port = htons(port); 767 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&remote); 768 if (((address = (struct netbuf *) 769 malloc(sizeof (struct netbuf))) == NULL) || 770 ((address->buf = (char *) 771 malloc(remote.len)) == NULL)) { 772 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 773 clnt_geterr(client, &rpc_createerr.cf_error); 774 if (address) { 775 free(address); 776 address = NULL; 777 } 778 goto error; 779 } 780 memcpy(address->buf, remote.buf, remote.len); 781 memcpy(&((char *)address->buf)[sizeof (short)], 782 (char *)(void *)&port, sizeof (short)); 783 address->len = address->maxlen = remote.len; 784 goto done; 785 } 786 #endif 787 788 try_rpcbind: 789 /* 790 * Now we try version 4 and then 3. 791 * We also send the remote system the address we used to 792 * contact it in case it can help to connect back with us 793 */ 794 parms.r_prog = program; 795 parms.r_vers = version; 796 /*LINTED const castaway*/ 797 parms.r_owner = (char *) &nullstring[0]; /* not needed; */ 798 /* just for xdring */ 799 parms.r_netid = nconf->nc_netid; /* not really needed */ 800 801 /* 802 * If a COTS transport is being used, try getting address via CLTS 803 * transport. This works only with version 4. 804 * NOTE: This is being done for all transports EXCEPT LOOPBACK 805 * because with loopback the cost to go to a COTS is same as 806 * the cost to go through CLTS, plus you get the advantage of 807 * finding out immediately if the local rpcbind process is dead. 808 */ 809 #if 1 810 if ((nconf->nc_semantics == NC_TPI_COTS_ORD || 811 nconf->nc_semantics == NC_TPI_COTS) && 812 (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0)) 813 #else 814 if (client != NULL) { 815 CLNT_DESTROY(client); 816 client = NULL; 817 } 818 if (nconf->nc_semantics == NC_TPI_CLTS) 819 #endif 820 { 821 void *handle; 822 struct netconfig *nconf_clts; 823 rpcb_entry_list_ptr relp = NULL; 824 825 if (client == NULL) { 826 /* This did not go through the above PORTMAP/TCP code */ 827 #if 1 828 if ((handle = __rpc_setconf("datagram_v")) != NULL) 829 #else 830 if ((handle = __rpc_setconf("circuit_v")) != NULL) 831 #endif 832 { 833 while ((nconf_clts = __rpc_getconf(handle)) 834 != NULL) { 835 if (strcmp(nconf_clts->nc_protofmly, 836 nconf->nc_protofmly) != 0) { 837 continue; 838 } 839 client = getclnthandle(host, nconf_clts, 840 &parms.r_addr); 841 break; 842 } 843 __rpc_endconf(handle); 844 } 845 if (client == NULL) 846 goto regular_rpcbind; /* Go the regular way */ 847 } else { 848 /* This is a UDP PORTMAP handle. Change to version 4 */ 849 vers = RPCBVERS4; 850 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); 851 } 852 /* 853 * We also send the remote system the address we used to 854 * contact it in case it can help it connect back with us 855 */ 856 if (parms.r_addr == NULL) { 857 /*LINTED const castaway*/ 858 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */ 859 } 860 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST, 861 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms, 862 (xdrproc_t) xdr_rpcb_entry_list_ptr, 863 (char *)(void *)&relp, tottimeout); 864 if (clnt_st == RPC_SUCCESS) { 865 if ((address = got_entry(relp, nconf)) != NULL) { 866 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr, 867 (char *)(void *)&relp); 868 CLNT_CONTROL(client, CLGET_SVC_ADDR, 869 (char *)(void *)&servaddr); 870 __rpc_fixup_addr(address, &servaddr); 871 goto done; 872 } 873 /* Entry not found for this transport */ 874 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr, 875 (char *)(void *)&relp); 876 /* 877 * XXX: should have perhaps returned with error but 878 * since the remote machine might not always be able 879 * to send the address on all transports, we try the 880 * regular way with regular_rpcbind 881 */ 882 goto regular_rpcbind; 883 } else if ((clnt_st == RPC_PROGVERSMISMATCH) || 884 (clnt_st == RPC_PROGUNAVAIL)) { 885 start_vers = RPCBVERS; /* Try version 3 now */ 886 goto regular_rpcbind; /* Try different versions */ 887 } else { 888 rpc_createerr.cf_stat = RPC_PMAPFAILURE; 889 clnt_geterr(client, &rpc_createerr.cf_error); 890 goto error; 891 } 892 } 893 894 regular_rpcbind: 895 896 /* Now the same transport is to be used to get the address */ 897 #if 1 898 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) || 899 (nconf->nc_semantics == NC_TPI_COTS))) 900 #else 901 if (client && nconf->nc_semantics == NC_TPI_CLTS) 902 #endif 903 { 904 /* A CLTS type of client - destroy it */ 905 CLNT_DESTROY(client); 906 client = NULL; 907 } 908 909 if (client == NULL) { 910 client = getclnthandle(host, nconf, &parms.r_addr); 911 if (client == NULL) { 912 goto error; 913 } 914 } 915 if (parms.r_addr == NULL) { 916 /*LINTED const castaway*/ 917 parms.r_addr = (char *) &nullstring[0]; 918 } 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 if ((address == NULL) || (address->len == 0)) { 968 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED; 969 clnt_geterr(client, &rpc_createerr.cf_error); 970 } 971 972 error: 973 if (client) { 974 CLNT_DESTROY(client); 975 client = NULL; 976 } 977 done: 978 if (nconf->nc_semantics != NC_TPI_CLTS) { 979 /* This client is the connectionless one */ 980 if (client) { 981 CLNT_DESTROY(client); 982 client = NULL; 983 } 984 } 985 if (clpp) { 986 *clpp = client; 987 } else if (client) { 988 CLNT_DESTROY(client); 989 } 990 return (address); 991 } 992 993 994 /* 995 * Find the mapped address for program, version. 996 * Calls the rpcbind service remotely to do the lookup. 997 * Uses the transport specified in nconf. 998 * Returns FALSE (0) if no map exists, else returns 1. 999 * 1000 * Assuming that the address is all properly allocated 1001 */ 1002 int 1003 rpcb_getaddr(program, version, nconf, address, host) 1004 rpcprog_t program; 1005 rpcvers_t version; 1006 const struct netconfig *nconf; 1007 struct netbuf *address; 1008 const char *host; 1009 { 1010 struct netbuf *na; 1011 1012 _DIAGASSERT(address != NULL); 1013 1014 if ((na = __rpcb_findaddr(program, version, nconf, 1015 host, (CLIENT **) NULL)) == NULL) 1016 return (FALSE); 1017 1018 if (na->len > address->maxlen) { 1019 /* Too long address */ 1020 free(na->buf); 1021 free(na); 1022 rpc_createerr.cf_stat = RPC_FAILED; 1023 return (FALSE); 1024 } 1025 memcpy(address->buf, na->buf, (size_t)na->len); 1026 address->len = na->len; 1027 free(na->buf); 1028 free(na); 1029 return (TRUE); 1030 } 1031 1032 /* 1033 * Get a copy of the current maps. 1034 * Calls the rpcbind service remotely to get the maps. 1035 * 1036 * It returns only a list of the services 1037 * It returns NULL on failure. 1038 */ 1039 rpcblist * 1040 rpcb_getmaps(nconf, host) 1041 const struct netconfig *nconf; 1042 const char *host; 1043 { 1044 rpcblist_ptr head = NULL; 1045 CLIENT *client; 1046 enum clnt_stat clnt_st; 1047 rpcvers_t vers = 0; 1048 1049 client = getclnthandle(host, nconf, NULL); 1050 if (client == NULL) { 1051 return (head); 1052 } 1053 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP, 1054 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr, 1055 (char *)(void *)&head, tottimeout); 1056 if (clnt_st == RPC_SUCCESS) 1057 goto done; 1058 1059 if ((clnt_st != RPC_PROGVERSMISMATCH) && 1060 (clnt_st != RPC_PROGUNAVAIL)) { 1061 rpc_createerr.cf_stat = RPC_RPCBFAILURE; 1062 clnt_geterr(client, &rpc_createerr.cf_error); 1063 goto done; 1064 } 1065 1066 /* fall back to earlier version */ 1067 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers); 1068 if (vers == RPCBVERS4) { 1069 vers = RPCBVERS; 1070 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); 1071 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP, 1072 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr, 1073 (char *)(void *)&head, tottimeout) == RPC_SUCCESS) 1074 goto done; 1075 } 1076 rpc_createerr.cf_stat = RPC_RPCBFAILURE; 1077 clnt_geterr(client, &rpc_createerr.cf_error); 1078 1079 done: 1080 CLNT_DESTROY(client); 1081 return (head); 1082 } 1083 1084 /* 1085 * rpcbinder remote-call-service interface. 1086 * This routine is used to call the rpcbind remote call service 1087 * which will look up a service program in the address maps, and then 1088 * remotely call that routine with the given parameters. This allows 1089 * programs to do a lookup and call in one step. 1090 */ 1091 enum clnt_stat 1092 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp, 1093 xdrres, resp, tout, addr_ptr) 1094 const struct netconfig *nconf; /* Netconfig structure */ 1095 const char *host; /* Remote host name */ 1096 rpcprog_t prog; 1097 rpcvers_t vers; 1098 rpcproc_t proc; /* Remote proc identifiers */ 1099 xdrproc_t xdrargs, xdrres; /* XDR routines */ 1100 caddr_t argsp, resp; /* Argument and Result */ 1101 struct timeval tout; /* Timeout value for this call */ 1102 const struct netbuf *addr_ptr; /* Preallocated netbuf address */ 1103 { 1104 CLIENT *client; 1105 enum clnt_stat stat; 1106 struct r_rpcb_rmtcallargs a; 1107 struct r_rpcb_rmtcallres r; 1108 rpcvers_t rpcb_vers; 1109 1110 client = getclnthandle(host, nconf, NULL); 1111 if (client == NULL) { 1112 return (RPC_FAILED); 1113 } 1114 /*LINTED const castaway*/ 1115 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout); 1116 a.prog = prog; 1117 a.vers = vers; 1118 a.proc = proc; 1119 a.args.args_val = argsp; 1120 a.xdr_args = xdrargs; 1121 r.addr = NULL; 1122 r.results.results_val = resp; 1123 r.xdr_res = xdrres; 1124 1125 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) { 1126 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers); 1127 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT, 1128 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a, 1129 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout); 1130 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) { 1131 struct netbuf *na; 1132 /*LINTED const castaway*/ 1133 na = uaddr2taddr((struct netconfig *) nconf, r.addr); 1134 if (!na) { 1135 stat = RPC_N2AXLATEFAILURE; 1136 /*LINTED const castaway*/ 1137 ((struct netbuf *) addr_ptr)->len = 0; 1138 goto error; 1139 } 1140 if (na->len > addr_ptr->maxlen) { 1141 /* Too long address */ 1142 stat = RPC_FAILED; /* XXX A better error no */ 1143 free(na->buf); 1144 free(na); 1145 /*LINTED const castaway*/ 1146 ((struct netbuf *) addr_ptr)->len = 0; 1147 goto error; 1148 } 1149 memcpy(addr_ptr->buf, na->buf, (size_t)na->len); 1150 /*LINTED const castaway*/ 1151 ((struct netbuf *)addr_ptr)->len = na->len; 1152 free(na->buf); 1153 free(na); 1154 break; 1155 } else if ((stat != RPC_PROGVERSMISMATCH) && 1156 (stat != RPC_PROGUNAVAIL)) { 1157 goto error; 1158 } 1159 } 1160 error: 1161 CLNT_DESTROY(client); 1162 if (r.addr) 1163 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr); 1164 return (stat); 1165 } 1166 1167 /* 1168 * Gets the time on the remote host. 1169 * Returns 1 if succeeds else 0. 1170 */ 1171 bool_t 1172 rpcb_gettime(host, timep) 1173 const char *host; 1174 time_t *timep; 1175 { 1176 CLIENT *client = NULL; 1177 void *handle; 1178 struct netconfig *nconf; 1179 rpcvers_t vers; 1180 enum clnt_stat st; 1181 1182 1183 if ((host == NULL) || (host[0] == 0)) { 1184 time(timep); 1185 return (TRUE); 1186 } 1187 1188 if ((handle = __rpc_setconf("netpath")) == NULL) { 1189 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1190 return (FALSE); 1191 } 1192 rpc_createerr.cf_stat = RPC_SUCCESS; 1193 while (client == NULL) { 1194 if ((nconf = __rpc_getconf(handle)) == NULL) { 1195 if (rpc_createerr.cf_stat == RPC_SUCCESS) 1196 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1197 break; 1198 } 1199 client = getclnthandle(host, nconf, NULL); 1200 if (client) 1201 break; 1202 } 1203 __rpc_endconf(handle); 1204 if (client == (CLIENT *) NULL) { 1205 return (FALSE); 1206 } 1207 1208 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME, 1209 (xdrproc_t) xdr_void, NULL, 1210 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout); 1211 1212 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) { 1213 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers); 1214 if (vers == RPCBVERS4) { 1215 /* fall back to earlier version */ 1216 vers = RPCBVERS; 1217 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers); 1218 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME, 1219 (xdrproc_t) xdr_void, NULL, 1220 (xdrproc_t) xdr_int, (char *)(void *)timep, 1221 tottimeout); 1222 } 1223 } 1224 CLNT_DESTROY(client); 1225 return (st == RPC_SUCCESS? TRUE: FALSE); 1226 } 1227 1228 /* 1229 * Converts taddr to universal address. This routine should never 1230 * really be called because local n2a libraries are always provided. 1231 */ 1232 char * 1233 rpcb_taddr2uaddr(nconf, taddr) 1234 struct netconfig *nconf; 1235 struct netbuf *taddr; 1236 { 1237 CLIENT *client; 1238 char *uaddr = NULL; 1239 1240 1241 /* parameter checking */ 1242 if (nconf == NULL) { 1243 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1244 return (NULL); 1245 } 1246 if (taddr == NULL) { 1247 rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 1248 return (NULL); 1249 } 1250 client = local_rpcb(); 1251 if (! client) { 1252 return (NULL); 1253 } 1254 1255 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR, 1256 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr, 1257 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout); 1258 CLNT_DESTROY(client); 1259 return (uaddr); 1260 } 1261 1262 /* 1263 * Converts universal address to netbuf. This routine should never 1264 * really be called because local n2a libraries are always provided. 1265 */ 1266 struct netbuf * 1267 rpcb_uaddr2taddr(nconf, uaddr) 1268 struct netconfig *nconf; 1269 char *uaddr; 1270 { 1271 CLIENT *client; 1272 struct netbuf *taddr; 1273 1274 1275 /* parameter checking */ 1276 if (nconf == NULL) { 1277 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1278 return (NULL); 1279 } 1280 if (uaddr == NULL) { 1281 rpc_createerr.cf_stat = RPC_UNKNOWNADDR; 1282 return (NULL); 1283 } 1284 client = local_rpcb(); 1285 if (! client) { 1286 return (NULL); 1287 } 1288 1289 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf)); 1290 if (taddr == NULL) { 1291 CLNT_DESTROY(client); 1292 return (NULL); 1293 } 1294 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR, 1295 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, 1296 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr, 1297 tottimeout) != RPC_SUCCESS) { 1298 free(taddr); 1299 taddr = NULL; 1300 } 1301 CLNT_DESTROY(client); 1302 return (taddr); 1303 } 1304