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