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