2 * Copyright (C) 1986, Sun Microsystems, Inc.
4 * @(#)rpcinfo.c 1.22 87/08/12 SMI
5 * @(#)rpcinfo.c 2.2 88/08/11 4.0 RPCSRC
6 * $FreeBSD: src/usr.bin/rpcinfo/rpcinfo.c,v 1.9.2.1 2001/03/04 09:00:23 kris Exp $
7 * $DragonFly: src/usr.bin/rpcinfo/rpcinfo.c,v 1.2 2003/06/17 06:29:31 dillon Exp $
10 * rpcinfo: ping a particular rpc program
11 * or dump the portmapper
15 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
16 * unrestricted use provided that this legend is included on all tape
17 * media and as a part of the software program in whole or part. Users
18 * may copy or modify Sun RPC without charge, but are not authorized
19 * to license or distribute it to anyone else except as part of a product or
20 * program developed by the user.
22 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
23 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
26 * Sun RPC is provided with no support and without any obligation on the
27 * part of Sun Microsystems, Inc. to assist in its use, correction,
28 * modification or enhancement.
30 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
31 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
32 * OR ANY PART THEREOF.
34 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
35 * or profits or other special, indirect and consequential damages, even if
36 * Sun has been advised of the possibility of such damages.
38 * Sun Microsystems, Inc.
40 * Mountain View, California 94043
47 #include <sys/socket.h>
49 #include <rpc/pmap_prot.h>
50 #include <rpc/pmap_clnt.h>
54 #include <sys/param.h>
55 #include <arpa/inet.h>
57 #define MAXHOSTLEN 256
59 #define MIN_VERS ((u_long) 0)
60 #define MAX_VERS ((u_long) 4294967295UL)
62 static void udpping(/*u_short portflag, int argc, char **argv*/);
63 static void tcpping(/*u_short portflag, int argc, char **argv*/);
64 static int pstatus(/*CLIENT *client, u_long prognum, u_long vers*/);
65 static void pmapdump(/*int argc, char **argv*/);
66 static bool_t
reply_proc(/*void *res, struct sockaddr_in *who*/);
67 static void brdcst(/*int argc, char **argv*/);
68 static void deletereg(/* int argc, char **argv */) ;
69 static void usage(/*void*/);
70 static u_long
getprognum(/*char *arg*/);
71 static u_long
getvers(/*char *arg*/);
72 static void get_inet_address(/*struct sockaddr_in *addr, char *host*/);
75 * Functions to be performed.
77 #define NONE 0 /* no function */
78 #define PMAPDUMP 1 /* dump portmapper registrations */
79 #define TCPPING 2 /* ping TCP service */
80 #define UDPPING 3 /* ping UDP service */
81 #define BRDCST 4 /* ping broadcast UDP service */
82 #define DELETES 5 /* delete registration for the service */
97 while ((c
= getopt(argc
, argv
, "ptubdn:")) != -1) {
101 if (function
!= NONE
)
108 if (function
!= NONE
)
115 if (function
!= NONE
)
122 if (function
!= NONE
)
129 portnum
= (u_short
) atoi(optarg
); /* hope we don't get bogus # */
133 if (function
!= NONE
)
144 if (errflg
|| function
== NONE
) {
156 pmapdump(argc
- optind
, argv
+ optind
);
160 udpping(portnum
, argc
- optind
, argv
+ optind
);
164 tcpping(portnum
, argc
- optind
, argv
+ optind
);
172 brdcst(argc
- optind
, argv
+ optind
);
176 deletereg(argc
- optind
, argv
+ optind
);
184 udpping(portnum
, argc
, argv
)
190 struct sockaddr_in addr
;
191 enum clnt_stat rpc_stat
;
193 u_long prognum
, vers
, minvers
, maxvers
;
194 int sock
= RPC_ANYSOCK
;
195 struct rpc_err rpcerr
;
198 if (argc
< 2 || argc
> 3) {
202 prognum
= getprognum(argv
[1]);
203 get_inet_address(&addr
, argv
[0]);
204 /* Open the socket here so it will survive calls to clnt_destroy */
205 sock
= socket( AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
207 perror("rpcinfo: socket");
213 * A call to version 0 should fail with a program/version
214 * mismatch, and give us the range of versions supported.
216 addr
.sin_port
= htons(portnum
);
219 if ((client
= clntudp_create(&addr
, prognum
, (u_long
)0,
220 to
, &sock
)) == NULL
) {
221 clnt_pcreateerror("rpcinfo");
222 printf("program %lu is not available\n",
228 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
, (char *)NULL
,
229 xdr_void
, (char *)NULL
, to
);
230 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
231 clnt_geterr(client
, &rpcerr
);
232 minvers
= rpcerr
.re_vers
.low
;
233 maxvers
= rpcerr
.re_vers
.high
;
234 } else if (rpc_stat
== RPC_SUCCESS
) {
236 * Oh dear, it DOES support version 0.
237 * Let's try version MAX_VERS.
239 addr
.sin_port
= htons(portnum
);
242 if ((client
= clntudp_create(&addr
, prognum
, MAX_VERS
,
243 to
, &sock
)) == NULL
) {
244 clnt_pcreateerror("rpcinfo");
245 printf("program %lu version %lu is not available\n",
251 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
252 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
253 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
254 clnt_geterr(client
, &rpcerr
);
255 minvers
= rpcerr
.re_vers
.low
;
256 maxvers
= rpcerr
.re_vers
.high
;
257 } else if (rpc_stat
== RPC_SUCCESS
) {
259 * It also supports version MAX_VERS.
260 * Looks like we have a wise guy.
261 * OK, we give them information on all
262 * 4 billion versions they support...
267 (void) pstatus(client
, prognum
, MAX_VERS
);
271 (void) pstatus(client
, prognum
, (u_long
)0);
274 clnt_destroy(client
);
275 for (vers
= minvers
; vers
<= maxvers
; vers
++) {
276 addr
.sin_port
= htons(portnum
);
279 if ((client
= clntudp_create(&addr
, prognum
, vers
,
280 to
, &sock
)) == NULL
) {
281 clnt_pcreateerror("rpcinfo");
282 printf("program %lu version %lu is not available\n",
288 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
289 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
290 if (pstatus(client
, prognum
, vers
) < 0)
292 clnt_destroy(client
);
296 vers
= getvers(argv
[2]);
297 addr
.sin_port
= htons(portnum
);
300 if ((client
= clntudp_create(&addr
, prognum
, vers
,
301 to
, &sock
)) == NULL
) {
302 clnt_pcreateerror("rpcinfo");
303 printf("program %lu version %lu is not available\n",
309 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
310 xdr_void
, (char *)NULL
, to
);
311 if (pstatus(client
, prognum
, vers
) < 0)
314 (void) close(sock
); /* Close it up again */
320 tcpping(portnum
, argc
, argv
)
326 struct sockaddr_in addr
;
327 enum clnt_stat rpc_stat
;
329 u_long prognum
, vers
, minvers
, maxvers
;
330 int sock
= RPC_ANYSOCK
;
331 struct rpc_err rpcerr
;
334 if (argc
< 2 || argc
> 3) {
338 prognum
= getprognum(argv
[1]);
339 get_inet_address(&addr
, argv
[0]);
343 * A call to version 0 should fail with a program/version
344 * mismatch, and give us the range of versions supported.
346 addr
.sin_port
= htons(portnum
);
347 if ((client
= clnttcp_create(&addr
, prognum
, MIN_VERS
,
348 &sock
, 0, 0)) == NULL
) {
349 clnt_pcreateerror("rpcinfo");
350 printf("program %lu is not available\n",
356 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
, (char *)NULL
,
357 xdr_void
, (char *)NULL
, to
);
358 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
359 clnt_geterr(client
, &rpcerr
);
360 minvers
= rpcerr
.re_vers
.low
;
361 maxvers
= rpcerr
.re_vers
.high
;
362 } else if (rpc_stat
== RPC_SUCCESS
) {
364 * Oh dear, it DOES support version 0.
365 * Let's try version MAX_VERS.
367 addr
.sin_port
= htons(portnum
);
368 if ((client
= clnttcp_create(&addr
, prognum
, MAX_VERS
,
369 &sock
, 0, 0)) == NULL
) {
370 clnt_pcreateerror("rpcinfo");
371 printf("program %lu version %lu is not available\n",
377 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
378 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
379 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
380 clnt_geterr(client
, &rpcerr
);
381 minvers
= rpcerr
.re_vers
.low
;
382 maxvers
= rpcerr
.re_vers
.high
;
383 } else if (rpc_stat
== RPC_SUCCESS
) {
385 * It also supports version MAX_VERS.
386 * Looks like we have a wise guy.
387 * OK, we give them information on all
388 * 4 billion versions they support...
393 (void) pstatus(client
, prognum
, MAX_VERS
);
397 (void) pstatus(client
, prognum
, MIN_VERS
);
400 clnt_destroy(client
);
402 sock
= RPC_ANYSOCK
; /* Re-initialize it for later */
403 for (vers
= minvers
; vers
<= maxvers
; vers
++) {
404 addr
.sin_port
= htons(portnum
);
405 if ((client
= clnttcp_create(&addr
, prognum
, vers
,
406 &sock
, 0, 0)) == NULL
) {
407 clnt_pcreateerror("rpcinfo");
408 printf("program %lu version %lu is not available\n",
414 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
415 xdr_void
, (char *)NULL
, to
);
416 if (pstatus(client
, prognum
, vers
) < 0)
418 clnt_destroy(client
);
424 vers
= getvers(argv
[2]);
425 addr
.sin_port
= htons(portnum
);
426 if ((client
= clnttcp_create(&addr
, prognum
, vers
, &sock
,
428 clnt_pcreateerror("rpcinfo");
429 printf("program %lu version %lu is not available\n",
435 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
436 xdr_void
, (char *)NULL
, to
);
437 if (pstatus(client
, prognum
, vers
) < 0)
445 * This routine should take a pointer to an "rpc_err" structure, rather than
446 * a pointer to a CLIENT structure, but "clnt_perror" takes a pointer to
447 * a CLIENT structure rather than a pointer to an "rpc_err" structure.
448 * As such, we have to keep the CLIENT structure around in order to print
449 * a good error message.
452 pstatus(client
, prognum
, vers
)
453 register CLIENT
*client
;
457 struct rpc_err rpcerr
;
459 clnt_geterr(client
, &rpcerr
);
460 if (rpcerr
.re_status
!= RPC_SUCCESS
) {
461 clnt_perror(client
, "rpcinfo");
462 printf("program %lu version %lu is not available\n",
466 printf("program %lu version %lu ready and waiting\n",
477 struct sockaddr_in server_addr
;
478 register struct hostent
*hp
;
479 struct pmaplist
*head
= NULL
;
480 int socket
= RPC_ANYSOCK
;
481 struct timeval minutetimeout
;
482 register CLIENT
*client
;
490 get_inet_address(&server_addr
, argv
[0]);
492 bzero((char *)&server_addr
, sizeof server_addr
);
493 server_addr
.sin_family
= AF_INET
;
494 if ((hp
= gethostbyname("localhost")) != NULL
)
495 bcopy(hp
->h_addr
, (caddr_t
)&server_addr
.sin_addr
,
496 MIN(hp
->h_length
,sizeof(server_addr
.sin_addr
)));
498 server_addr
.sin_addr
.s_addr
= inet_addr("0.0.0.0");
500 minutetimeout
.tv_sec
= 60;
501 minutetimeout
.tv_usec
= 0;
502 server_addr
.sin_port
= htons(PMAPPORT
);
503 if ((client
= clnttcp_create(&server_addr
, PMAPPROG
,
504 PMAPVERS
, &socket
, 50, 500)) == NULL
) {
505 clnt_pcreateerror("rpcinfo: can't contact portmapper");
508 if (clnt_call(client
, PMAPPROC_DUMP
, xdr_void
, NULL
,
509 xdr_pmaplist
, &head
, minutetimeout
) != RPC_SUCCESS
) {
510 fprintf(stderr
, "rpcinfo: can't contact portmapper: ");
511 clnt_perror(client
, "rpcinfo");
515 printf("No remote programs registered.\n");
517 printf(" program vers proto port\n");
518 for (; head
!= NULL
; head
= head
->pml_next
) {
520 head
->pml_map
.pm_prog
,
521 head
->pml_map
.pm_vers
);
522 if (head
->pml_map
.pm_prot
== IPPROTO_UDP
)
523 printf("%6s", "udp");
524 else if (head
->pml_map
.pm_prot
== IPPROTO_TCP
)
525 printf("%6s", "tcp");
527 printf("%6ld", head
->pml_map
.pm_prot
);
528 printf("%7ld", head
->pml_map
.pm_port
);
529 rpc
= getrpcbynumber(head
->pml_map
.pm_prog
);
531 printf(" %s\n", rpc
->r_name
);
539 * reply_proc collects replies from the broadcast.
540 * to get a unique list of responses the output of rpcinfo should
541 * be piped through sort(1) and then uniq(1).
547 void *res
; /* Nothing comes back */
548 struct sockaddr_in
*who
; /* Who sent us the reply */
550 register struct hostent
*hp
;
552 hp
= gethostbyaddr((char *) &who
->sin_addr
, sizeof who
->sin_addr
,
554 printf("%s %s\n", inet_ntoa(who
->sin_addr
),
555 (hp
== NULL
) ? "(unknown)" : hp
->h_name
);
564 enum clnt_stat rpc_stat
;
565 u_long prognum
, vers
;
571 prognum
= getprognum(argv
[0]);
572 vers
= getvers(argv
[1]);
573 rpc_stat
= clnt_broadcast(prognum
, vers
, NULLPROC
, xdr_void
,
574 (char *)NULL
, xdr_void
, (char *)NULL
, reply_proc
);
575 if ((rpc_stat
!= RPC_SUCCESS
) && (rpc_stat
!= RPC_TIMEDOUT
)) {
576 fprintf(stderr
, "rpcinfo: broadcast failed: %s\n",
577 clnt_sperrno(rpc_stat
));
584 deletereg(argc
, argv
)
587 { u_long prog_num
, version_num
;
593 if (getuid()) /* This command allowed only to root */
594 errx(1, "sorry, you are not root") ;
595 prog_num
= getprognum(argv
[0]);
596 version_num
= getvers(argv
[1]);
597 if ((pmap_unset(prog_num
, version_num
)) == 0)
598 errx(1, "could not delete registration for prog %s version %s",
605 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n",
606 "usage: rpcinfo [-n portnum] -u host prognum [versnum]",
607 " rpcinfo [-n portnum] -t host prognum [versnum]",
608 " rpcinfo -p [host]",
609 " rpcinfo -b prognum versnum",
610 " rpcinfo -d prognum versnum");
617 register struct rpcent
*rpc
;
618 register u_long prognum
;
621 rpc
= getrpcbyname(arg
);
623 errx(1, "%s is unknown service", arg
);
624 prognum
= rpc
->r_number
;
626 prognum
= (u_long
) atoi(arg
);
636 register u_long vers
;
638 vers
= (int) atoi(arg
);
643 get_inet_address(addr
, host
)
644 struct sockaddr_in
*addr
;
647 register struct hostent
*hp
;
649 bzero((char *)addr
, sizeof *addr
);
650 addr
->sin_addr
.s_addr
= (u_long
) inet_addr(host
);
651 if (addr
->sin_addr
.s_addr
== -1 || addr
->sin_addr
.s_addr
== 0) {
652 if ((hp
= gethostbyname(host
)) == NULL
)
653 errx(1, "%s is unknown host\n", host
);
654 bcopy(hp
->h_addr
, (char *)&addr
->sin_addr
,
655 MIN(hp
->h_length
,sizeof(addr
->sin_addr
)));
657 addr
->sin_family
= AF_INET
;