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 /* #ident "@(#)rpcinfo.c 1.18 93/07/05 SMI" */
40 static char sccsid[] = "@(#)rpcinfo.c 1.16 89/04/05 Copyr 1986 Sun Micro";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * rpcinfo: ping a particular rpc program
49 * or dump the registered programs on the remote machine.
53 * We are for now defining PORTMAP here. It doesn't even compile
54 * unless it is defined.
61 * If PORTMAP is defined, rpcinfo will talk to both portmapper and
62 * rpcbind programs; else it talks only to rpcbind. In the latter case
63 * all the portmapper specific options such as -u, -t, -p become void.
65 #include <sys/types.h>
66 #include <sys/param.h>
67 #include <sys/socket.h>
71 #include <rpc/rpcb_prot.h>
72 #include <rpc/rpcent.h>
73 #include <rpc/nettype.h>
74 #include <rpc/rpc_com.h>
81 #ifdef PORTMAP /* Support for version 2 portmapper */
82 #include <netinet/in.h>
84 #include <arpa/inet.h>
85 #include <rpc/pmap_prot.h>
86 #include <rpc/pmap_clnt.h>
89 #define MAXHOSTLEN 256
90 #define MIN_VERS ((u_long) 0)
91 #define MAX_VERS ((u_long) 4294967295UL)
92 #define UNKNOWN "unknown"
95 * Functions to be performed.
97 #define NONE 0 /* no function */
98 #define PMAPDUMP 1 /* dump portmapper registrations */
99 #define TCPPING 2 /* ping TCP service */
100 #define UDPPING 3 /* ping UDP service */
101 #define BROADCAST 4 /* ping broadcast service */
102 #define DELETES 5 /* delete registration for the service */
103 #define ADDRPING 6 /* pings at the given address */
104 #define PROGPING 7 /* pings a program on a given host */
105 #define RPCBDUMP 8 /* dump rpcbind registrations */
106 #define RPCBDUMP_SHORT 9 /* dump rpcbind registrations - short version */
107 #define RPCBADDRLIST 10 /* dump addr list about one prog */
108 #define RPCBGETSTAT 11 /* Get statistics */
112 struct netidlist *next;
117 struct verslist *next;
120 struct rpcbdump_short {
122 struct verslist *vlist;
123 struct netidlist *nlist;
124 struct rpcbdump_short *next;
131 static void ip_ping(u_short, const char *, int, char **);
132 static CLIENT *clnt_com_create(struct sockaddr_in *, u_long, u_long, int *,
134 static void pmapdump(int, char **);
135 static void get_inet_address(struct sockaddr_in *, char *);
138 static bool_t reply_proc(void *, struct netbuf *, struct netconfig *);
139 static void brdcst(int, char **);
140 static void addrping(char *, char *, int, char **);
141 static void progping(char *, int, char **);
142 static CLIENT *clnt_addr_create(char *, struct netconfig *, u_long, u_long);
143 static CLIENT *clnt_rpcbind_create(char *, int, struct netbuf **);
144 static CLIENT *getclnthandle(char *, struct netconfig *, u_long,
146 static CLIENT *local_rpcb(u_long, u_long);
147 static int pstatus(CLIENT *, u_long, u_long);
148 static void rpcbdump(int, char *, int, char **);
149 static void rpcbgetstat(int, char **);
150 static void rpcbaddrlist(char *, int, char **);
151 static void deletereg(char *, int, char **);
152 static void print_rmtcallstat(int, rpcb_stat *);
153 static void print_getaddrstat(int, rpcb_stat *);
154 static void usage(void);
155 static u_long getprognum(char *);
156 static u_long getvers(char *);
157 static char *spaces(int);
158 static bool_t add_version(struct rpcbdump_short *, u_long);
159 static bool_t add_netid(struct rpcbdump_short *, char *);
162 main(int argc, char **argv)
168 char *address = NULL;
177 while ((c = getopt(argc, argv, "a:bdlmn:pstT:u")) != -1) {
179 while ((c = getopt(argc, argv, "a:bdlmn:sT:")) != -1) {
184 if (function != NONE)
191 if (function != NONE)
198 if (function != NONE)
205 portnum = (u_short) strtol(optarg, &strptr, 10);
206 if (strptr == optarg || *strptr != '\0')
207 errx(1, "%s is illegal port number", optarg);
212 if (function != NONE)
218 if (function != NONE)
221 function = BROADCAST;
225 if (function != NONE)
232 if (function != NONE)
235 function = RPCBADDRLIST;
239 if (function != NONE)
242 function = RPCBGETSTAT;
246 if (function != NONE)
249 function = RPCBDUMP_SHORT;
261 if (errflg || ((function == ADDRPING) && !netid))
264 if (function == NONE) {
265 if (argc - optind > 1)
276 pmapdump(argc - optind, argv + optind);
280 ip_ping(portnum, "udp", argc - optind, argv + optind);
284 ip_ping(portnum, "tcp", argc - optind, argv + optind);
288 brdcst(argc - optind, argv + optind);
291 deletereg(netid, argc - optind, argv + optind);
294 addrping(address, netid, argc - optind, argv + optind);
297 progping(netid, argc - optind, argv + optind);
301 rpcbdump(function, netid, argc - optind, argv + optind);
304 rpcbgetstat(argc - optind, argv + optind);
307 rpcbaddrlist(netid, argc - optind, argv + optind);
314 local_rpcb(u_long prog, u_long vers)
317 struct netconfig *nconf;
320 localhandle = setnetconfig();
321 while ((nconf = getnetconfig(localhandle)) != NULL) {
322 if (nconf->nc_protofmly != NULL &&
323 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
327 warnx("getnetconfig: %s", nc_sperror());
331 clnt = clnt_tp_create(NULL, prog, vers, nconf);
332 endnetconfig(localhandle);
338 clnt_com_create(struct sockaddr_in *addr, u_long prog, u_long vers,
339 int *fdp, const char *trans)
343 if (strcmp(trans, "tcp") == 0) {
344 clnt = clnttcp_create(addr, prog, vers, fdp, 0, 0);
350 clnt = clntudp_create(addr, prog, vers, to, fdp);
352 if (clnt == (CLIENT *)NULL) {
353 clnt_pcreateerror("rpcinfo");
354 if (vers == MIN_VERS)
355 printf("program %lu is not available\n", prog);
357 printf("program %lu version %lu is not available\n",
365 * If portnum is 0, then go and get the address from portmapper, which happens
366 * transparently through clnt*_create(); If version number is not given, it
367 * tries to find out the version number by making a call to version 0 and if
368 * that fails, it obtains the high order and the low order version number. If
369 * version 0 calls succeeds, it tries for MAXVERS call and repeats the same.
372 ip_ping(u_short portnum, const char *trans, int argc, char **argv)
377 struct sockaddr_in addr;
378 enum clnt_stat rpc_stat;
379 u_long prognum, vers, minvers, maxvers;
380 struct rpc_err rpcerr;
383 if (argc < 2 || argc > 3)
387 prognum = getprognum(argv[1]);
388 get_inet_address(&addr, argv[0]);
389 if (argc == 2) { /* Version number not known */
391 * A call to version 0 should fail with a program/version
392 * mismatch, and give us the range of versions supported.
396 vers = getvers(argv[2]);
398 addr.sin_port = htons(portnum);
399 client = clnt_com_create(&addr, prognum, vers, &fd, trans);
400 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
401 (char *)NULL, (xdrproc_t) xdr_void, (char *)NULL,
404 /* Version number was known */
405 if (pstatus(client, prognum, vers) < 0)
407 (void) CLNT_DESTROY(client);
410 /* Version number not known */
411 (void) CLNT_CONTROL(client, CLSET_FD_NCLOSE, (char *)NULL);
412 if (rpc_stat == RPC_PROGVERSMISMATCH) {
413 clnt_geterr(client, &rpcerr);
414 minvers = rpcerr.re_vers.low;
415 maxvers = rpcerr.re_vers.high;
416 } else if (rpc_stat == RPC_SUCCESS) {
418 * Oh dear, it DOES support version 0.
419 * Let's try version MAX_VERS.
421 (void) CLNT_DESTROY(client);
422 addr.sin_port = htons(portnum);
423 client = clnt_com_create(&addr, prognum, MAX_VERS, &fd, trans);
424 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
425 (char *)NULL, (xdrproc_t) xdr_void,
427 if (rpc_stat == RPC_PROGVERSMISMATCH) {
428 clnt_geterr(client, &rpcerr);
429 minvers = rpcerr.re_vers.low;
430 maxvers = rpcerr.re_vers.high;
431 } else if (rpc_stat == RPC_SUCCESS) {
433 * It also supports version MAX_VERS.
434 * Looks like we have a wise guy.
435 * OK, we give them information on all
436 * 4 billion versions they support...
441 (void) pstatus(client, prognum, MAX_VERS);
445 (void) pstatus(client, prognum, (u_long)0);
448 (void) CLNT_DESTROY(client);
449 for (vers = minvers; vers <= maxvers; vers++) {
450 addr.sin_port = htons(portnum);
451 client = clnt_com_create(&addr, prognum, vers, &fd, trans);
452 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
453 (char *)NULL, (xdrproc_t) xdr_void,
455 if (pstatus(client, prognum, vers) < 0)
457 (void) CLNT_DESTROY(client);
466 * Dump all the portmapper registerations
469 pmapdump(int argc, char **argv)
471 struct sockaddr_in server_addr;
472 struct pmaplist *head = NULL;
473 int socket = RPC_ANYSOCK;
474 struct timeval minutetimeout;
475 register CLIENT *client;
477 enum clnt_stat clnt_st;
485 get_inet_address(&server_addr, host);
486 server_addr.sin_port = htons(PMAPPORT);
487 client = clnttcp_create(&server_addr, PMAPPROG, PMAPVERS,
490 client = local_rpcb(PMAPPROG, PMAPVERS);
492 if (client == NULL) {
493 if (rpc_createerr.cf_stat == RPC_TLIERROR) {
495 * "Misc. TLI error" is not too helpful. Most likely
496 * the connection to the remote server timed out, so
497 * this error is at least less perplexing.
499 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
500 rpc_createerr.cf_error.re_status = RPC_FAILED;
502 clnt_pcreateerror("rpcinfo: can't contact portmapper");
506 minutetimeout.tv_sec = 60;
507 minutetimeout.tv_usec = 0;
509 clnt_st = CLNT_CALL(client, PMAPPROC_DUMP, (xdrproc_t) xdr_void,
510 NULL, (xdrproc_t) xdr_pmaplist_ptr, (char *)&head,
512 if (clnt_st != RPC_SUCCESS) {
513 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
514 (clnt_st == RPC_PROGUNAVAIL)) {
515 CLNT_GETERR(client, &err);
516 if (err.re_vers.low > PMAPVERS) {
518 warnx("%s does not support portmapper."
519 "Try rpcinfo %s instead", host,
522 warnx("local host does not support "
523 "portmapper. Try 'rpcinfo' "
528 clnt_perror(client, "rpcinfo: can't contact portmapper");
532 printf("No remote programs registered.\n");
534 printf(" program vers proto port service\n");
535 for (; head != NULL; head = head->pml_next) {
537 head->pml_map.pm_prog,
538 head->pml_map.pm_vers);
539 if (head->pml_map.pm_prot == IPPROTO_UDP)
540 printf("%6s", "udp");
541 else if (head->pml_map.pm_prot == IPPROTO_TCP)
542 printf("%6s", "tcp");
543 else if (head->pml_map.pm_prot == IPPROTO_ST)
544 printf("%6s", "local");
546 printf("%6ld", head->pml_map.pm_prot);
547 printf("%7ld", head->pml_map.pm_port);
548 rpc = getrpcbynumber(head->pml_map.pm_prog);
550 printf(" %s\n", rpc->r_name);
558 get_inet_address(struct sockaddr_in *addr, char *host)
560 struct netconfig *nconf;
561 struct addrinfo hints, *res;
564 (void) memset((char *)addr, 0, sizeof (*addr));
565 addr->sin_addr.s_addr = inet_addr(host);
566 if (addr->sin_addr.s_addr == -1 || addr->sin_addr.s_addr == 0) {
567 if ((nconf = __rpc_getconfip("udp")) == NULL &&
568 (nconf = __rpc_getconfip("tcp")) == NULL)
569 errx(1, "couldn't find a suitable transport");
571 memset(&hints, 0, sizeof hints);
572 hints.ai_family = AF_INET;
573 if ((error = getaddrinfo(host, "rpcbind", &hints, &res))
575 errx(1, "%s: %s", host, gai_strerror(error));
577 memcpy(addr, res->ai_addr, res->ai_addrlen);
580 (void) freenetconfigent(nconf);
583 addr->sin_family = AF_INET;
589 * reply_proc collects replies from the broadcast.
590 * to get a unique list of responses the output of rpcinfo should
591 * be piped through sort(1) and then uniq(1).
596 reply_proc(void *res, struct netbuf *who, struct netconfig *nconf)
597 /* void *res; Nothing comes back */
598 /* struct netbuf *who; Who sent us the reply */
599 /* struct netconfig *nconf; On which transport the reply came */
602 char hostbuf[NI_MAXHOST];
603 const char *hostname;
604 struct sockaddr *sa = (struct sockaddr *)who->buf;
606 if (getnameinfo(sa, sa->sa_len, hostbuf, NI_MAXHOST, NULL, 0, 0)) {
611 if (!(uaddr = taddr2uaddr(nconf, who))) {
614 printf("%s\t%s\n", uaddr, hostname);
615 if (strcmp(uaddr, UNKNOWN))
621 brdcst(int argc, char **argv)
623 enum clnt_stat rpc_stat;
624 u_long prognum, vers;
628 prognum = getprognum(argv[0]);
629 vers = getvers(argv[1]);
630 rpc_stat = rpc_broadcast(prognum, vers, NULLPROC,
631 (xdrproc_t) xdr_void, (char *)NULL, (xdrproc_t) xdr_void,
632 (char *)NULL, (resultproc_t) reply_proc, NULL);
633 if ((rpc_stat != RPC_SUCCESS) && (rpc_stat != RPC_TIMEDOUT))
634 errx(1, "broadcast failed: %s", clnt_sperrno(rpc_stat));
639 add_version(struct rpcbdump_short *rs, u_long vers)
643 for (vl = rs->vlist; vl; vl = vl->next)
644 if (vl->vers == vers)
648 vl = (struct verslist *)malloc(sizeof (struct verslist));
652 vl->next = rs->vlist;
658 add_netid(struct rpcbdump_short *rs, char *netid)
660 struct netidlist *nl;
662 for (nl = rs->nlist; nl; nl = nl->next)
663 if (strcmp(nl->netid, netid) == 0)
667 nl = (struct netidlist *)malloc(sizeof (struct netidlist));
671 nl->next = rs->nlist;
677 rpcbdump(int dumptype, char *netid, int argc, char **argv)
679 rpcblist_ptr head = NULL;
680 struct timeval minutetimeout;
681 register CLIENT *client;
684 struct netidlist *nl;
686 struct rpcbdump_short *rs, *rs_tail;
688 enum clnt_stat clnt_st;
690 struct rpcbdump_short *rs_head = NULL;
697 client = clnt_rpcbind_create(host, RPCBVERS, NULL);
699 struct netconfig *nconf;
701 nconf = getnetconfigent(netid);
703 nc_perror("rpcinfo: invalid transport");
706 client = getclnthandle(host, nconf, RPCBVERS, NULL);
708 (void) freenetconfigent(nconf);
711 client = local_rpcb(PMAPPROG, RPCBVERS);
713 if (client == (CLIENT *)NULL) {
714 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
718 minutetimeout.tv_sec = 60;
719 minutetimeout.tv_usec = 0;
720 clnt_st = CLNT_CALL(client, RPCBPROC_DUMP, (xdrproc_t) xdr_void,
721 NULL, (xdrproc_t) xdr_rpcblist_ptr, (char *) &head,
723 if (clnt_st != RPC_SUCCESS) {
724 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
725 (clnt_st == RPC_PROGUNAVAIL)) {
728 CLNT_GETERR(client, &err);
729 if (err.re_vers.low == RPCBVERS4) {
731 clnt_control(client, CLSET_VERS, (char *)&vers);
732 clnt_st = CLNT_CALL(client, RPCBPROC_DUMP,
733 (xdrproc_t) xdr_void, NULL,
734 (xdrproc_t) xdr_rpcblist_ptr, (char *) &head,
736 if (clnt_st != RPC_SUCCESS)
739 if (err.re_vers.high == PMAPVERS) {
741 struct pmaplist *pmaphead = NULL;
742 rpcblist_ptr list, prev;
745 clnt_control(client, CLSET_VERS, (char *)&vers);
746 clnt_st = CLNT_CALL(client, PMAPPROC_DUMP,
747 (xdrproc_t) xdr_void, NULL,
748 (xdrproc_t) xdr_pmaplist_ptr,
749 (char *)&pmaphead, minutetimeout);
750 if (clnt_st != RPC_SUCCESS)
753 * convert to rpcblist_ptr format
755 for (head = NULL; pmaphead != NULL;
756 pmaphead = pmaphead->pml_next) {
757 list = (rpcblist *)malloc(sizeof (rpcblist));
763 prev->rpcb_next = (rpcblist_ptr) list;
765 list->rpcb_next = NULL;
766 list->rpcb_map.r_prog = pmaphead->pml_map.pm_prog;
767 list->rpcb_map.r_vers = pmaphead->pml_map.pm_vers;
768 if (pmaphead->pml_map.pm_prot == IPPROTO_UDP)
769 list->rpcb_map.r_netid = "udp";
770 else if (pmaphead->pml_map.pm_prot == IPPROTO_TCP)
771 list->rpcb_map.r_netid = "tcp";
773 #define MAXLONG_AS_STRING "2147483648"
774 list->rpcb_map.r_netid =
775 malloc(strlen(MAXLONG_AS_STRING) + 1);
776 if (list->rpcb_map.r_netid == NULL)
778 sprintf(list->rpcb_map.r_netid, "%6ld",
779 pmaphead->pml_map.pm_prot);
781 list->rpcb_map.r_owner = UNKNOWN;
782 low = pmaphead->pml_map.pm_port & 0xff;
783 high = (pmaphead->pml_map.pm_port >> 8) & 0xff;
784 list->rpcb_map.r_addr = strdup("0.0.0.0.XXX.XXX");
785 sprintf(&list->rpcb_map.r_addr[8], "%d.%d",
791 } else { /* any other error */
793 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
798 printf("No remote programs registered.\n");
799 } else if (dumptype == RPCBDUMP) {
801 " program version netid address service owner\n");
802 for (; head != NULL; head = head->rpcb_next) {
804 head->rpcb_map.r_prog, head->rpcb_map.r_vers);
805 printf("%-9s ", head->rpcb_map.r_netid);
806 printf("%-22s", head->rpcb_map.r_addr);
807 rpc = getrpcbynumber(head->rpcb_map.r_prog);
809 printf(" %-10s", rpc->r_name);
811 printf(" %-10s", "-");
812 printf(" %s\n", head->rpcb_map.r_owner);
814 } else if (dumptype == RPCBDUMP_SHORT) {
815 for (; head != NULL; head = head->rpcb_next) {
816 for (rs = rs_head; rs; rs = rs->next)
817 if (head->rpcb_map.r_prog == rs->prog)
820 rs = (struct rpcbdump_short *)
821 malloc(sizeof (struct rpcbdump_short));
825 if (rs_head == NULL) {
832 rs->prog = head->rpcb_map.r_prog;
833 rs->owner = head->rpcb_map.r_owner;
837 if (add_version(rs, head->rpcb_map.r_vers) == FALSE)
839 if (add_netid(rs, head->rpcb_map.r_netid) == FALSE)
843 " program version(s) netid(s) service owner\n");
844 for (rs = rs_head; rs; rs = rs->next) {
847 printf("%10ld ", rs->prog);
848 for (vl = rs->vlist; vl; vl = vl->next) {
849 sprintf(p, "%d", vl->vers);
854 printf("%-10s", buf);
856 for (nl = rs->nlist; nl; nl = nl->next) {
857 strcat(buf, nl->netid);
861 printf("%-32s", buf);
862 rpc = getrpcbynumber(rs->prog);
864 printf(" %-11s", rpc->r_name);
866 printf(" %-11s", "-");
867 printf(" %s\n", rs->owner);
870 clnt_destroy(client);
872 error: warnx("no memory");
876 static char nullstring[] = "\000";
879 rpcbaddrlist(char *netid, int argc, char **argv)
881 rpcb_entry_list_ptr head = NULL;
882 struct timeval minutetimeout;
883 register CLIENT *client;
887 struct netbuf *targaddr;
893 client = clnt_rpcbind_create(host, RPCBVERS4, &targaddr);
895 struct netconfig *nconf;
897 nconf = getnetconfigent(netid);
899 nc_perror("rpcinfo: invalid transport");
902 client = getclnthandle(host, nconf, RPCBVERS4, &targaddr);
904 (void) freenetconfigent(nconf);
906 if (client == (CLIENT *)NULL) {
907 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
910 minutetimeout.tv_sec = 60;
911 minutetimeout.tv_usec = 0;
913 parms.r_prog = getprognum(argv[1]);
914 parms.r_vers = getvers(argv[2]);
915 parms.r_netid = client->cl_netid;
916 if (targaddr == NULL) {
917 parms.r_addr = nullstring; /* for XDRing */
920 * We also send the remote system the address we
921 * used to contact it in case it can help it
922 * connect back with us
924 struct netconfig *nconf;
926 nconf = getnetconfigent(client->cl_netid);
928 parms.r_addr = taddr2uaddr(nconf, targaddr);
929 if (parms.r_addr == NULL)
930 parms.r_addr = nullstring;
931 freenetconfigent(nconf);
933 parms.r_addr = nullstring; /* for XDRing */
938 parms.r_owner = nullstring;
940 if (CLNT_CALL(client, RPCBPROC_GETADDRLIST, (xdrproc_t) xdr_rpcb,
941 (char *) &parms, (xdrproc_t) xdr_rpcb_entry_list_ptr,
942 (char *) &head, minutetimeout) != RPC_SUCCESS) {
943 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
947 printf("No remote programs registered.\n");
950 " program vers tp_family/name/class address\t\t service\n");
951 for (; head != NULL; head = head->rpcb_entry_next) {
955 re = &head->rpcb_entry_map;
957 parms.r_prog, parms.r_vers);
958 sprintf(buf, "%s/%s/%s ",
959 re->r_nc_protofmly, re->r_nc_proto,
960 re->r_nc_semantics == NC_TPI_CLTS ? "clts" :
961 re->r_nc_semantics == NC_TPI_COTS ? "cots" :
963 printf("%-24s", buf);
964 printf("%-24s", re->r_maddr);
965 rpc = getrpcbynumber(parms.r_prog);
967 printf(" %-13s", rpc->r_name);
969 printf(" %-13s", "-");
973 clnt_destroy(client);
981 rpcbgetstat(int argc, char **argv)
983 rpcb_stat_byvers inf;
984 struct timeval minutetimeout;
985 register CLIENT *client;
989 rpcbs_rmtcalllist *pr;
992 char fieldbuf[MAXFIELD];
994 char linebuf[MAXLINE];
996 const char *pmaphdr[] = {
997 "NULL", "SET", "UNSET", "GETPORT",
1000 const char *rpcb3hdr[] = {
1001 "NULL", "SET", "UNSET", "GETADDR", "DUMP", "CALLIT", "TIME",
1004 const char *rpcb4hdr[] = {
1005 "NULL", "SET", "UNSET", "GETADDR", "DUMP", "CALLIT", "TIME",
1006 "U2T", "T2U", "VERADDR", "INDRECT", "GETLIST", "GETSTAT"
1013 client = clnt_rpcbind_create(host, RPCBVERS4, NULL);
1015 client = local_rpcb(PMAPPROG, RPCBVERS4);
1016 if (client == (CLIENT *)NULL) {
1017 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
1020 minutetimeout.tv_sec = 60;
1021 minutetimeout.tv_usec = 0;
1022 memset((char *)&inf, 0, sizeof (rpcb_stat_byvers));
1023 if (CLNT_CALL(client, RPCBPROC_GETSTAT, (xdrproc_t) xdr_void, NULL,
1024 (xdrproc_t) xdr_rpcb_stat_byvers, (char *)&inf, minutetimeout)
1026 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
1029 printf("PORTMAP (version 2) statistics\n");
1031 for (i = 0; i <= rpcb_highproc_2; i++) {
1035 sprintf(fieldbuf, "%d/", inf[RPCBVERS_2_STAT].setinfo);
1037 case PMAPPROC_UNSET:
1038 sprintf(fieldbuf, "%d/",
1039 inf[RPCBVERS_2_STAT].unsetinfo);
1041 case PMAPPROC_GETPORT:
1043 for (pa = inf[RPCBVERS_2_STAT].addrinfo; pa;
1046 sprintf(fieldbuf, "%d/", cnt);
1048 case PMAPPROC_CALLIT:
1050 for (pr = inf[RPCBVERS_2_STAT].rmtinfo; pr;
1053 sprintf(fieldbuf, "%d/", cnt);
1055 default: break; /* For the remaining ones */
1057 cp = &fieldbuf[0] + strlen(fieldbuf);
1058 sprintf(cp, "%d", inf[RPCBVERS_2_STAT].info[i]);
1059 flen = strlen(fieldbuf);
1060 printf("%s%s", pmaphdr[i],
1061 spaces((TABSTOP * (1 + flen / TABSTOP))
1062 - strlen(pmaphdr[i])));
1063 sprintf(lp, "%s%s", fieldbuf,
1064 spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP))
1068 printf("\n%s\n\n", linebuf);
1070 if (inf[RPCBVERS_2_STAT].info[PMAPPROC_CALLIT]) {
1071 printf("PMAP_RMTCALL call statistics\n");
1072 print_rmtcallstat(RPCBVERS_2_STAT, &inf[RPCBVERS_2_STAT]);
1076 if (inf[RPCBVERS_2_STAT].info[PMAPPROC_GETPORT]) {
1077 printf("PMAP_GETPORT call statistics\n");
1078 print_getaddrstat(RPCBVERS_2_STAT, &inf[RPCBVERS_2_STAT]);
1082 printf("RPCBIND (version 3) statistics\n");
1084 for (i = 0; i <= rpcb_highproc_3; i++) {
1088 sprintf(fieldbuf, "%d/", inf[RPCBVERS_3_STAT].setinfo);
1090 case RPCBPROC_UNSET:
1091 sprintf(fieldbuf, "%d/",
1092 inf[RPCBVERS_3_STAT].unsetinfo);
1094 case RPCBPROC_GETADDR:
1096 for (pa = inf[RPCBVERS_3_STAT].addrinfo; pa;
1099 sprintf(fieldbuf, "%d/", cnt);
1101 case RPCBPROC_CALLIT:
1103 for (pr = inf[RPCBVERS_3_STAT].rmtinfo; pr;
1106 sprintf(fieldbuf, "%d/", cnt);
1108 default: break; /* For the remaining ones */
1110 cp = &fieldbuf[0] + strlen(fieldbuf);
1111 sprintf(cp, "%d", inf[RPCBVERS_3_STAT].info[i]);
1112 flen = strlen(fieldbuf);
1113 printf("%s%s", rpcb3hdr[i],
1114 spaces((TABSTOP * (1 + flen / TABSTOP))
1115 - strlen(rpcb3hdr[i])));
1116 sprintf(lp, "%s%s", fieldbuf,
1117 spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP))
1121 printf("\n%s\n\n", linebuf);
1123 if (inf[RPCBVERS_3_STAT].info[RPCBPROC_CALLIT]) {
1124 printf("RPCB_RMTCALL (version 3) call statistics\n");
1125 print_rmtcallstat(RPCBVERS_3_STAT, &inf[RPCBVERS_3_STAT]);
1129 if (inf[RPCBVERS_3_STAT].info[RPCBPROC_GETADDR]) {
1130 printf("RPCB_GETADDR (version 3) call statistics\n");
1131 print_getaddrstat(RPCBVERS_3_STAT, &inf[RPCBVERS_3_STAT]);
1135 printf("RPCBIND (version 4) statistics\n");
1137 for (j = 0; j <= 9; j += 9) { /* Just two iterations for printing */
1139 for (i = j; i <= MAX(8, rpcb_highproc_4 - 9 + j); i++) {
1143 sprintf(fieldbuf, "%d/",
1144 inf[RPCBVERS_4_STAT].setinfo);
1146 case RPCBPROC_UNSET:
1147 sprintf(fieldbuf, "%d/",
1148 inf[RPCBVERS_4_STAT].unsetinfo);
1150 case RPCBPROC_GETADDR:
1152 for (pa = inf[RPCBVERS_4_STAT].addrinfo; pa;
1155 sprintf(fieldbuf, "%d/", cnt);
1157 case RPCBPROC_CALLIT:
1159 for (pr = inf[RPCBVERS_4_STAT].rmtinfo; pr;
1162 sprintf(fieldbuf, "%d/", cnt);
1164 default: break; /* For the remaining ones */
1166 cp = &fieldbuf[0] + strlen(fieldbuf);
1168 * XXX: We also add RPCBPROC_GETADDRLIST queries to
1169 * RPCB_GETADDR because rpcbind includes the
1170 * RPCB_GETADDRLIST successes in RPCB_GETADDR.
1172 if (i != RPCBPROC_GETADDR)
1173 sprintf(cp, "%d", inf[RPCBVERS_4_STAT].info[i]);
1175 sprintf(cp, "%d", inf[RPCBVERS_4_STAT].info[i] +
1176 inf[RPCBVERS_4_STAT].info[RPCBPROC_GETADDRLIST]);
1177 flen = strlen(fieldbuf);
1178 printf("%s%s", rpcb4hdr[i],
1179 spaces((TABSTOP * (1 + flen / TABSTOP))
1180 - strlen(rpcb4hdr[i])));
1181 sprintf(lp, "%s%s", fieldbuf,
1182 spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP))
1186 printf("\n%s\n", linebuf);
1189 if (inf[RPCBVERS_4_STAT].info[RPCBPROC_CALLIT] ||
1190 inf[RPCBVERS_4_STAT].info[RPCBPROC_INDIRECT]) {
1192 printf("RPCB_RMTCALL (version 4) call statistics\n");
1193 print_rmtcallstat(RPCBVERS_4_STAT, &inf[RPCBVERS_4_STAT]);
1196 if (inf[RPCBVERS_4_STAT].info[RPCBPROC_GETADDR]) {
1198 printf("RPCB_GETADDR (version 4) call statistics\n");
1199 print_getaddrstat(RPCBVERS_4_STAT, &inf[RPCBVERS_4_STAT]);
1201 clnt_destroy(client);
1205 * Delete registeration for this (prog, vers, netid)
1208 deletereg(char *netid, int argc, char **argv)
1210 struct netconfig *nconf = NULL;
1215 nconf = getnetconfigent(netid);
1217 errx(1, "netid %s not supported", netid);
1219 if ((rpcb_unset(getprognum(argv[0]), getvers(argv[1]), nconf)) == 0)
1221 "could not delete registration for prog %s version %s",
1226 * Create and return a handle for the given nconf.
1227 * Exit if cannot create handle.
1230 clnt_addr_create(char *address, struct netconfig *nconf,
1231 u_long prog, u_long vers)
1234 static struct netbuf *nbuf;
1235 static int fd = RPC_ANYFD;
1237 if (fd == RPC_ANYFD) {
1238 if ((fd = __rpc_nconf2fd(nconf)) == -1) {
1239 rpc_createerr.cf_stat = RPC_TLIERROR;
1240 clnt_pcreateerror("rpcinfo");
1243 /* Convert the uaddr to taddr */
1244 nbuf = uaddr2taddr(nconf, address);
1246 errx(1, "no address for client handle");
1248 client = clnt_tli_create(fd, nconf, nbuf, prog, vers, 0, 0);
1249 if (client == (CLIENT *)NULL) {
1250 clnt_pcreateerror("rpcinfo");
1257 * If the version number is given, ping that (prog, vers); else try to find
1258 * the version numbers supported for that prog and ping all the versions.
1259 * Remote rpcbind is not contacted for this service. The requests are
1260 * sent directly to the services themselves.
1263 addrping(char *address, char *netid, int argc, char **argv)
1267 enum clnt_stat rpc_stat;
1268 u_long prognum, versnum, minvers, maxvers;
1269 struct rpc_err rpcerr;
1271 struct netconfig *nconf;
1274 if (argc < 1 || argc > 2 || (netid == NULL))
1276 nconf = getnetconfigent(netid);
1277 if (nconf == (struct netconfig *)NULL)
1278 errx(1, "could not find %s", netid);
1281 prognum = getprognum(argv[0]);
1282 if (argc == 1) { /* Version number not known */
1284 * A call to version 0 should fail with a program/version
1285 * mismatch, and give us the range of versions supported.
1289 versnum = getvers(argv[1]);
1291 client = clnt_addr_create(address, nconf, prognum, versnum);
1292 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1293 (char *)NULL, (xdrproc_t) xdr_void,
1296 /* Version number was known */
1297 if (pstatus(client, prognum, versnum) < 0)
1299 (void) CLNT_DESTROY(client);
1304 /* Version number not known */
1305 (void) CLNT_CONTROL(client, CLSET_FD_NCLOSE, (char *)NULL);
1306 (void) CLNT_CONTROL(client, CLGET_FD, (char *)&fd);
1307 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1308 clnt_geterr(client, &rpcerr);
1309 minvers = rpcerr.re_vers.low;
1310 maxvers = rpcerr.re_vers.high;
1311 } else if (rpc_stat == RPC_SUCCESS) {
1313 * Oh dear, it DOES support version 0.
1314 * Let's try version MAX_VERS.
1316 (void) CLNT_DESTROY(client);
1317 client = clnt_addr_create(address, nconf, prognum, MAX_VERS);
1318 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1319 (char *)NULL, (xdrproc_t) xdr_void,
1321 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1322 clnt_geterr(client, &rpcerr);
1323 minvers = rpcerr.re_vers.low;
1324 maxvers = rpcerr.re_vers.high;
1325 } else if (rpc_stat == RPC_SUCCESS) {
1327 * It also supports version MAX_VERS.
1328 * Looks like we have a wise guy.
1329 * OK, we give them information on all
1330 * 4 billion versions they support...
1335 (void) pstatus(client, prognum, MAX_VERS);
1339 (void) pstatus(client, prognum, (u_long)0);
1342 (void) CLNT_DESTROY(client);
1343 for (versnum = minvers; versnum <= maxvers; versnum++) {
1344 client = clnt_addr_create(address, nconf, prognum, versnum);
1345 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1346 (char *)NULL, (xdrproc_t) xdr_void,
1348 if (pstatus(client, prognum, versnum) < 0)
1350 (void) CLNT_DESTROY(client);
1359 * If the version number is given, ping that (prog, vers); else try to find
1360 * the version numbers supported for that prog and ping all the versions.
1361 * Remote rpcbind is *contacted* for this service. The requests are
1362 * then sent directly to the services themselves.
1365 progping(char *netid, int argc, char **argv)
1369 enum clnt_stat rpc_stat;
1370 u_long prognum, versnum, minvers, maxvers;
1371 struct rpc_err rpcerr;
1373 struct netconfig *nconf;
1375 if (argc < 2 || argc > 3 || (netid == NULL))
1377 prognum = getprognum(argv[1]);
1378 if (argc == 2) { /* Version number not known */
1380 * A call to version 0 should fail with a program/version
1381 * mismatch, and give us the range of versions supported.
1385 versnum = getvers(argv[2]);
1388 nconf = getnetconfigent(netid);
1389 if (nconf == (struct netconfig *)NULL)
1390 errx(1, "could not find %s", netid);
1391 client = clnt_tp_create(argv[0], prognum, versnum, nconf);
1393 client = clnt_create(argv[0], prognum, versnum, "NETPATH");
1395 if (client == (CLIENT *)NULL) {
1396 clnt_pcreateerror("rpcinfo");
1401 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1402 (char *)NULL, (xdrproc_t) xdr_void,
1405 /* Version number was known */
1406 if (pstatus(client, prognum, versnum) < 0)
1408 (void) CLNT_DESTROY(client);
1413 /* Version number not known */
1414 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1415 clnt_geterr(client, &rpcerr);
1416 minvers = rpcerr.re_vers.low;
1417 maxvers = rpcerr.re_vers.high;
1418 } else if (rpc_stat == RPC_SUCCESS) {
1420 * Oh dear, it DOES support version 0.
1421 * Let's try version MAX_VERS.
1424 (void) CLNT_CONTROL(client, CLSET_VERS, (char *)&versnum);
1425 rpc_stat = CLNT_CALL(client, NULLPROC,
1426 (xdrproc_t) xdr_void, (char *)NULL,
1427 (xdrproc_t) xdr_void, (char *)NULL, to);
1428 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1429 clnt_geterr(client, &rpcerr);
1430 minvers = rpcerr.re_vers.low;
1431 maxvers = rpcerr.re_vers.high;
1432 } else if (rpc_stat == RPC_SUCCESS) {
1434 * It also supports version MAX_VERS.
1435 * Looks like we have a wise guy.
1436 * OK, we give them information on all
1437 * 4 billion versions they support...
1442 (void) pstatus(client, prognum, MAX_VERS);
1446 (void) pstatus(client, prognum, (u_long)0);
1449 for (versnum = minvers; versnum <= maxvers; versnum++) {
1450 (void) CLNT_CONTROL(client, CLSET_VERS, (char *)&versnum);
1451 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1452 (char *)NULL, (xdrproc_t) xdr_void,
1454 if (pstatus(client, prognum, versnum) < 0)
1457 (void) CLNT_DESTROY(client);
1466 fprintf(stderr, "usage: rpcinfo [-m | -s] [host]\n");
1468 fprintf(stderr, " rpcinfo -p [host]\n");
1470 fprintf(stderr, " rpcinfo -T netid host prognum [versnum]\n");
1471 fprintf(stderr, " rpcinfo -l host prognum versnum\n");
1474 " rpcinfo [-n portnum] -u | -t host prognum [versnum]\n");
1477 " rpcinfo -a serv_address -T netid prognum [version]\n");
1478 fprintf(stderr, " rpcinfo -b prognum versnum\n");
1479 fprintf(stderr, " rpcinfo -d [-T netid] prognum versnum\n");
1484 getprognum (char *arg)
1487 register struct rpcent *rpc;
1488 register u_long prognum;
1491 while (*tptr && isdigit(*tptr++));
1492 if (*tptr || isalpha(*(tptr - 1))) {
1493 rpc = getrpcbyname(arg);
1495 errx(1, "%s is unknown service", arg);
1496 prognum = rpc->r_number;
1498 prognum = strtol(arg, &strptr, 10);
1499 if (strptr == arg || *strptr != '\0')
1500 errx(1, "%s is illegal program number", arg);
1509 register u_long vers;
1511 vers = (int) strtol(arg, &strptr, 10);
1512 if (strptr == arg || *strptr != '\0')
1513 errx(1, "%s is illegal version number", arg);
1518 * This routine should take a pointer to an "rpc_err" structure, rather than
1519 * a pointer to a CLIENT structure, but "clnt_perror" takes a pointer to
1520 * a CLIENT structure rather than a pointer to an "rpc_err" structure.
1521 * As such, we have to keep the CLIENT structure around in order to print
1522 * a good error message.
1525 pstatus(register CLIENT *client, u_long prog, u_long vers)
1527 struct rpc_err rpcerr;
1529 clnt_geterr(client, &rpcerr);
1530 if (rpcerr.re_status != RPC_SUCCESS) {
1531 clnt_perror(client, "rpcinfo");
1532 printf("program %lu version %lu is not available\n",
1536 printf("program %lu version %lu ready and waiting\n",
1543 clnt_rpcbind_create(char *host, int rpcbversnum, struct netbuf **targaddr)
1545 static const char *tlist[3] = {
1546 "circuit_n", "circuit_v", "datagram_v"
1549 struct netconfig *nconf;
1550 CLIENT *clnt = NULL;
1553 rpc_createerr.cf_stat = RPC_SUCCESS;
1554 for (i = 0; i < 3; i++) {
1555 if ((handle = __rpc_setconf(tlist[i])) == NULL)
1557 while (clnt == (CLIENT *)NULL) {
1558 if ((nconf = __rpc_getconf(handle)) == NULL) {
1559 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1560 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1563 clnt = getclnthandle(host, nconf, rpcbversnum,
1568 __rpc_endconf(handle);
1574 getclnthandle(char *host, struct netconfig *nconf,
1575 u_long rpcbversnum, struct netbuf **targaddr)
1578 struct addrinfo hints, *res;
1579 CLIENT *client = NULL;
1581 /* Get the address of the rpcbind */
1582 memset(&hints, 0, sizeof hints);
1583 if (getaddrinfo(host, "rpcbind", &hints, &res) != 0) {
1584 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1587 addr.len = addr.maxlen = res->ai_addrlen;
1588 addr.buf = res->ai_addr;
1589 client = clnt_tli_create(RPC_ANYFD, nconf, &addr, RPCBPROG,
1592 if (targaddr != NULL) {
1594 (struct netbuf *)malloc(sizeof (struct netbuf));
1595 if (*targaddr != NULL) {
1596 (*targaddr)->maxlen = addr.maxlen;
1597 (*targaddr)->len = addr.len;
1598 (*targaddr)->buf = (char *)malloc(addr.len);
1599 if ((*targaddr)->buf != NULL) {
1600 memcpy((*targaddr)->buf, addr.buf,
1606 if (rpc_createerr.cf_stat == RPC_TLIERROR) {
1608 * Assume that the other system is dead; this is a
1609 * better error to display to the user.
1611 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1612 rpc_createerr.cf_error.re_status = RPC_FAILED;
1620 print_rmtcallstat(int rtype, rpcb_stat *infp)
1622 register rpcbs_rmtcalllist_ptr pr;
1625 if (rtype == RPCBVERS_4_STAT)
1627 "prog\t\tvers\tproc\tnetid\tindirect success failure\n");
1629 printf("prog\t\tvers\tproc\tnetid\tsuccess\tfailure\n");
1630 for (pr = infp->rmtinfo; pr; pr = pr->next) {
1631 rpc = getrpcbynumber(pr->prog);
1633 printf("%-16s", rpc->r_name);
1635 printf("%-16d", pr->prog);
1636 printf("%d\t%d\t%s\t",
1637 pr->vers, pr->proc, pr->netid);
1638 if (rtype == RPCBVERS_4_STAT)
1639 printf("%d\t ", pr->indirect);
1640 printf("%d\t%d\n", pr->success, pr->failure);
1645 print_getaddrstat(int rtype, rpcb_stat *infp)
1647 rpcbs_addrlist_ptr al;
1648 register struct rpcent *rpc;
1650 printf("prog\t\tvers\tnetid\t success\tfailure\n");
1651 for (al = infp->addrinfo; al; al = al->next) {
1652 rpc = getrpcbynumber(al->prog);
1654 printf("%-16s", rpc->r_name);
1656 printf("%-16d", al->prog);
1657 printf("%d\t%s\t %-12d\t%d\n",
1658 al->vers, al->netid,
1659 al->success, al->failure);
1666 static char space_array[] = /* 64 spaces */
1669 if (howmany <= 0 || howmany > sizeof (space_array)) {
1672 return (&space_array[sizeof (space_array) - howmany - 1]);