]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/showmount/showmount.c
showmount command: Remove unused local getport() implementation
[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 /*
82  *  Perform a non-blocking connect on the socket fd.
83  *
84  *  tout contains the timeout.  It will be modified to contain the time
85  *  remaining (i.e. time provided - time elasped).
86  *
87  *  Returns zero on success; otherwise, -1 is returned and errno is set
88  *  to reflect the nature of the error.
89  */
90 static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
91 {
92         int flags, ret;
93         socklen_t len;
94         fd_set rset;
95
96         flags = fcntl(fd, F_GETFL, 0);
97         if (flags < 0)
98                 return -1;
99
100         ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
101         if (ret < 0)
102                 return -1;
103
104         /*
105          * From here on subsequent sys calls could change errno so
106          * we set ret = -errno to capture it in case we decide to
107          * use it later.
108          */
109         len = sizeof(struct sockaddr);
110         ret = connect(fd, (struct sockaddr *)addr, len);
111         if (ret < 0 && errno != EINPROGRESS) {
112                 ret = -1;
113                 goto done;
114         }
115
116         if (ret == 0)
117                 goto done;
118
119         /* now wait */
120         FD_ZERO(&rset);
121         FD_SET(fd, &rset);
122
123         ret = select(fd + 1, NULL, &rset, NULL, tout);
124         if (ret <= 0) {
125                 if (ret == 0)
126                         errno = ETIMEDOUT;
127                 ret = -1;
128                 goto done;
129         }
130
131         if (FD_ISSET(fd, &rset)) {
132                 int status;
133
134                 len = sizeof(ret);
135                 status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
136                 if (status < 0) {
137                         ret = -1;
138                         goto done;
139                 }
140
141                 /* Oops - something wrong with connect */
142                 if (ret != 0) {
143                         errno = ret;
144                         ret = -1;
145                 }
146         }
147
148 done:
149         fcntl(fd, F_SETFL, flags);
150         return ret;
151 }
152
153 int main(int argc, char **argv)
154 {
155         char hostname_buf[MAXHOSTLEN];
156         char *hostname;
157         enum clnt_stat clnt_stat;
158         struct hostent *hp;
159         struct sockaddr_in server_addr;
160         int ret, msock;
161         struct timeval total_timeout;
162         struct timeval pertry_timeout;
163         int c;
164         CLIENT *mclient;
165         groups grouplist;
166         exports exportlist, exl;
167         mountlist dumplist;
168         mountlist list;
169         int i;
170         int n;
171         int maxlen;
172         char **dumpv;
173
174         program_name = argv[0];
175         while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
176                 switch (c) {
177                 case 'a':
178                         aflag = 1;
179                         break;
180                 case 'd':
181                         dflag = 1;
182                         break;
183                 case 'e':
184                         eflag = 1;
185                         break;
186                 case 'h':
187                         usage(stdout, 0);
188                         break;
189                 case 'v':
190                         printf("%s\n", version);
191                         exit(0);
192                 case 0:
193                         break;
194                 case '?':
195                 default:
196                         usage(stderr, 1);
197                         break;
198                 }
199         }
200         argc -= optind;
201         argv += optind;
202
203         switch (aflag + dflag + eflag) {
204         case 0:
205                 hflag = 1;
206                 break;
207         case 1:
208                 break;
209         default:
210                 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
211                         program_name);
212                 exit(1);
213                 break;
214         }
215
216         switch (argc) {
217         case 0:
218                 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
219                         perror("getting hostname");
220                         exit(1);
221                 }
222                 hostname = hostname_buf;
223                 break;
224         case 1:
225                 hostname = argv[0];
226                 break;
227         default:
228                 fprintf(stderr, "%s: only one hostname is allowed\n",
229                         program_name);
230                 exit(1);
231                 break;
232         }
233
234         if (inet_aton(hostname, &server_addr.sin_addr)) {
235                 server_addr.sin_family = AF_INET;
236         }
237         else {
238                 if ((hp = gethostbyname(hostname)) == NULL) {
239                         fprintf(stderr, "%s: can't get address for %s\n",
240                                 program_name, hostname);
241                         exit(1);
242                 }
243                 server_addr.sin_family = AF_INET;
244                 memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
245         }
246
247         /* create mount deamon client */
248
249         mclient = NULL;
250         msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
251         if (msock != -1) {
252                 if (nfs_getport_ping((struct sockaddr *)&server_addr,
253                                         sizeof(server_addr), MOUNTPROG,
254                                         MOUNTVERS, IPPROTO_TCP)) {
255                         ret = connect_nb(msock, &server_addr, 0);
256                         if (ret == 0) /* success */
257                                 mclient = clnttcp_create(&server_addr,
258                                                 MOUNTPROG, MOUNTVERS, &msock,
259                                                 0, 0);
260                         else
261                                 close(msock);
262                 } else
263                         close(msock);
264         }
265
266         if (!mclient) {
267                 if (nfs_getport_ping((struct sockaddr *)&server_addr,
268                                         sizeof(server_addr), MOUNTPROG,
269                                         MOUNTVERS, IPPROTO_UDP)) {
270                         clnt_pcreateerror("showmount");
271                         exit(1);
272                 }
273                 msock = RPC_ANYSOCK;
274                 pertry_timeout.tv_sec = TIMEOUT_UDP;
275                 pertry_timeout.tv_usec = 0;
276                 if ((mclient = clntudp_create(&server_addr,
277                     MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
278                         clnt_pcreateerror("mount clntudp_create");
279                         exit(1);
280                 }
281         }
282         mclient->cl_auth = authunix_create_default();
283         total_timeout.tv_sec = TOTAL_TIMEOUT;
284         total_timeout.tv_usec = 0;
285
286         if (eflag) {
287                 memset(&exportlist, '\0', sizeof(exportlist));
288
289                 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
290                         (xdrproc_t) xdr_void, NULL,
291                         (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
292                         total_timeout);
293                 if (clnt_stat != RPC_SUCCESS) {
294                         clnt_perror(mclient, "rpc mount export");
295                         clnt_destroy(mclient);
296                         exit(1);
297                 }
298                 if (headers)
299                         printf("Export list for %s:\n", hostname);
300                 maxlen = 0;
301                 for (exl = exportlist; exl; exl = exl->ex_next) {
302                         if ((n = strlen(exl->ex_dir)) > maxlen)
303                                 maxlen = n;
304                 }
305                 while (exportlist) {
306                         printf("%-*s ", maxlen, exportlist->ex_dir);
307                         grouplist = exportlist->ex_groups;
308                         if (grouplist)
309                                 while (grouplist) {
310                                         printf("%s%s", grouplist->gr_name,
311                                                 grouplist->gr_next ? "," : "");
312                                         grouplist = grouplist->gr_next;
313                                 }
314                         else
315                                 printf("(everyone)");
316                         printf("\n");
317                         exportlist = exportlist->ex_next;
318                 }
319                 clnt_destroy(mclient);
320                 exit(0);
321         }
322
323         memset(&dumplist, '\0', sizeof(dumplist));
324         clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
325                 (xdrproc_t) xdr_void, NULL,
326                 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
327                 total_timeout);
328         if (clnt_stat != RPC_SUCCESS) {
329                 clnt_perror(mclient, "rpc mount dump");
330                 clnt_destroy(mclient);
331                 exit(1);
332         }
333         clnt_destroy(mclient);
334
335         n = 0;
336         for (list = dumplist; list; list = list->ml_next)
337                 n++;
338         dumpv = (char **) calloc(n, sizeof (char *));
339         if (n && !dumpv) {
340                 fprintf(stderr, "%s: out of memory\n", program_name);
341                 exit(1);
342         }
343         i = 0;
344
345         if (hflag) {
346                 if (headers)
347                         printf("Hosts on %s:\n", hostname);
348                 while (dumplist) {
349                         dumpv[i++] = dumplist->ml_hostname;
350                         dumplist = dumplist->ml_next;
351                 }
352         }
353         else if (aflag) {
354                 if (headers)
355                         printf("All mount points on %s:\n", hostname);
356                 while (dumplist) {
357                         char *t;
358
359                         t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
360                         if (!t)
361                         {
362                                 fprintf(stderr, "%s: out of memory\n", program_name);
363                                 exit(1);
364                         }
365                         sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
366                         dumpv[i++] = t;
367                         dumplist = dumplist->ml_next;
368                 }
369         }
370         else if (dflag) {
371                 if (headers)
372                         printf("Directories on %s:\n", hostname);
373                 while (dumplist) {
374                         dumpv[i++] = dumplist->ml_directory;
375                         dumplist = dumplist->ml_next;
376                 }
377         }
378
379         qsort(dumpv, n, sizeof (char *), dump_cmp);
380         
381         for (i = 0; i < n; i++) {
382                 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
383                         printf("%s\n", dumpv[i]);
384         }
385         exit(0);
386 }
387