4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
30 /*
31 * Portions of this source code were derived from Berkeley
32 * 4.3 BSD under license from the Regents of the University of
33 * California.
34 */
35
36 #pragma ident "%Z%%M% %I% %E% SMI"
37
38 /*
39 * interface to rpcbind rpc service.
40 */
41
42 #include "mt.h"
43 #include "rpc_mt.h"
44 #include <assert.h>
45 #include <rpc/rpc.h>
46 #include <rpc/rpcb_prot.h>
47 #include <netconfig.h>
48 #include <netdir.h>
49 #include <rpc/nettype.h>
50 #include <syslog.h>
51 #ifdef PORTMAP
52 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
53 #include <rpc/pmap_prot.h>
54 #endif
55 #ifdef ND_DEBUG
56 #include <stdio.h>
57 #endif
58 #include <sys/utsname.h>
59 #include <errno.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 static struct timeval tottimeout = { 60, 0 };
65 static const struct timeval rmttimeout = { 3, 0 };
66 static struct timeval rpcbrmttime = { 15, 0 };
67
68 extern bool_t xdr_wrapstring(XDR *, char **);
69
70 static const char nullstring[] = "\000";
71
72 extern CLIENT *_clnt_tli_create_timed(int, const struct netconfig *,
73 struct netbuf *, rpcprog_t, rpcvers_t, uint_t, uint_t,
74 const struct timeval *);
75
76 static CLIENT *_getclnthandle_timed(char *, struct netconfig *, char **,
77 struct timeval *);
153 */
154 extern rwlock_t rpcbaddr_cache_lock;
155
156 /*
157 * The routines check_cache(), add_cache(), delete_cache() manage the
158 * cache of rpcbind addresses for (host, netid).
159 */
160
161 static struct address_cache *
162 check_cache(char *host, char *netid)
163 {
164 struct address_cache *cptr;
165
166 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
167
168 assert(RW_READ_HELD(&rpcbaddr_cache_lock));
169 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
170 if ((strcmp(cptr->ac_host, host) == 0) &&
171 (strcmp(cptr->ac_netid, netid) == 0) &&
172 (time(NULL) <= cptr->ac_maxtime)) {
173 #ifdef ND_DEBUG
174 fprintf(stderr, "Found cache entry for %s: %s\n",
175 host, netid);
176 #endif
177 return (cptr);
178 }
179 }
180 return (NULL);
181 }
182
183 static void
184 delete_cache(struct netbuf *addr)
185 {
186 struct address_cache *cptr, *prevptr = NULL;
187
188 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
189 assert(RW_WRITE_HELD(&rpcbaddr_cache_lock));
190 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
191 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
192 free(cptr->ac_host);
193 free(cptr->ac_netid);
194 free(cptr->ac_taddr->buf);
195 free(cptr->ac_taddr);
196 if (cptr->ac_uaddr)
216 if (!ad_cache) {
217 goto memerr;
218 }
219 ad_cache->ac_maxtime = time(NULL) + CACHE_TTL;
220 ad_cache->ac_host = strdup(host);
221 ad_cache->ac_netid = strdup(netid);
222 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
223 ad_cache->ac_taddr = malloc(sizeof (struct netbuf));
224 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
225 (uaddr && !ad_cache->ac_uaddr)) {
226 goto memerr1;
227 }
228
229 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
230 ad_cache->ac_taddr->buf = malloc(taddr->len);
231 if (ad_cache->ac_taddr->buf == NULL) {
232 goto memerr1;
233 }
234
235 (void) memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
236 #ifdef ND_DEBUG
237 (void) fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
238 #endif
239
240 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
241
242 (void) rw_wrlock(&rpcbaddr_cache_lock);
243 if (cachesize < CACHESIZE) {
244 ad_cache->ac_next = front;
245 front = ad_cache;
246 cachesize++;
247 } else {
248 /* Free the last entry */
249 cptr = front;
250 prevptr = NULL;
251 while (cptr->ac_next) {
252 prevptr = cptr;
253 cptr = cptr->ac_next;
254 }
255
256 #ifdef ND_DEBUG
257 fprintf(stderr, "Deleted from cache: %s : %s\n",
258 cptr->ac_host, cptr->ac_netid);
259 #endif
260 free(cptr->ac_host);
261 free(cptr->ac_netid);
262 free(cptr->ac_taddr->buf);
263 free(cptr->ac_taddr);
264 if (cptr->ac_uaddr)
265 free(cptr->ac_uaddr);
266
267 if (prevptr) {
268 prevptr->ac_next = NULL;
269 ad_cache->ac_next = front;
270 front = ad_cache;
271 } else {
272 front = ad_cache;
273 ad_cache->ac_next = NULL;
274 }
275 free(cptr);
276 }
277 (void) rw_unlock(&rpcbaddr_cache_lock);
278 return;
279 memerr1:
364 addr_to_delete.buf = malloc(addr->len);
365 if (addr_to_delete.buf == NULL) {
366 addr_to_delete.len = 0;
367 } else {
368 (void) memcpy(addr_to_delete.buf, addr->buf, addr->len);
369 }
370 }
371 (void) rw_unlock(&rpcbaddr_cache_lock);
372 if (addr_to_delete.len != 0) {
373 /*
374 * Assume this may be due to cache data being
375 * outdated
376 */
377 (void) rw_wrlock(&rpcbaddr_cache_lock);
378 delete_cache(&addr_to_delete);
379 (void) rw_unlock(&rpcbaddr_cache_lock);
380 free(addr_to_delete.buf);
381 }
382 rpcbind_hs.h_host = host;
383 rpcbind_hs.h_serv = "rpcbind";
384 #ifdef ND_DEBUG
385 fprintf(stderr, "rpcbind client routines: diagnostics :\n");
386 fprintf(stderr, "\tGetting address for (%s, %s, %s) ... \n",
387 rpcbind_hs.h_host, rpcbind_hs.h_serv, nconf->nc_netid);
388 #endif
389
390 if ((neterr = netdir_getbyname(nconf, &rpcbind_hs, &nas)) != 0) {
391 if (neterr == ND_NOHOST)
392 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
393 else
394 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
395 return (NULL);
396 }
397 /* XXX nas should perhaps be cached for better performance */
398
399 for (j = 0; j < nas->n_cnt; j++) {
400 addr = &(nas->n_addrs[j]);
401 #ifdef ND_DEBUG
402 {
403 int i;
404 char *ua;
405
406 ua = taddr2uaddr(nconf, &(nas->n_addrs[j]));
407 fprintf(stderr, "Got it [%s]\n", ua);
408 free(ua);
409
410 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
411 addr->len, addr->maxlen);
412 fprintf(stderr, "\tAddress is ");
413 for (i = 0; i < addr->len; i++)
414 fprintf(stderr, "%u.", addr->buf[i]);
415 fprintf(stderr, "\n");
416 }
417 #endif
418 client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr, RPCBPROG,
419 RPCBVERS4, 0, 0, tp);
420 if (client)
421 break;
422 }
423 #ifdef ND_DEBUG
424 if (!client) {
425 clnt_pcreateerror("rpcbind clnt interface");
426 }
427 #endif
428
429 if (client) {
430 tmpaddr = targaddr ? taddr2uaddr(nconf, addr) : NULL;
431 add_cache(host, nconf->nc_netid, addr, tmpaddr);
432 if (targaddr) {
433 *targaddr = tmpaddr;
434 }
435 }
436 netdir_free((char *)nas, ND_ADDRLIST);
437 return (client);
438 }
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(void)
446 {
447 static struct netconfig *loopnconf;
448 static char *hostname;
449 extern mutex_t loopnconf_lock;
450
451 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
452 (void) mutex_lock(&loopnconf_lock);
453 if (loopnconf == NULL) {
454 struct utsname utsname;
455 struct netconfig *nconf, *tmpnconf = NULL;
456 void *nc_handle;
457
458 if (hostname == NULL) {
459 #if defined(__i386) && !defined(__amd64)
460 if ((_nuname(&utsname) == -1) ||
461 #else
462 if ((uname(&utsname) == -1) ||
463 #endif
464 ((hostname = strdup(utsname.nodename)) == NULL)) {
465 syslog(LOG_ERR, "local_rpcb : strdup failed.");
466 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
467 (void) mutex_unlock(&loopnconf_lock);
468 return (NULL);
469 }
470 }
471 nc_handle = setnetconfig();
472 if (nc_handle == NULL) {
473 /* fails to open netconfig file */
474 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
475 (void) mutex_unlock(&loopnconf_lock);
476 return (NULL);
477 }
478 while (nconf = getnetconfig(nc_handle)) {
479 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
480 tmpnconf = nconf;
481 if (nconf->nc_semantics == NC_TPI_CLTS)
482 break;
483 }
484 }
485 if (tmpnconf == NULL) {
486 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
487 (void) mutex_unlock(&loopnconf_lock);
488 return (NULL);
489 }
582 return (rslt);
583 }
584
585 /*
586 * From the merged list, find the appropriate entry
587 */
588 static struct netbuf *
589 got_entry(rpcb_entry_list_ptr relp, struct netconfig *nconf)
590 {
591 struct netbuf *na = NULL;
592 rpcb_entry_list_ptr sp;
593 rpcb_entry *rmap;
594
595 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
596 rmap = &sp->rpcb_entry_map;
597 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
598 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
599 (nconf->nc_semantics == rmap->r_nc_semantics) &&
600 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
601 na = uaddr2taddr(nconf, rmap->r_maddr);
602 #ifdef ND_DEBUG
603 fprintf(stderr, "\tRemote address is [%s].\n",
604 rmap->r_maddr);
605 if (!na)
606 fprintf(stderr,
607 "\tCouldn't resolve remote address!\n");
608 #endif
609 break;
610 }
611 }
612 return (na);
613 }
614
615 /*
616 * Quick check to see if rpcbind is up. Tries to connect over
617 * local transport.
618 */
619 bool_t
620 __rpcbind_is_up(void)
621 {
622 struct utsname name;
623 char uaddr[SYS_NMLN];
624 struct netbuf *addr;
625 int fd;
626 struct t_call *sndcall;
627 struct netconfig *netconf;
628 bool_t res;
669 sndcall->addr.len = addr->len;
670 sndcall->addr.buf = addr->buf;
671
672 if (t_connect(fd, sndcall, NULL) == -1)
673 res = FALSE;
674 else
675 res = TRUE;
676
677 sndcall->addr.maxlen = sndcall->addr.len = 0;
678 sndcall->addr.buf = NULL;
679 (void) t_free((char *)sndcall, T_CALL);
680 free(addr->buf);
681 free(addr);
682 (void) t_close(fd);
683
684 return (res);
685 }
686
687
688 /*
689 * An internal function which optimizes rpcb_getaddr function. It also
690 * returns the client handle that it uses to contact the remote rpcbind.
691 *
692 * The algorithm used: If the transports is TCP or UDP, it first tries
693 * version 2 (portmap), 4 and then 3 (svr4). This order should be
694 * changed in the next OS release to 4, 2 and 3. We are assuming that by
695 * that time, version 4 would be available on many machines on the network.
696 * With this algorithm, we get performance as well as a plan for
697 * obsoleting version 2.
698 *
699 * For all other transports, the algorithm remains as 4 and then 3.
700 *
701 * XXX: Due to some problems with t_connect(), we do not reuse the same client
702 * handle for COTS cases and hence in these cases we do not return the
703 * client handle. This code will change if t_connect() ever
704 * starts working properly. Also look under clnt_vc.c.
705 */
706 struct netbuf *
707 __rpcb_findaddr_timed(rpcprog_t program, rpcvers_t version,
708 struct netconfig *nconf, char *host, CLIENT **clpp, struct timeval *tp)
709 {
710 static bool_t check_rpcbind = TRUE;
711 CLIENT *client = NULL;
712 RPCB parms;
713 enum clnt_stat clnt_st;
714 char *ua = NULL;
715 uint_t vers;
716 struct netbuf *address = NULL;
717 uint_t start_vers = RPCBVERS4;
718
719 /* parameter checking */
720 if (nconf == NULL) {
721 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
722 return (NULL);
723 }
724
725 parms.r_addr = NULL;
726
727 /*
728 * Use default total timeout if no timeout is specified.
729 */
730 if (tp == NULL)
731 tp = &tottimeout;
732
733 #ifdef PORTMAP
734 /* Try version 2 for TCP or UDP */
735 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
736 ushort_t port = 0;
737 struct netbuf remote;
738 uint_t pmapvers = 2;
739 struct pmap pmapparms;
740
741 /*
742 * Try UDP only - there are some portmappers out
743 * there that use UDP only.
744 */
745 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
746 struct netconfig *newnconf;
747 void *handle;
748
749 if ((handle = __rpc_setconf("udp")) == NULL) {
750 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
751 return (NULL);
752 }
753
754 /*
755 * The following to reinforce that you can
756 * only request for remote address through
757 * the same transport you are requesting.
758 * ie. requesting unversial address
759 * of IPv4 has to be carried through IPv4.
760 * Can't use IPv6 to send out the request.
761 * The mergeaddr in rpcbind can't handle
762 * this.
763 */
764 for (;;) {
765 if ((newnconf = __rpc_getconf(handle))
766 == NULL) {
767 __rpc_endconf(handle);
768 rpc_createerr.cf_stat =
769 RPC_UNKNOWNPROTO;
770 return (NULL);
771 }
772 /*
773 * here check the protocol family to
774 * be consistent with the request one
775 */
776 if (strcmp(newnconf->nc_protofmly,
777 nconf->nc_protofmly) == NULL)
778 break;
779 }
780
781 client = _getclnthandle_timed(host, newnconf,
782 &parms.r_addr, tp);
783 __rpc_endconf(handle);
784 } else {
785 client = _getclnthandle_timed(host, nconf,
786 &parms.r_addr, tp);
787 }
788 if (client == NULL)
789 return (NULL);
790
791 /*
792 * Set version and retry timeout.
793 */
794 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
795 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
796
797 pmapparms.pm_prog = program;
798 pmapparms.pm_vers = version;
799 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
800 IPPROTO_UDP : IPPROTO_TCP;
801 pmapparms.pm_port = 0; /* not needed */
802 clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
803 (xdrproc_t)xdr_pmap, (caddr_t)&pmapparms,
804 (xdrproc_t)xdr_u_short, (caddr_t)&port,
805 *tp);
806 if (clnt_st != RPC_SUCCESS) {
807 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
808 (clnt_st == RPC_PROGUNAVAIL))
809 goto try_rpcbind; /* Try different versions */
810 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
811 clnt_geterr(client, &rpc_createerr.cf_error);
812 goto error;
813 } else if (port == 0) {
814 address = NULL;
815 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
816 goto error;
817 }
818 port = htons(port);
819 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
820 if (((address = malloc(sizeof (struct netbuf))) == NULL) ||
821 ((address->buf = malloc(remote.len)) == NULL)) {
822 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
823 clnt_geterr(client, &rpc_createerr.cf_error);
824 if (address) {
825 free(address);
826 address = NULL;
827 }
828 goto error;
829 }
830 (void) memcpy(address->buf, remote.buf, remote.len);
831 (void) memcpy(&address->buf[sizeof (short)], &port,
832 sizeof (short));
833 address->len = address->maxlen = remote.len;
834 goto done;
835 }
836 #endif
837
838 try_rpcbind:
839 /*
840 * Check if rpcbind is up. This prevents needless delays when
841 * accessing applications such as the keyserver while booting
842 * disklessly.
843 */
844 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
845 if (!__rpcbind_is_up()) {
846 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
847 rpc_createerr.cf_error.re_errno = 0;
848 rpc_createerr.cf_error.re_terrno = 0;
849 goto error;
850 }
851 check_rpcbind = FALSE;
852 }
853
854 /*
855 * Now we try version 4 and then 3.
856 * We also send the remote system the address we used to
857 * contact it in case it can help to connect back with us
858 */
859 parms.r_prog = program;
860 parms.r_vers = version;
861 parms.r_owner = (char *)&nullstring[0]; /* not needed; */
862 /* just for xdring */
863 parms.r_netid = nconf->nc_netid; /* not really needed */
864
865 /*
866 * If a COTS transport is being used, try getting address via CLTS
867 * transport. This works only with version 4.
868 */
869 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
870 nconf->nc_semantics == NC_TPI_COTS) {
871 void *handle;
872 struct netconfig *nconf_clts;
873 rpcb_entry_list_ptr relp = NULL;
874
875 if (client == NULL) {
876 /* This did not go through the above PORTMAP/TCP code */
877 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
878 while ((nconf_clts = __rpc_getconf(handle))
879 != NULL) {
880 if (strcmp(nconf_clts->nc_protofmly,
881 nconf->nc_protofmly) != 0) {
882 continue;
883 }
884 client = _getclnthandle_timed(host,
885 nconf_clts, &parms.r_addr,
886 tp);
887 break;
888 }
889 __rpc_endconf(handle);
890 }
891 if (client == NULL)
892 goto regular_rpcbind; /* Go the regular way */
893 } else {
894 /* This is a UDP PORTMAP handle. Change to version 4 */
895 vers = RPCBVERS4;
896 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
897 }
898 /*
899 * We also send the remote system the address we used to
900 * contact it in case it can help it connect back with us
901 */
902 if (parms.r_addr == NULL) {
903 parms.r_addr = strdup(""); /* for XDRing */
904 if (parms.r_addr == NULL) {
905 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
906 "strdup failed.");
907 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
908 address = NULL;
909 goto error;
910 }
911 }
912
913 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
914
915 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
916 (xdrproc_t)xdr_rpcb, (char *)&parms,
917 (xdrproc_t)xdr_rpcb_entry_list_ptr,
918 (char *)&relp, *tp);
919 if (clnt_st == RPC_SUCCESS) {
920 if (address = got_entry(relp, nconf)) {
921 xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
922 (char *)&relp);
923 goto done;
924 }
925 /* Entry not found for this transport */
926 xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
927 (char *)&relp);
928 /*
929 * XXX: should have perhaps returned with error but
930 * since the remote machine might not always be able
931 * to send the address on all transports, we try the
932 * regular way with regular_rpcbind
933 */
934 goto regular_rpcbind;
935 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
936 (clnt_st == RPC_PROGUNAVAIL)) {
937 start_vers = RPCBVERS; /* Try version 3 now */
938 goto regular_rpcbind; /* Try different versions */
939 } else {
940 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
941 clnt_geterr(client, &rpc_createerr.cf_error);
942 goto error;
943 }
944 }
945
946 regular_rpcbind:
947
948 /* Now the same transport is to be used to get the address */
949 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
950 (nconf->nc_semantics == NC_TPI_COTS))) {
951 /* A CLTS type of client - destroy it */
952 CLNT_DESTROY(client);
953 client = NULL;
954 free(parms.r_addr);
955 parms.r_addr = NULL;
956 }
957
958 if (client == NULL) {
959 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
960 if (client == NULL) {
961 address = NULL;
962 goto error;
963 }
964 }
965 if (parms.r_addr == NULL) {
966 parms.r_addr = strdup(""); /* for XDRing */
967 if (parms.r_addr == NULL) {
968 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
969 "strdup failed.");
970 address = NULL;
971 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
972 goto error;
973 }
974 }
975
976 /* First try from start_vers and then version 3 (RPCBVERS) */
977
978 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
979 for (vers = start_vers; vers >= RPCBVERS; vers--) {
980 /* Set the version */
981 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
982 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
983 (xdrproc_t)xdr_rpcb, (char *)&parms,
984 (xdrproc_t)xdr_wrapstring,
985 (char *)&ua, *tp);
986 if (clnt_st == RPC_SUCCESS) {
987 if ((ua == NULL) || (ua[0] == NULL)) {
988 if (ua != NULL)
989 xdr_free(xdr_wrapstring, (char *)&ua);
990
991 /* address unknown */
992 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
993 goto error;
994 }
995 address = uaddr2taddr(nconf, ua);
996 #ifdef ND_DEBUG
997 fprintf(stderr, "\tRemote address is [%s]\n", ua);
998 if (!address)
999 fprintf(stderr,
1000 "\tCouldn't resolve remote address!\n");
1001 #endif
1002 xdr_free((xdrproc_t)xdr_wrapstring, (char *)&ua);
1003
1004 if (!address) {
1005 /* We don't know about your universal address */
1006 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1007 goto error;
1008 }
1009 goto done;
1010 }
1011 if (clnt_st == RPC_PROGVERSMISMATCH) {
1012 struct rpc_err rpcerr;
1013
1014 clnt_geterr(client, &rpcerr);
1015 if (rpcerr.re_vers.low > RPCBVERS4)
1016 goto error; /* a new version, can't handle */
1017 } else if (clnt_st != RPC_PROGUNAVAIL) {
1018 /* Cant handle this error */
1019 goto error;
1020 }
1021 }
1022
1023 if ((address == NULL) || (address->len == 0)) {
1024 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1025 clnt_geterr(client, &rpc_createerr.cf_error);
1026 }
1027
1028 error:
1029 if (client) {
1030 CLNT_DESTROY(client);
1031 client = NULL;
1032 }
1033 done:
1034 if (nconf->nc_semantics != NC_TPI_CLTS) {
1035 /* This client is the connectionless one */
1036 if (client) {
1037 CLNT_DESTROY(client);
1038 client = NULL;
1039 }
1040 }
1041 if (clpp) {
1042 *clpp = client;
1043 } else if (client) {
1044 CLNT_DESTROY(client);
1045 }
1046 if (parms.r_addr)
1047 free(parms.r_addr);
1048 return (address);
1049 }
1050
1051
1052 /*
1053 * Find the mapped address for program, version.
1054 * Calls the rpcbind service remotely to do the lookup.
1055 * Uses the transport specified in nconf.
1056 * Returns FALSE (0) if no map exists, else returns 1.
1057 *
1058 * Assuming that the address is all properly allocated
1059 */
1060 int
1061 rpcb_getaddr(const rpcprog_t program, const rpcvers_t version,
1062 const struct netconfig *nconf, struct netbuf *address, const char *host)
1063 {
1064 struct netbuf *na;
1065
1066 if ((na = __rpcb_findaddr_timed(program, version,
|
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2014 Gary Mills
25 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
27 */
28
29 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
30 /* All Rights Reserved */
31 /*
32 * Portions of this source code were derived from Berkeley
33 * 4.3 BSD under license from the Regents of the University of
34 * California.
35 */
36
37 /*
38 * interface to rpcbind rpc service.
39 */
40
41 #include "mt.h"
42 #include "rpc_mt.h"
43 #include <assert.h>
44 #include <rpc/rpc.h>
45 #include <rpc/rpcb_prot.h>
46 #include <netconfig.h>
47 #include <netdir.h>
48 #include <rpc/nettype.h>
49 #include <syslog.h>
50 #ifdef PORTMAP
51 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
52 #include <rpc/pmap_prot.h>
53 #endif
54 #include <sys/utsname.h>
55 #include <errno.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 static struct timeval tottimeout = { 60, 0 };
61 static const struct timeval rmttimeout = { 3, 0 };
62 static struct timeval rpcbrmttime = { 15, 0 };
63
64 extern bool_t xdr_wrapstring(XDR *, char **);
65
66 static const char nullstring[] = "\000";
67
68 extern CLIENT *_clnt_tli_create_timed(int, const struct netconfig *,
69 struct netbuf *, rpcprog_t, rpcvers_t, uint_t, uint_t,
70 const struct timeval *);
71
72 static CLIENT *_getclnthandle_timed(char *, struct netconfig *, char **,
73 struct timeval *);
149 */
150 extern rwlock_t rpcbaddr_cache_lock;
151
152 /*
153 * The routines check_cache(), add_cache(), delete_cache() manage the
154 * cache of rpcbind addresses for (host, netid).
155 */
156
157 static struct address_cache *
158 check_cache(char *host, char *netid)
159 {
160 struct address_cache *cptr;
161
162 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
163
164 assert(RW_READ_HELD(&rpcbaddr_cache_lock));
165 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
166 if ((strcmp(cptr->ac_host, host) == 0) &&
167 (strcmp(cptr->ac_netid, netid) == 0) &&
168 (time(NULL) <= cptr->ac_maxtime)) {
169 return (cptr);
170 }
171 }
172 return (NULL);
173 }
174
175 static void
176 delete_cache(struct netbuf *addr)
177 {
178 struct address_cache *cptr, *prevptr = NULL;
179
180 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
181 assert(RW_WRITE_HELD(&rpcbaddr_cache_lock));
182 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
183 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
184 free(cptr->ac_host);
185 free(cptr->ac_netid);
186 free(cptr->ac_taddr->buf);
187 free(cptr->ac_taddr);
188 if (cptr->ac_uaddr)
208 if (!ad_cache) {
209 goto memerr;
210 }
211 ad_cache->ac_maxtime = time(NULL) + CACHE_TTL;
212 ad_cache->ac_host = strdup(host);
213 ad_cache->ac_netid = strdup(netid);
214 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
215 ad_cache->ac_taddr = malloc(sizeof (struct netbuf));
216 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
217 (uaddr && !ad_cache->ac_uaddr)) {
218 goto memerr1;
219 }
220
221 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
222 ad_cache->ac_taddr->buf = malloc(taddr->len);
223 if (ad_cache->ac_taddr->buf == NULL) {
224 goto memerr1;
225 }
226
227 (void) memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
228
229 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
230
231 (void) rw_wrlock(&rpcbaddr_cache_lock);
232 if (cachesize < CACHESIZE) {
233 ad_cache->ac_next = front;
234 front = ad_cache;
235 cachesize++;
236 } else {
237 /* Free the last entry */
238 cptr = front;
239 prevptr = NULL;
240 while (cptr->ac_next) {
241 prevptr = cptr;
242 cptr = cptr->ac_next;
243 }
244
245 free(cptr->ac_host);
246 free(cptr->ac_netid);
247 free(cptr->ac_taddr->buf);
248 free(cptr->ac_taddr);
249 if (cptr->ac_uaddr)
250 free(cptr->ac_uaddr);
251
252 if (prevptr) {
253 prevptr->ac_next = NULL;
254 ad_cache->ac_next = front;
255 front = ad_cache;
256 } else {
257 front = ad_cache;
258 ad_cache->ac_next = NULL;
259 }
260 free(cptr);
261 }
262 (void) rw_unlock(&rpcbaddr_cache_lock);
263 return;
264 memerr1:
349 addr_to_delete.buf = malloc(addr->len);
350 if (addr_to_delete.buf == NULL) {
351 addr_to_delete.len = 0;
352 } else {
353 (void) memcpy(addr_to_delete.buf, addr->buf, addr->len);
354 }
355 }
356 (void) rw_unlock(&rpcbaddr_cache_lock);
357 if (addr_to_delete.len != 0) {
358 /*
359 * Assume this may be due to cache data being
360 * outdated
361 */
362 (void) rw_wrlock(&rpcbaddr_cache_lock);
363 delete_cache(&addr_to_delete);
364 (void) rw_unlock(&rpcbaddr_cache_lock);
365 free(addr_to_delete.buf);
366 }
367 rpcbind_hs.h_host = host;
368 rpcbind_hs.h_serv = "rpcbind";
369
370 if ((neterr = netdir_getbyname(nconf, &rpcbind_hs, &nas)) != 0) {
371 if (neterr == ND_NOHOST)
372 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
373 else
374 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
375 return (NULL);
376 }
377 /* XXX nas should perhaps be cached for better performance */
378
379 for (j = 0; j < nas->n_cnt; j++) {
380 addr = &(nas->n_addrs[j]);
381 client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr, RPCBPROG,
382 RPCBVERS4, 0, 0, tp);
383 if (client)
384 break;
385 }
386
387 if (client) {
388 tmpaddr = targaddr ? taddr2uaddr(nconf, addr) : NULL;
389 add_cache(host, nconf->nc_netid, addr, tmpaddr);
390 if (targaddr) {
391 *targaddr = tmpaddr;
392 }
393 }
394 netdir_free((char *)nas, ND_ADDRLIST);
395 return (client);
396 }
397
398 /*
399 * This routine will return a client handle that is connected to the local
400 * rpcbind. Returns NULL on error.
401 */
402 static CLIENT *
403 local_rpcb(void)
404 {
405 static struct netconfig *loopnconf;
406 static char *hostname;
407 extern mutex_t loopnconf_lock;
408
409 /* VARIABLES PROTECTED BY loopnconf_lock: hostname loopnconf */
410 (void) mutex_lock(&loopnconf_lock);
411 if (loopnconf == NULL) {
412 struct utsname utsname;
413 struct netconfig *nconf, *tmpnconf = NULL;
414 void *nc_handle;
415
416 if (hostname == NULL) {
417 #if defined(__i386) && !defined(__amd64)
418 if ((_nuname(&utsname) == -1) ||
419 ((hostname = strdup(utsname.nodename)) == NULL)) {
420 #else
421 if ((uname(&utsname) == -1) ||
422 ((hostname = strdup(utsname.nodename)) == NULL)) {
423 #endif
424 syslog(LOG_ERR, "local_rpcb : strdup failed.");
425 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
426 (void) mutex_unlock(&loopnconf_lock);
427 return (NULL);
428 }
429 /* hostname is never freed */
430 }
431 nc_handle = setnetconfig();
432 if (nc_handle == NULL) {
433 /* fails to open netconfig file */
434 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
435 (void) mutex_unlock(&loopnconf_lock);
436 return (NULL);
437 }
438 while (nconf = getnetconfig(nc_handle)) {
439 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
440 tmpnconf = nconf;
441 if (nconf->nc_semantics == NC_TPI_CLTS)
442 break;
443 }
444 }
445 if (tmpnconf == NULL) {
446 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
447 (void) mutex_unlock(&loopnconf_lock);
448 return (NULL);
449 }
542 return (rslt);
543 }
544
545 /*
546 * From the merged list, find the appropriate entry
547 */
548 static struct netbuf *
549 got_entry(rpcb_entry_list_ptr relp, struct netconfig *nconf)
550 {
551 struct netbuf *na = NULL;
552 rpcb_entry_list_ptr sp;
553 rpcb_entry *rmap;
554
555 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
556 rmap = &sp->rpcb_entry_map;
557 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
558 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
559 (nconf->nc_semantics == rmap->r_nc_semantics) &&
560 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
561 na = uaddr2taddr(nconf, rmap->r_maddr);
562 break;
563 }
564 }
565 return (na);
566 }
567
568 /*
569 * Quick check to see if rpcbind is up. Tries to connect over
570 * local transport.
571 */
572 bool_t
573 __rpcbind_is_up(void)
574 {
575 struct utsname name;
576 char uaddr[SYS_NMLN];
577 struct netbuf *addr;
578 int fd;
579 struct t_call *sndcall;
580 struct netconfig *netconf;
581 bool_t res;
622 sndcall->addr.len = addr->len;
623 sndcall->addr.buf = addr->buf;
624
625 if (t_connect(fd, sndcall, NULL) == -1)
626 res = FALSE;
627 else
628 res = TRUE;
629
630 sndcall->addr.maxlen = sndcall->addr.len = 0;
631 sndcall->addr.buf = NULL;
632 (void) t_free((char *)sndcall, T_CALL);
633 free(addr->buf);
634 free(addr);
635 (void) t_close(fd);
636
637 return (res);
638 }
639
640
641 /*
642 * An internal function which optimizes rpcb_getaddr function. It returns
643 * the universal address of the remote service or NULL. It also optionally
644 * returns the client handle that it uses to contact the remote rpcbind.
645 * The caller will re-purpose the client to contact the remote service.
646 *
647 * The algorithm used: First try version 4. Then try version 3 (svr4).
648 * Finally, if the transport is TCP or UDP, try version 2 (portmap).
649 * Version 4 is now available with all current systems on the network.
650 * With this algorithm, we get performance as well as a plan for
651 * obsoleting version 2.
652 *
653 * XXX: Due to some problems with t_connect(), we do not reuse the same client
654 * handle for COTS cases and hence in these cases we do not return the
655 * client handle. This code will change if t_connect() ever
656 * starts working properly. Also look under clnt_vc.c.
657 */
658 struct netbuf *
659 __rpcb_findaddr_timed(rpcprog_t program, rpcvers_t version,
660 struct netconfig *nconf, char *host, CLIENT **clpp, struct timeval *tp)
661 {
662 static bool_t check_rpcbind = TRUE;
663 CLIENT *client = NULL;
664 RPCB parms;
665 enum clnt_stat clnt_st;
666 char *ua = NULL;
667 uint_t vers;
668 struct netbuf *address = NULL;
669 void *handle;
670 rpcb_entry_list_ptr relp = NULL;
671 bool_t tmp_client = FALSE;
672
673 /* parameter checking */
674 if (nconf == NULL) {
675 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
676 return (NULL);
677 }
678
679 parms.r_addr = NULL;
680
681 /*
682 * Use default total timeout if no timeout is specified.
683 */
684 if (tp == NULL)
685 tp = &tottimeout;
686
687 /*
688 * Check if rpcbind is up. This prevents needless delays when
689 * accessing applications such as the keyserver while booting
690 * disklessly.
691 */
692 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
693 if (!__rpcbind_is_up()) {
694 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
695 rpc_createerr.cf_error.re_errno = 0;
696 rpc_createerr.cf_error.re_terrno = 0;
697 goto error;
698 }
699 check_rpcbind = FALSE;
700 }
701
702 /*
703 * First try version 4.
704 */
705 parms.r_prog = program;
706 parms.r_vers = version;
707 parms.r_owner = (char *)&nullstring[0]; /* not needed; */
708 /* just for xdring */
709 parms.r_netid = nconf->nc_netid; /* not really needed */
710
711 /*
712 * If a COTS transport is being used, try getting address via CLTS
713 * transport. This works only with version 4.
714 */
715 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
716 nconf->nc_semantics == NC_TPI_COTS) {
717 tmp_client = TRUE;
718 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
719 struct netconfig *nconf_clts;
720
721 while ((nconf_clts = __rpc_getconf(handle)) != NULL) {
722 if (strcmp(nconf_clts->nc_protofmly,
723 nconf->nc_protofmly) != 0) {
724 continue;
725 }
726 client = _getclnthandle_timed(host, nconf_clts,
727 &parms.r_addr, tp);
728 break;
729 }
730 __rpc_endconf(handle);
731 }
732 } else {
733 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
734 /* Sets cf_error members on failure */
735 }
736
737 if (client != NULL) {
738
739 /* Set rpcbind version 4 */
740 vers = RPCBVERS4;
741 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
742
743 /*
744 * We also send the remote system the address we used to
745 * contact it in case it can help it connect back with us
746 */
747 if (parms.r_addr == NULL) {
748 parms.r_addr = strdup(""); /* for XDRing */
749 if (parms.r_addr == NULL) {
750 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
751 "strdup failed.");
752 /* cf_error is still zeroed */
753 rpc_createerr.cf_error.re_errno = errno;
754 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
755 goto error;
756 }
757 }
758
759 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
760 (char *)&rpcbrmttime);
761
762 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
763 (xdrproc_t)xdr_rpcb, (char *)&parms,
764 (xdrproc_t)xdr_rpcb_entry_list_ptr, (char *)&relp, *tp);
765 /* Sets error structure members in client handle */
766 switch (clnt_st) {
767 case RPC_SUCCESS: /* Call succeeded */
768 address = got_entry(relp, nconf);
769 xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
770 (char *)&relp);
771 if (address != NULL) {
772 /* Program number and version number matched */
773 goto done;
774 }
775 /* Program and version not found for this transport */
776 /*
777 * XXX: should have returned with RPC_PROGUNAVAIL
778 * or perhaps RPC_PROGNOTREGISTERED error but
779 * since the remote machine might not always be able
780 * to send the address on all transports, we try the
781 * regular way with version 3, then 2
782 */
783 /* Try the next version */
784 break;
785 case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
786 clnt_geterr(client, &rpc_createerr.cf_error);
787 if (rpc_createerr.cf_error.re_vers.low > vers) {
788 rpc_createerr.cf_stat = clnt_st;
789 goto error; /* a new version, can't handle */
790 }
791 /* Try the next version */
792 break;
793 case RPC_PROCUNAVAIL: /* Procedure unavailable */
794 case RPC_PROGUNAVAIL: /* Program not available */
795 case RPC_TIMEDOUT: /* Call timed out */
796 /* Try the next version */
797 break;
798 default:
799 clnt_geterr(client, &rpc_createerr.cf_error);
800 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
801 goto error;
802 break;
803 }
804
805 } else {
806
807 /* No client */
808 tmp_client = FALSE;
809
810 } /* End of version 4 */
811
812 /* Destroy a temporary client */
813 if (client != NULL && tmp_client) {
814 CLNT_DESTROY(client);
815 client = NULL;
816 free(parms.r_addr);
817 parms.r_addr = NULL;
818 }
819 tmp_client = FALSE;
820
821 /*
822 * Try version 3
823 */
824
825 /* Now the same transport is to be used to get the address */
826 if (client == NULL) {
827 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
828 /* Sets cf_error members on failure */
829 }
830 address = NULL;
831 if (client != NULL) {
832 if (parms.r_addr == NULL) {
833 parms.r_addr = strdup(""); /* for XDRing */
834 if (parms.r_addr == NULL) {
835 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
836 "strdup failed.");
837 /* cf_error is still zeroed */
838 rpc_createerr.cf_error.re_errno = errno;
839 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
840 goto error;
841 }
842 }
843
844 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
845 (char *)&rpcbrmttime);
846 vers = RPCBVERS; /* Set the version */
847 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
848 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
849 (xdrproc_t)xdr_rpcb, (char *)&parms,
850 (xdrproc_t)xdr_wrapstring, (char *)&ua, *tp);
851 /* Sets error structure members in client handle */
852 switch (clnt_st) {
853 case RPC_SUCCESS: /* Call succeeded */
854 if (ua != NULL) {
855 if (ua[0] != '\0') {
856 address = uaddr2taddr(nconf, ua);
857 }
858 xdr_free((xdrproc_t)xdr_wrapstring,
859 (char *)&ua);
860
861 if (address != NULL) {
862 goto done;
863 }
864 /* We don't know about your universal addr */
865 /* cf_error is still zeroed */
866 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
867 goto error;
868 }
869 #ifndef PORTMAP
870 clnt_geterr(client, &rpc_createerr.cf_error);
871 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
872 goto error;
873 #endif
874 /* Try the next version */
875 break;
876 case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
877 clnt_geterr(client, &rpc_createerr.cf_error);
878 #ifdef PORTMAP
879 if (rpc_createerr.cf_error.re_vers.low > vers) {
880 rpc_createerr.cf_stat = clnt_st;
881 goto error; /* a new version, can't handle */
882 }
883 #else
884 rpc_createerr.cf_stat = clnt_st;
885 goto error;
886 #endif
887 /* Try the next version */
888 break;
889 #ifdef PORTMAP
890 case RPC_PROCUNAVAIL: /* Procedure unavailable */
891 case RPC_PROGUNAVAIL: /* Program not available */
892 case RPC_TIMEDOUT: /* Call timed out */
893 /* Try the next version */
894 break;
895 #endif
896 default:
897 clnt_geterr(client, &rpc_createerr.cf_error);
898 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
899 goto error;
900 break;
901 }
902 } /* End of version 3 */
903 #ifndef PORTMAP
904 /* cf_error members set by creation failure */
905 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
906 #endif
907 /*
908 * Try version 2
909 */
910
911 #ifdef PORTMAP
912 /* Try version 2 for TCP or UDP */
913 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
914 ushort_t port = 0;
915 struct netbuf remote;
916 uint_t pmapvers = 2;
917 struct pmap pmapparms;
918
919 /*
920 * Try UDP only - there are some portmappers out
921 * there that use UDP only.
922 */
923 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
924 struct netconfig *newnconf;
925
926 if (client != NULL) {
927 CLNT_DESTROY(client);
928 client = NULL;
929 free(parms.r_addr);
930 parms.r_addr = NULL;
931 }
932 if ((handle = __rpc_setconf("udp")) == NULL) {
933 /* cf_error is still zeroed */
934 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
935 goto error;
936 }
937
938 /*
939 * The following to reinforce that you can
940 * only request for remote address through
941 * the same transport you are requesting.
942 * ie. requesting unversial address
943 * of IPv4 has to be carried through IPv4.
944 * Can't use IPv6 to send out the request.
945 * The mergeaddr in rpcbind can't handle
946 * this.
947 */
948 for (;;) {
949 if ((newnconf = __rpc_getconf(handle))
950 == NULL) {
951 __rpc_endconf(handle);
952 /* cf_error is still zeroed */
953 rpc_createerr.cf_stat =
954 RPC_UNKNOWNPROTO;
955 goto error;
956 }
957 /*
958 * here check the protocol family to
959 * be consistent with the request one
960 */
961 if (strcmp(newnconf->nc_protofmly,
962 nconf->nc_protofmly) == 0)
963 break;
964 }
965
966 client = _getclnthandle_timed(host, newnconf,
967 &parms.r_addr, tp);
968 /* Sets cf_error members on failure */
969 __rpc_endconf(handle);
970 tmp_client = TRUE;
971 }
972 if (client == NULL) {
973 /* cf_error members set by creation failure */
974 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
975 tmp_client = FALSE;
976 goto error;
977 }
978
979 /*
980 * Set version and retry timeout.
981 */
982 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
983 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
984
985 pmapparms.pm_prog = program;
986 pmapparms.pm_vers = version;
987 pmapparms.pm_prot = (strcmp(nconf->nc_proto, NC_TCP) != 0) ?
988 IPPROTO_UDP : IPPROTO_TCP;
989 pmapparms.pm_port = 0; /* not needed */
990 clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
991 (xdrproc_t)xdr_pmap, (caddr_t)&pmapparms,
992 (xdrproc_t)xdr_u_short, (caddr_t)&port, *tp);
993 /* Sets error structure members in client handle */
994 if (clnt_st != RPC_SUCCESS) {
995 clnt_geterr(client, &rpc_createerr.cf_error);
996 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
997 goto error;
998 } else if (port == 0) {
999 /* cf_error is still zeroed */
1000 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1001 goto error;
1002 }
1003 port = htons(port);
1004 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
1005 if (((address = malloc(sizeof (struct netbuf))) == NULL) ||
1006 ((address->buf = malloc(remote.len)) == NULL)) {
1007 /* cf_error is still zeroed */
1008 rpc_createerr.cf_error.re_errno = errno;
1009 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1010 free(address);
1011 address = NULL;
1012 goto error;
1013 }
1014 (void) memcpy(address->buf, remote.buf, remote.len);
1015 (void) memcpy(&address->buf[sizeof (short)], &port,
1016 sizeof (short));
1017 address->len = address->maxlen = remote.len;
1018 goto done;
1019 } else {
1020 /* Not NC_INET */
1021 if (client != NULL && clnt_st != RPC_SUCCESS) {
1022 clnt_geterr(client, &rpc_createerr.cf_error);
1023 rpc_createerr.cf_stat = clnt_st;
1024 } else {
1025 /* No client handle */
1026 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1027 }
1028 }
1029 #endif
1030
1031 error:
1032 /* Return NULL address and NULL client */
1033 address = NULL;
1034 if (client != NULL) {
1035 CLNT_DESTROY(client);
1036 client = NULL;
1037 }
1038
1039 done:
1040 /* Return an address and optional client */
1041 if (client != NULL && tmp_client) {
1042 /* This client is the temporary one */
1043 CLNT_DESTROY(client);
1044 client = NULL;
1045 }
1046 if (clpp != NULL) {
1047 *clpp = client;
1048 } else if (client != NULL) {
1049 CLNT_DESTROY(client);
1050 }
1051 free(parms.r_addr);
1052 return (address);
1053 }
1054
1055
1056 /*
1057 * Find the mapped address for program, version.
1058 * Calls the rpcbind service remotely to do the lookup.
1059 * Uses the transport specified in nconf.
1060 * Returns FALSE (0) if no map exists, else returns 1.
1061 *
1062 * Assuming that the address is all properly allocated
1063 */
1064 int
1065 rpcb_getaddr(const rpcprog_t program, const rpcvers_t version,
1066 const struct netconfig *nconf, struct netbuf *address, const char *host)
1067 {
1068 struct netbuf *na;
1069
1070 if ((na = __rpcb_findaddr_timed(program, version,
|