]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/exportfs/exportfs.c
4c04098e1579475a5f66b5314872db308c599f41
[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         } else {
210                 exp = export_lookup(hname, path, 0);
211         }
212
213         if (!exp) {
214                 if (!(eep = mkexportent(hname, path, options)) ||
215                     !(exp = export_create(eep, 0))) {
216                         if (hp) free (hp);
217                         return;
218                 }
219         } else if (!updateexportent(&exp->m_export, options)) {
220                 if (hp) free (hp);
221                 return;
222         }
223
224         if (verbose)
225                 printf("exporting %s:%s\n", exp->m_client->m_hostname, 
226                         exp->m_export.e_path);
227         exp->m_xtabent = 1;
228         exp->m_mayexport = 1;
229         exp->m_changed = 1;
230         if (hp) free (hp);
231 }
232
233 static void
234 unexportfs(char *arg, int verbose)
235 {
236         nfs_export      *exp;
237         struct hostent  *hp = NULL;
238         char            *path;
239         char            *hname = arg;
240         int             htype;
241
242         if ((path = strchr(arg, ':')) != NULL)
243                 *path++ = '\0';
244
245         if (!path || *path != '/') {
246                 fprintf(stderr, "Invalid unexporting option: %s\n",
247                         arg);
248                 return;
249         }
250
251         if ((htype = client_gettype(hname)) == MCL_FQDN) {
252                 if ((hp = gethostbyname(hname)) != 0) {
253                         hp = hostent_dup (hp);
254                         hname = (char *) hp->h_name;
255                 }
256         }
257
258         for (exp = exportlist[htype]; exp; exp = exp->m_next) {
259                 if (path && strcmp(path, exp->m_export.e_path))
260                         continue;
261                 if (htype != exp->m_client->m_type
262                     || (htype == MCL_FQDN
263                         && !matchhostname(exp->m_export.e_hostname,
264                                           hname)))
265                         continue;
266                 if (verbose) {
267                         if (exp->m_exported) {
268                                 printf("unexporting %s:%s from kernel\n",
269                                        exp->m_client->m_hostname,
270                                        exp->m_export.e_path);
271                         }
272                         else {
273                                 printf("unexporting %s:%s\n",
274                                         exp->m_client->m_hostname, 
275                                         exp->m_export.e_path);
276                         }
277                 }
278                 if (exp->m_exported && !export_unexport(exp))
279                         error(exp, errno);
280                 exp->m_xtabent = 0;
281                 exp->m_mayexport = 0;
282         }
283
284         if (hp) free (hp);
285 }
286
287 static char
288 dumpopt(char c, char *fmt, ...)
289 {
290         va_list ap;
291
292         va_start(ap, fmt);
293         printf("%c", c);
294         vprintf(fmt, ap);
295         va_end(ap);
296         return ',';
297 }
298
299 static void
300 dump(int verbose)
301 {
302         nfs_export      *exp;
303         struct exportent *ep;
304         int             htype;
305         char            *hname, c;
306
307         for (htype = 0; htype < MCL_MAXTYPES; htype++) {
308                 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
309                         ep = &exp->m_export;
310                         if (!exp->m_xtabent)
311                             continue; /* neilb */
312                         if (htype == MCL_ANONYMOUS)
313                                 hname = "<world>";
314                         else
315                                 hname = ep->e_hostname;
316                         if (strlen(ep->e_path) > 14)
317                                 printf("%-14s\n\t\t%s", ep->e_path, hname);
318                         else
319                                 printf("%-14s\t%s", ep->e_path, hname);
320                         if (!verbose) {
321                                 printf("\n");
322                                 continue;
323                         }
324                         c = '(';
325                         if (ep->e_flags & NFSEXP_READONLY)
326                                 c = dumpopt(c, "ro");
327                         else
328                                 c = dumpopt(c, "rw");
329                         if (ep->e_flags & NFSEXP_ASYNC)
330                                 c = dumpopt(c, "async");
331                         if (ep->e_flags & NFSEXP_GATHERED_WRITES)
332                                 c = dumpopt(c, "wdelay");
333                         if (ep->e_flags & NFSEXP_CROSSMNT)
334                                 c = dumpopt(c, "nohide");
335                         if (ep->e_flags & NFSEXP_INSECURE_PORT)
336                                 c = dumpopt(c, "insecure");
337                         if (ep->e_flags & NFSEXP_ROOTSQUASH)
338                                 c = dumpopt(c, "root_squash");
339                         else
340                                 c = dumpopt(c, "no_root_squash");
341                         if (ep->e_flags & NFSEXP_ALLSQUASH)
342                                 c = dumpopt(c, "all_squash");
343                         if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
344                                 c = dumpopt(c, "no_subtree_check");
345                         if (ep->e_flags & NFSEXP_NOAUTHNLM)
346                                 c = dumpopt(c, "insecure_locks");
347                         if (ep->e_maptype == CLE_MAP_UGIDD)
348                                 c = dumpopt(c, "mapping=ugidd");
349                         else if (ep->e_maptype == CLE_MAP_FILE)
350                                 c = dumpopt(c, "mapping=file");
351                         if (ep->e_anonuid != -2)
352                                 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
353                         if (ep->e_anongid != -2)
354                                 c = dumpopt(c, "anongid=%d", ep->e_anongid);
355
356                         printf("%c\n", (c != '(')? ')' : ' ');
357                 }
358         }
359 }
360
361 static void
362 error(nfs_export *exp, int err)
363 {
364         fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname, 
365                 exp->m_export.e_path, strerror(err));
366 }
367
368 static void
369 usage(void)
370 {
371         fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");
372         exit(1);
373 }