Change CROSSMNT to CROSSMOUNT
[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                 rmtab_read();
134         }
135         if (!new_cache) {
136                 xtab_mount_read();
137                 exports_update(f_verbose);
138         }
139         xtab_export_write();
140         if (new_cache)
141                 cache_flush(force_flush);
142         if (!new_cache)
143                 xtab_mount_write();
144
145         return export_errno;
146 }
147
148 /* we synchronise intention with reality.
149  * entries with m_mayexport get exported
150  * entries with m_exported but not m_mayexport get unexported
151  * looking at m_client->m_type == MCL_FQDN only
152  */
153 static void
154 exports_update(int verbose)
155 {
156         nfs_export      *exp;
157
158         for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
159                 /* check mountpoint option */
160                 if (exp->m_mayexport && 
161                     exp->m_export.e_mountpoint &&
162                     !is_mountpoint(exp->m_export.e_mountpoint[0]?
163                                    exp->m_export.e_mountpoint:
164                                    exp->m_export.e_path)) {
165                         printf("%s not exported as %s not a mountpoint.\n",
166                                exp->m_export.e_path, exp->m_export.e_mountpoint);
167                         exp->m_mayexport = 0;
168                 }
169                 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
170                         if (verbose)
171                                 printf("%sexporting %s:%s to kernel\n",
172                                        exp->m_exported ?"re":"",
173                                        exp->m_client->m_hostname,
174                                        exp->m_export.e_path);
175                         if (!export_export(exp))
176                                 error(exp, errno);
177                 }
178                 if (exp->m_exported && ! exp->m_mayexport) {
179                         if (verbose)
180                                 printf("unexporting %s:%s from kernel\n",
181                                        exp->m_client->m_hostname,
182                                        exp->m_export.e_path);
183                         if (!export_unexport(exp))
184                                 error(exp, errno);
185                 }
186         }
187 }
188                         
189 /*
190  * export_all finds all entries and
191  *    marks them xtabent and mayexport so that they get exported
192  */
193 static void
194 export_all(int verbose)
195 {
196         nfs_export      *exp;
197         int             i;
198
199         for (i = 0; i < MCL_MAXTYPES; i++) {
200                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
201                         if (verbose)
202                                 printf("exporting %s:%s\n",
203                                        exp->m_client->m_hostname, 
204                                        exp->m_export.e_path);
205                         exp->m_xtabent = 1;
206                         exp->m_mayexport = 1;
207                         exp->m_changed = 1;
208                 }
209         }
210 }
211
212
213 static void
214 exportfs(char *arg, char *options, int verbose)
215 {
216         struct exportent *eep;
217         nfs_export      *exp;
218         struct hostent  *hp = NULL;
219         char            *path;
220         char            *hname = arg;
221         int             htype;
222
223         if ((path = strchr(arg, ':')) != NULL)
224                 *path++ = '\0';
225
226         if (!path || *path != '/') {
227                 fprintf(stderr, "Invalid exporting option: %s\n", arg);
228                 return;
229         }
230
231         if ((htype = client_gettype(hname)) == MCL_FQDN &&
232             (hp = gethostbyname(hname)) != NULL) {
233                 struct hostent *hp2 = hostent_dup (hp);
234                 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
235                                    hp2->h_addrtype);
236                 if (hp) {
237                         free(hp2);
238                         hp = hostent_dup(hp);
239                 } else
240                         hp = hp2;
241                 exp = export_find(hp, path);
242                 hname = hp->h_name;
243         } else {
244                 exp = export_lookup(hname, path, 0);
245         }
246
247         if (!exp) {
248                 if (!(eep = mkexportent(hname, path, options)) ||
249                     !(exp = export_create(eep, 0))) {
250                         if (hp) free (hp);
251                         return;
252                 }
253         } else if (!updateexportent(&exp->m_export, options)) {
254                 if (hp) free (hp);
255                 return;
256         }
257
258         if (verbose)
259                 printf("exporting %s:%s\n", exp->m_client->m_hostname, 
260                         exp->m_export.e_path);
261         exp->m_xtabent = 1;
262         exp->m_mayexport = 1;
263         exp->m_changed = 1;
264         if (hp) free (hp);
265 }
266
267 static void
268 unexportfs(char *arg, int verbose)
269 {
270         nfs_export      *exp;
271         struct hostent  *hp = NULL;
272         char            *path;
273         char            *hname = arg;
274         int             htype;
275
276         if ((path = strchr(arg, ':')) != NULL)
277                 *path++ = '\0';
278
279         if (!path || *path != '/') {
280                 fprintf(stderr, "Invalid unexporting option: %s\n",
281                         arg);
282                 return;
283         }
284
285         if ((htype = client_gettype(hname)) == MCL_FQDN) {
286                 if ((hp = gethostbyname(hname)) != 0) {
287                         hp = hostent_dup (hp);
288                         hname = (char *) hp->h_name;
289                 }
290         }
291
292         for (exp = exportlist[htype]; exp; exp = exp->m_next) {
293                 if (path && strcmp(path, exp->m_export.e_path))
294                         continue;
295                 if (htype != exp->m_client->m_type)
296                         continue;
297                 if (htype == MCL_FQDN
298                     && !matchhostname(exp->m_export.e_hostname,
299                                           hname))
300                         continue;
301                 if (htype != MCL_FQDN
302                     && strcasecmp(exp->m_export.e_hostname, hname))
303                         continue;
304                 if (verbose) {
305 #if 0
306                         if (exp->m_exported) {
307                                 printf("unexporting %s:%s from kernel\n",
308                                        exp->m_client->m_hostname,
309                                        exp->m_export.e_path);
310                         }
311                         else
312 #endif
313                                 printf("unexporting %s:%s\n",
314                                         exp->m_client->m_hostname, 
315                                         exp->m_export.e_path);
316                 }
317 #if 0
318                 if (exp->m_exported && !export_unexport(exp))
319                         error(exp, errno);
320 #endif
321                 exp->m_xtabent = 0;
322                 exp->m_mayexport = 0;
323         }
324
325         if (hp) free (hp);
326 }
327
328 static char
329 dumpopt(char c, char *fmt, ...)
330 {
331         va_list ap;
332
333         va_start(ap, fmt);
334         printf("%c", c);
335         vprintf(fmt, ap);
336         va_end(ap);
337         return ',';
338 }
339
340 static void
341 dump(int verbose)
342 {
343         nfs_export      *exp;
344         struct exportent *ep;
345         int             htype;
346         char            *hname, c;
347
348         for (htype = 0; htype < MCL_MAXTYPES; htype++) {
349                 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
350                         ep = &exp->m_export;
351                         if (!exp->m_xtabent)
352                             continue; /* neilb */
353                         if (htype == MCL_ANONYMOUS)
354                                 hname = "<world>";
355                         else
356                                 hname = ep->e_hostname;
357                         if (strlen(ep->e_path) > 14)
358                                 printf("%-14s\n\t\t%s", ep->e_path, hname);
359                         else
360                                 printf("%-14s\t%s", ep->e_path, hname);
361                         if (!verbose) {
362                                 printf("\n");
363                                 continue;
364                         }
365                         c = '(';
366                         if (ep->e_flags & NFSEXP_READONLY)
367                                 c = dumpopt(c, "ro");
368                         else
369                                 c = dumpopt(c, "rw");
370                         if (ep->e_flags & NFSEXP_ASYNC)
371                                 c = dumpopt(c, "async");
372                         if (ep->e_flags & NFSEXP_GATHERED_WRITES)
373                                 c = dumpopt(c, "wdelay");
374                         if (ep->e_flags & NFSEXP_NOHIDE)
375                                 c = dumpopt(c, "nohide");
376                         if (ep->e_flags & NFSEXP_CROSSMOUNT)
377                                 c = dumpopt(c, "crossmnt");
378                         if (ep->e_flags & NFSEXP_INSECURE_PORT)
379                                 c = dumpopt(c, "insecure");
380                         if (ep->e_flags & NFSEXP_ROOTSQUASH)
381                                 c = dumpopt(c, "root_squash");
382                         else
383                                 c = dumpopt(c, "no_root_squash");
384                         if (ep->e_flags & NFSEXP_ALLSQUASH)
385                                 c = dumpopt(c, "all_squash");
386                         if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
387                                 c = dumpopt(c, "no_subtree_check");
388                         if (ep->e_flags & NFSEXP_NOAUTHNLM)
389                                 c = dumpopt(c, "insecure_locks");
390                         if (ep->e_flags & NFSEXP_FSID)
391                                 c = dumpopt(c, "fsid=%d", ep->e_fsid);
392                         if (ep->e_mountpoint)
393                                 c = dumpopt(c, "mountpoint%s%s", 
394                                             ep->e_mountpoint[0]?"=":"", 
395                                             ep->e_mountpoint);
396                         if (ep->e_maptype == CLE_MAP_UGIDD)
397                                 c = dumpopt(c, "mapping=ugidd");
398                         else if (ep->e_maptype == CLE_MAP_FILE)
399                                 c = dumpopt(c, "mapping=file");
400                         if (ep->e_anonuid != -2)
401                                 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
402                         if (ep->e_anongid != -2)
403                                 c = dumpopt(c, "anongid=%d", ep->e_anongid);
404
405                         printf("%c\n", (c != '(')? ')' : ' ');
406                 }
407         }
408 }
409
410 static void
411 error(nfs_export *exp, int err)
412 {
413         fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname, 
414                 exp->m_export.e_path, strerror(err));
415 }
416
417 static void
418 usage(void)
419 {
420         fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");
421         exit(1);
422 }