]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/rmtab.c
libexport.a: Refactor rmtab_read()
[nfs-utils.git] / support / export / rmtab.c
1 /*
2  * support/export/rmntab.c
3  *
4  * Interface to the rmnt file.
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include "xmalloc.h"
16 #include "misc.h"
17 #include "nfslib.h"
18 #include "exportfs.h"
19 #include "xio.h"
20 #include "xlog.h"
21
22 /*
23  * See if the entry already exists.  If not,
24  * this was an instantiated wild card, and we
25  * must add it.
26  */
27 static void
28 rmtab_read_wildcard(struct rmtabent *rep)
29 {
30         nfs_export *exp, *exp2;
31         struct hostent *hp;
32
33         hp = gethostbyname(rep->r_client);
34         if (hp == NULL)
35                 return;
36         hp = hostent_dup(hp);
37         if (hp == NULL)
38                 return;
39
40         exp = export_allowed(hp, rep->r_path);
41         free(hp);
42         if (exp == NULL)
43                 return;
44
45         exp2 = export_lookup(rep->r_client, exp->m_export.e_path, 0);
46         if (exp2 == NULL) {
47                 struct exportent ee;
48
49                 memset(&ee, 0, sizeof(ee));
50                 dupexportent(&ee, &exp->m_export);
51
52                 ee.e_hostname = rep->r_client;
53                 exp2 = export_create(&ee, 0);
54                 exp2->m_changed = exp->m_changed;
55         }
56         exp2->m_mayexport = 1;
57 }
58
59 int
60 rmtab_read(void)
61 {
62         struct rmtabent         *rep;
63
64         setrmtabent("r");
65         while ((rep = getrmtabent(1, NULL)) != NULL) {
66                 int                     htype;
67
68                 htype = client_gettype(rep->r_client);
69                 if (htype == MCL_FQDN || htype == MCL_SUBNETWORK)
70                         rmtab_read_wildcard(rep);
71         }
72
73         if (errno == EINVAL) {
74                 /* Something goes wrong. We need to fix the rmtab
75                    file. */
76                 int     lockid;
77                 FILE    *fp;
78                 if ((lockid = xflock(_PATH_RMTABLCK, "w")) < 0)
79                         return -1;
80                 rewindrmtabent();
81                 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
82                         endrmtabent ();
83                         xfunlock(lockid);
84                         return -1;
85                 }
86                 while ((rep = getrmtabent(0, NULL)) != NULL) {
87                         fputrmtabent(fp, rep, NULL);
88                 }
89                 if (rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
90                         xlog(L_ERROR, "couldn't rename %s to %s",
91                              _PATH_RMTABTMP, _PATH_RMTAB);
92                 }
93                 endrmtabent();
94                 fendrmtabent(fp);
95                 xfunlock(lockid);
96         }
97         else {
98                 endrmtabent();
99         }
100         return 0;
101 }