1 /* $NetBSD: rpcinfo.c,v 1.15 2000/10/04 20:09:05 mjl Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
33 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
36 #include <sys/cdefs.h>
38 * rpcinfo: ping a particular rpc program
39 * or dump the registered programs on the remote machine.
43 * We are for now defining PORTMAP here. It doesn't even compile
44 * unless it is defined.
51 * If PORTMAP is defined, rpcinfo will talk to both portmapper and
52 * rpcbind programs; else it talks only to rpcbind. In the latter case
53 * all the portmapper specific options such as -u, -t, -p become void.
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
61 #include <rpc/rpcb_prot.h>
62 #include <rpc/rpcent.h>
63 #include <rpc/nettype.h>
64 #include <rpc/rpc_com.h>
71 #ifdef PORTMAP /* Support for version 2 portmapper */
72 #include <netinet/in.h>
74 #include <arpa/inet.h>
75 #include <rpc/pmap_prot.h>
76 #include <rpc/pmap_clnt.h>
79 #define MAXHOSTLEN 256
80 #define MIN_VERS ((u_long) 0)
81 #define MAX_VERS ((u_long) 4294967295UL)
82 #define UNKNOWN "unknown"
85 * Functions to be performed.
87 #define NONE 0 /* no function */
88 #define PMAPDUMP 1 /* dump portmapper registrations */
89 #define TCPPING 2 /* ping TCP service */
90 #define UDPPING 3 /* ping UDP service */
91 #define BROADCAST 4 /* ping broadcast service */
92 #define DELETES 5 /* delete registration for the service */
93 #define ADDRPING 6 /* pings at the given address */
94 #define PROGPING 7 /* pings a program on a given host */
95 #define RPCBDUMP 8 /* dump rpcbind registrations */
96 #define RPCBDUMP_SHORT 9 /* dump rpcbind registrations - short version */
97 #define RPCBADDRLIST 10 /* dump addr list about one prog */
98 #define RPCBGETSTAT 11 /* Get statistics */
102 struct netidlist *next;
107 struct verslist *next;
110 struct rpcbdump_short {
112 struct verslist *vlist;
113 struct netidlist *nlist;
114 struct rpcbdump_short *next;
121 static void ip_ping(u_short, const char *, int, char **);
122 static CLIENT *clnt_com_create(struct sockaddr_in *, u_long, u_long, int *,
124 static void pmapdump(int, char **);
125 static void get_inet_address(struct sockaddr_in *, char *);
128 static bool_t reply_proc(void *, struct netbuf *, struct netconfig *);
129 static void brdcst(int, char **);
130 static void addrping(char *, char *, int, char **);
131 static void progping(char *, int, char **);
132 static CLIENT *clnt_addr_create(char *, struct netconfig *, u_long, u_long);
133 static CLIENT *clnt_rpcbind_create(char *, int, struct netbuf **);
134 static CLIENT *getclnthandle(char *, struct netconfig *, u_long,
136 static CLIENT *local_rpcb(u_long, u_long);
137 static int pstatus(CLIENT *, u_long, u_long);
138 static void rpcbdump(int, char *, int, char **);
139 static void rpcbgetstat(int, char **);
140 static void rpcbaddrlist(char *, int, char **);
141 static void deletereg(char *, int, char **);
142 static void print_rmtcallstat(int, rpcb_stat *);
143 static void print_getaddrstat(int, rpcb_stat *);
144 static void usage(void);
145 static u_long getprognum(char *);
146 static u_long getvers(char *);
147 static char *spaces(int);
148 static bool_t add_version(struct rpcbdump_short *, u_long);
149 static bool_t add_netid(struct rpcbdump_short *, char *);
152 main(int argc, char **argv)
158 char *address = NULL;
167 while ((c = getopt(argc, argv, "a:bdlmn:pstT:u")) != -1) {
169 while ((c = getopt(argc, argv, "a:bdlmn:sT:")) != -1) {
174 if (function != NONE)
181 if (function != NONE)
188 if (function != NONE)
195 portnum = (u_short) strtol(optarg, &strptr, 10);
196 if (strptr == optarg || *strptr != '\0')
197 errx(1, "%s is illegal port number", optarg);
202 if (function != NONE)
208 if (function != NONE)
211 function = BROADCAST;
215 if (function != NONE)
222 if (function != NONE)
225 function = RPCBADDRLIST;
229 if (function != NONE)
232 function = RPCBGETSTAT;
236 if (function != NONE)
239 function = RPCBDUMP_SHORT;
251 if (errflg || ((function == ADDRPING) && !netid))
254 if (function == NONE) {
255 if (argc - optind > 1)
266 pmapdump(argc - optind, argv + optind);
270 ip_ping(portnum, "udp", argc - optind, argv + optind);
274 ip_ping(portnum, "tcp", argc - optind, argv + optind);
278 brdcst(argc - optind, argv + optind);
281 deletereg(netid, argc - optind, argv + optind);
284 addrping(address, netid, argc - optind, argv + optind);
287 progping(netid, argc - optind, argv + optind);
291 rpcbdump(function, netid, argc - optind, argv + optind);
294 rpcbgetstat(argc - optind, argv + optind);
297 rpcbaddrlist(netid, argc - optind, argv + optind);
304 local_rpcb(u_long prog, u_long vers)
307 struct netconfig *nconf;
310 localhandle = setnetconfig();
311 while ((nconf = getnetconfig(localhandle)) != NULL) {
312 if (nconf->nc_protofmly != NULL &&
313 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
317 warnx("getnetconfig: %s", nc_sperror());
321 clnt = clnt_tp_create(NULL, prog, vers, nconf);
322 endnetconfig(localhandle);
328 clnt_com_create(struct sockaddr_in *addr, u_long prog, u_long vers,
329 int *fdp, const char *trans)
333 if (strcmp(trans, "tcp") == 0) {
334 clnt = clnttcp_create(addr, prog, vers, fdp, 0, 0);
340 clnt = clntudp_create(addr, prog, vers, to, fdp);
342 if (clnt == (CLIENT *)NULL) {
343 clnt_pcreateerror("rpcinfo");
344 if (vers == MIN_VERS)
345 printf("program %lu is not available\n", prog);
347 printf("program %lu version %lu is not available\n",
355 * If portnum is 0, then go and get the address from portmapper, which happens
356 * transparently through clnt*_create(); If version number is not given, it
357 * tries to find out the version number by making a call to version 0 and if
358 * that fails, it obtains the high order and the low order version number. If
359 * version 0 calls succeeds, it tries for MAXVERS call and repeats the same.
362 ip_ping(u_short portnum, const char *trans, int argc, char **argv)
367 struct sockaddr_in addr;
368 enum clnt_stat rpc_stat;
369 u_long prognum, vers, minvers, maxvers;
370 struct rpc_err rpcerr;
373 if (argc < 2 || argc > 3)
377 prognum = getprognum(argv[1]);
378 get_inet_address(&addr, argv[0]);
379 if (argc == 2) { /* Version number not known */
381 * A call to version 0 should fail with a program/version
382 * mismatch, and give us the range of versions supported.
386 vers = getvers(argv[2]);
388 addr.sin_port = htons(portnum);
389 client = clnt_com_create(&addr, prognum, vers, &fd, trans);
390 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
391 (char *)NULL, (xdrproc_t) xdr_void, (char *)NULL,
394 /* Version number was known */
395 if (pstatus(client, prognum, vers) < 0)
397 (void) CLNT_DESTROY(client);
400 /* Version number not known */
401 (void) CLNT_CONTROL(client, CLSET_FD_NCLOSE, (char *)NULL);
402 if (rpc_stat == RPC_PROGVERSMISMATCH) {
403 clnt_geterr(client, &rpcerr);
404 minvers = rpcerr.re_vers.low;
405 maxvers = rpcerr.re_vers.high;
406 } else if (rpc_stat == RPC_SUCCESS) {
408 * Oh dear, it DOES support version 0.
409 * Let's try version MAX_VERS.
411 (void) CLNT_DESTROY(client);
412 addr.sin_port = htons(portnum);
413 client = clnt_com_create(&addr, prognum, MAX_VERS, &fd, trans);
414 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
415 (char *)NULL, (xdrproc_t) xdr_void,
417 if (rpc_stat == RPC_PROGVERSMISMATCH) {
418 clnt_geterr(client, &rpcerr);
419 minvers = rpcerr.re_vers.low;
420 maxvers = rpcerr.re_vers.high;
421 } else if (rpc_stat == RPC_SUCCESS) {
423 * It also supports version MAX_VERS.
424 * Looks like we have a wise guy.
425 * OK, we give them information on all
426 * 4 billion versions they support...
431 (void) pstatus(client, prognum, MAX_VERS);
435 (void) pstatus(client, prognum, (u_long)0);
438 (void) CLNT_DESTROY(client);
439 for (vers = minvers; vers <= maxvers; vers++) {
440 addr.sin_port = htons(portnum);
441 client = clnt_com_create(&addr, prognum, vers, &fd, trans);
442 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
443 (char *)NULL, (xdrproc_t) xdr_void,
445 if (pstatus(client, prognum, vers) < 0)
447 (void) CLNT_DESTROY(client);
456 * Dump all the portmapper registerations
459 pmapdump(int argc, char **argv)
461 struct sockaddr_in server_addr;
462 struct pmaplist *head = NULL;
463 int socket = RPC_ANYSOCK;
464 struct timeval minutetimeout;
465 register CLIENT *client;
467 enum clnt_stat clnt_st;
475 get_inet_address(&server_addr, host);
476 server_addr.sin_port = htons(PMAPPORT);
477 client = clnttcp_create(&server_addr, PMAPPROG, PMAPVERS,
480 client = local_rpcb(PMAPPROG, PMAPVERS);
482 if (client == NULL) {
483 if (rpc_createerr.cf_stat == RPC_TLIERROR) {
485 * "Misc. TLI error" is not too helpful. Most likely
486 * the connection to the remote server timed out, so
487 * this error is at least less perplexing.
489 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
490 rpc_createerr.cf_error.re_status = RPC_FAILED;
492 clnt_pcreateerror("rpcinfo: can't contact portmapper");
496 minutetimeout.tv_sec = 60;
497 minutetimeout.tv_usec = 0;
499 clnt_st = CLNT_CALL(client, PMAPPROC_DUMP, (xdrproc_t) xdr_void,
500 NULL, (xdrproc_t) xdr_pmaplist_ptr, (char *)&head,
502 if (clnt_st != RPC_SUCCESS) {
503 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
504 (clnt_st == RPC_PROGUNAVAIL)) {
505 CLNT_GETERR(client, &err);
506 if (err.re_vers.low > PMAPVERS) {
508 warnx("%s does not support portmapper."
509 "Try rpcinfo %s instead", host,
512 warnx("local host does not support "
513 "portmapper. Try 'rpcinfo' "
518 clnt_perror(client, "rpcinfo: can't contact portmapper");
522 printf("No remote programs registered.\n");
524 printf(" program vers proto port service\n");
525 for (; head != NULL; head = head->pml_next) {
527 head->pml_map.pm_prog,
528 head->pml_map.pm_vers);
529 if (head->pml_map.pm_prot == IPPROTO_UDP)
530 printf("%6s", "udp");
531 else if (head->pml_map.pm_prot == IPPROTO_TCP)
532 printf("%6s", "tcp");
533 else if (head->pml_map.pm_prot == IPPROTO_ST)
534 printf("%6s", "local");
536 printf("%6ld", head->pml_map.pm_prot);
537 printf("%7ld", head->pml_map.pm_port);
538 rpc = getrpcbynumber(head->pml_map.pm_prog);
540 printf(" %s\n", rpc->r_name);
548 get_inet_address(struct sockaddr_in *addr, char *host)
550 struct netconfig *nconf;
551 struct addrinfo hints, *res;
554 (void) memset((char *)addr, 0, sizeof (*addr));
555 addr->sin_addr.s_addr = inet_addr(host);
556 if (addr->sin_addr.s_addr == INADDR_NONE ||
557 addr->sin_addr.s_addr == INADDR_ANY) {
558 if ((nconf = __rpc_getconfip("udp")) == NULL &&
559 (nconf = __rpc_getconfip("tcp")) == NULL)
560 errx(1, "couldn't find a suitable transport");
562 memset(&hints, 0, sizeof hints);
563 hints.ai_family = AF_INET;
564 if ((error = getaddrinfo(host, "rpcbind", &hints, &res))
566 errx(1, "%s: %s", host, gai_strerror(error));
568 memcpy(addr, res->ai_addr, res->ai_addrlen);
571 (void) freenetconfigent(nconf);
574 addr->sin_family = AF_INET;
580 * reply_proc collects replies from the broadcast.
581 * to get a unique list of responses the output of rpcinfo should
582 * be piped through sort(1) and then uniq(1).
587 reply_proc(void *res, struct netbuf *who, struct netconfig *nconf)
588 /* void *res; Nothing comes back */
589 /* struct netbuf *who; Who sent us the reply */
590 /* struct netconfig *nconf; On which transport the reply came */
593 char hostbuf[NI_MAXHOST];
594 const char *hostname;
595 struct sockaddr *sa = (struct sockaddr *)who->buf;
597 if (getnameinfo(sa, sa->sa_len, hostbuf, NI_MAXHOST, NULL, 0, 0)) {
602 uaddr = taddr2uaddr(nconf, who);
604 printf("%s\t%s\n", UNKNOWN, hostname);
606 printf("%s\t%s\n", uaddr, hostname);
613 brdcst(int argc, char **argv)
615 enum clnt_stat rpc_stat;
616 u_long prognum, vers;
620 prognum = getprognum(argv[0]);
621 vers = getvers(argv[1]);
622 rpc_stat = rpc_broadcast(prognum, vers, NULLPROC,
623 (xdrproc_t) xdr_void, (char *)NULL, (xdrproc_t) xdr_void,
624 (char *)NULL, (resultproc_t) reply_proc, NULL);
625 if ((rpc_stat != RPC_SUCCESS) && (rpc_stat != RPC_TIMEDOUT))
626 errx(1, "broadcast failed: %s", clnt_sperrno(rpc_stat));
631 add_version(struct rpcbdump_short *rs, u_long vers)
635 for (vl = rs->vlist; vl; vl = vl->next)
636 if (vl->vers == vers)
640 vl = (struct verslist *)malloc(sizeof (struct verslist));
644 vl->next = rs->vlist;
650 add_netid(struct rpcbdump_short *rs, char *netid)
652 struct netidlist *nl;
654 for (nl = rs->nlist; nl; nl = nl->next)
655 if (strcmp(nl->netid, netid) == 0)
659 nl = (struct netidlist *)malloc(sizeof (struct netidlist));
663 nl->next = rs->nlist;
669 rpcbdump(int dumptype, char *netid, int argc, char **argv)
671 rpcblist_ptr head = NULL;
672 struct timeval minutetimeout;
673 register CLIENT *client;
676 struct netidlist *nl;
678 struct rpcbdump_short *rs, *rs_tail;
680 enum clnt_stat clnt_st;
682 struct rpcbdump_short *rs_head = NULL;
689 client = clnt_rpcbind_create(host, RPCBVERS, NULL);
691 struct netconfig *nconf;
693 nconf = getnetconfigent(netid);
695 nc_perror("rpcinfo: invalid transport");
698 client = getclnthandle(host, nconf, RPCBVERS, NULL);
700 (void) freenetconfigent(nconf);
703 client = local_rpcb(PMAPPROG, RPCBVERS);
705 if (client == (CLIENT *)NULL) {
706 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
710 minutetimeout.tv_sec = 60;
711 minutetimeout.tv_usec = 0;
712 clnt_st = CLNT_CALL(client, RPCBPROC_DUMP, (xdrproc_t) xdr_void,
713 NULL, (xdrproc_t) xdr_rpcblist_ptr, (char *) &head,
715 if (clnt_st != RPC_SUCCESS) {
716 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
717 (clnt_st == RPC_PROGUNAVAIL)) {
720 CLNT_GETERR(client, &err);
721 if (err.re_vers.low == RPCBVERS4) {
723 clnt_control(client, CLSET_VERS, (char *)&vers);
724 clnt_st = CLNT_CALL(client, RPCBPROC_DUMP,
725 (xdrproc_t) xdr_void, NULL,
726 (xdrproc_t) xdr_rpcblist_ptr, (char *) &head,
728 if (clnt_st != RPC_SUCCESS)
731 if (err.re_vers.high == PMAPVERS) {
733 struct pmaplist *pmaphead = NULL;
734 rpcblist_ptr list, prev;
737 clnt_control(client, CLSET_VERS, (char *)&vers);
738 clnt_st = CLNT_CALL(client, PMAPPROC_DUMP,
739 (xdrproc_t) xdr_void, NULL,
740 (xdrproc_t) xdr_pmaplist_ptr,
741 (char *)&pmaphead, minutetimeout);
742 if (clnt_st != RPC_SUCCESS)
745 * convert to rpcblist_ptr format
747 for (head = NULL; pmaphead != NULL;
748 pmaphead = pmaphead->pml_next) {
749 list = (rpcblist *)malloc(sizeof (rpcblist));
755 prev->rpcb_next = (rpcblist_ptr) list;
757 list->rpcb_next = NULL;
758 list->rpcb_map.r_prog = pmaphead->pml_map.pm_prog;
759 list->rpcb_map.r_vers = pmaphead->pml_map.pm_vers;
760 if (pmaphead->pml_map.pm_prot == IPPROTO_UDP)
761 list->rpcb_map.r_netid = "udp";
762 else if (pmaphead->pml_map.pm_prot == IPPROTO_TCP)
763 list->rpcb_map.r_netid = "tcp";
765 #define MAXLONG_AS_STRING "2147483648"
766 list->rpcb_map.r_netid =
767 malloc(strlen(MAXLONG_AS_STRING) + 1);
768 if (list->rpcb_map.r_netid == NULL)
770 sprintf(list->rpcb_map.r_netid, "%6ld",
771 pmaphead->pml_map.pm_prot);
773 list->rpcb_map.r_owner = UNKNOWN;
774 low = pmaphead->pml_map.pm_port & 0xff;
775 high = (pmaphead->pml_map.pm_port >> 8) & 0xff;
776 list->rpcb_map.r_addr = strdup("0.0.0.0.XXX.XXX");
777 sprintf(&list->rpcb_map.r_addr[8], "%d.%d",
783 } else { /* any other error */
785 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
790 printf("No remote programs registered.\n");
791 } else if (dumptype == RPCBDUMP) {
793 " program version netid address service owner\n");
794 for (; head != NULL; head = head->rpcb_next) {
796 head->rpcb_map.r_prog, head->rpcb_map.r_vers);
797 printf("%-9s ", head->rpcb_map.r_netid);
798 printf("%-22s", head->rpcb_map.r_addr);
799 rpc = getrpcbynumber(head->rpcb_map.r_prog);
801 printf(" %-10s", rpc->r_name);
803 printf(" %-10s", "-");
804 printf(" %s\n", head->rpcb_map.r_owner);
806 } else if (dumptype == RPCBDUMP_SHORT) {
807 for (; head != NULL; head = head->rpcb_next) {
808 for (rs = rs_head; rs; rs = rs->next)
809 if (head->rpcb_map.r_prog == rs->prog)
812 rs = (struct rpcbdump_short *)
813 malloc(sizeof (struct rpcbdump_short));
817 if (rs_head == NULL) {
824 rs->prog = head->rpcb_map.r_prog;
825 rs->owner = head->rpcb_map.r_owner;
829 if (add_version(rs, head->rpcb_map.r_vers) == FALSE)
831 if (add_netid(rs, head->rpcb_map.r_netid) == FALSE)
835 " program version(s) netid(s) service owner\n");
836 for (rs = rs_head; rs; rs = rs->next) {
839 printf("%10ld ", rs->prog);
840 for (vl = rs->vlist; vl; vl = vl->next) {
841 sprintf(p, "%d", vl->vers);
846 printf("%-10s", buf);
848 for (nl = rs->nlist; nl; nl = nl->next) {
849 strlcat(buf, nl->netid, sizeof(buf));
851 strlcat(buf, ",", sizeof(buf));
853 printf("%-32s", buf);
854 rpc = getrpcbynumber(rs->prog);
856 printf(" %-11s", rpc->r_name);
858 printf(" %-11s", "-");
859 printf(" %s\n", rs->owner);
862 clnt_destroy(client);
864 error: warnx("no memory");
868 static char nullstring[] = "\000";
871 rpcbaddrlist(char *netid, int argc, char **argv)
873 rpcb_entry_list_ptr head = NULL;
874 struct timeval minutetimeout;
875 register CLIENT *client;
879 struct netbuf *targaddr;
885 client = clnt_rpcbind_create(host, RPCBVERS4, &targaddr);
887 struct netconfig *nconf;
889 nconf = getnetconfigent(netid);
891 nc_perror("rpcinfo: invalid transport");
894 client = getclnthandle(host, nconf, RPCBVERS4, &targaddr);
896 (void) freenetconfigent(nconf);
898 if (client == (CLIENT *)NULL) {
899 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
902 minutetimeout.tv_sec = 60;
903 minutetimeout.tv_usec = 0;
905 parms.r_prog = getprognum(argv[1]);
906 parms.r_vers = getvers(argv[2]);
907 parms.r_netid = client->cl_netid;
908 if (targaddr == NULL) {
909 parms.r_addr = nullstring; /* for XDRing */
912 * We also send the remote system the address we
913 * used to contact it in case it can help it
914 * connect back with us
916 struct netconfig *nconf;
918 nconf = getnetconfigent(client->cl_netid);
920 parms.r_addr = taddr2uaddr(nconf, targaddr);
921 if (parms.r_addr == NULL)
922 parms.r_addr = nullstring;
923 freenetconfigent(nconf);
925 parms.r_addr = nullstring; /* for XDRing */
930 parms.r_owner = nullstring;
932 if (CLNT_CALL(client, RPCBPROC_GETADDRLIST, (xdrproc_t) xdr_rpcb,
933 (char *) &parms, (xdrproc_t) xdr_rpcb_entry_list_ptr,
934 (char *) &head, minutetimeout) != RPC_SUCCESS) {
935 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
939 printf("No remote programs registered.\n");
942 " program vers tp_family/name/class address\t\t service\n");
943 for (; head != NULL; head = head->rpcb_entry_next) {
947 re = &head->rpcb_entry_map;
949 parms.r_prog, parms.r_vers);
950 sprintf(buf, "%s/%s/%s ",
951 re->r_nc_protofmly, re->r_nc_proto,
952 re->r_nc_semantics == NC_TPI_CLTS ? "clts" :
953 re->r_nc_semantics == NC_TPI_COTS ? "cots" :
955 printf("%-24s", buf);
956 printf("%-24s", re->r_maddr);
957 rpc = getrpcbynumber(parms.r_prog);
959 printf(" %-13s", rpc->r_name);
961 printf(" %-13s", "-");
965 clnt_destroy(client);
973 rpcbgetstat(int argc, char **argv)
975 rpcb_stat_byvers inf;
976 struct timeval minutetimeout;
977 register CLIENT *client;
981 rpcbs_rmtcalllist *pr;
984 char fieldbuf[MAXFIELD];
986 char linebuf[MAXLINE];
988 const char *pmaphdr[] = {
989 "NULL", "SET", "UNSET", "GETPORT",
992 const char *rpcb3hdr[] = {
993 "NULL", "SET", "UNSET", "GETADDR", "DUMP", "CALLIT", "TIME",
996 const char *rpcb4hdr[] = {
997 "NULL", "SET", "UNSET", "GETADDR", "DUMP", "CALLIT", "TIME",
998 "U2T", "T2U", "VERADDR", "INDRECT", "GETLIST", "GETSTAT"
1005 client = clnt_rpcbind_create(host, RPCBVERS4, NULL);
1007 client = local_rpcb(PMAPPROG, RPCBVERS4);
1008 if (client == (CLIENT *)NULL) {
1009 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
1012 minutetimeout.tv_sec = 60;
1013 minutetimeout.tv_usec = 0;
1014 memset((char *)&inf, 0, sizeof (rpcb_stat_byvers));
1015 if (CLNT_CALL(client, RPCBPROC_GETSTAT, (xdrproc_t) xdr_void, NULL,
1016 (xdrproc_t) xdr_rpcb_stat_byvers, (char *)&inf, minutetimeout)
1018 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
1021 printf("PORTMAP (version 2) statistics\n");
1023 for (i = 0; i <= rpcb_highproc_2; i++) {
1027 sprintf(fieldbuf, "%d/", inf[RPCBVERS_2_STAT].setinfo);
1029 case PMAPPROC_UNSET:
1030 sprintf(fieldbuf, "%d/",
1031 inf[RPCBVERS_2_STAT].unsetinfo);
1033 case PMAPPROC_GETPORT:
1035 for (pa = inf[RPCBVERS_2_STAT].addrinfo; pa;
1038 sprintf(fieldbuf, "%d/", cnt);
1040 case PMAPPROC_CALLIT:
1042 for (pr = inf[RPCBVERS_2_STAT].rmtinfo; pr;
1045 sprintf(fieldbuf, "%d/", cnt);
1047 default: break; /* For the remaining ones */
1049 cp = &fieldbuf[0] + strlen(fieldbuf);
1050 sprintf(cp, "%d", inf[RPCBVERS_2_STAT].info[i]);
1051 flen = strlen(fieldbuf);
1052 printf("%s%s", pmaphdr[i],
1053 spaces((TABSTOP * (1 + flen / TABSTOP))
1054 - strlen(pmaphdr[i])));
1055 sprintf(lp, "%s%s", fieldbuf,
1056 spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP))
1060 printf("\n%s\n\n", linebuf);
1062 if (inf[RPCBVERS_2_STAT].info[PMAPPROC_CALLIT]) {
1063 printf("PMAP_RMTCALL call statistics\n");
1064 print_rmtcallstat(RPCBVERS_2_STAT, &inf[RPCBVERS_2_STAT]);
1068 if (inf[RPCBVERS_2_STAT].info[PMAPPROC_GETPORT]) {
1069 printf("PMAP_GETPORT call statistics\n");
1070 print_getaddrstat(RPCBVERS_2_STAT, &inf[RPCBVERS_2_STAT]);
1074 printf("RPCBIND (version 3) statistics\n");
1076 for (i = 0; i <= rpcb_highproc_3; i++) {
1080 sprintf(fieldbuf, "%d/", inf[RPCBVERS_3_STAT].setinfo);
1082 case RPCBPROC_UNSET:
1083 sprintf(fieldbuf, "%d/",
1084 inf[RPCBVERS_3_STAT].unsetinfo);
1086 case RPCBPROC_GETADDR:
1088 for (pa = inf[RPCBVERS_3_STAT].addrinfo; pa;
1091 sprintf(fieldbuf, "%d/", cnt);
1093 case RPCBPROC_CALLIT:
1095 for (pr = inf[RPCBVERS_3_STAT].rmtinfo; pr;
1098 sprintf(fieldbuf, "%d/", cnt);
1100 default: break; /* For the remaining ones */
1102 cp = &fieldbuf[0] + strlen(fieldbuf);
1103 sprintf(cp, "%d", inf[RPCBVERS_3_STAT].info[i]);
1104 flen = strlen(fieldbuf);
1105 printf("%s%s", rpcb3hdr[i],
1106 spaces((TABSTOP * (1 + flen / TABSTOP))
1107 - strlen(rpcb3hdr[i])));
1108 sprintf(lp, "%s%s", fieldbuf,
1109 spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP))
1113 printf("\n%s\n\n", linebuf);
1115 if (inf[RPCBVERS_3_STAT].info[RPCBPROC_CALLIT]) {
1116 printf("RPCB_RMTCALL (version 3) call statistics\n");
1117 print_rmtcallstat(RPCBVERS_3_STAT, &inf[RPCBVERS_3_STAT]);
1121 if (inf[RPCBVERS_3_STAT].info[RPCBPROC_GETADDR]) {
1122 printf("RPCB_GETADDR (version 3) call statistics\n");
1123 print_getaddrstat(RPCBVERS_3_STAT, &inf[RPCBVERS_3_STAT]);
1127 printf("RPCBIND (version 4) statistics\n");
1129 for (j = 0; j <= 9; j += 9) { /* Just two iterations for printing */
1131 for (i = j; i <= MAX(8, rpcb_highproc_4 - 9 + j); i++) {
1135 sprintf(fieldbuf, "%d/",
1136 inf[RPCBVERS_4_STAT].setinfo);
1138 case RPCBPROC_UNSET:
1139 sprintf(fieldbuf, "%d/",
1140 inf[RPCBVERS_4_STAT].unsetinfo);
1142 case RPCBPROC_GETADDR:
1144 for (pa = inf[RPCBVERS_4_STAT].addrinfo; pa;
1147 sprintf(fieldbuf, "%d/", cnt);
1149 case RPCBPROC_CALLIT:
1151 for (pr = inf[RPCBVERS_4_STAT].rmtinfo; pr;
1154 sprintf(fieldbuf, "%d/", cnt);
1156 default: break; /* For the remaining ones */
1158 cp = &fieldbuf[0] + strlen(fieldbuf);
1160 * XXX: We also add RPCBPROC_GETADDRLIST queries to
1161 * RPCB_GETADDR because rpcbind includes the
1162 * RPCB_GETADDRLIST successes in RPCB_GETADDR.
1164 if (i != RPCBPROC_GETADDR)
1165 sprintf(cp, "%d", inf[RPCBVERS_4_STAT].info[i]);
1167 sprintf(cp, "%d", inf[RPCBVERS_4_STAT].info[i] +
1168 inf[RPCBVERS_4_STAT].info[RPCBPROC_GETADDRLIST]);
1169 flen = strlen(fieldbuf);
1170 printf("%s%s", rpcb4hdr[i],
1171 spaces((TABSTOP * (1 + flen / TABSTOP))
1172 - strlen(rpcb4hdr[i])));
1173 sprintf(lp, "%s%s", fieldbuf,
1174 spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP))
1178 printf("\n%s\n", linebuf);
1181 if (inf[RPCBVERS_4_STAT].info[RPCBPROC_CALLIT] ||
1182 inf[RPCBVERS_4_STAT].info[RPCBPROC_INDIRECT]) {
1184 printf("RPCB_RMTCALL (version 4) call statistics\n");
1185 print_rmtcallstat(RPCBVERS_4_STAT, &inf[RPCBVERS_4_STAT]);
1188 if (inf[RPCBVERS_4_STAT].info[RPCBPROC_GETADDR]) {
1190 printf("RPCB_GETADDR (version 4) call statistics\n");
1191 print_getaddrstat(RPCBVERS_4_STAT, &inf[RPCBVERS_4_STAT]);
1193 clnt_destroy(client);
1197 * Delete registeration for this (prog, vers, netid)
1200 deletereg(char *netid, int argc, char **argv)
1202 struct netconfig *nconf = NULL;
1207 nconf = getnetconfigent(netid);
1209 errx(1, "netid %s not supported", netid);
1211 if ((rpcb_unset(getprognum(argv[0]), getvers(argv[1]), nconf)) == 0)
1213 "could not delete registration for prog %s version %s",
1218 * Create and return a handle for the given nconf.
1219 * Exit if cannot create handle.
1222 clnt_addr_create(char *address, struct netconfig *nconf,
1223 u_long prog, u_long vers)
1226 static struct netbuf *nbuf;
1227 static int fd = RPC_ANYFD;
1229 if (fd == RPC_ANYFD) {
1230 if ((fd = __rpc_nconf2fd(nconf)) == -1) {
1231 rpc_createerr.cf_stat = RPC_TLIERROR;
1232 clnt_pcreateerror("rpcinfo");
1235 /* Convert the uaddr to taddr */
1236 nbuf = uaddr2taddr(nconf, address);
1238 errx(1, "no address for client handle");
1240 client = clnt_tli_create(fd, nconf, nbuf, prog, vers, 0, 0);
1241 if (client == (CLIENT *)NULL) {
1242 clnt_pcreateerror("rpcinfo");
1249 * If the version number is given, ping that (prog, vers); else try to find
1250 * the version numbers supported for that prog and ping all the versions.
1251 * Remote rpcbind is not contacted for this service. The requests are
1252 * sent directly to the services themselves.
1255 addrping(char *address, char *netid, int argc, char **argv)
1259 enum clnt_stat rpc_stat;
1260 u_long prognum, versnum, minvers, maxvers;
1261 struct rpc_err rpcerr;
1263 struct netconfig *nconf;
1266 if (argc < 1 || argc > 2 || (netid == NULL))
1268 nconf = getnetconfigent(netid);
1269 if (nconf == (struct netconfig *)NULL)
1270 errx(1, "could not find %s", netid);
1273 prognum = getprognum(argv[0]);
1274 if (argc == 1) { /* Version number not known */
1276 * A call to version 0 should fail with a program/version
1277 * mismatch, and give us the range of versions supported.
1281 versnum = getvers(argv[1]);
1283 client = clnt_addr_create(address, nconf, prognum, versnum);
1284 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1285 (char *)NULL, (xdrproc_t) xdr_void,
1288 /* Version number was known */
1289 if (pstatus(client, prognum, versnum) < 0)
1291 (void) CLNT_DESTROY(client);
1296 /* Version number not known */
1297 (void) CLNT_CONTROL(client, CLSET_FD_NCLOSE, (char *)NULL);
1298 (void) CLNT_CONTROL(client, CLGET_FD, (char *)&fd);
1299 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1300 clnt_geterr(client, &rpcerr);
1301 minvers = rpcerr.re_vers.low;
1302 maxvers = rpcerr.re_vers.high;
1303 } else if (rpc_stat == RPC_SUCCESS) {
1305 * Oh dear, it DOES support version 0.
1306 * Let's try version MAX_VERS.
1308 (void) CLNT_DESTROY(client);
1309 client = clnt_addr_create(address, nconf, prognum, MAX_VERS);
1310 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1311 (char *)NULL, (xdrproc_t) xdr_void,
1313 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1314 clnt_geterr(client, &rpcerr);
1315 minvers = rpcerr.re_vers.low;
1316 maxvers = rpcerr.re_vers.high;
1317 } else if (rpc_stat == RPC_SUCCESS) {
1319 * It also supports version MAX_VERS.
1320 * Looks like we have a wise guy.
1321 * OK, we give them information on all
1322 * 4 billion versions they support...
1327 (void) pstatus(client, prognum, MAX_VERS);
1331 (void) pstatus(client, prognum, (u_long)0);
1334 (void) CLNT_DESTROY(client);
1335 for (versnum = minvers; versnum <= maxvers; versnum++) {
1336 client = clnt_addr_create(address, nconf, prognum, versnum);
1337 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1338 (char *)NULL, (xdrproc_t) xdr_void,
1340 if (pstatus(client, prognum, versnum) < 0)
1342 (void) CLNT_DESTROY(client);
1351 * If the version number is given, ping that (prog, vers); else try to find
1352 * the version numbers supported for that prog and ping all the versions.
1353 * Remote rpcbind is *contacted* for this service. The requests are
1354 * then sent directly to the services themselves.
1357 progping(char *netid, int argc, char **argv)
1361 enum clnt_stat rpc_stat;
1362 u_long prognum, versnum, minvers, maxvers;
1363 struct rpc_err rpcerr;
1365 struct netconfig *nconf;
1367 if (argc < 2 || argc > 3 || (netid == NULL))
1369 prognum = getprognum(argv[1]);
1370 if (argc == 2) { /* Version number not known */
1372 * A call to version 0 should fail with a program/version
1373 * mismatch, and give us the range of versions supported.
1377 versnum = getvers(argv[2]);
1380 nconf = getnetconfigent(netid);
1381 if (nconf == (struct netconfig *)NULL)
1382 errx(1, "could not find %s", netid);
1383 client = clnt_tp_create(argv[0], prognum, versnum, nconf);
1385 client = clnt_create(argv[0], prognum, versnum, "NETPATH");
1387 if (client == (CLIENT *)NULL) {
1388 clnt_pcreateerror("rpcinfo");
1393 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1394 (char *)NULL, (xdrproc_t) xdr_void,
1397 /* Version number was known */
1398 if (pstatus(client, prognum, versnum) < 0)
1400 (void) CLNT_DESTROY(client);
1405 /* Version number not known */
1406 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1407 clnt_geterr(client, &rpcerr);
1408 minvers = rpcerr.re_vers.low;
1409 maxvers = rpcerr.re_vers.high;
1410 } else if (rpc_stat == RPC_SUCCESS) {
1412 * Oh dear, it DOES support version 0.
1413 * Let's try version MAX_VERS.
1416 (void) CLNT_CONTROL(client, CLSET_VERS, (char *)&versnum);
1417 rpc_stat = CLNT_CALL(client, NULLPROC,
1418 (xdrproc_t) xdr_void, (char *)NULL,
1419 (xdrproc_t) xdr_void, (char *)NULL, to);
1420 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1421 clnt_geterr(client, &rpcerr);
1422 minvers = rpcerr.re_vers.low;
1423 maxvers = rpcerr.re_vers.high;
1424 } else if (rpc_stat == RPC_SUCCESS) {
1426 * It also supports version MAX_VERS.
1427 * Looks like we have a wise guy.
1428 * OK, we give them information on all
1429 * 4 billion versions they support...
1434 (void) pstatus(client, prognum, MAX_VERS);
1438 (void) pstatus(client, prognum, (u_long)0);
1441 for (versnum = minvers; versnum <= maxvers; versnum++) {
1442 (void) CLNT_CONTROL(client, CLSET_VERS, (char *)&versnum);
1443 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1444 (char *)NULL, (xdrproc_t) xdr_void,
1446 if (pstatus(client, prognum, versnum) < 0)
1449 (void) CLNT_DESTROY(client);
1458 fprintf(stderr, "usage: rpcinfo [-m | -s] [host]\n");
1460 fprintf(stderr, " rpcinfo -p [host]\n");
1462 fprintf(stderr, " rpcinfo -T netid host prognum [versnum]\n");
1463 fprintf(stderr, " rpcinfo -l host prognum versnum\n");
1466 " rpcinfo [-n portnum] -u | -t host prognum [versnum]\n");
1469 " rpcinfo -a serv_address -T netid prognum [version]\n");
1470 fprintf(stderr, " rpcinfo -b prognum versnum\n");
1471 fprintf(stderr, " rpcinfo -d [-T netid] prognum versnum\n");
1476 getprognum (char *arg)
1479 register struct rpcent *rpc;
1480 register u_long prognum;
1483 while (*tptr && isdigit(*tptr++));
1484 if (*tptr || isalpha(*(tptr - 1))) {
1485 rpc = getrpcbyname(arg);
1487 errx(1, "%s is unknown service", arg);
1488 prognum = rpc->r_number;
1490 prognum = strtol(arg, &strptr, 10);
1491 if (strptr == arg || *strptr != '\0')
1492 errx(1, "%s is illegal program number", arg);
1501 register u_long vers;
1503 vers = (int) strtol(arg, &strptr, 10);
1504 if (strptr == arg || *strptr != '\0')
1505 errx(1, "%s is illegal version number", arg);
1510 * This routine should take a pointer to an "rpc_err" structure, rather than
1511 * a pointer to a CLIENT structure, but "clnt_perror" takes a pointer to
1512 * a CLIENT structure rather than a pointer to an "rpc_err" structure.
1513 * As such, we have to keep the CLIENT structure around in order to print
1514 * a good error message.
1517 pstatus(register CLIENT *client, u_long prog, u_long vers)
1519 struct rpc_err rpcerr;
1521 clnt_geterr(client, &rpcerr);
1522 if (rpcerr.re_status != RPC_SUCCESS) {
1523 clnt_perror(client, "rpcinfo");
1524 printf("program %lu version %lu is not available\n",
1528 printf("program %lu version %lu ready and waiting\n",
1535 clnt_rpcbind_create(char *host, int rpcbversnum, struct netbuf **targaddr)
1537 static const char *tlist[3] = {
1538 "circuit_n", "circuit_v", "datagram_v"
1541 struct netconfig *nconf;
1542 CLIENT *clnt = NULL;
1545 rpc_createerr.cf_stat = RPC_SUCCESS;
1546 for (i = 0; i < 3; i++) {
1547 if ((handle = __rpc_setconf(tlist[i])) == NULL)
1549 while (clnt == (CLIENT *)NULL) {
1550 if ((nconf = __rpc_getconf(handle)) == NULL) {
1551 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1552 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1555 clnt = getclnthandle(host, nconf, rpcbversnum,
1560 __rpc_endconf(handle);
1566 getclnthandle(char *host, struct netconfig *nconf,
1567 u_long rpcbversnum, struct netbuf **targaddr)
1570 struct addrinfo hints, *res;
1571 CLIENT *client = NULL;
1573 /* Get the address of the rpcbind */
1574 memset(&hints, 0, sizeof hints);
1575 if (getaddrinfo(host, "rpcbind", &hints, &res) != 0) {
1576 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1579 addr.len = addr.maxlen = res->ai_addrlen;
1580 addr.buf = res->ai_addr;
1581 client = clnt_tli_create(RPC_ANYFD, nconf, &addr, RPCBPROG,
1584 if (targaddr != NULL) {
1586 (struct netbuf *)malloc(sizeof (struct netbuf));
1587 if (*targaddr != NULL) {
1588 (*targaddr)->maxlen = addr.maxlen;
1589 (*targaddr)->len = addr.len;
1590 (*targaddr)->buf = (char *)malloc(addr.len);
1591 if ((*targaddr)->buf != NULL) {
1592 memcpy((*targaddr)->buf, addr.buf,
1598 if (rpc_createerr.cf_stat == RPC_TLIERROR) {
1600 * Assume that the other system is dead; this is a
1601 * better error to display to the user.
1603 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1604 rpc_createerr.cf_error.re_status = RPC_FAILED;
1612 print_rmtcallstat(int rtype, rpcb_stat *infp)
1614 register rpcbs_rmtcalllist_ptr pr;
1617 if (rtype == RPCBVERS_4_STAT)
1619 "prog\t\tvers\tproc\tnetid\tindirect success failure\n");
1621 printf("prog\t\tvers\tproc\tnetid\tsuccess\tfailure\n");
1622 for (pr = infp->rmtinfo; pr; pr = pr->next) {
1623 rpc = getrpcbynumber(pr->prog);
1625 printf("%-16s", rpc->r_name);
1627 printf("%-16d", pr->prog);
1628 printf("%d\t%d\t%s\t",
1629 pr->vers, pr->proc, pr->netid);
1630 if (rtype == RPCBVERS_4_STAT)
1631 printf("%d\t ", pr->indirect);
1632 printf("%d\t%d\n", pr->success, pr->failure);
1637 print_getaddrstat(int rtype, rpcb_stat *infp)
1639 rpcbs_addrlist_ptr al;
1640 register struct rpcent *rpc;
1642 printf("prog\t\tvers\tnetid\t success\tfailure\n");
1643 for (al = infp->addrinfo; al; al = al->next) {
1644 rpc = getrpcbynumber(al->prog);
1646 printf("%-16s", rpc->r_name);
1648 printf("%-16d", al->prog);
1649 printf("%d\t%s\t %-12d\t%d\n",
1650 al->vers, al->netid,
1651 al->success, al->failure);
1658 static char space_array[] = /* 64 spaces */
1661 if (howmany <= 0 || howmany > sizeof (space_array)) {
1664 return (&space_array[sizeof (space_array) - howmany - 1]);