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