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