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