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