]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/exportfs/exportfs.c
Work towards support new cache in 2.5
[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                         else 
288 #endif
289                                 printf("unexporting %s:%s\n",
290                                         exp->m_client->m_hostname, 
291                                         exp->m_export.e_path);
292                 }
293 #if 0
294                 if (exp->m_exported && !export_unexport(exp))
295                         error(exp, errno);
296 #endif
297                 exp->m_xtabent = 0;
298                 exp->m_mayexport = 0;
299         }
300
301         if (hp) free (hp);
302 }
303
304 static char
305 dumpopt(char c, char *fmt, ...)
306 {
307         va_list ap;
308
309         va_start(ap, fmt);
310         printf("%c", c);
311         vprintf(fmt, ap);
312         va_end(ap);
313         return ',';
314 }
315
316 static void
317 dump(int verbose)
318 {
319         nfs_export      *exp;
320         struct exportent *ep;
321         int             htype;
322         char            *hname, c;
323
324         for (htype = 0; htype < MCL_MAXTYPES; htype++) {
325                 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
326                         ep = &exp->m_export;
327                         if (!exp->m_xtabent)
328                             continue; /* neilb */
329                         if (htype == MCL_ANONYMOUS)
330                                 hname = "<world>";
331                         else
332                                 hname = ep->e_hostname;
333                         if (strlen(ep->e_path) > 14)
334                                 printf("%-14s\n\t\t%s", ep->e_path, hname);
335                         else
336                                 printf("%-14s\t%s", ep->e_path, hname);
337                         if (!verbose) {
338                                 printf("\n");
339                                 continue;
340                         }
341                         c = '(';
342                         if (ep->e_flags & NFSEXP_READONLY)
343                                 c = dumpopt(c, "ro");
344                         else
345                                 c = dumpopt(c, "rw");
346                         if (ep->e_flags & NFSEXP_ASYNC)
347                                 c = dumpopt(c, "async");
348                         if (ep->e_flags & NFSEXP_GATHERED_WRITES)
349                                 c = dumpopt(c, "wdelay");
350                         if (ep->e_flags & NFSEXP_NOHIDE)
351                                 c = dumpopt(c, "nohide");
352                         if (ep->e_flags & NFSEXP_CROSSMNT)
353                                 c = dumpopt(c, "crossmnt");
354                         if (ep->e_flags & NFSEXP_INSECURE_PORT)
355                                 c = dumpopt(c, "insecure");
356                         if (ep->e_flags & NFSEXP_ROOTSQUASH)
357                                 c = dumpopt(c, "root_squash");
358                         else
359                                 c = dumpopt(c, "no_root_squash");
360                         if (ep->e_flags & NFSEXP_ALLSQUASH)
361                                 c = dumpopt(c, "all_squash");
362                         if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
363                                 c = dumpopt(c, "no_subtree_check");
364                         if (ep->e_flags & NFSEXP_NOAUTHNLM)
365                                 c = dumpopt(c, "insecure_locks");
366                         if (ep->e_flags & NFSEXP_FSID)
367                                 c = dumpopt(c, "fsid=%d", ep->e_fsid);
368                         if (ep->e_maptype == CLE_MAP_UGIDD)
369                                 c = dumpopt(c, "mapping=ugidd");
370                         else if (ep->e_maptype == CLE_MAP_FILE)
371                                 c = dumpopt(c, "mapping=file");
372                         if (ep->e_anonuid != -2)
373                                 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
374                         if (ep->e_anongid != -2)
375                                 c = dumpopt(c, "anongid=%d", ep->e_anongid);
376
377                         printf("%c\n", (c != '(')? ')' : ' ');
378                 }
379         }
380 }
381
382 static void
383 error(nfs_export *exp, int err)
384 {
385         fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname, 
386                 exp->m_export.e_path, strerror(err));
387 }
388
389 static void
390 usage(void)
391 {
392         fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");
393         exit(1);
394 }