]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/exportfs/exportfs.c
call gethostbyaddr to make sure that we have canonical hostname
[nfs-utils.git] / utils / exportfs / exportfs.c
1 /*
2  * utils/exportfs/exportfs.c
3  *
4  * Export file systems to knfsd
5  *
6  * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
7  *
8  * Extensive changes, 1999, Neil Brown <neilb@cse.unsw.edu.au>
9  */
10
11 #include "config.h"
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include <getopt.h>
17 #include <netdb.h>
18 #include <errno.h>
19 #include "xmalloc.h"
20 #include "misc.h"
21 #include "nfslib.h"
22 #include "exportfs.h"
23 #include "xmalloc.h"
24 #include "xlog.h"
25
26 static void     export_all(int verbose);
27 static void     unexport_all(int verbose);
28 static void     exportfs(char *arg, char *options, int verbose);
29 static void     unexportfs(char *arg, int verbose);
30 static void     exports_update(int verbose);
31 static void     dump(int verbose);
32 static void     error(nfs_export *exp, int err);
33 static void     usage(void);
34
35
36 int
37 main(int argc, char **argv)
38 {
39         char    *options = NULL;
40         int     f_export = 1;
41         int     f_all = 0;
42         int     f_verbose = 0;
43         int     f_reexport = 0;
44         int     f_ignore = 0;
45         int     i, c;
46
47         xlog_open("exportfs");
48
49         while ((c = getopt(argc, argv, "aio:ruv")) != EOF) {
50                 switch(c) {
51                 case 'a':
52                         f_all = 1;
53                         break;
54                 case 'i':
55                         f_ignore = 1;
56                         break;
57                 case 'o':
58                         options = optarg;
59                         break;
60                 case 'r':
61                         f_reexport = 1;
62                         f_all = 1;
63                         break;
64                 case 'u':
65                         f_export = 0;
66                         break;
67                 case 'v':
68                         f_verbose = 1;
69                         break;
70                 default:
71                         usage();
72                         break;
73                 }
74         }
75
76         if (optind != argc && f_all) {
77                 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
78                 return 1;
79         }
80         if (f_ignore && (f_all || ! f_export)) {
81                 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
82                 return 1;
83         }
84         if (f_reexport && ! f_export) {
85                 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
86                 return 1;
87         }
88         if (optind == argc && ! f_all) {
89                 xtab_export_read();
90                 dump(f_verbose);
91                 return 0;
92         }
93
94         if (f_export && ! f_ignore)
95                 export_read(_PATH_EXPORTS);
96         if (f_export) {
97                 if (f_all)
98                         export_all(f_verbose);
99                 else
100                         for (i = optind; i < argc ; i++)
101                                 exportfs(argv[i], options, f_verbose);
102         }
103         /* note: xtab_*_read does not update entries if they already exist,
104          * so this will not lose new options
105          */
106         if (!f_reexport)
107                 xtab_export_read();
108         if (!f_export) {
109                 if (f_all)
110                         unexport_all(f_verbose);
111                 else
112                         for (i = optind ; i < argc ; i++)
113                                 unexportfs(argv[i], f_verbose);
114         }
115         rmtab_read();
116         xtab_mount_read();
117         exports_update(f_verbose);
118         xtab_export_write();
119         xtab_mount_write();
120
121         return 0;
122 }
123
124 /* we synchronise intention with reality.
125  * entries with m_mayexport get exported
126  * entries with m_exported but not m_mayexport get unexported
127  * looking at m_client->m_type == MCL_FQDN only
128  */
129 static void
130 exports_update(int verbose)
131 {
132         nfs_export      *exp;
133
134         for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
135                 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
136                         if (verbose)
137                                 printf("%sexporting %s:%s to kernel\n",
138                                        exp->m_exported ?"re":"",
139                                        exp->m_client->m_hostname,
140                                        exp->m_export.e_path);
141                         if (!export_export(exp))
142                                 error(exp, errno);
143                 }
144                 if (exp->m_exported && ! exp->m_mayexport) {
145                         if (verbose)
146                                 printf("unexporting %s:%s from kernel\n",
147                                        exp->m_client->m_hostname,
148                                        exp->m_export.e_path);
149                         if (!export_unexport(exp))
150                                 error(exp, errno);
151                 }
152         }
153 }
154                         
155 /*
156  * export_all finds all entries and
157  *    marks them xtabent and mayexport so that they get exported
158  */
159 static void
160 export_all(int verbose)
161 {
162         nfs_export      *exp;
163         int             i;
164
165         for (i = 0; i < MCL_MAXTYPES; i++) {
166                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
167                         if (verbose)
168                                 printf("exporting %s:%s\n",
169                                        exp->m_client->m_hostname, 
170                                        exp->m_export.e_path);
171                         exp->m_xtabent = 1;
172                         exp->m_mayexport = 1;
173                         exp->m_changed = 1;
174                 }
175         }
176 }
177 /*
178  * unexport_all finds all entries that are mayexport, and
179  *    marks them not xtabent and not mayexport
180  */
181 static void
182 unexport_all(int verbose)
183 {
184         nfs_export      *exp;
185         int             i;
186
187         for (i = 0; i < MCL_MAXTYPES; i++) {
188                 for (exp = exportlist[i]; exp; exp = exp->m_next)
189                         if (exp->m_mayexport) {
190                                 if (verbose) {
191                                         if (exp->m_exported) {
192                                                 printf("unexporting %s:%s from kernel\n",
193                                                        exp->m_client->m_hostname,
194                                                        exp->m_export.e_path);
195                                         }
196                                         else {
197                                                 printf("unexporting %s:%s\n",
198                                                         exp->m_client->m_hostname, 
199                                                         exp->m_export.e_path);
200                                         }
201                                 }
202                                 if (exp->m_exported && !export_unexport(exp))
203                                         error(exp, errno);
204                                 exp->m_xtabent = 0;
205                                 exp->m_mayexport = 0;
206                         }
207         }
208 }
209
210
211 static void
212 exportfs(char *arg, char *options, int verbose)
213 {
214         struct exportent *eep;
215         nfs_export      *exp;
216         struct hostent  *hp = NULL;
217         char            *path;
218         char            *hname = arg;
219         int             htype;
220
221         if ((path = strchr(arg, ':')) != NULL)
222                 *path++ = '\0';
223
224         if (!path || *path != '/') {
225                 fprintf(stderr, "Invalid exporting option: %s\n", arg);
226                 return;
227         }
228
229         if ((htype = client_gettype(hname)) == MCL_FQDN &&
230             (hp = gethostbyname(hname)) != NULL) {
231                 struct hostent *hp2 = hostent_dup (hp);
232                 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
233                                    hp2->h_addrtype);
234                 if (hp) {
235                         free(hp2);
236                         hp = hostent_dup(hp);
237                 } else
238                         hp = hp2;
239                 exp = export_find(hp, path);
240         } else {
241                 exp = export_lookup(hname, path);
242         }
243
244         if (!exp) {
245                 if (!(eep = mkexportent(hname, path, options)) ||
246                     !(exp = export_create(eep))) {
247                         if (hp) free (hp);
248                         return;
249                 }
250         } else if (!updateexportent(&exp->m_export, options)) {
251                 if (hp) free (hp);
252                 return;
253         }
254
255         if (verbose)
256                 printf("exporting %s:%s\n", exp->m_client->m_hostname, 
257                         exp->m_export.e_path);
258         exp->m_xtabent = 1;
259         exp->m_mayexport = 1;
260         exp->m_changed = 1;
261         if (hp) free (hp);
262 }
263
264 static void
265 unexportfs(char *arg, int verbose)
266 {
267         nfs_export      *exp;
268         struct hostent  *hp = NULL;
269         char            *path;
270         char            *hname = arg;
271         int             htype;
272
273         if ((path = strchr(arg, ':')) != NULL)
274                 *path++ = '\0';
275
276         if (!path || *path != '/') {
277                 fprintf(stderr, "Invalid unexporting option: %s\n",
278                         arg);
279                 return;
280         }
281
282         if ((htype = client_gettype(hname)) == MCL_FQDN) {
283                 if ((hp = gethostbyname(hname)) != 0) {
284                         hp = hostent_dup (hp);
285                         hname = (char *) hp->h_name;
286                 }
287         }
288
289         for (exp = exportlist[htype]; exp; exp = exp->m_next) {
290                 if (path && strcmp(path, exp->m_export.e_path))
291                         continue;
292                 if (htype != exp->m_client->m_type
293                     || (htype == MCL_FQDN
294                         && !matchhostname(exp->m_export.e_hostname,
295                                           hname)))
296                         continue;
297                 if (verbose) {
298                         if (exp->m_exported) {
299                                 printf("unexporting %s:%s from kernel\n",
300                                        exp->m_client->m_hostname,
301                                        exp->m_export.e_path);
302                         }
303                         else {
304                                 printf("unexporting %s:%s\n",
305                                         exp->m_client->m_hostname, 
306                                         exp->m_export.e_path);
307                         }
308                 }
309                 if (exp->m_exported && !export_unexport(exp))
310                         error(exp, errno);
311                 exp->m_xtabent = 0;
312                 exp->m_mayexport = 0;
313         }
314
315         if (hp) free (hp);
316 }
317
318 static char
319 dumpopt(char c, char *fmt, ...)
320 {
321         va_list ap;
322
323         va_start(ap, fmt);
324         printf("%c", c);
325         vprintf(fmt, ap);
326         va_end(ap);
327         return ',';
328 }
329
330 static void
331 dump(int verbose)
332 {
333         nfs_export      *exp;
334         struct exportent *ep;
335         int             htype;
336         char            *hname, c;
337
338         for (htype = 0; htype < MCL_MAXTYPES; htype++) {
339                 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
340                         ep = &exp->m_export;
341                         if (!exp->m_xtabent)
342                             continue; /* neilb */
343                         if (htype == MCL_ANONYMOUS)
344                                 hname = "<world>";
345                         else
346                                 hname = ep->e_hostname;
347                         if (strlen(ep->e_path) > 14)
348                                 printf("%-14s\n\t\t%s", ep->e_path, hname);
349                         else
350                                 printf("%-14s\t%s", ep->e_path, hname);
351                         if (!verbose) {
352                                 printf("\n");
353                                 continue;
354                         }
355                         c = '(';
356                         if (ep->e_flags & NFSEXP_READONLY)
357                                 c = dumpopt(c, "ro");
358                         else
359                                 c = dumpopt(c, "rw");
360                         if (ep->e_flags & NFSEXP_ASYNC)
361                                 c = dumpopt(c, "async");
362                         if (ep->e_flags & NFSEXP_GATHERED_WRITES)
363                                 c = dumpopt(c, "wdelay");
364                         if (ep->e_flags & NFSEXP_CROSSMNT)
365                                 c = dumpopt(c, "nohide");
366                         if (ep->e_flags & NFSEXP_INSECURE_PORT)
367                                 c = dumpopt(c, "insecure");
368                         if (ep->e_flags & NFSEXP_ROOTSQUASH)
369                                 c = dumpopt(c, "root_squash");
370                         else
371                                 c = dumpopt(c, "no_root_squash");
372                         if (ep->e_flags & NFSEXP_ALLSQUASH)
373                                 c = dumpopt(c, "all_squash");
374                         if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
375                                 c = dumpopt(c, "no_subtree_check");
376                         if (ep->e_flags & NFSEXP_NOAUTHNLM)
377                                 c = dumpopt(c, "insecure_locks");
378                         if (ep->e_maptype == CLE_MAP_UGIDD)
379                                 c = dumpopt(c, "mapping=ugidd");
380                         else if (ep->e_maptype == CLE_MAP_FILE)
381                                 c = dumpopt(c, "mapping=file");
382                         if (ep->e_anonuid != -2)
383                                 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
384                         if (ep->e_anongid != -2)
385                                 c = dumpopt(c, "anongid=%d", ep->e_anongid);
386
387                         printf("%c\n", (c != '(')? ')' : ' ');
388                 }
389         }
390 }
391
392 static void
393 error(nfs_export *exp, int err)
394 {
395         fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname, 
396                 exp->m_export.e_path, strerror(err));
397 }
398
399 static void
400 usage(void)
401 {
402         fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");
403         exit(1);
404 }