* utils/showmount/showmount.c (main): Fix inet_ntoa() parameter.
[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 #include "config.h"
17
18 #include <stdio.h>
19 #include <rpc/rpc.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <sys/time.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <memory.h>
27 #include <stdlib.h>
28
29 #include <netdb.h>
30 #include <arpa/inet.h>
31 #include <errno.h>
32 #include <getopt.h>
33 #include <mount.h>
34 #include <unistd.h>
35
36 static char *   version = "showmount for " VERSION;
37 static char *   program_name;
38 static int      headers = 1;
39 static int      hflag = 0;
40 static int      aflag = 0;
41 static int      dflag = 0;
42 static int      eflag = 0;
43
44 static struct option longopts[] =
45 {
46         { "all", 0, 0, 'a' },
47         { "directories", 0, 0, 'd' },
48         { "exports", 0, 0, 'e' },
49         { "no-headers", 0, &headers, 0 },
50         { "version", 0, 0, 'v' },
51         { "help", 0, 0, 'h' },
52         { NULL, 0, 0, 0 }
53 };
54
55 #define MAXHOSTLEN 256
56
57 int dump_cmp(p, q)
58 char **p;
59 char **q;
60 {
61         return strcmp(*p, *q);
62 }
63
64 static void usage(fp, n)
65 FILE *fp;
66 int n;
67 {
68         fprintf(fp, "Usage: %s [-adehv]\n", program_name);
69         fprintf(fp, "       [--all] [--directories] [--exports]\n");
70         fprintf(fp, "       [--no-headers] [--help] [--version] [host]\n");
71         exit(n);
72 }
73
74 int main(argc, argv)
75 int argc;
76 char **argv;
77 {
78         char hostname_buf[MAXHOSTLEN];
79         char *hostname;
80         enum clnt_stat clnt_stat;
81         struct hostent *hp;
82         struct sockaddr_in server_addr;
83         int msock;
84         struct timeval total_timeout;
85         struct timeval pertry_timeout;
86         int c;
87         CLIENT *mclient;
88         groups grouplist;
89         exports exportlist, exl;
90         mountlist dumplist;
91         mountlist list;
92         int i;
93         int n;
94         int maxlen;
95         char **dumpv;
96
97         program_name = argv[0];
98         while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
99                 switch (c) {
100                 case 'a':
101                         aflag = 1;
102                         break;
103                 case 'd':
104                         dflag = 1;
105                         break;
106                 case 'e':
107                         eflag = 1;
108                         break;
109                 case 'h':
110                         usage(stdout, 0);
111                         break;
112                 case 'v':
113                         printf("%s\n", version);
114                         exit(0);
115                 case 0:
116                         break;
117                 case '?':
118                 default:
119                         usage(stderr, 1);
120                         break;
121                 }
122         }
123         argc -= optind;
124         argv += optind;
125
126         switch (aflag + dflag + eflag) {
127         case 0:
128                 hflag = 1;
129                 break;
130         case 1:
131                 break;
132         default:
133                 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
134                         program_name);
135                 exit(1);
136                 break;
137         }
138
139         switch (argc) {
140         case 0:
141                 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
142                         perror("getting hostname");
143                         exit(1);
144                 }
145                 hostname = hostname_buf;
146                 break;
147         case 1:
148                 hostname = argv[0];
149                 break;
150         default:
151                 fprintf(stderr, "%s: only one hostname is allowed\n",
152                         program_name);
153                 exit(1);
154                 break;
155         }
156
157         if (inet_aton(hostname, &server_addr.sin_addr)) {
158                 server_addr.sin_family = AF_INET;
159         }
160         else {
161                 if ((hp = gethostbyname(hostname)) == NULL) {
162                         fprintf(stderr, "%s: can't get address for %s\n",
163                                 program_name, hostname);
164                         exit(1);
165                 }
166                 server_addr.sin_family = AF_INET;
167                 memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
168         }
169
170         /* create mount deamon client */
171
172         server_addr.sin_port = 0;
173         msock = RPC_ANYSOCK;
174         if ((mclient = clnttcp_create(&server_addr,
175             MOUNTPROG, MOUNTVERS, &msock, 0, 0)) == NULL) {
176                 server_addr.sin_port = 0;
177                 msock = RPC_ANYSOCK;
178                 pertry_timeout.tv_sec = 3;
179                 pertry_timeout.tv_usec = 0;
180                 if ((mclient = clntudp_create(&server_addr,
181                     MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
182                         clnt_pcreateerror("mount clntudp_create");
183                         exit(1);
184                 }
185         }
186         mclient->cl_auth = authunix_create_default();
187         total_timeout.tv_sec = 20;
188         total_timeout.tv_usec = 0;
189
190         if (eflag) {
191                 memset(&exportlist, '\0', sizeof(exportlist));
192                 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
193                         (xdrproc_t) xdr_void, NULL,
194                         (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
195                         total_timeout);
196                 if (clnt_stat != RPC_SUCCESS) {
197                         clnt_perror(mclient, "rpc mount export");
198                         exit(1);
199                 }
200                 if (headers)
201                         printf("Export list for %s:\n", hostname);
202                 maxlen = 0;
203                 for (exl = exportlist; exl; exl = exl->ex_next) {
204                         if ((n = strlen(exl->ex_dir)) > maxlen)
205                                 maxlen = n;
206                 }
207                 while (exportlist) {
208                         printf("%-*s ", maxlen, exportlist->ex_dir);
209                         grouplist = exportlist->ex_groups;
210                         if (grouplist)
211                                 while (grouplist) {
212                                         printf("%s%s", grouplist->gr_name,
213                                                 grouplist->gr_next ? "," : "");
214                                         grouplist = grouplist->gr_next;
215                                 }
216                         else
217                                 printf("(everyone)");
218                         printf("\n");
219                         exportlist = exportlist->ex_next;
220                 }
221                 exit(0);
222         }
223
224         memset(&dumplist, '\0', sizeof(dumplist));
225         clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
226                 (xdrproc_t) xdr_void, NULL,
227                 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
228                 total_timeout);
229         if (clnt_stat != RPC_SUCCESS) {
230                 clnt_perror(mclient, "rpc mount dump");
231                 exit(1);
232         }
233
234         n = 0;
235         for (list = dumplist; list; list = list->ml_next)
236                 n++;
237         dumpv = (char **) calloc(n, sizeof (char *));
238         if (n && !dumpv) {
239                 fprintf(stderr, "%s: out of memory\n", program_name);
240                 exit(1);
241         }
242         i = 0;
243
244         if (hflag) {
245                 if (headers)
246                         printf("Hosts on %s:\n", hostname);
247                 while (dumplist) {
248                         dumpv[i++] = dumplist->ml_hostname;
249                         dumplist = dumplist->ml_next;
250                 }
251         }
252         else if (aflag) {
253                 if (headers)
254                         printf("All mount points on %s:\n", hostname);
255                 while (dumplist) {
256                         char *t;
257
258                         t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
259                         if (!t)
260                         {
261                                 fprintf(stderr, "%s: out of memory\n", program_name);
262                                 exit(1);
263                         }
264                         sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
265                         dumpv[i++] = t;
266                         dumplist = dumplist->ml_next;
267                 }
268         }
269         else if (dflag) {
270                 if (headers)
271                         printf("Directories on %s:\n", hostname);
272                 while (dumplist) {
273                         dumpv[i++] = dumplist->ml_directory;
274                         dumplist = dumplist->ml_next;
275                 }
276         }
277
278         qsort(dumpv, n, sizeof (char *), dump_cmp);
279         
280         for (i = 0; i < n; i++) {
281                 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
282                         printf("%s\n", dumpv[i]);
283         }
284         exit(0);
285 }
286