]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/showmount/showmount.c
213a573613c3215314e71543e9d762ad012f8f30
[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 #define TIMEOUT_UDP     3
41 #define TIMEOUT_TCP     10
42 #define TOTAL_TIMEOUT   20
43
44 static char *   version = "showmount for " VERSION;
45 static char *   program_name;
46 static int      headers = 1;
47 static int      hflag = 0;
48 static int      aflag = 0;
49 static int      dflag = 0;
50 static int      eflag = 0;
51
52 static struct option longopts[] =
53 {
54         { "all", 0, 0, 'a' },
55         { "directories", 0, 0, 'd' },
56         { "exports", 0, 0, 'e' },
57         { "no-headers", 0, &headers, 0 },
58         { "version", 0, 0, 'v' },
59         { "help", 0, 0, 'h' },
60         { NULL, 0, 0, 0 }
61 };
62
63 #define MAXHOSTLEN 256
64
65 static int dump_cmp(const void *pv, const void *qv)
66 {
67         const char **p = (const char **)pv;
68         const char **q = (const char **)qv;
69         return strcmp(*p, *q);
70 }
71
72 static void usage(FILE *fp, int n)
73 {
74         fprintf(fp, "Usage: %s [-adehv]\n", program_name);
75         fprintf(fp, "       [--all] [--directories] [--exports]\n");
76         fprintf(fp, "       [--no-headers] [--help] [--version] [host]\n");
77         exit(n);
78 }
79
80 /*
81  *  Perform a non-blocking connect on the socket fd.
82  *
83  *  tout contains the timeout.  It will be modified to contain the time
84  *  remaining (i.e. time provided - time elasped).
85  *
86  *  Returns 0 for success 
87  */
88 static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
89 {
90         int flags, ret;
91         socklen_t len;
92         fd_set rset;
93
94         flags = fcntl(fd, F_GETFL, 0);
95         if (flags < 0)
96                 return -1;
97
98         ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
99         if (ret < 0)
100                 return -1;
101
102         /*
103          * From here on subsequent sys calls could change errno so
104          * we set ret = -errno to capture it in case we decide to
105          * use it later.
106          */
107         len = sizeof(struct sockaddr);
108         ret = connect(fd, (struct sockaddr *)addr, len);
109         if (ret < 0 && errno != EINPROGRESS) {
110                 ret = -errno;
111                 goto done;
112         }
113
114         if (ret == 0)
115                 goto done;
116
117         /* now wait */
118         FD_ZERO(&rset);
119         FD_SET(fd, &rset);
120
121         ret = select(fd + 1, NULL, &rset, NULL, tout);
122         if (ret <= 0) {
123                 if (ret == 0)
124                         ret = -ETIMEDOUT;
125                 else
126                         ret = -errno;
127                 goto done;
128         }
129
130         if (FD_ISSET(fd, &rset)) {
131                 int status;
132
133                 len = sizeof(ret);
134                 status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
135                 if (status < 0) {
136                         ret = -errno;
137                         goto done;
138                 }
139
140                 /* Oops - something wrong with connect */
141                 if (ret)
142                         ret = -ret;
143         }
144
145 done:
146         fcntl(fd, F_SETFL, flags);
147         return ret;
148 }
149
150 static unsigned short getport(struct sockaddr_in *addr,
151                          unsigned long prog, unsigned long vers, int prot)
152 {
153         CLIENT *client;
154         enum clnt_stat status;
155         struct pmap parms;
156         int ret, sock;
157         struct sockaddr_in laddr, saddr;
158         struct timeval tout = {0, 0};
159         socklen_t len;
160         unsigned int send_sz = 0;
161         unsigned int recv_sz = 0;
162         unsigned short port;
163
164         memset(&laddr, 0, sizeof(laddr));
165         memset(&saddr, 0, sizeof(saddr));
166         memset(&parms, 0, sizeof(parms));
167
168         memcpy(&saddr, addr, sizeof(saddr));
169         saddr.sin_port = htons(PMAPPORT);
170
171         if (prot == IPPROTO_TCP) {
172                 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
173                 if (sock == -1) {
174                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
175                         rpc_createerr.cf_error.re_errno = errno;
176                         return 0;
177                 }
178
179                 tout.tv_sec = TIMEOUT_TCP;
180
181                 ret = connect_nb(sock, &saddr, &tout);
182                 if (ret < 0) {
183                         close(sock);
184                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
185                         rpc_createerr.cf_error.re_errno = errno;
186                         return 0;
187                 }
188                 client = clnttcp_create(&saddr,
189                                         PMAPPROG, PMAPVERS, &sock,
190                                         0, 0);
191         } else {
192                 /*
193                  * bind to any unused port.  If we left this up to the rpc
194                  * layer, it would bind to a reserved port, which has been shown
195                  * to exhaust the reserved port range in some situations.
196                  */
197                 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
198                 if (sock == -1) {
199                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
200                         rpc_createerr.cf_error.re_errno = errno;
201                         return 0;
202                 }
203
204                 laddr.sin_family = AF_INET;
205                 laddr.sin_port = 0;
206                 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
207
208                 tout.tv_sec = TIMEOUT_UDP;
209
210                 send_sz = RPCSMALLMSGSIZE;
211                 recv_sz = RPCSMALLMSGSIZE;
212
213                 len = sizeof(struct sockaddr_in);
214                 if (bind(sock, (struct sockaddr *)&laddr, len) < 0) {
215                         close(sock);
216                         sock = RPC_ANYSOCK;
217                         /* FALLTHROUGH */
218                 }
219                 client = clntudp_bufcreate(&saddr, PMAPPROG, PMAPVERS,
220                                            tout, &sock, send_sz, recv_sz);
221         }
222
223         if (!client) {
224                 close(sock);
225                 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
226                 return 0;
227         }
228
229         clnt_control(client, CLSET_FD_CLOSE, NULL);
230
231         parms.pm_prog = prog;
232         parms.pm_vers = vers;
233         parms.pm_prot = prot;
234
235         status = clnt_call(client, PMAPPROC_GETPORT,
236                            (xdrproc_t) xdr_pmap, (caddr_t) &parms,
237                            (xdrproc_t) xdr_u_short, (caddr_t) &port,
238                            tout);
239
240         if (status != RPC_SUCCESS) {
241                 clnt_geterr(client, &rpc_createerr.cf_error);
242                 rpc_createerr.cf_stat = status;
243                 clnt_destroy(client);
244                 return 0;
245         } else if (port == 0) {
246                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
247         }
248
249         clnt_destroy(client);
250
251         return htons(port);
252 }
253
254 int main(int argc, char **argv)
255 {
256         char hostname_buf[MAXHOSTLEN];
257         char *hostname;
258         enum clnt_stat clnt_stat;
259         struct hostent *hp;
260         struct sockaddr_in server_addr;
261         int ret, msock;
262         struct timeval total_timeout;
263         struct timeval pertry_timeout;
264         int c;
265         CLIENT *mclient;
266         groups grouplist;
267         exports exportlist, exl;
268         mountlist dumplist;
269         mountlist list;
270         int i;
271         int n;
272         int maxlen;
273         char **dumpv;
274
275         program_name = argv[0];
276         while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
277                 switch (c) {
278                 case 'a':
279                         aflag = 1;
280                         break;
281                 case 'd':
282                         dflag = 1;
283                         break;
284                 case 'e':
285                         eflag = 1;
286                         break;
287                 case 'h':
288                         usage(stdout, 0);
289                         break;
290                 case 'v':
291                         printf("%s\n", version);
292                         exit(0);
293                 case 0:
294                         break;
295                 case '?':
296                 default:
297                         usage(stderr, 1);
298                         break;
299                 }
300         }
301         argc -= optind;
302         argv += optind;
303
304         switch (aflag + dflag + eflag) {
305         case 0:
306                 hflag = 1;
307                 break;
308         case 1:
309                 break;
310         default:
311                 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
312                         program_name);
313                 exit(1);
314                 break;
315         }
316
317         switch (argc) {
318         case 0:
319                 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
320                         perror("getting hostname");
321                         exit(1);
322                 }
323                 hostname = hostname_buf;
324                 break;
325         case 1:
326                 hostname = argv[0];
327                 break;
328         default:
329                 fprintf(stderr, "%s: only one hostname is allowed\n",
330                         program_name);
331                 exit(1);
332                 break;
333         }
334
335         if (inet_aton(hostname, &server_addr.sin_addr)) {
336                 server_addr.sin_family = AF_INET;
337         }
338         else {
339                 if ((hp = gethostbyname(hostname)) == NULL) {
340                         fprintf(stderr, "%s: can't get address for %s\n",
341                                 program_name, hostname);
342                         exit(1);
343                 }
344                 server_addr.sin_family = AF_INET;
345                 memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
346         }
347
348         /* create mount deamon client */
349
350         mclient = NULL;
351         msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
352         if (msock != -1) {
353                 server_addr.sin_port = getport(&server_addr,
354                                          MOUNTPROG, MOUNTVERS, IPPROTO_TCP);
355                 if (server_addr.sin_port) {
356                         ret = connect_nb(msock, &server_addr, 0);
357                         if (ret == 0) /* success */
358                                 mclient = clnttcp_create(&server_addr,
359                                                 MOUNTPROG, MOUNTVERS, &msock,
360                                                 0, 0);
361                         else
362                                 close(msock);
363                 } else
364                         close(msock);
365         }
366
367         if (!mclient) {
368                 server_addr.sin_port = getport(&server_addr,
369                                          MOUNTPROG, MOUNTVERS, IPPROTO_UDP);
370                 if (!server_addr.sin_port) {
371                         clnt_pcreateerror("showmount");
372                         exit(1);
373                 }
374                 msock = RPC_ANYSOCK;
375                 pertry_timeout.tv_sec = TIMEOUT_UDP;
376                 pertry_timeout.tv_usec = 0;
377                 if ((mclient = clntudp_create(&server_addr,
378                     MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
379                         clnt_pcreateerror("mount clntudp_create");
380                         exit(1);
381                 }
382         }
383         mclient->cl_auth = authunix_create_default();
384         total_timeout.tv_sec = TOTAL_TIMEOUT;
385         total_timeout.tv_usec = 0;
386
387         if (eflag) {
388                 memset(&exportlist, '\0', sizeof(exportlist));
389
390                 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
391                         (xdrproc_t) xdr_void, NULL,
392                         (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
393                         total_timeout);
394                 if (clnt_stat != RPC_SUCCESS) {
395                         clnt_perror(mclient, "rpc mount export");
396                         exit(1);
397                 }
398                 if (headers)
399                         printf("Export list for %s:\n", hostname);
400                 maxlen = 0;
401                 for (exl = exportlist; exl; exl = exl->ex_next) {
402                         if ((n = strlen(exl->ex_dir)) > maxlen)
403                                 maxlen = n;
404                 }
405                 while (exportlist) {
406                         printf("%-*s ", maxlen, exportlist->ex_dir);
407                         grouplist = exportlist->ex_groups;
408                         if (grouplist)
409                                 while (grouplist) {
410                                         printf("%s%s", grouplist->gr_name,
411                                                 grouplist->gr_next ? "," : "");
412                                         grouplist = grouplist->gr_next;
413                                 }
414                         else
415                                 printf("(everyone)");
416                         printf("\n");
417                         exportlist = exportlist->ex_next;
418                 }
419                 exit(0);
420         }
421
422         memset(&dumplist, '\0', sizeof(dumplist));
423         clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
424                 (xdrproc_t) xdr_void, NULL,
425                 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
426                 total_timeout);
427         if (clnt_stat != RPC_SUCCESS) {
428                 clnt_perror(mclient, "rpc mount dump");
429                 exit(1);
430         }
431
432         n = 0;
433         for (list = dumplist; list; list = list->ml_next)
434                 n++;
435         dumpv = (char **) calloc(n, sizeof (char *));
436         if (n && !dumpv) {
437                 fprintf(stderr, "%s: out of memory\n", program_name);
438                 exit(1);
439         }
440         i = 0;
441
442         if (hflag) {
443                 if (headers)
444                         printf("Hosts on %s:\n", hostname);
445                 while (dumplist) {
446                         dumpv[i++] = dumplist->ml_hostname;
447                         dumplist = dumplist->ml_next;
448                 }
449         }
450         else if (aflag) {
451                 if (headers)
452                         printf("All mount points on %s:\n", hostname);
453                 while (dumplist) {
454                         char *t;
455
456                         t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
457                         if (!t)
458                         {
459                                 fprintf(stderr, "%s: out of memory\n", program_name);
460                                 exit(1);
461                         }
462                         sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
463                         dumpv[i++] = t;
464                         dumplist = dumplist->ml_next;
465                 }
466         }
467         else if (dflag) {
468                 if (headers)
469                         printf("Directories on %s:\n", hostname);
470                 while (dumplist) {
471                         dumpv[i++] = dumplist->ml_directory;
472                         dumplist = dumplist->ml_next;
473                 }
474         }
475
476         qsort(dumpv, n, sizeof (char *), dump_cmp);
477         
478         for (i = 0; i < n; i++) {
479                 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
480                         printf("%s\n", dumpv[i]);
481         }
482         exit(0);
483 }
484