mountd: Replace "struct hostent" with "struct addrinfo"
[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 addrinfo *ai;
32
33         ai = host_addrinfo(rep->r_client);
34         if (ai == NULL)
35                 return;
36
37         exp = export_allowed(ai, rep->r_path);
38         freeaddrinfo(ai);
39         if (exp == NULL)
40                 return;
41
42         exp2 = export_lookup(rep->r_client, exp->m_export.e_path, 0);
43         if (exp2 == NULL) {
44                 struct exportent ee;
45
46                 memset(&ee, 0, sizeof(ee));
47                 dupexportent(&ee, &exp->m_export);
48
49                 ee.e_hostname = rep->r_client;
50                 exp2 = export_create(&ee, 0);
51                 exp2->m_changed = exp->m_changed;
52         }
53         exp2->m_mayexport = 1;
54 }
55
56 int
57 rmtab_read(void)
58 {
59         struct rmtabent         *rep;
60
61         setrmtabent("r");
62         while ((rep = getrmtabent(1, NULL)) != NULL) {
63                 int                     htype;
64
65                 htype = client_gettype(rep->r_client);
66                 if (htype == MCL_FQDN || htype == MCL_SUBNETWORK)
67                         rmtab_read_wildcard(rep);
68         }
69
70         if (errno == EINVAL) {
71                 /* Something goes wrong. We need to fix the rmtab
72                    file. */
73                 int     lockid;
74                 FILE    *fp;
75                 if ((lockid = xflock(_PATH_RMTABLCK, "w")) < 0)
76                         return -1;
77                 rewindrmtabent();
78                 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
79                         endrmtabent ();
80                         xfunlock(lockid);
81                         return -1;
82                 }
83                 while ((rep = getrmtabent(0, NULL)) != NULL) {
84                         fputrmtabent(fp, rep, NULL);
85                 }
86                 if (rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
87                         xlog(L_ERROR, "couldn't rename %s to %s",
88                              _PATH_RMTABTMP, _PATH_RMTAB);
89                 }
90                 endrmtabent();
91                 fendrmtabent(fp);
92                 xfunlock(lockid);
93         }
94         else {
95                 endrmtabent();
96         }
97         return 0;
98 }