1 /* $NetBSD: rpcinfo.c,v 1.15 2000/10/04 20:09:05 mjl Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 /*
33 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
34 */
35
36 #include <sys/cdefs.h>
37 /*
38 * rpcinfo: ping a particular rpc program
39 * or dump the registered programs on the remote machine.
40 */
41
42 /*
43 * We are for now defining PORTMAP here. It doesn't even compile
44 * unless it is defined.
45 */
46 #ifndef PORTMAP
47 #define PORTMAP
48 #endif
49
50 /*
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.
54 */
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <sys/un.h>
59 #include <rpc/rpc.h>
60 #include <stdio.h>
61 #include <rpc/rpcb_prot.h>
62 #include <rpc/rpcent.h>
63 #include <rpc/nettype.h>
64 #include <rpc/rpc_com.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <err.h>
69 #include <ctype.h>
70
71 #ifdef PORTMAP /* Support for version 2 portmapper */
72 #include <netinet/in.h>
73 #include <netdb.h>
74 #include <arpa/inet.h>
75 #include <rpc/pmap_prot.h>
76 #include <rpc/pmap_clnt.h>
77 #endif
78
79 #define MAXHOSTLEN 256
80 #define MIN_VERS ((u_long) 0)
81 #define MAX_VERS ((u_long) 4294967295UL)
82 static char unknown[] = "unknown";
83
84 /*
85 * Functions to be performed.
86 */
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 */
99
100 struct netidlist {
101 char *netid;
102 struct netidlist *next;
103 };
104
105 struct verslist {
106 rpcvers_t vers;
107 struct verslist *next;
108 };
109
110 struct rpcbdump_short {
111 u_long prog;
112 struct verslist *vlist;
113 struct netidlist *nlist;
114 struct rpcbdump_short *next;
115 char *owner;
116 };
117
118
119
120 #ifdef PORTMAP
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 *,
123 const char *);
124 static void pmapdump(int, char **);
125 static void get_inet_address(struct sockaddr_in *, char *);
126 #endif
127
128 static bool_t reply_proc(char *, const struct netbuf *,
129 const struct netconfig *);
130 static void brdcst(int, char **);
131 static void addrping(char *, char *, int, char **);
132 static void progping(char *, int, char **);
133 static CLIENT *clnt_addr_create(char *, struct netconfig *, u_long, u_long);
134 static CLIENT *clnt_rpcbind_create(char *, int, struct netbuf **);
135 static CLIENT *getclnthandle(char *, struct netconfig *, u_long,
136 struct netbuf **);
137 static CLIENT *local_rpcb(u_long, u_long);
138 static int pstatus(CLIENT *, u_long, u_long);
139 static void rpcbdump(int, char *, int, char **);
140 static void rpcbgetstat(int, char **);
141 static void rpcbaddrlist(char *, int, char **);
142 static void deletereg(char *, int, char **);
143 static void print_rmtcallstat(int, rpcb_stat *);
144 static void print_getaddrstat(int, rpcb_stat *);
145 static void usage(void);
146 static u_long getprognum(char *);
147 static u_long getvers(char *);
148 static const char *spaces(size_t);
149 static bool_t add_version(struct rpcbdump_short *, rpcvers_t);
150 static bool_t add_netid(struct rpcbdump_short *, char *);
151
152 int
main(int argc,char ** argv)153 main(int argc, char **argv)
154 {
155 register int c;
156 int errflg;
157 int function;
158 char *netid = NULL;
159 char *address = NULL;
160 #ifdef PORTMAP
161 char *strptr;
162 u_short portnum = 0;
163 #endif
164
165 function = NONE;
166 errflg = 0;
167 #ifdef PORTMAP
168 while ((c = getopt(argc, argv, "a:bdlmn:pstT:u")) != -1) {
169 #else
170 while ((c = getopt(argc, argv, "a:bdlmn:sT:")) != -1) {
171 #endif
172 switch (c) {
173 #ifdef PORTMAP
174 case 'p':
175 if (function != NONE)
176 errflg = 1;
177 else
178 function = PMAPDUMP;
179 break;
180
181 case 't':
182 if (function != NONE)
183 errflg = 1;
184 else
185 function = TCPPING;
186 break;
187
188 case 'u':
189 if (function != NONE)
190 errflg = 1;
191 else
192 function = UDPPING;
193 break;
194
195 case 'n':
196 portnum = (u_short) strtol(optarg, &strptr, 10);
197 if (strptr == optarg || *strptr != '\0')
198 errx(1, "%s is illegal port number", optarg);
199 break;
200 #endif
201 case 'a':
202 address = optarg;
203 if (function != NONE)
204 errflg = 1;
205 else
206 function = ADDRPING;
207 break;
208 case 'b':
209 if (function != NONE)
210 errflg = 1;
211 else
212 function = BROADCAST;
213 break;
214
215 case 'd':
216 if (function != NONE)
217 errflg = 1;
218 else
219 function = DELETES;
220 break;
221
222 case 'l':
223 if (function != NONE)
224 errflg = 1;
225 else
226 function = RPCBADDRLIST;
227 break;
228
229 case 'm':
230 if (function != NONE)
231 errflg = 1;
232 else
233 function = RPCBGETSTAT;
234 break;
235
236 case 's':
237 if (function != NONE)
238 errflg = 1;
239 else
240 function = RPCBDUMP_SHORT;
241 break;
242
243 case 'T':
244 netid = optarg;
245 break;
246 case '?':
247 errflg = 1;
248 break;
249 }
250 }
251
252 if (errflg || ((function == ADDRPING) && !netid))
253 usage();
254
255 if (function == NONE) {
256 if (argc - optind > 1)
257 function = PROGPING;
258 else
259 function = RPCBDUMP;
260 }
261
262 switch (function) {
263 #ifdef PORTMAP
264 case PMAPDUMP:
265 if (portnum != 0)
266 usage();
267 pmapdump(argc - optind, argv + optind);
268 break;
269
270 case UDPPING:
271 ip_ping(portnum, "udp", argc - optind, argv + optind);
272 break;
273
274 case TCPPING:
275 ip_ping(portnum, "tcp", argc - optind, argv + optind);
276 break;
277 #endif
278 case BROADCAST:
279 brdcst(argc - optind, argv + optind);
280 break;
281 case DELETES:
282 deletereg(netid, argc - optind, argv + optind);
283 break;
284 case ADDRPING:
285 addrping(address, netid, argc - optind, argv + optind);
286 break;
287 case PROGPING:
288 progping(netid, argc - optind, argv + optind);
289 break;
290 case RPCBDUMP:
291 case RPCBDUMP_SHORT:
292 rpcbdump(function, netid, argc - optind, argv + optind);
293 break;
294 case RPCBGETSTAT:
295 rpcbgetstat(argc - optind, argv + optind);
296 break;
297 case RPCBADDRLIST:
298 rpcbaddrlist(netid, argc - optind, argv + optind);
299 break;
300 }
301 return (0);
302 }
303
304 static CLIENT *
305 local_rpcb(u_long prog, u_long vers)
306 {
307 void *localhandle;
308 struct netconfig *nconf;
309 CLIENT *clnt;
310
311 localhandle = setnetconfig();
312 while ((nconf = getnetconfig(localhandle)) != NULL) {
313 if (nconf->nc_protofmly != NULL &&
314 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
315 break;
316 }
317 if (nconf == NULL) {
318 warnx("getnetconfig: %s", nc_sperror());
319 return (NULL);
320 }
321
322 clnt = clnt_tp_create(NULL, prog, vers, nconf);
323 endnetconfig(localhandle);
324 return clnt;
325 }
326
327 #ifdef PORTMAP
328 static CLIENT *
329 clnt_com_create(struct sockaddr_in *addr, u_long prog, u_long vers,
330 int *fdp, const char *trans)
331 {
332 CLIENT *clnt;
333
334 if (strcmp(trans, "tcp") == 0) {
335 clnt = clnttcp_create(addr, prog, vers, fdp, 0, 0);
336 } else {
337 struct timeval to;
338
339 to.tv_sec = 5;
340 to.tv_usec = 0;
341 clnt = clntudp_create(addr, prog, vers, to, fdp);
342 }
343 if (clnt == (CLIENT *)NULL) {
344 clnt_pcreateerror("rpcinfo");
345 if (vers == MIN_VERS)
346 printf("program %lu is not available\n", prog);
347 else
348 printf("program %lu version %lu is not available\n",
349 prog, vers);
350 exit(1);
351 }
352 return (clnt);
353 }
354
355 /*
356 * If portnum is 0, then go and get the address from portmapper, which happens
357 * transparently through clnt*_create(); If version number is not given, it
358 * tries to find out the version number by making a call to version 0 and if
359 * that fails, it obtains the high order and the low order version number. If
360 * version 0 calls succeeds, it tries for MAXVERS call and repeats the same.
361 */
362 static void
363 ip_ping(u_short portnum, const char *trans, int argc, char **argv)
364 {
365 CLIENT *client;
366 int fd = RPC_ANYFD;
367 struct timeval to;
368 struct sockaddr_in addr;
369 enum clnt_stat rpc_stat;
370 u_long prognum, vers, minvers, maxvers;
371 struct rpc_err rpcerr;
372 int failure = 0;
373
374 if (argc < 2 || argc > 3)
375 usage();
376 to.tv_sec = 10;
377 to.tv_usec = 0;
378 prognum = getprognum(argv[1]);
379 get_inet_address(&addr, argv[0]);
380 if (argc == 2) { /* Version number not known */
381 /*
382 * A call to version 0 should fail with a program/version
383 * mismatch, and give us the range of versions supported.
384 */
385 vers = MIN_VERS;
386 } else {
387 vers = getvers(argv[2]);
388 }
389 addr.sin_port = htons(portnum);
390 client = clnt_com_create(&addr, prognum, vers, &fd, trans);
391 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
392 (char *)NULL, (xdrproc_t) xdr_void, (char *)NULL,
393 to);
394 if (argc != 2) {
395 /* Version number was known */
396 if (pstatus(client, prognum, vers) < 0)
397 exit(1);
398 (void) CLNT_DESTROY(client);
399 return;
400 }
401 /* Version number not known */
402 (void) CLNT_CONTROL(client, CLSET_FD_NCLOSE, (char *)NULL);
403 if (rpc_stat == RPC_PROGVERSMISMATCH) {
404 clnt_geterr(client, &rpcerr);
405 minvers = rpcerr.re_vers.low;
406 maxvers = rpcerr.re_vers.high;
407 } else if (rpc_stat == RPC_SUCCESS) {
408 /*
409 * Oh dear, it DOES support version 0.
410 * Let's try version MAX_VERS.
411 */
412 (void) CLNT_DESTROY(client);
413 addr.sin_port = htons(portnum);
414 client = clnt_com_create(&addr, prognum, MAX_VERS, &fd, trans);
415 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
416 (char *)NULL, (xdrproc_t) xdr_void,
417 (char *)NULL, to);
418 if (rpc_stat == RPC_PROGVERSMISMATCH) {
419 clnt_geterr(client, &rpcerr);
420 minvers = rpcerr.re_vers.low;
421 maxvers = rpcerr.re_vers.high;
422 } else if (rpc_stat == RPC_SUCCESS) {
423 /*
424 * It also supports version MAX_VERS.
425 * Looks like we have a wise guy.
426 * OK, we give them information on all
427 * 4 billion versions they support...
428 */
429 minvers = 0;
430 maxvers = MAX_VERS;
431 } else {
432 (void) pstatus(client, prognum, MAX_VERS);
433 exit(1);
434 }
435 } else {
436 (void) pstatus(client, prognum, (u_long)0);
437 exit(1);
438 }
439 (void) CLNT_DESTROY(client);
440 for (vers = minvers; vers <= maxvers; vers++) {
441 addr.sin_port = htons(portnum);
442 client = clnt_com_create(&addr, prognum, vers, &fd, trans);
443 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
444 (char *)NULL, (xdrproc_t) xdr_void,
445 (char *)NULL, to);
446 if (pstatus(client, prognum, vers) < 0)
447 failure = 1;
448 (void) CLNT_DESTROY(client);
449 }
450 if (failure)
451 exit(1);
452 (void) close(fd);
453 return;
454 }
455
456 /*
457 * Dump all the portmapper registerations
458 */
459 static void
460 pmapdump(int argc, char **argv)
461 {
462 struct sockaddr_in server_addr;
463 struct pmaplist *head = NULL;
464 int socket = RPC_ANYSOCK;
465 struct timeval minutetimeout;
466 register CLIENT *client;
467 struct rpcent *rpc;
468 enum clnt_stat clnt_st;
469 struct rpc_err err;
470 char *host = NULL;
471
472 if (argc > 1)
473 usage();
474 if (argc == 1) {
475 host = argv[0];
476 get_inet_address(&server_addr, host);
477 server_addr.sin_port = htons(PMAPPORT);
478 client = clnttcp_create(&server_addr, PMAPPROG, PMAPVERS,
479 &socket, 50, 500);
480 } else
481 client = local_rpcb(PMAPPROG, PMAPVERS);
482
483 if (client == NULL) {
484 if (rpc_createerr.cf_stat == RPC_TLIERROR) {
485 /*
486 * "Misc. TLI error" is not too helpful. Most likely
487 * the connection to the remote server timed out, so
488 * this error is at least less perplexing.
489 */
490 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
491 rpc_createerr.cf_error.re_status = RPC_FAILED;
492 }
493 clnt_pcreateerror("rpcinfo: can't contact portmapper");
494 exit(1);
495 }
496
497 minutetimeout.tv_sec = 60;
498 minutetimeout.tv_usec = 0;
499
500 clnt_st = CLNT_CALL(client, PMAPPROC_DUMP, (xdrproc_t) xdr_void,
501 NULL, (xdrproc_t) xdr_pmaplist_ptr, (char *)&head,
502 minutetimeout);
503 if (clnt_st != RPC_SUCCESS) {
504 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
505 (clnt_st == RPC_PROGUNAVAIL)) {
506 CLNT_GETERR(client, &err);
507 if (err.re_vers.low > PMAPVERS) {
508 if (host)
509 warnx("%s does not support portmapper."
510 "Try rpcinfo %s instead", host,
511 host);
512 else
513 warnx("local host does not support "
514 "portmapper. Try 'rpcinfo' "
515 "instead");
516 }
517 exit(1);
518 }
519 clnt_perror(client, "rpcinfo: can't contact portmapper");
520 exit(1);
521 }
522 if (head == NULL) {
523 printf("No remote programs registered.\n");
524 } else {
525 printf(" program vers proto port service\n");
526 for (; head != NULL; head = head->pml_next) {
527 printf("%10ld%5ld",
528 head->pml_map.pm_prog,
529 head->pml_map.pm_vers);
530 if (head->pml_map.pm_prot == IPPROTO_UDP)
531 printf("%6s", "udp");
532 else if (head->pml_map.pm_prot == IPPROTO_TCP)
533 printf("%6s", "tcp");
534 else if (head->pml_map.pm_prot == IPPROTO_ST)
535 printf("%6s", "local");
536 else
537 printf("%6ld", head->pml_map.pm_prot);
538 printf("%7ld", head->pml_map.pm_port);
539 rpc = getrpcbynumber(head->pml_map.pm_prog);
540 if (rpc)
541 printf(" %s\n", rpc->r_name);
542 else
543 printf("\n");
544 }
545 }
546 }
547
548 static void
549 get_inet_address(struct sockaddr_in *addr, char *host)
550 {
551 struct netconfig *nconf;
552 struct addrinfo hints, *res;
553 int error;
554
555 (void) memset((char *)addr, 0, sizeof (*addr));
556 addr->sin_addr.s_addr = inet_addr(host);
557 if (addr->sin_addr.s_addr == INADDR_NONE ||
558 addr->sin_addr.s_addr == INADDR_ANY) {
559 if ((nconf = __rpc_getconfip("udp")) == NULL &&
560 (nconf = __rpc_getconfip("tcp")) == NULL)
561 errx(1, "couldn't find a suitable transport");
562 else {
563 memset(&hints, 0, sizeof hints);
564 hints.ai_family = AF_INET;
565 if ((error = getaddrinfo(host, "rpcbind", &hints, &res))
566 != 0)
567 errx(1, "%s: %s", host, gai_strerror(error));
568 else {
569 memcpy(addr, res->ai_addr, res->ai_addrlen);
570 freeaddrinfo(res);
571 }
572 (void) freenetconfigent(nconf);
573 }
574 } else {
575 addr->sin_family = AF_INET;
576 }
577 }
578 #endif /* PORTMAP */
579
580 /*
581 * reply_proc collects replies from the broadcast.
582 * to get a unique list of responses the output of rpcinfo should
583 * be piped through sort(1) and then uniq(1).
584 */
585
586 /*ARGSUSED*/
587 static bool_t
588 reply_proc(char *res __unused, const struct netbuf *who,
589 const struct netconfig *nconf)
590 /* void *res; Nothing comes back */
591 /* struct netbuf *who; Who sent us the reply */
592 /* struct netconfig *nconf; On which transport the reply came */
593 {
594 char *uaddr;
595 char hostbuf[NI_MAXHOST];
596 const char *hostname;
597 struct sockaddr *sa = (struct sockaddr *)who->buf;
598
599 if (getnameinfo(sa, sa->sa_len, hostbuf, NI_MAXHOST, NULL, 0, 0)) {
600 hostname = unknown;
601 } else {
602 hostname = hostbuf;
603 }
604 uaddr = taddr2uaddr(nconf, who);
605 if (uaddr == NULL) {
606 printf("%s\t%s\n", unknown, hostname);
607 } else {
608 printf("%s\t%s\n", uaddr, hostname);
609 free((char *)uaddr);
610 }
611 return (FALSE);
612 }
613
614 static void
615 brdcst(int argc, char **argv)
616 {
617 enum clnt_stat rpc_stat;
618 u_long prognum, vers;
619
620 if (argc != 2)
621 usage();
622 prognum = getprognum(argv[0]);
623 vers = getvers(argv[1]);
624 rpc_stat = rpc_broadcast(prognum, vers, NULLPROC,
625 (xdrproc_t) xdr_void, (char *)NULL, (xdrproc_t) xdr_void,
626 (char *)NULL, reply_proc, NULL);
627 if ((rpc_stat != RPC_SUCCESS) && (rpc_stat != RPC_TIMEDOUT))
628 errx(1, "broadcast failed: %s", clnt_sperrno(rpc_stat));
629 exit(0);
630 }
631
632 static bool_t
633 add_version(struct rpcbdump_short *rs, rpcvers_t vers)
634 {
635 struct verslist *vl;
636
637 for (vl = rs->vlist; vl; vl = vl->next)
638 if (vl->vers == vers)
639 break;
640 if (vl)
641 return (TRUE);
642 vl = (struct verslist *)malloc(sizeof (struct verslist));
643 if (vl == NULL)
644 return (FALSE);
645 vl->vers = vers;
646 vl->next = rs->vlist;
647 rs->vlist = vl;
648 return (TRUE);
649 }
650
651 static bool_t
652 add_netid(struct rpcbdump_short *rs, char *netid)
653 {
654 struct netidlist *nl;
655
656 for (nl = rs->nlist; nl; nl = nl->next)
657 if (strcmp(nl->netid, netid) == 0)
658 break;
659 if (nl)
660 return (TRUE);
661 nl = (struct netidlist *)malloc(sizeof (struct netidlist));
662 if (nl == NULL)
663 return (FALSE);
664 nl->netid = netid;
665 nl->next = rs->nlist;
666 rs->nlist = nl;
667 return (TRUE);
668 }
669
670 static void
671 rpcbdump(int dumptype, char *netid, int argc, char **argv)
672 {
673 rpcblist_ptr head = NULL;
674 struct timeval minutetimeout;
675 register CLIENT *client;
676 struct rpcent *rpc;
677 char *host;
678 struct netidlist *nl;
679 struct verslist *vl;
680 struct rpcbdump_short *rs, *rs_tail;
681 char buf[256];
682 enum clnt_stat clnt_st;
683 struct rpc_err err;
684 struct rpcbdump_short *rs_head = NULL;
685
686 if (argc > 1)
687 usage();
688 if (argc == 1) {
689 host = argv[0];
690 if (netid == NULL) {
691 client = clnt_rpcbind_create(host, RPCBVERS, NULL);
692 } else {
693 struct netconfig *nconf;
694
695 nconf = getnetconfigent(netid);
696 if (nconf == NULL) {
697 nc_perror("rpcinfo: invalid transport");
698 exit(1);
699 }
700 client = getclnthandle(host, nconf, RPCBVERS, NULL);
701 if (nconf)
702 (void) freenetconfigent(nconf);
703 }
704 } else
705 client = local_rpcb(PMAPPROG, RPCBVERS);
706
707 if (client == (CLIENT *)NULL) {
708 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
709 exit(1);
710 }
711
712 minutetimeout.tv_sec = 60;
713 minutetimeout.tv_usec = 0;
714 clnt_st = CLNT_CALL(client, RPCBPROC_DUMP, (xdrproc_t) xdr_void,
715 NULL, (xdrproc_t) xdr_rpcblist_ptr, (char *) &head,
716 minutetimeout);
717 if (clnt_st != RPC_SUCCESS) {
718 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
719 (clnt_st == RPC_PROGUNAVAIL)) {
720 int vers;
721
722 CLNT_GETERR(client, &err);
723 if (err.re_vers.low == RPCBVERS4) {
724 vers = RPCBVERS4;
725 clnt_control(client, CLSET_VERS, (char *)&vers);
726 clnt_st = CLNT_CALL(client, RPCBPROC_DUMP,
727 (xdrproc_t) xdr_void, NULL,
728 (xdrproc_t) xdr_rpcblist_ptr, (char *) &head,
729 minutetimeout);
730 if (clnt_st != RPC_SUCCESS)
731 goto failed;
732 } else {
733 if (err.re_vers.high == PMAPVERS) {
734 int high, low;
735 struct pmaplist *pmaphead = NULL;
736 rpcblist_ptr list, prev;
737
738 vers = PMAPVERS;
739 clnt_control(client, CLSET_VERS, (char *)&vers);
740 clnt_st = CLNT_CALL(client, PMAPPROC_DUMP,
741 (xdrproc_t) xdr_void, NULL,
742 (xdrproc_t) xdr_pmaplist_ptr,
743 (char *)&pmaphead, minutetimeout);
744 if (clnt_st != RPC_SUCCESS)
745 goto failed;
746 /*
747 * convert to rpcblist_ptr format
748 */
749 for (head = NULL; pmaphead != NULL;
750 pmaphead = pmaphead->pml_next) {
751 list = (rpcblist *)malloc(sizeof (rpcblist));
752 if (list == NULL)
753 goto error;
754 if (head == NULL)
755 head = list;
756 else
757 prev->rpcb_next = (rpcblist_ptr) list;
758
759 list->rpcb_next = NULL;
760 list->rpcb_map.r_prog = pmaphead->pml_map.pm_prog;
761 list->rpcb_map.r_vers = pmaphead->pml_map.pm_vers;
762 if (pmaphead->pml_map.pm_prot == IPPROTO_UDP)
763 list->rpcb_map.r_netid = strdup("udp");
764 else if (pmaphead->pml_map.pm_prot == IPPROTO_TCP)
765 list->rpcb_map.r_netid = strdup("tcp");
766 else {
767 (void)asprintf(&list->rpcb_map.r_netid, "%6ld",
768 pmaphead->pml_map.pm_prot);
769 }
770 if (list->rpcb_map.r_netid == NULL)
771 goto error;
772 list->rpcb_map.r_owner = unknown;
773 low = pmaphead->pml_map.pm_port & 0xff;
774 high = (pmaphead->pml_map.pm_port >> 8) & 0xff;
775 (void)asprintf(&list->rpcb_map.r_addr,
776 "0.0.0.0.%d.%d", high, low);
777 if (list->rpcb_map.r_addr == NULL)
778 goto error;
779 prev = list;
780 }
781 }
782 }
783 } else { /* any other error */
784 failed:
785 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
786 exit(1);
787 }
788 }
789 if (head == NULL) {
790 printf("No remote programs registered.\n");
791 } else if (dumptype == RPCBDUMP) {
792 printf(
793 " program version netid address service owner\n");
794 for (; head != NULL; head = head->rpcb_next) {
795 printf("%10u%5u ",
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);
800 if (rpc)
801 printf(" %-10s", rpc->r_name);
802 else
803 printf(" %-10s", "-");
804 printf(" %s\n", head->rpcb_map.r_owner);
805 }
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)
810 break;
811 if (rs == NULL) {
812 rs = (struct rpcbdump_short *)
813 malloc(sizeof (struct rpcbdump_short));
814 if (rs == NULL)
815 goto error;
816 rs->next = NULL;
817 if (rs_head == NULL) {
818 rs_head = rs;
819 rs_tail = rs;
820 } else {
821 rs_tail->next = rs;
822 rs_tail = rs;
823 }
824 rs->prog = head->rpcb_map.r_prog;
825 rs->owner = head->rpcb_map.r_owner;
826 rs->nlist = NULL;
827 rs->vlist = NULL;
828 }
829 if (add_version(rs, head->rpcb_map.r_vers) == FALSE)
830 goto error;
831 if (add_netid(rs, head->rpcb_map.r_netid) == FALSE)
832 goto error;
833 }
834 printf(
835 " program version(s) netid(s) service owner\n");
836 for (rs = rs_head; rs; rs = rs->next) {
837 char *p = buf;
838
839 printf("%10ld ", rs->prog);
840 for (vl = rs->vlist; vl; vl = vl->next) {
841 if ((size_t)(p - buf) >= sizeof(buf))
842 break;
843 (void)snprintf(p, sizeof(buf) - (p - buf),
844 "%d%s", vl->vers, vl->next ? "," : "");
845 p = p + strlen(p);
846 }
847 printf("%-10s", buf);
848 buf[0] = '\0';
849 for (nl = rs->nlist; nl; nl = nl->next) {
850 strlcat(buf, nl->netid, sizeof(buf));
851 if (nl->next)
852 strlcat(buf, ",", sizeof(buf));
853 }
854 printf("%-32s", buf);
855 rpc = getrpcbynumber(rs->prog);
856 if (rpc)
857 printf(" %-11s", rpc->r_name);
858 else
859 printf(" %-11s", "-");
860 printf(" %s\n", rs->owner);
861 }
862 }
863 clnt_destroy(client);
864 return;
865 error: warnx("no memory");
866 return;
867 }
868
869 static char nullstring[] = "\000";
870
871 static void
872 rpcbaddrlist(char *netid, int argc, char **argv)
873 {
874 rpcb_entry_list_ptr head = NULL;
875 struct timeval minutetimeout;
876 register CLIENT *client;
877 struct rpcent *rpc;
878 char *host;
879 RPCB parms;
880 struct netbuf *targaddr;
881
882 if (argc != 3)
883 usage();
884 host = argv[0];
885 if (netid == NULL) {
886 client = clnt_rpcbind_create(host, RPCBVERS4, &targaddr);
887 } else {
888 struct netconfig *nconf;
889
890 nconf = getnetconfigent(netid);
891 if (nconf == NULL) {
892 nc_perror("rpcinfo: invalid transport");
893 exit(1);
894 }
895 client = getclnthandle(host, nconf, RPCBVERS4, &targaddr);
896 if (nconf)
897 (void) freenetconfigent(nconf);
898 }
899 if (client == (CLIENT *)NULL) {
900 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
901 exit(1);
902 }
903 minutetimeout.tv_sec = 60;
904 minutetimeout.tv_usec = 0;
905
906 parms.r_prog = getprognum(argv[1]);
907 parms.r_vers = getvers(argv[2]);
908 parms.r_netid = client->cl_netid;
909 if (targaddr == NULL) {
910 parms.r_addr = nullstring; /* for XDRing */
911 } else {
912 /*
913 * We also send the remote system the address we
914 * used to contact it in case it can help it
915 * connect back with us
916 */
917 struct netconfig *nconf;
918
919 nconf = getnetconfigent(client->cl_netid);
920 if (nconf != NULL) {
921 parms.r_addr = taddr2uaddr(nconf, targaddr);
922 if (parms.r_addr == NULL)
923 parms.r_addr = nullstring;
924 freenetconfigent(nconf);
925 } else {
926 parms.r_addr = nullstring; /* for XDRing */
927 }
928 free(targaddr->buf);
929 free(targaddr);
930 }
931 parms.r_owner = nullstring;
932
933 if (CLNT_CALL(client, RPCBPROC_GETADDRLIST, (xdrproc_t) xdr_rpcb,
934 (char *) &parms, (xdrproc_t) xdr_rpcb_entry_list_ptr,
935 (char *) &head, minutetimeout) != RPC_SUCCESS) {
936 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
937 exit(1);
938 }
939 if (head == NULL) {
940 printf("No remote programs registered.\n");
941 } else {
942 printf(
943 " program vers tp_family/name/class address\t\t service\n");
944 for (; head != NULL; head = head->rpcb_entry_next) {
945 rpcb_entry *re;
946 char buf[128];
947
948 re = &head->rpcb_entry_map;
949 printf("%10u%3u ",
950 parms.r_prog, parms.r_vers);
951 (void)snprintf(buf, sizeof(buf), "%s/%s/%s ",
952 re->r_nc_protofmly, re->r_nc_proto,
953 re->r_nc_semantics == NC_TPI_CLTS ? "clts" :
954 re->r_nc_semantics == NC_TPI_COTS ? "cots" :
955 "cots_ord");
956 printf("%-24s", buf);
957 printf("%-24s", re->r_maddr);
958 rpc = getrpcbynumber(parms.r_prog);
959 if (rpc)
960 printf(" %-13s", rpc->r_name);
961 else
962 printf(" %-13s", "-");
963 printf("\n");
964 }
965 }
966 clnt_destroy(client);
967 return;
968 }
969
970 /*
971 * monitor rpcbind
972 */
973 static void
974 rpcbgetstat(int argc, char **argv)
975 {
976 rpcb_stat_byvers inf;
977 struct timeval minutetimeout;
978 register CLIENT *client;
979 char *host;
980 unsigned int i;
981 int j;
982 rpcbs_addrlist *pa;
983 rpcbs_rmtcalllist *pr;
984 int cnt, flen;
985 #define MAXFIELD 64
986 char fieldbuf[MAXFIELD];
987 #define MAXLINE 256
988 char linebuf[MAXLINE];
989 char *cp, *lp;
990 const char *pmaphdr[] = {
991 "NULL", "SET", "UNSET", "GETPORT",
992 "DUMP", "CALLIT"
993 };
994 const char *rpcb3hdr[] = {
995 "NULL", "SET", "UNSET", "GETADDR", "DUMP", "CALLIT", "TIME",
996 "U2T", "T2U"
997 };
998 const char *rpcb4hdr[] = {
999 "NULL", "SET", "UNSET", "GETADDR", "DUMP", "CALLIT", "TIME",
1000 "U2T", "T2U", "VERADDR", "INDRECT", "GETLIST", "GETSTAT"
1001 };
1002
1003 #define TABSTOP 8
1004
1005 if (argc >= 1) {
1006 host = argv[0];
1007 client = clnt_rpcbind_create(host, RPCBVERS4, NULL);
1008 } else
1009 client = local_rpcb(PMAPPROG, RPCBVERS4);
1010 if (client == (CLIENT *)NULL) {
1011 clnt_pcreateerror("rpcinfo: can't contact rpcbind");
1012 exit(1);
1013 }
1014 minutetimeout.tv_sec = 60;
1015 minutetimeout.tv_usec = 0;
1016 memset((char *)&inf, 0, sizeof (rpcb_stat_byvers));
1017 if (CLNT_CALL(client, RPCBPROC_GETSTAT, (xdrproc_t) xdr_void, NULL,
1018 (xdrproc_t) xdr_rpcb_stat_byvers, (char *)&inf, minutetimeout)
1019 != RPC_SUCCESS) {
1020 clnt_perror(client, "rpcinfo: can't contact rpcbind: ");
1021 exit(1);
1022 }
1023 printf("PORTMAP (version 2) statistics\n");
1024 lp = linebuf;
1025 for (i = 0; i <= rpcb_highproc_2; i++) {
1026 fieldbuf[0] = '\0';
1027 switch (i) {
1028 case PMAPPROC_SET:
1029 (void)snprintf(fieldbuf, sizeof(fieldbuf), "%d/",
1030 inf[RPCBVERS_2_STAT].setinfo);
1031 break;
1032 case PMAPPROC_UNSET:
1033 (void)snprintf(fieldbuf, sizeof(fieldbuf), "%d/",
1034 inf[RPCBVERS_2_STAT].unsetinfo);
1035 break;
1036 case PMAPPROC_GETPORT:
1037 cnt = 0;
1038 for (pa = inf[RPCBVERS_2_STAT].addrinfo; pa;
1039 pa = pa->next)
1040 cnt += pa->success;
1041 (void)snprintf(fieldbuf, sizeof(fieldbuf), "%d/", cnt);
1042 break;
1043 case PMAPPROC_CALLIT:
1044 cnt = 0;
1045 for (pr = inf[RPCBVERS_2_STAT].rmtinfo; pr;
1046 pr = pr->next)
1047 cnt += pr->success;
1048 (void)snprintf(fieldbuf, sizeof(fieldbuf), "%d/", cnt);
1049 break;
1050 default: break; /* For the remaining ones */
1051 }
1052 cp = &fieldbuf[0] + strlen(fieldbuf);
1053 (void)snprintf(cp, sizeof(fieldbuf) - (cp - fieldbuf), "%d",
1054 inf[RPCBVERS_2_STAT].info[i]);
1055 flen = strlen(fieldbuf);
1056 printf("%s%s", pmaphdr[i],
1057 spaces((TABSTOP * (1 + flen / TABSTOP))
1058 - strlen(pmaphdr[i])));
1059 if ((size_t)(lp - linebuf) >= sizeof(linebuf))
1060 break;
1061 (void)snprintf(lp, sizeof(linebuf) - (lp - linebuf), "%s%s",
1062 fieldbuf, spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP)) -
1063 flen)));
1064 lp += (flen + cnt);
1065 }
1066 printf("\n%s\n\n", linebuf);
1067
1068 if (inf[RPCBVERS_2_STAT].info[PMAPPROC_CALLIT]) {
1069 printf("PMAP_RMTCALL call statistics\n");
1070 print_rmtcallstat(RPCBVERS_2_STAT, &inf[RPCBVERS_2_STAT]);
1071 printf("\n");
1072 }
1073
1074 if (inf[RPCBVERS_2_STAT].info[PMAPPROC_GETPORT]) {
1075 printf("PMAP_GETPORT call statistics\n");
1076 print_getaddrstat(RPCBVERS_2_STAT, &inf[RPCBVERS_2_STAT]);
1077 printf("\n");
1078 }
1079
1080 printf("RPCBIND (version 3) statistics\n");
1081 lp = linebuf;
1082 for (i = 0; i <= rpcb_highproc_3; i++) {
1083 fieldbuf[0] = '\0';
1084 switch (i) {
1085 case RPCBPROC_SET:
1086 (void)snprintf(fieldbuf, sizeof(fieldbuf), "%d/",
1087 inf[RPCBVERS_3_STAT].setinfo);
1088 break;
1089 case RPCBPROC_UNSET:
1090 (void)snprintf(fieldbuf, sizeof(fieldbuf), "%d/",
1091 inf[RPCBVERS_3_STAT].unsetinfo);
1092 break;
1093 case RPCBPROC_GETADDR:
1094 cnt = 0;
1095 for (pa = inf[RPCBVERS_3_STAT].addrinfo; pa;
1096 pa = pa->next)
1097 cnt += pa->success;
1098 (void)snprintf(fieldbuf, sizeof(fieldbuf), "%d/", cnt);
1099 break;
1100 case RPCBPROC_CALLIT:
1101 cnt = 0;
1102 for (pr = inf[RPCBVERS_3_STAT].rmtinfo; pr;
1103 pr = pr->next)
1104 cnt += pr->success;
1105 (void)snprintf(fieldbuf, sizeof(fieldbuf), "%d/", cnt);
1106 break;
1107 default: break; /* For the remaining ones */
1108 }
1109 cp = &fieldbuf[0] + strlen(fieldbuf);
1110 (void)snprintf(cp, sizeof(fieldbuf) - (cp - fieldbuf),
1111 "%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 if ((size_t)(lp - linebuf) >= sizeof(linebuf))
1117 break;
1118 (void)snprintf(lp, sizeof(linebuf) - (lp - linebuf), "%s%s",
1119 fieldbuf, spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP)) -
1120 flen)));
1121 lp += (flen + cnt);
1122 }
1123 printf("\n%s\n\n", linebuf);
1124
1125 if (inf[RPCBVERS_3_STAT].info[RPCBPROC_CALLIT]) {
1126 printf("RPCB_RMTCALL (version 3) call statistics\n");
1127 print_rmtcallstat(RPCBVERS_3_STAT, &inf[RPCBVERS_3_STAT]);
1128 printf("\n");
1129 }
1130
1131 if (inf[RPCBVERS_3_STAT].info[RPCBPROC_GETADDR]) {
1132 printf("RPCB_GETADDR (version 3) call statistics\n");
1133 print_getaddrstat(RPCBVERS_3_STAT, &inf[RPCBVERS_3_STAT]);
1134 printf("\n");
1135 }
1136
1137 printf("RPCBIND (version 4) statistics\n");
1138
1139 for (j = 0; j <= 9; j += 9) { /* Just two iterations for printing */
1140 lp = linebuf;
1141 for (i = j; i <= MAX(8, rpcb_highproc_4 - 9 + j); i++) {
1142 fieldbuf[0] = '\0';
1143 switch (i) {
1144 case RPCBPROC_SET:
1145 (void)snprintf(fieldbuf, sizeof(fieldbuf),
1146 "%d/", inf[RPCBVERS_4_STAT].setinfo);
1147 break;
1148 case RPCBPROC_UNSET:
1149 (void)snprintf(fieldbuf, sizeof(fieldbuf),
1150 "%d/", inf[RPCBVERS_4_STAT].unsetinfo);
1151 break;
1152 case RPCBPROC_GETADDR:
1153 cnt = 0;
1154 for (pa = inf[RPCBVERS_4_STAT].addrinfo; pa;
1155 pa = pa->next)
1156 cnt += pa->success;
1157 (void)snprintf(fieldbuf, sizeof(fieldbuf),
1158 "%d/", cnt);
1159 break;
1160 case RPCBPROC_CALLIT:
1161 cnt = 0;
1162 for (pr = inf[RPCBVERS_4_STAT].rmtinfo; pr;
1163 pr = pr->next)
1164 cnt += pr->success;
1165 (void)snprintf(fieldbuf, sizeof(fieldbuf),
1166 "%d/", cnt);
1167 break;
1168 default: break; /* For the remaining ones */
1169 }
1170 cp = &fieldbuf[0] + strlen(fieldbuf);
1171 /*
1172 * XXX: We also add RPCBPROC_GETADDRLIST queries to
1173 * RPCB_GETADDR because rpcbind includes the
1174 * RPCB_GETADDRLIST successes in RPCB_GETADDR.
1175 */
1176 if (i != RPCBPROC_GETADDR)
1177 (void)snprintf(cp,
1178 sizeof(fieldbuf) - (cp - fieldbuf),
1179 "%d", inf[RPCBVERS_4_STAT].info[i]);
1180 else
1181 (void)snprintf(cp,
1182 sizeof(fieldbuf) - (cp - fieldbuf),
1183 "%d", inf[RPCBVERS_4_STAT].info[i] +
1184 inf[RPCBVERS_4_STAT].
1185 info[RPCBPROC_GETADDRLIST]);
1186 flen = strlen(fieldbuf);
1187 printf("%s%s", rpcb4hdr[i],
1188 spaces((TABSTOP * (1 + flen / TABSTOP))
1189 - strlen(rpcb4hdr[i])));
1190 if ((size_t)(lp - linebuf) >= sizeof(linebuf))
1191 break;
1192 (void)snprintf(lp, sizeof(linebuf) - (lp - linebuf),
1193 "%s%s", fieldbuf,
1194 spaces(cnt = ((TABSTOP * (1 + flen / TABSTOP)) -
1195 flen)));
1196 lp += (flen + cnt);
1197 }
1198 printf("\n%s\n", linebuf);
1199 }
1200
1201 if (inf[RPCBVERS_4_STAT].info[RPCBPROC_CALLIT] ||
1202 inf[RPCBVERS_4_STAT].info[RPCBPROC_INDIRECT]) {
1203 printf("\n");
1204 printf("RPCB_RMTCALL (version 4) call statistics\n");
1205 print_rmtcallstat(RPCBVERS_4_STAT, &inf[RPCBVERS_4_STAT]);
1206 }
1207
1208 if (inf[RPCBVERS_4_STAT].info[RPCBPROC_GETADDR]) {
1209 printf("\n");
1210 printf("RPCB_GETADDR (version 4) call statistics\n");
1211 print_getaddrstat(RPCBVERS_4_STAT, &inf[RPCBVERS_4_STAT]);
1212 }
1213 clnt_destroy(client);
1214 }
1215
1216 /*
1217 * Delete registeration for this (prog, vers, netid)
1218 */
1219 static void
1220 deletereg(char *netid, int argc, char **argv)
1221 {
1222 struct netconfig *nconf = NULL;
1223
1224 if (argc != 2)
1225 usage();
1226 if (netid) {
1227 nconf = getnetconfigent(netid);
1228 if (nconf == NULL)
1229 errx(1, "netid %s not supported", netid);
1230 }
1231 if ((rpcb_unset(getprognum(argv[0]), getvers(argv[1]), nconf)) == 0)
1232 errx(1,
1233 "could not delete registration for prog %s version %s",
1234 argv[0], argv[1]);
1235 }
1236
1237 /*
1238 * Create and return a handle for the given nconf.
1239 * Exit if cannot create handle.
1240 */
1241 static CLIENT *
1242 clnt_addr_create(char *address, struct netconfig *nconf,
1243 u_long prog, u_long vers)
1244 {
1245 CLIENT *client;
1246 static struct netbuf *nbuf;
1247 static int fd = RPC_ANYFD;
1248
1249 if (fd == RPC_ANYFD) {
1250 if ((fd = __rpc_nconf2fd(nconf)) == -1) {
1251 rpc_createerr.cf_stat = RPC_TLIERROR;
1252 clnt_pcreateerror("rpcinfo");
1253 exit(1);
1254 }
1255 /* Convert the uaddr to taddr */
1256 nbuf = uaddr2taddr(nconf, address);
1257 if (nbuf == NULL)
1258 errx(1, "no address for client handle");
1259 }
1260 client = clnt_tli_create(fd, nconf, nbuf, prog, vers, 0, 0);
1261 if (client == (CLIENT *)NULL) {
1262 clnt_pcreateerror("rpcinfo");
1263 exit(1);
1264 }
1265 return (client);
1266 }
1267
1268 /*
1269 * If the version number is given, ping that (prog, vers); else try to find
1270 * the version numbers supported for that prog and ping all the versions.
1271 * Remote rpcbind is not contacted for this service. The requests are
1272 * sent directly to the services themselves.
1273 */
1274 static void
1275 addrping(char *address, char *netid, int argc, char **argv)
1276 {
1277 CLIENT *client;
1278 struct timeval to;
1279 enum clnt_stat rpc_stat;
1280 u_long prognum, versnum, minvers, maxvers;
1281 struct rpc_err rpcerr;
1282 int failure = 0;
1283 struct netconfig *nconf;
1284 int fd;
1285
1286 if (argc < 1 || argc > 2 || (netid == NULL))
1287 usage();
1288 nconf = getnetconfigent(netid);
1289 if (nconf == (struct netconfig *)NULL)
1290 errx(1, "could not find %s", netid);
1291 to.tv_sec = 10;
1292 to.tv_usec = 0;
1293 prognum = getprognum(argv[0]);
1294 if (argc == 1) { /* Version number not known */
1295 /*
1296 * A call to version 0 should fail with a program/version
1297 * mismatch, and give us the range of versions supported.
1298 */
1299 versnum = MIN_VERS;
1300 } else {
1301 versnum = getvers(argv[1]);
1302 }
1303 client = clnt_addr_create(address, nconf, prognum, versnum);
1304 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1305 (char *)NULL, (xdrproc_t) xdr_void,
1306 (char *)NULL, to);
1307 if (argc == 2) {
1308 /* Version number was known */
1309 if (pstatus(client, prognum, versnum) < 0)
1310 failure = 1;
1311 (void) CLNT_DESTROY(client);
1312 if (failure)
1313 exit(1);
1314 return;
1315 }
1316 /* Version number not known */
1317 (void) CLNT_CONTROL(client, CLSET_FD_NCLOSE, (char *)NULL);
1318 (void) CLNT_CONTROL(client, CLGET_FD, (char *)&fd);
1319 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1320 clnt_geterr(client, &rpcerr);
1321 minvers = rpcerr.re_vers.low;
1322 maxvers = rpcerr.re_vers.high;
1323 } else if (rpc_stat == RPC_SUCCESS) {
1324 /*
1325 * Oh dear, it DOES support version 0.
1326 * Let's try version MAX_VERS.
1327 */
1328 (void) CLNT_DESTROY(client);
1329 client = clnt_addr_create(address, nconf, prognum, MAX_VERS);
1330 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1331 (char *)NULL, (xdrproc_t) xdr_void,
1332 (char *)NULL, to);
1333 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1334 clnt_geterr(client, &rpcerr);
1335 minvers = rpcerr.re_vers.low;
1336 maxvers = rpcerr.re_vers.high;
1337 } else if (rpc_stat == RPC_SUCCESS) {
1338 /*
1339 * It also supports version MAX_VERS.
1340 * Looks like we have a wise guy.
1341 * OK, we give them information on all
1342 * 4 billion versions they support...
1343 */
1344 minvers = 0;
1345 maxvers = MAX_VERS;
1346 } else {
1347 (void) pstatus(client, prognum, MAX_VERS);
1348 exit(1);
1349 }
1350 } else {
1351 (void) pstatus(client, prognum, (u_long)0);
1352 exit(1);
1353 }
1354 (void) CLNT_DESTROY(client);
1355 for (versnum = minvers; versnum <= maxvers; versnum++) {
1356 client = clnt_addr_create(address, nconf, prognum, versnum);
1357 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1358 (char *)NULL, (xdrproc_t) xdr_void,
1359 (char *)NULL, to);
1360 if (pstatus(client, prognum, versnum) < 0)
1361 failure = 1;
1362 (void) CLNT_DESTROY(client);
1363 }
1364 (void) close(fd);
1365 if (failure)
1366 exit(1);
1367 return;
1368 }
1369
1370 /*
1371 * If the version number is given, ping that (prog, vers); else try to find
1372 * the version numbers supported for that prog and ping all the versions.
1373 * Remote rpcbind is *contacted* for this service. The requests are
1374 * then sent directly to the services themselves.
1375 */
1376 static void
1377 progping(char *netid, int argc, char **argv)
1378 {
1379 CLIENT *client;
1380 struct timeval to;
1381 enum clnt_stat rpc_stat;
1382 u_long prognum, versnum, minvers, maxvers;
1383 struct rpc_err rpcerr;
1384 int failure = 0;
1385 struct netconfig *nconf;
1386
1387 if (argc < 2 || argc > 3 || (netid == NULL))
1388 usage();
1389 prognum = getprognum(argv[1]);
1390 if (argc == 2) { /* Version number not known */
1391 /*
1392 * A call to version 0 should fail with a program/version
1393 * mismatch, and give us the range of versions supported.
1394 */
1395 versnum = MIN_VERS;
1396 } else {
1397 versnum = getvers(argv[2]);
1398 }
1399 if (netid) {
1400 nconf = getnetconfigent(netid);
1401 if (nconf == (struct netconfig *)NULL)
1402 errx(1, "could not find %s", netid);
1403 client = clnt_tp_create(argv[0], prognum, versnum, nconf);
1404 } else {
1405 client = clnt_create(argv[0], prognum, versnum, "NETPATH");
1406 }
1407 if (client == (CLIENT *)NULL) {
1408 clnt_pcreateerror("rpcinfo");
1409 exit(1);
1410 }
1411 to.tv_sec = 10;
1412 to.tv_usec = 0;
1413 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1414 (char *)NULL, (xdrproc_t) xdr_void,
1415 (char *)NULL, to);
1416 if (argc == 3) {
1417 /* Version number was known */
1418 if (pstatus(client, prognum, versnum) < 0)
1419 failure = 1;
1420 (void) CLNT_DESTROY(client);
1421 if (failure)
1422 exit(1);
1423 return;
1424 }
1425 /* Version number not known */
1426 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1427 clnt_geterr(client, &rpcerr);
1428 minvers = rpcerr.re_vers.low;
1429 maxvers = rpcerr.re_vers.high;
1430 } else if (rpc_stat == RPC_SUCCESS) {
1431 /*
1432 * Oh dear, it DOES support version 0.
1433 * Let's try version MAX_VERS.
1434 */
1435 versnum = MAX_VERS;
1436 (void) CLNT_CONTROL(client, CLSET_VERS, (char *)&versnum);
1437 rpc_stat = CLNT_CALL(client, NULLPROC,
1438 (xdrproc_t) xdr_void, (char *)NULL,
1439 (xdrproc_t) xdr_void, (char *)NULL, to);
1440 if (rpc_stat == RPC_PROGVERSMISMATCH) {
1441 clnt_geterr(client, &rpcerr);
1442 minvers = rpcerr.re_vers.low;
1443 maxvers = rpcerr.re_vers.high;
1444 } else if (rpc_stat == RPC_SUCCESS) {
1445 /*
1446 * It also supports version MAX_VERS.
1447 * Looks like we have a wise guy.
1448 * OK, we give them information on all
1449 * 4 billion versions they support...
1450 */
1451 minvers = 0;
1452 maxvers = MAX_VERS;
1453 } else {
1454 (void) pstatus(client, prognum, MAX_VERS);
1455 exit(1);
1456 }
1457 } else {
1458 (void) pstatus(client, prognum, (u_long)0);
1459 exit(1);
1460 }
1461 for (versnum = minvers; versnum <= maxvers; versnum++) {
1462 (void) CLNT_CONTROL(client, CLSET_VERS, (char *)&versnum);
1463 rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t) xdr_void,
1464 (char *)NULL, (xdrproc_t) xdr_void,
1465 (char *)NULL, to);
1466 if (pstatus(client, prognum, versnum) < 0)
1467 failure = 1;
1468 }
1469 (void) CLNT_DESTROY(client);
1470 if (failure)
1471 exit(1);
1472 return;
1473 }
1474
1475 static void
1476 usage(void)
1477 {
1478 fprintf(stderr, "usage: rpcinfo [-m | -s] [host]\n");
1479 #ifdef PORTMAP
1480 fprintf(stderr, " rpcinfo -p [host]\n");
1481 #endif
1482 fprintf(stderr, " rpcinfo -T netid host prognum [versnum]\n");
1483 fprintf(stderr, " rpcinfo -l host prognum versnum\n");
1484 #ifdef PORTMAP
1485 fprintf(stderr,
1486 " rpcinfo [-n portnum] -u | -t host prognum [versnum]\n");
1487 #endif
1488 fprintf(stderr,
1489 " rpcinfo -a serv_address -T netid prognum [version]\n");
1490 fprintf(stderr, " rpcinfo -b prognum versnum\n");
1491 fprintf(stderr, " rpcinfo -d [-T netid] prognum versnum\n");
1492 exit(1);
1493 }
1494
1495 static u_long
1496 getprognum (char *arg)
1497 {
1498 char *strptr;
1499 register struct rpcent *rpc;
1500 register u_long prognum;
1501 char *tptr = arg;
1502
1503 while (*tptr && isdigit(*tptr++));
1504 if (*tptr || isalpha(*(tptr - 1))) {
1505 rpc = getrpcbyname(arg);
1506 if (rpc == NULL)
1507 errx(1, "%s is unknown service", arg);
1508 prognum = rpc->r_number;
1509 } else {
1510 prognum = strtol(arg, &strptr, 10);
1511 if (strptr == arg || *strptr != '\0')
1512 errx(1, "%s is illegal program number", arg);
1513 }
1514 return (prognum);
1515 }
1516
1517 static u_long
1518 getvers(char *arg)
1519 {
1520 char *strptr;
1521 register u_long vers;
1522
1523 vers = (int) strtol(arg, &strptr, 10);
1524 if (strptr == arg || *strptr != '\0')
1525 errx(1, "%s is illegal version number", arg);
1526 return (vers);
1527 }
1528
1529 /*
1530 * This routine should take a pointer to an "rpc_err" structure, rather than
1531 * a pointer to a CLIENT structure, but "clnt_perror" takes a pointer to
1532 * a CLIENT structure rather than a pointer to an "rpc_err" structure.
1533 * As such, we have to keep the CLIENT structure around in order to print
1534 * a good error message.
1535 */
1536 static int
1537 pstatus(register CLIENT *client, u_long prog, u_long vers)
1538 {
1539 struct rpc_err rpcerr;
1540
1541 clnt_geterr(client, &rpcerr);
1542 if (rpcerr.re_status != RPC_SUCCESS) {
1543 clnt_perror(client, "rpcinfo");
1544 printf("program %lu version %lu is not available\n",
1545 prog, vers);
1546 return (-1);
1547 } else {
1548 printf("program %lu version %lu ready and waiting\n",
1549 prog, vers);
1550 return (0);
1551 }
1552 }
1553
1554 static CLIENT *
1555 clnt_rpcbind_create(char *host, int rpcbversnum, struct netbuf **targaddr)
1556 {
1557 static const char *tlist[3] = {
1558 "circuit_n", "circuit_v", "datagram_v"
1559 };
1560 int i;
1561 struct netconfig *nconf;
1562 CLIENT *clnt = NULL;
1563 void *handle;
1564
1565 rpc_createerr.cf_stat = RPC_SUCCESS;
1566 for (i = 0; i < 3; i++) {
1567 if ((handle = __rpc_setconf(tlist[i])) == NULL)
1568 continue;
1569 while (clnt == (CLIENT *)NULL) {
1570 if ((nconf = __rpc_getconf(handle)) == NULL) {
1571 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1572 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1573 break;
1574 }
1575 clnt = getclnthandle(host, nconf, rpcbversnum,
1576 targaddr);
1577 }
1578 if (clnt)
1579 break;
1580 __rpc_endconf(handle);
1581 }
1582 return (clnt);
1583 }
1584
1585 static CLIENT*
1586 getclnthandle(char *host, struct netconfig *nconf,
1587 u_long rpcbversnum, struct netbuf **targaddr)
1588 {
1589 struct netbuf addr;
1590 struct addrinfo hints, *res;
1591 CLIENT *client = NULL;
1592
1593 /* Get the address of the rpcbind */
1594 memset(&hints, 0, sizeof hints);
1595 if (getaddrinfo(host, "rpcbind", &hints, &res) != 0) {
1596 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1597 return (NULL);
1598 }
1599 addr.len = addr.maxlen = res->ai_addrlen;
1600 addr.buf = res->ai_addr;
1601 client = clnt_tli_create(RPC_ANYFD, nconf, &addr, RPCBPROG,
1602 rpcbversnum, 0, 0);
1603 if (client) {
1604 if (targaddr != NULL) {
1605 *targaddr =
1606 (struct netbuf *)malloc(sizeof (struct netbuf));
1607 if (*targaddr != NULL) {
1608 (*targaddr)->maxlen = addr.maxlen;
1609 (*targaddr)->len = addr.len;
1610 (*targaddr)->buf = (char *)malloc(addr.len);
1611 if ((*targaddr)->buf != NULL) {
1612 memcpy((*targaddr)->buf, addr.buf,
1613 addr.len);
1614 }
1615 }
1616 }
1617 } else {
1618 if (rpc_createerr.cf_stat == RPC_TLIERROR) {
1619 /*
1620 * Assume that the other system is dead; this is a
1621 * better error to display to the user.
1622 */
1623 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1624 rpc_createerr.cf_error.re_status = RPC_FAILED;
1625 }
1626 }
1627 freeaddrinfo(res);
1628 return (client);
1629 }
1630
1631 static void
1632 print_rmtcallstat(int rtype, rpcb_stat *infp)
1633 {
1634 register rpcbs_rmtcalllist_ptr pr;
1635 struct rpcent *rpc;
1636
1637 if (rtype == RPCBVERS_4_STAT)
1638 printf(
1639 "prog\t\tvers\tproc\tnetid\tindirect success failure\n");
1640 else
1641 printf("prog\t\tvers\tproc\tnetid\tsuccess\tfailure\n");
1642 for (pr = infp->rmtinfo; pr; pr = pr->next) {
1643 rpc = getrpcbynumber(pr->prog);
1644 if (rpc)
1645 printf("%-16s", rpc->r_name);
1646 else
1647 printf("%-16d", pr->prog);
1648 printf("%d\t%d\t%s\t",
1649 pr->vers, pr->proc, pr->netid);
1650 if (rtype == RPCBVERS_4_STAT)
1651 printf("%d\t ", pr->indirect);
1652 printf("%d\t%d\n", pr->success, pr->failure);
1653 }
1654 }
1655
1656 static void
1657 print_getaddrstat(int rtype __unused, rpcb_stat *infp)
1658 {
1659 rpcbs_addrlist_ptr al;
1660 register struct rpcent *rpc;
1661
1662 printf("prog\t\tvers\tnetid\t success\tfailure\n");
1663 for (al = infp->addrinfo; al; al = al->next) {
1664 rpc = getrpcbynumber(al->prog);
1665 if (rpc)
1666 printf("%-16s", rpc->r_name);
1667 else
1668 printf("%-16d", al->prog);
1669 printf("%d\t%s\t %-12d\t%d\n",
1670 al->vers, al->netid,
1671 al->success, al->failure);
1672 }
1673 }
1674
1675 static const char *
1676 spaces(size_t howmany)
1677 {
1678 static char space_array[] = /* 64 spaces */
1679 " ";
1680
1681 if (howmany >= sizeof(space_array)) {
1682 return ("");
1683 }
1684 return (&space_array[sizeof(space_array) - howmany - 1]);
1685 }
1686