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