]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/rmtab.c
Autogen update
[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 int
23 rmtab_read(void)
24 {
25         struct rmtabent         *rep;
26         nfs_export              *exp;
27
28         setrmtabent("r");
29         while ((rep = getrmtabent(1, NULL)) != NULL) {
30                 struct hostent          *hp = NULL;
31                 int                     htype;
32                 
33                 htype = client_gettype(rep->r_client);
34                 if (htype == MCL_FQDN
35                     && (hp = gethostbyname (rep->r_client))
36                     && (hp = hostent_dup (hp),
37                         exp = export_allowed (hp, rep->r_path))) {
38                         /* see if the entry already exists, otherwise this was an instantiated
39                          * wild card, and we must add it
40                          */
41                         nfs_export *exp2 = export_lookup(rep->r_client,
42                                                         exp->m_export.e_path, 0);
43                         if (!exp2) {
44                                 struct exportent ee;
45                                 dupexportent(&ee, &exp->m_export);
46                                 strncpy (ee.e_hostname, rep->r_client,
47                                          sizeof (ee.e_hostname) - 1);
48                                 ee.e_hostname[sizeof (ee.e_hostname) -1] = '\0';
49                                 exp2 = export_create(&ee, 0);
50                                 exp2->m_changed = exp->m_changed;
51                         }
52                         free (hp);
53                         exp2->m_mayexport = 1;
54                 } else if (hp) /* export_allowed failed */
55                         free(hp);
56         }
57         if (errno == EINVAL) {
58                 /* Something goes wrong. We need to fix the rmtab
59                    file. */
60                 int     lockid;
61                 FILE    *fp;
62                 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
63                         return -1;
64                 rewindrmtabent();
65                 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
66                         endrmtabent ();
67                         xfunlock(lockid);
68                         return -1;
69                 }
70                 while ((rep = getrmtabent(0, NULL)) != NULL) {
71                         fputrmtabent(fp, rep, NULL);
72                 }
73                 if (rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
74                         xlog(L_ERROR, "couldn't rename %s to %s",
75                              _PATH_RMTABTMP, _PATH_RMTAB);
76                 }
77                 endrmtabent();
78                 fendrmtabent(fp);
79                 xfunlock(lockid);
80         }
81         else {
82                 endrmtabent();
83         }
84         return 0;
85 }