2005-08-26 Kevin Coffman <kwc@citi.umich.edu>
[nfs-utils.git] / support / export / rmtab.c
1 /*
2  * support/export/rmntab.c
3  *
4  * Interface to the rmnt file.
5  *
6  */
7
8 #include "config.h"
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "xmalloc.h"
14 #include "misc.h"
15 #include "nfslib.h"
16 #include "exportfs.h"
17 #include "xio.h"
18 #include "xlog.h"
19
20 int
21 rmtab_read(void)
22 {
23         struct rmtabent         *rep;
24         nfs_export              *exp;
25
26         setrmtabent("r");
27         while ((rep = getrmtabent(1, NULL)) != NULL) {
28                 struct hostent          *hp = NULL;
29                 int                     htype;
30                 
31                 htype = client_gettype(rep->r_client);
32                 if (htype == MCL_FQDN
33                     && (hp = gethostbyname (rep->r_client))
34                     && (hp = hostent_dup (hp),
35                         exp = export_allowed (hp, rep->r_path))) {
36                         /* see if the entry already exists, otherwise this was an instantiated
37                          * wild card, and we must add it
38                          */
39                         nfs_export *exp2 = export_lookup(rep->r_client,
40                                                         exp->m_export.e_path, 0);
41                         if (!exp2) {
42                                 struct exportent ee;
43                                 dupexportent(&ee, &exp->m_export);
44                                 strncpy (ee.e_hostname, rep->r_client,
45                                          sizeof (ee.e_hostname) - 1);
46                                 ee.e_hostname[sizeof (ee.e_hostname) -1] = '\0';
47                                 exp2 = export_create(&ee, 0);
48                                 exp2->m_changed = exp->m_changed;
49                         }
50                         free (hp);
51                         exp2->m_mayexport = 1;
52                 } else if (hp) /* export_allowed failed */
53                         free(hp);
54         }
55         if (errno == EINVAL) {
56                 /* Something goes wrong. We need to fix the rmtab
57                    file. */
58                 int     lockid;
59                 FILE    *fp;
60                 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
61                         return -1;
62                 rewindrmtabent();
63                 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
64                         endrmtabent ();
65                         xfunlock(lockid);
66                         return -1;
67                 }
68                 while ((rep = getrmtabent(0, NULL)) != NULL) {
69                         fputrmtabent(fp, rep, NULL);
70                 }
71                 if (rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
72                         xlog(L_ERROR, "couldn't rename %s to %s",
73                              _PATH_RMTABTMP, _PATH_RMTAB);
74                 }
75                 endrmtabent();
76                 fendrmtabent(fp);
77                 xfunlock(lockid);
78         }
79         else {
80                 endrmtabent();
81         }
82         return 0;
83 }