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