/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
* unrestricted use provided that this legend is included on all tape
* media and as a part of the software program in whole or part. Users
* may copy or modify Sun RPC without charge, but are not authorized
* to license or distribute it to anyone else except as part of a product or
* program developed by the user.
*
* SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* Sun RPC is provided with no support and without any obligation on the
* part of Sun Microsystems, Inc. to assist in its use, correction,
* modification or enhancement.
*
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
* OR ANY PART THEREOF.
*
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Sun has been advised of the possibility of such damages.
*
* Sun Microsystems, Inc.
* 2550 Garcia Avenue
* Mountain View, California 94043
*/
/*
* Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
*/
/*
* rpcinfo: ping a particular rpc program
* or dump the registered programs on the remote machine.
*/
/*
* We are for now defining PORTMAP here. It doesnt even compile
* unless it is defined.
*/
#ifndef PORTMAP
#define PORTMAP
#endif
/*
* If PORTMAP is defined, rpcinfo will talk to both portmapper and
* rpcbind programs; else it talks only to rpcbind. In the latter case
* all the portmapper specific options such as -u, -t, -p become void.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/un.h>
#include <rpc/rpc.h>
#include <stdio.h>
#include <rpc/rpcb_prot.h>
#include <rpc/rpcent.h>
#include <rpc/nettype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <ctype.h>
#include <errno.h>
#ifdef PORTMAP /* Support for version 2 portmapper */
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <rpc/pmap_prot.h>
#include <rpc/pmap_clnt.h>
#endif
/*
* Functions to be performed.
*/
#define NONE 0 /* no function */
#define PMAPDUMP 1 /* dump portmapper registrations */
#define TCPPING 2 /* ping TCP service */
#define UDPPING 3 /* ping UDP service */
#define BROADCAST 4 /* ping broadcast service */
#define DELETES 5 /* delete registration for the service */
#define ADDRPING 6 /* pings at the given address */
#define PROGPING 7 /* pings a program on a given host */
#define RPCBDUMP 8 /* dump rpcbind registrations */
#define RPCBDUMP_SHORT 9 /* dump rpcbind registrations - short version */
#define RPCBADDRLIST 10 /* dump addr list about one prog */
#define RPCBGETSTAT 11 /* Get statistics */
to.tv_sec = 5;
to.tv_usec = 0;
clnt = clntudp_create(addr, UL(prog), UL(vers), to, fdp);
}
if (clnt == NULL) {
char *m = clnt_spcreateerror("") + 2;
if (vers == MIN_VERS)
errx(1, "Program %lu is not available (%s)",
(unsigned long)prog, m);
else
errx(1, "Program %lu version %lu is not available (%s)",
(unsigned long)prog, (unsigned long)vers, m);
}
return clnt;
}
/*
* If portnum is 0, then go and get the address from portmapper, which happens
* transparently through clnt*_create(); If version number is not given, it
* tries to find out the version number by making a call to version 0 and if
* that fails, it obtains the high order and the low order version number. If
* version 0 calls succeeds, it tries for MAXVERS call and repeats the same.
*/
static void
ip_ping(u_short portnum, const char *trans, int argc, char **argv)
{
CLIENT *client;
int fd = RPC_ANYFD;
struct timeval to;
struct sockaddr_in addr;
enum clnt_stat rpc_stat;
rpcprog_t prognum, vers, minvers, maxvers;
struct rpc_err rpcerr;
int failure = 0;
if (argc < 2 || argc > 3)
usage();
to.tv_sec = 10;
to.tv_usec = 0;
prognum = getprognum(argv[1]);
get_inet_address(&addr, argv[0]);
if (argc == 2) { /* Version number not known */
/*
* A call to version 0 should fail with a program/version
* mismatch, and give us the range of versions supported.
*/
vers = MIN_VERS;
} else {
vers = getvers(argv[2]);
}
addr.sin_port = htons(portnum);
client = clnt_com_create(&addr, prognum, vers, &fd, trans);
rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_void, NULL, to);
if (argc != 2) {
/* Version number was known */
if (pstatus(client, prognum, vers) < 0)
exit(1);
(void)CLNT_DESTROY(client);
return;
}
/* Version number not known */
(void)CLNT_CONTROL(client, CLSET_FD_NCLOSE, NULL);
if (rpc_stat == RPC_PROGVERSMISMATCH) {
clnt_geterr(client, &rpcerr);
minvers = rpcerr.re_vers.low;
maxvers = rpcerr.re_vers.high;
} else if (rpc_stat == RPC_SUCCESS) {
/*
* Oh dear, it DOES support version 0.
* Let's try version MAX_VERS.
*/
(void)CLNT_DESTROY(client);
addr.sin_port = htons(portnum);
client = clnt_com_create(&addr, (unsigned int)prognum,
MAX_VERS, &fd, trans);
rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_void, NULL, to);
if (rpc_stat == RPC_PROGVERSMISMATCH) {
clnt_geterr(client, &rpcerr);
minvers = rpcerr.re_vers.low;
maxvers = rpcerr.re_vers.high;
} else if (rpc_stat == RPC_SUCCESS) {
/*
* It also supports version MAX_VERS.
* Looks like we have a wise guy.
* OK, we give them information on all
* 4 billion versions they support...
*/
minvers = 0;
maxvers = MAX_VERS;
} else {
(void)pstatus(client, prognum, MAX_VERS);
exit(1);
}
} else {
(void)pstatus(client, prognum, MIN_VERS);
exit(1);
}
(void)CLNT_DESTROY(client);
for (vers = minvers; vers <= maxvers; vers++) {
addr.sin_port = htons(portnum);
client = clnt_com_create(&addr, prognum, vers, &fd, trans);
rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_void, NULL, to);
if (pstatus(client, prognum, vers) < 0)
failure = 1;
(void)CLNT_DESTROY(client);
}
if (failure)
exit(1);
(void)close(fd);
return;
}
if (client == NULL) {
if (rpc_createerr.cf_stat == RPC_TLIERROR) {
/*
* "Misc. TLI error" is not too helpful. Most likely
* the connection to the remote server timed out, so
* this error is at least less perplexing.
*/
rpc_createerr.cf_stat = RPC_PMAPFAILURE;
rpc_createerr.cf_error.re_status = RPC_FAILED;
}
errx(1, "Can't contact portmapper (%s)",
clnt_spcreateerror("") + 2);
}
/*
* reply_proc collects replies from the broadcast.
* to get a unique list of responses the output of rpcinfo should
* be piped through sort(1) and then uniq(1).
*/
/*ARGSUSED*/
static bool_t
reply_proc(
void *res, /* Nothing comes back */
struct netbuf *who, /* Who sent us the reply */
struct netconfig *nconf /* On which transport the reply came */
)
{
const char *uaddr;
char *uf;
char hostbuf[NI_MAXHOST];
const char *hostname;
struct sockaddr *sa = (struct sockaddr *)who->buf;
nconf = getnetconfigent(netid);
if (nconf == NULL)
errx(1, "Invalid transport (%s)", nc_sperror());
client = getclnthandle(host, nconf, RPCBVERS4, &targaddr);
if (nconf)
(void)freenetconfigent(nconf);
}
if (client == NULL)
errx(1, "Can't contact rpcbind (%s)",
clnt_spcreateerror("") + 2);
minutetimeout.tv_sec = 60;
minutetimeout.tv_usec = 0;
parms.r_prog = getprognum(argv[1]);
parms.r_vers = getvers(argv[2]);
parms.r_netid = client->cl_netid;
if (targaddr == NULL) {
parms.r_addr = nullstring; /* for XDRing */
} else {
/*
* We also send the remote system the address we
* used to contact it in case it can help it
* connect back with us
*/
struct netconfig *nconf;
/*
* Delete registeration for this (prog, vers, netid)
*/
static void
deletereg(const char *netid, int argc, char **argv)
{
struct netconfig *nconf = NULL;
if (argc != 2)
usage();
if (netid) {
nconf = getnetconfigent(netid);
if (nconf == NULL)
errx(1, "netid %s not supported", netid);
}
if ((rpcb_unset(getprognum(argv[0]), getvers(argv[1]), nconf)) == 0)
errx(1, "Could not delete registration for prog %s version %s",
argv[0], argv[1]);
}
/*
* Create and return a handle for the given nconf.
* Exit if cannot create handle.
*/
static CLIENT *
clnt_addr_create(const char *address, const struct netconfig *nconf,
rpcprog_t prog, rpcvers_t vers)
{
CLIENT *client;
static struct netbuf *nbuf;
static int fd = RPC_ANYFD;
if (fd == RPC_ANYFD) {
if ((fd = __rpc_nconf2fd(nconf)) == -1) {
rpc_createerr.cf_stat = RPC_TLIERROR;
clnt_pcreateerror(getprogname());
exit(1);
}
/* Convert the uaddr to taddr */
nbuf = uaddr2taddr(nconf, address);
if (nbuf == NULL)
errx(1, "No address for client handle");
}
client = clnt_tli_create(fd, nconf, nbuf, prog, vers, 0, 0);
if (client == NULL) {
clnt_pcreateerror(getprogname());
exit(1);
}
return client;
}
/*
* If the version number is given, ping that (prog, vers); else try to find
* the version numbers supported for that prog and ping all the versions.
* Remote rpcbind is not contacted for this service. The requests are
* sent directly to the services themselves.
*/
static void
addrping(const char *address, const char *netid, int argc, char **argv)
{
CLIENT *client;
struct timeval to;
enum clnt_stat rpc_stat;
rpcprog_t prognum, versnum, minvers, maxvers;
struct rpc_err rpcerr;
int failure = 0;
struct netconfig *nconf;
int fd;
if (argc < 1 || argc > 2 || (netid == NULL))
usage();
nconf = getnetconfigent(netid);
if (nconf == NULL)
errx(1, "Could not find %s", netid);
to.tv_sec = 10;
to.tv_usec = 0;
prognum = getprognum(argv[0]);
if (argc == 1) { /* Version number not known */
/*
* A call to version 0 should fail with a program/version
* mismatch, and give us the range of versions supported.
*/
versnum = MIN_VERS;
} else {
versnum = getvers(argv[1]);
}
client = clnt_addr_create(address, nconf, prognum, versnum);
rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_void, NULL, to);
if (argc == 2) {
/* Version number was known */
if (pstatus(client, prognum, versnum) < 0)
failure = 1;
(void)CLNT_DESTROY(client);
if (failure)
exit(1);
return;
}
/* Version number not known */
(void)CLNT_CONTROL(client, CLSET_FD_NCLOSE, NULL);
(void)CLNT_CONTROL(client, CLGET_FD, (char *)(void *)&fd);
if (rpc_stat == RPC_PROGVERSMISMATCH) {
clnt_geterr(client, &rpcerr);
minvers = rpcerr.re_vers.low;
maxvers = rpcerr.re_vers.high;
} else if (rpc_stat == RPC_SUCCESS) {
/*
* Oh dear, it DOES support version 0.
* Let's try version MAX_VERS.
*/
(void)CLNT_DESTROY(client);
client = clnt_addr_create(address, nconf, prognum, MAX_VERS);
rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_void, NULL, to);
if (rpc_stat == RPC_PROGVERSMISMATCH) {
clnt_geterr(client, &rpcerr);
minvers = rpcerr.re_vers.low;
maxvers = rpcerr.re_vers.high;
} else if (rpc_stat == RPC_SUCCESS) {
/*
* It also supports version MAX_VERS.
* Looks like we have a wise guy.
* OK, we give them information on all
* 4 billion versions they support...
*/
minvers = 0;
maxvers = MAX_VERS;
} else {
(void)pstatus(client, prognum, MAX_VERS);
exit(1);
}
} else {
(void)pstatus(client, prognum, MIN_VERS);
exit(1);
}
(void)CLNT_DESTROY(client);
for (versnum = minvers; versnum <= maxvers; versnum++) {
client = clnt_addr_create(address, nconf, prognum, versnum);
rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_void, NULL, to);
if (pstatus(client, prognum, versnum) < 0)
failure = 1;
(void)CLNT_DESTROY(client);
}
(void)close(fd);
if (failure)
exit(1);
return;
}
/*
* If the version number is given, ping that (prog, vers); else try to find
* the version numbers supported for that prog and ping all the versions.
* Remote rpcbind is *contacted* for this service. The requests are
* then sent directly to the services themselves.
*/
static void
progping(const char *netid, int argc, char **argv)
{
CLIENT *client;
struct timeval to;
enum clnt_stat rpc_stat;
rpcprog_t prognum;
rpcvers_t versnum, minvers, maxvers;
struct rpc_err rpcerr;
int failure = 0;
struct netconfig *nconf;
if (argc < 2 || argc > 3 || (netid == NULL))
usage();
prognum = getprognum(argv[1]);
if (argc == 2) { /* Version number not known */
/*
* A call to version 0 should fail with a program/version
* mismatch, and give us the range of versions supported.
*/
versnum = MIN_VERS;
} else {
versnum = getvers(argv[2]);
}
if (netid) {
nconf = getnetconfigent(netid);
if (nconf == NULL)
errx(1, "Could not find `%s'", netid);
client = clnt_tp_create(argv[0], prognum, versnum, nconf);
} else {
client = clnt_create(argv[0], prognum, versnum, "NETPATH");
}
if (client == NULL) {
clnt_pcreateerror(getprogname());
exit(1);
}
to.tv_sec = 10;
to.tv_usec = 0;
rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_void, NULL, to);
if (argc == 3) {
/* Version number was known */
if (pstatus(client, prognum, versnum) < 0)
failure = 1;
(void)CLNT_DESTROY(client);
if (failure)
exit(1);
return;
}
/* Version number not known */
if (rpc_stat == RPC_PROGVERSMISMATCH) {
clnt_geterr(client, &rpcerr);
minvers = rpcerr.re_vers.low;
maxvers = rpcerr.re_vers.high;
} else if (rpc_stat == RPC_SUCCESS) {
/*
* Oh dear, it DOES support version 0.
* Let's try version MAX_VERS.
*/
versnum = MAX_VERS;
(void)CLNT_CONTROL(client, CLSET_VERS,
(char *)(void *)&versnum);
rpc_stat = CLNT_CALL(client, NULLPROC,
(xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_void, NULL, to);
if (rpc_stat == RPC_PROGVERSMISMATCH) {
clnt_geterr(client, &rpcerr);
minvers = rpcerr.re_vers.low;
maxvers = rpcerr.re_vers.high;
} else if (rpc_stat == RPC_SUCCESS) {
/*
* It also supports version MAX_VERS.
* Looks like we have a wise guy.
* OK, we give them information on all
* 4 billion versions they support...
*/
minvers = 0;
maxvers = MAX_VERS;
} else {
(void)pstatus(client, prognum, MAX_VERS);
exit(1);
}
} else {
(void)pstatus(client, prognum, MIN_VERS);
exit(1);
}
for (versnum = minvers; versnum <= maxvers; versnum++) {
(void)CLNT_CONTROL(client, CLSET_VERS,
(char *)(void *)&versnum);
rpc_stat = CLNT_CALL(client, NULLPROC, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_void, NULL, to);
if (pstatus(client, prognum, versnum) < 0)
failure = 1;
}
(void)CLNT_DESTROY(client);
if (failure)
exit(1);
return;
}
/*
* This routine should take a pointer to an "rpc_err" structure, rather than
* a pointer to a CLIENT structure, but "clnt_sperror" takes a pointer to
* a CLIENT structure rather than a pointer to an "rpc_err" structure.
* As such, we have to keep the CLIENT structure around in order to print
* a good error message.
*/
static int
pstatus(CLIENT *client, rpcprog_t prog, rpcvers_t vers)
{
struct rpc_err rpcerr;
clnt_geterr(client, &rpcerr);
if (rpcerr.re_status != RPC_SUCCESS) {
warnx("Program %lu version %lu is not available (%s)",
(unsigned long)prog, (unsigned long)vers,
clnt_sperror(client, "") + 2);
return -1;
} else {
in_port_t portnum;
struct netbuf nb;
CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&nb);
portnum = ntohs(getport(&nb));
(void)printf("program %lu version %lu ready and waiting"
" at port %u\n", (unsigned long)prog, (unsigned long)vers,
portnum);
return 0;
}
}