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