#include <stdio.h>
#include <rpc/rpc.h>
+#include <rpc/pmap_prot.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <unistd.h>
#include <memory.h>
#include <stdlib.h>
+#include <fcntl.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <mount.h>
#include <unistd.h>
+#include "nfsrpc.h"
+
+#define TIMEOUT_UDP 3
+#define TOTAL_TIMEOUT 20
+
static char * version = "showmount for " VERSION;
static char * program_name;
static int headers = 1;
#define MAXHOSTLEN 256
-int dump_cmp(p, q)
-char **p;
-char **q;
+static int dump_cmp(const void *pv, const void *qv)
{
+ const char **p = (const char **)pv;
+ const char **q = (const char **)qv;
return strcmp(*p, *q);
}
-static void usage(fp, n)
-FILE *fp;
-int n;
+static void usage(FILE *fp, int n)
{
fprintf(fp, "Usage: %s [-adehv]\n", program_name);
fprintf(fp, " [--all] [--directories] [--exports]\n");
exit(n);
}
-int main(argc, argv)
-int argc;
-char **argv;
+static const char *mount_pgm_tbl[] = {
+ "showmount",
+ "mount",
+ "mountd",
+ NULL,
+};
+
+static const rpcvers_t mount_vers_tbl[] = {
+ MOUNTVERS_NFSV3,
+ MOUNTVERS_POSIX,
+ MOUNTVERS,
+};
+static const unsigned int max_vers_tblsz =
+ (sizeof(mount_vers_tbl)/sizeof(mount_vers_tbl[0]));
+
+/*
+ * Generate an RPC client handle connected to the mountd service
+ * at @hostname, or die trying.
+ *
+ * Supports both AF_INET and AF_INET6 server addresses.
+ */
+static CLIENT *nfs_get_mount_client(const char *hostname, rpcvers_t vers)
+{
+ rpcprog_t program = nfs_getrpcbyname(MOUNTPROG, mount_pgm_tbl);
+ CLIENT *client;
+
+ client = clnt_create(hostname, program, vers, "tcp");
+ if (client)
+ return client;
+ client = clnt_create(hostname, program, vers, "udp");
+ if (client)
+ return client;
+
+ clnt_pcreateerror("clnt_create");
+ exit(1);
+}
+
+int main(int argc, char **argv)
{
char hostname_buf[MAXHOSTLEN];
char *hostname;
enum clnt_stat clnt_stat;
- struct hostent *hp;
- struct sockaddr_in server_addr;
- int msock;
struct timeval total_timeout;
- struct timeval pertry_timeout;
int c;
CLIENT *mclient;
groups grouplist;
int i;
int n;
int maxlen;
+ int unsigned vers=0;
char **dumpv;
program_name = argv[0];
break;
}
- if (inet_aton(hostname, &server_addr.sin_addr)) {
- server_addr.sin_family = AF_INET;
- }
- else {
- if ((hp = gethostbyname(hostname)) == NULL) {
- fprintf(stderr, "%s: can't get address for %s\n",
- program_name, hostname);
- exit(1);
- }
- server_addr.sin_family = AF_INET;
- memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
- }
-
- /* create mount deamon client */
-
- server_addr.sin_port = 0;
- msock = RPC_ANYSOCK;
- if ((mclient = clnttcp_create(&server_addr,
- MOUNTPROG, MOUNTVERS, &msock, 0, 0)) == NULL) {
- server_addr.sin_port = 0;
- msock = RPC_ANYSOCK;
- pertry_timeout.tv_sec = 3;
- pertry_timeout.tv_usec = 0;
- if ((mclient = clntudp_create(&server_addr,
- MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
- clnt_pcreateerror("mount clntudp_create");
- exit(1);
- }
+ mclient = nfs_get_mount_client(hostname, mount_vers_tbl[vers]);
+ mclient->cl_auth = nfs_authsys_create();
+ if (mclient->cl_auth == NULL) {
+ fprintf(stderr, "%s: unable to create RPC auth handle.\n",
+ program_name);
+ clnt_destroy(mclient);
+ exit(1);
}
- mclient->cl_auth = authunix_create_default();
- total_timeout.tv_sec = 20;
+ total_timeout.tv_sec = TOTAL_TIMEOUT;
total_timeout.tv_usec = 0;
+again:
if (eflag) {
memset(&exportlist, '\0', sizeof(exportlist));
+
clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
(xdrproc_t) xdr_void, NULL,
(xdrproc_t) xdr_exports, (caddr_t) &exportlist,
total_timeout);
+ if (clnt_stat == RPC_PROGVERSMISMATCH) {
+ if (++vers < max_vers_tblsz) {
+ (void)CLNT_CONTROL(mclient, CLSET_VERS,
+ (void *)&mount_vers_tbl[vers]);
+ goto again;
+ }
+ }
if (clnt_stat != RPC_SUCCESS) {
clnt_perror(mclient, "rpc mount export");
+ clnt_destroy(mclient);
exit(1);
}
if (headers)
printf("\n");
exportlist = exportlist->ex_next;
}
+ clnt_destroy(mclient);
exit(0);
}
(xdrproc_t) xdr_void, NULL,
(xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
total_timeout);
+ if (clnt_stat == RPC_PROGVERSMISMATCH) {
+ if (++vers < max_vers_tblsz) {
+ (void)CLNT_CONTROL(mclient, CLSET_VERS,
+ (void *)&mount_vers_tbl[vers]);
+ goto again;
+ }
+ }
if (clnt_stat != RPC_SUCCESS) {
clnt_perror(mclient, "rpc mount dump");
+ clnt_destroy(mclient);
exit(1);
}
+ clnt_destroy(mclient);
n = 0;
for (list = dumplist; list; list = list->ml_next)