showmount: Try the highest mount version then fall back to lower ones
[nfs-utils.git] / utils / showmount / showmount.c
1 /*
2  * showmount.c -- show mount information for an NFS server
3  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include <stdio.h>
21 #include <rpc/rpc.h>
22 #include <rpc/pmap_prot.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <sys/time.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <memory.h>
30 #include <stdlib.h>
31 #include <fcntl.h>
32
33 #include <netdb.h>
34 #include <arpa/inet.h>
35 #include <errno.h>
36 #include <getopt.h>
37 #include <mount.h>
38 #include <unistd.h>
39
40 #include "nfsrpc.h"
41
42 #define TIMEOUT_UDP     3
43 #define TOTAL_TIMEOUT   20
44
45 static char *   version = "showmount for " VERSION;
46 static char *   program_name;
47 static int      headers = 1;
48 static int      hflag = 0;
49 static int      aflag = 0;
50 static int      dflag = 0;
51 static int      eflag = 0;
52
53 static struct option longopts[] =
54 {
55         { "all", 0, 0, 'a' },
56         { "directories", 0, 0, 'd' },
57         { "exports", 0, 0, 'e' },
58         { "no-headers", 0, &headers, 0 },
59         { "version", 0, 0, 'v' },
60         { "help", 0, 0, 'h' },
61         { NULL, 0, 0, 0 }
62 };
63
64 #define MAXHOSTLEN 256
65
66 static int dump_cmp(const void *pv, const void *qv)
67 {
68         const char **p = (const char **)pv;
69         const char **q = (const char **)qv;
70         return strcmp(*p, *q);
71 }
72
73 static void usage(FILE *fp, int n)
74 {
75         fprintf(fp, "Usage: %s [-adehv]\n", program_name);
76         fprintf(fp, "       [--all] [--directories] [--exports]\n");
77         fprintf(fp, "       [--no-headers] [--help] [--version] [host]\n");
78         exit(n);
79 }
80
81 static const char *mount_pgm_tbl[] = {
82         "showmount",
83         "mount",
84         "mountd",
85         NULL,
86 };
87
88 static const rpcvers_t mount_vers_tbl[] = {
89         MOUNTVERS_NFSV3,
90         MOUNTVERS_POSIX,
91         MOUNTVERS,
92 };
93 static const unsigned int max_vers_tblsz = 
94         (sizeof(mount_vers_tbl)/sizeof(mount_vers_tbl[0]));
95
96 /*
97  * Generate an RPC client handle connected to the mountd service
98  * at @hostname, or die trying.
99  *
100  * Supports both AF_INET and AF_INET6 server addresses.
101  */
102 static CLIENT *nfs_get_mount_client(const char *hostname, rpcvers_t vers)
103 {
104         rpcprog_t program = nfs_getrpcbyname(MOUNTPROG, mount_pgm_tbl);
105         CLIENT *client;
106
107         client = clnt_create(hostname, program, vers, "tcp");
108         if (client)
109                 return client;
110         client = clnt_create(hostname, program, vers, "udp");
111         if (client)
112                 return client;
113
114         clnt_pcreateerror("clnt_create");
115         exit(1);
116 }
117
118 int main(int argc, char **argv)
119 {
120         char hostname_buf[MAXHOSTLEN];
121         char *hostname;
122         enum clnt_stat clnt_stat;
123         struct timeval total_timeout;
124         int c;
125         CLIENT *mclient;
126         groups grouplist;
127         exports exportlist, exl;
128         mountlist dumplist;
129         mountlist list;
130         int i;
131         int n;
132         int maxlen;
133         int unsigned vers=0;
134         char **dumpv;
135
136         program_name = argv[0];
137         while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
138                 switch (c) {
139                 case 'a':
140                         aflag = 1;
141                         break;
142                 case 'd':
143                         dflag = 1;
144                         break;
145                 case 'e':
146                         eflag = 1;
147                         break;
148                 case 'h':
149                         usage(stdout, 0);
150                         break;
151                 case 'v':
152                         printf("%s\n", version);
153                         exit(0);
154                 case 0:
155                         break;
156                 case '?':
157                 default:
158                         usage(stderr, 1);
159                         break;
160                 }
161         }
162         argc -= optind;
163         argv += optind;
164
165         switch (aflag + dflag + eflag) {
166         case 0:
167                 hflag = 1;
168                 break;
169         case 1:
170                 break;
171         default:
172                 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
173                         program_name);
174                 exit(1);
175                 break;
176         }
177
178         switch (argc) {
179         case 0:
180                 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
181                         perror("getting hostname");
182                         exit(1);
183                 }
184                 hostname = hostname_buf;
185                 break;
186         case 1:
187                 hostname = argv[0];
188                 break;
189         default:
190                 fprintf(stderr, "%s: only one hostname is allowed\n",
191                         program_name);
192                 exit(1);
193                 break;
194         }
195
196         mclient = nfs_get_mount_client(hostname, mount_vers_tbl[vers]);
197         mclient->cl_auth = authunix_create_default();
198         total_timeout.tv_sec = TOTAL_TIMEOUT;
199         total_timeout.tv_usec = 0;
200
201 again:
202         if (eflag) {
203                 memset(&exportlist, '\0', sizeof(exportlist));
204
205                 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
206                         (xdrproc_t) xdr_void, NULL,
207                         (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
208                         total_timeout);
209                 if (clnt_stat == RPC_PROGVERSMISMATCH) {
210                         if (++vers <  max_vers_tblsz) {
211                                 (void)CLNT_CONTROL(mclient, CLSET_VERS, 
212                                         (void *)&mount_vers_tbl[vers]);
213                                 goto again;
214                                 }
215                 }
216                 if (clnt_stat != RPC_SUCCESS) {
217                         clnt_perror(mclient, "rpc mount export");
218                         clnt_destroy(mclient);
219                         exit(1);
220                 }
221                 if (headers)
222                         printf("Export list for %s:\n", hostname);
223                 maxlen = 0;
224                 for (exl = exportlist; exl; exl = exl->ex_next) {
225                         if ((n = strlen(exl->ex_dir)) > maxlen)
226                                 maxlen = n;
227                 }
228                 while (exportlist) {
229                         printf("%-*s ", maxlen, exportlist->ex_dir);
230                         grouplist = exportlist->ex_groups;
231                         if (grouplist)
232                                 while (grouplist) {
233                                         printf("%s%s", grouplist->gr_name,
234                                                 grouplist->gr_next ? "," : "");
235                                         grouplist = grouplist->gr_next;
236                                 }
237                         else
238                                 printf("(everyone)");
239                         printf("\n");
240                         exportlist = exportlist->ex_next;
241                 }
242                 clnt_destroy(mclient);
243                 exit(0);
244         }
245
246         memset(&dumplist, '\0', sizeof(dumplist));
247         clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
248                 (xdrproc_t) xdr_void, NULL,
249                 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
250                 total_timeout);
251         if (clnt_stat == RPC_PROGVERSMISMATCH) {
252                 if (++vers <  max_vers_tblsz) {
253                         (void)CLNT_CONTROL(mclient, CLSET_VERS, 
254                                 (void *)&mount_vers_tbl[vers]);
255                         goto again;
256                 }
257         }
258         if (clnt_stat != RPC_SUCCESS) {
259                 clnt_perror(mclient, "rpc mount dump");
260                 clnt_destroy(mclient);
261                 exit(1);
262         }
263         clnt_destroy(mclient);
264
265         n = 0;
266         for (list = dumplist; list; list = list->ml_next)
267                 n++;
268         dumpv = (char **) calloc(n, sizeof (char *));
269         if (n && !dumpv) {
270                 fprintf(stderr, "%s: out of memory\n", program_name);
271                 exit(1);
272         }
273         i = 0;
274
275         if (hflag) {
276                 if (headers)
277                         printf("Hosts on %s:\n", hostname);
278                 while (dumplist) {
279                         dumpv[i++] = dumplist->ml_hostname;
280                         dumplist = dumplist->ml_next;
281                 }
282         }
283         else if (aflag) {
284                 if (headers)
285                         printf("All mount points on %s:\n", hostname);
286                 while (dumplist) {
287                         char *t;
288
289                         t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
290                         if (!t)
291                         {
292                                 fprintf(stderr, "%s: out of memory\n", program_name);
293                                 exit(1);
294                         }
295                         sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
296                         dumpv[i++] = t;
297                         dumplist = dumplist->ml_next;
298                 }
299         }
300         else if (dflag) {
301                 if (headers)
302                         printf("Directories on %s:\n", hostname);
303                 while (dumplist) {
304                         dumpv[i++] = dumplist->ml_directory;
305                         dumplist = dumplist->ml_next;
306                 }
307         }
308
309         qsort(dumpv, n, sizeof (char *), dump_cmp);
310         
311         for (i = 0; i < n; i++) {
312                 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
313                         printf("%s\n", dumpv[i]);
314         }
315         exit(0);
316 }
317