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