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