]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/showmount/showmount.c
statd: Replace note() with xlog() in rpc.statd
[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 *nfs_sm_pgmtbl[] = {
82         "showmount",
83         "mount",
84         "mountd",
85         NULL,
86 };
87
88 /*
89  * Generate an RPC client handle connected to the mountd service
90  * at @hostname, or die trying.
91  *
92  * Supports both AF_INET and AF_INET6 server addresses.
93  */
94 static CLIENT *nfs_get_mount_client(const char *hostname)
95 {
96         rpcprog_t program = nfs_getrpcbyname(MOUNTPROG, nfs_sm_pgmtbl);
97         CLIENT *client;
98
99         client = clnt_create(hostname, program, MOUNTVERS, "tcp");
100         if (client)
101                 return client;
102
103         client = clnt_create(hostname, program, MOUNTVERS, "udp");
104         if (client)
105                 return client;
106
107         clnt_pcreateerror("clnt_create");
108         exit(1);
109 }
110
111 int main(int argc, char **argv)
112 {
113         char hostname_buf[MAXHOSTLEN];
114         char *hostname;
115         enum clnt_stat clnt_stat;
116         struct timeval total_timeout;
117         int c;
118         CLIENT *mclient;
119         groups grouplist;
120         exports exportlist, exl;
121         mountlist dumplist;
122         mountlist list;
123         int i;
124         int n;
125         int maxlen;
126         char **dumpv;
127
128         program_name = argv[0];
129         while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
130                 switch (c) {
131                 case 'a':
132                         aflag = 1;
133                         break;
134                 case 'd':
135                         dflag = 1;
136                         break;
137                 case 'e':
138                         eflag = 1;
139                         break;
140                 case 'h':
141                         usage(stdout, 0);
142                         break;
143                 case 'v':
144                         printf("%s\n", version);
145                         exit(0);
146                 case 0:
147                         break;
148                 case '?':
149                 default:
150                         usage(stderr, 1);
151                         break;
152                 }
153         }
154         argc -= optind;
155         argv += optind;
156
157         switch (aflag + dflag + eflag) {
158         case 0:
159                 hflag = 1;
160                 break;
161         case 1:
162                 break;
163         default:
164                 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
165                         program_name);
166                 exit(1);
167                 break;
168         }
169
170         switch (argc) {
171         case 0:
172                 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
173                         perror("getting hostname");
174                         exit(1);
175                 }
176                 hostname = hostname_buf;
177                 break;
178         case 1:
179                 hostname = argv[0];
180                 break;
181         default:
182                 fprintf(stderr, "%s: only one hostname is allowed\n",
183                         program_name);
184                 exit(1);
185                 break;
186         }
187
188         mclient = nfs_get_mount_client(hostname);
189         mclient->cl_auth = authunix_create_default();
190         total_timeout.tv_sec = TOTAL_TIMEOUT;
191         total_timeout.tv_usec = 0;
192
193         if (eflag) {
194                 memset(&exportlist, '\0', sizeof(exportlist));
195
196                 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
197                         (xdrproc_t) xdr_void, NULL,
198                         (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
199                         total_timeout);
200                 if (clnt_stat != RPC_SUCCESS) {
201                         clnt_perror(mclient, "rpc mount export");
202                         clnt_destroy(mclient);
203                         exit(1);
204                 }
205                 if (headers)
206                         printf("Export list for %s:\n", hostname);
207                 maxlen = 0;
208                 for (exl = exportlist; exl; exl = exl->ex_next) {
209                         if ((n = strlen(exl->ex_dir)) > maxlen)
210                                 maxlen = n;
211                 }
212                 while (exportlist) {
213                         printf("%-*s ", maxlen, exportlist->ex_dir);
214                         grouplist = exportlist->ex_groups;
215                         if (grouplist)
216                                 while (grouplist) {
217                                         printf("%s%s", grouplist->gr_name,
218                                                 grouplist->gr_next ? "," : "");
219                                         grouplist = grouplist->gr_next;
220                                 }
221                         else
222                                 printf("(everyone)");
223                         printf("\n");
224                         exportlist = exportlist->ex_next;
225                 }
226                 clnt_destroy(mclient);
227                 exit(0);
228         }
229
230         memset(&dumplist, '\0', sizeof(dumplist));
231         clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
232                 (xdrproc_t) xdr_void, NULL,
233                 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
234                 total_timeout);
235         if (clnt_stat != RPC_SUCCESS) {
236                 clnt_perror(mclient, "rpc mount dump");
237                 clnt_destroy(mclient);
238                 exit(1);
239         }
240         clnt_destroy(mclient);
241
242         n = 0;
243         for (list = dumplist; list; list = list->ml_next)
244                 n++;
245         dumpv = (char **) calloc(n, sizeof (char *));
246         if (n && !dumpv) {
247                 fprintf(stderr, "%s: out of memory\n", program_name);
248                 exit(1);
249         }
250         i = 0;
251
252         if (hflag) {
253                 if (headers)
254                         printf("Hosts on %s:\n", hostname);
255                 while (dumplist) {
256                         dumpv[i++] = dumplist->ml_hostname;
257                         dumplist = dumplist->ml_next;
258                 }
259         }
260         else if (aflag) {
261                 if (headers)
262                         printf("All mount points on %s:\n", hostname);
263                 while (dumplist) {
264                         char *t;
265
266                         t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
267                         if (!t)
268                         {
269                                 fprintf(stderr, "%s: out of memory\n", program_name);
270                                 exit(1);
271                         }
272                         sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
273                         dumpv[i++] = t;
274                         dumplist = dumplist->ml_next;
275                 }
276         }
277         else if (dflag) {
278                 if (headers)
279                         printf("Directories on %s:\n", hostname);
280                 while (dumplist) {
281                         dumpv[i++] = dumplist->ml_directory;
282                         dumplist = dumplist->ml_next;
283                 }
284         }
285
286         qsort(dumpv, n, sizeof (char *), dump_cmp);
287         
288         for (i = 0; i < n; i++) {
289                 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
290                         printf("%s\n", dumpv[i]);
291         }
292         exit(0);
293 }
294