]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/export.c
configure: use "--disable-uuid" instead of "--without-uuid"
[nfs-utils.git] / support / export / export.c
1 /*
2  * support/export/export.c
3  *
4  * Maintain list of exported file systems.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
16 #include <netinet/in.h>
17 #include <stdlib.h>
18 #include "xmalloc.h"
19 #include "nfslib.h"
20 #include "exportfs.h"
21
22 nfs_export      *exportlist[MCL_MAXTYPES] = { NULL, };
23
24 static void     export_init(nfs_export *exp, nfs_client *clp,
25                                         struct exportent *nep);
26 static int      export_check(nfs_export *, struct hostent *, char *);
27 static nfs_export *
28                 export_allowed_internal(struct hostent *hp, char *path);
29
30 int
31 export_read(char *fname)
32 {
33         struct exportent        *eep;
34         nfs_export              *exp;
35
36         setexportent(fname, "r");
37         while ((eep = getexportent(0,1)) != NULL) {
38           exp = export_lookup(eep->e_hostname, eep->e_path, 0);
39           if (!exp)
40             export_create(eep,0);
41           else {
42             if (exp->m_export.e_flags != eep->e_flags) {
43               xlog(L_ERROR, "incompatible duplicated export entries:");
44               xlog(L_ERROR, "\t%s:%s (0x%x) [IGNORED]", eep->e_hostname,
45                    eep->e_path, eep->e_flags);
46               xlog(L_ERROR, "\t%s:%s (0x%x)", exp->m_export.e_hostname,
47                    exp->m_export.e_path, exp->m_export.e_flags);
48             }
49             else {
50               xlog(L_ERROR, "duplicated export entries:");
51               xlog(L_ERROR, "\t%s:%s", eep->e_hostname, eep->e_path);
52               xlog(L_ERROR, "\t%s:%s", exp->m_export.e_hostname,
53                    exp->m_export.e_path);
54             }
55           }
56         }
57         endexportent();
58
59         return 0;
60 }
61
62 /*
63  * Create an in-core export struct from an export entry.
64  */
65 nfs_export *
66 export_create(struct exportent *xep, int canonical)
67 {
68         nfs_client      *clp;
69         nfs_export      *exp;
70
71         if (!(clp = client_lookup(xep->e_hostname, canonical))) {
72                 /* bad export entry; complaint already logged */
73                 return NULL;
74         }
75         exp = (nfs_export *) xmalloc(sizeof(*exp));
76         export_init(exp, clp, xep);
77         export_add(exp);
78
79         return exp;
80 }
81
82 static void
83 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
84 {
85         struct exportent        *e = &exp->m_export;
86
87         dupexportent(e, nep);
88         if (nep->e_hostname)
89                 e->e_hostname = xstrdup(nep->e_hostname);
90
91         exp->m_exported = 0;
92         exp->m_xtabent = 0;
93         exp->m_mayexport = 0;
94         exp->m_changed = 0;
95         exp->m_warned = 0;
96         exp->m_client = clp;
97         clp->m_count++;
98 }
99
100 /*
101  * Duplicate exports data. The in-core export struct retains the
102  * original hostname from /etc/exports, while the in-core client struct
103  * gets the newly found FQDN.
104  */
105 nfs_export *
106 export_dup(nfs_export *exp, struct hostent *hp)
107 {
108         nfs_export              *new;
109         nfs_client              *clp;
110
111         new = (nfs_export *) xmalloc(sizeof(*new));
112         memcpy(new, exp, sizeof(*new));
113         dupexportent(&new->m_export, &exp->m_export);
114         if (exp->m_export.e_hostname)
115                 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
116         clp = client_dup(exp->m_client, hp);
117         clp->m_count++;
118         new->m_client = clp;
119         new->m_mayexport = exp->m_mayexport;
120         new->m_exported = 0;
121         new->m_xtabent = 0;
122         new->m_changed = 0;
123         new->m_warned = 0;
124         export_add(new);
125
126         return new;
127 }
128
129 void
130 export_add(nfs_export *exp)
131 {
132         nfs_export      **epp;
133         int             type = exp->m_client->m_type;
134         int             slen = strlen(exp->m_export.e_path);
135
136         if (type < 0 || type >= MCL_MAXTYPES)
137                 xlog(L_FATAL, "unknown client type in export_add");
138
139         epp = exportlist + type;
140         while (*epp && slen <= strlen((*epp)->m_export.e_path))
141                 epp = &((*epp)->m_next);
142         exp->m_next = *epp;
143         *epp = exp;
144 }
145
146 nfs_export *
147 export_find(struct hostent *hp, char *path)
148 {
149         nfs_export      *exp;
150         int             i;
151
152         for (i = 0; i < MCL_MAXTYPES; i++) {
153                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
154                         if (!export_check(exp, hp, path))
155                                 continue;
156                         if (exp->m_client->m_type == MCL_FQDN)
157                                 return exp;
158                         return export_dup(exp, hp);
159                 }
160         }
161
162         return NULL;
163 }
164
165 static nfs_export *
166 export_allowed_internal (struct hostent *hp, char *path)
167 {
168         nfs_export      *exp;
169         int             i;
170
171         for (i = 0; i < MCL_MAXTYPES; i++) {
172                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
173                         if (!exp->m_mayexport ||
174                             !export_check(exp, hp, path))
175                                 continue;
176                         return exp;
177                 }
178         }
179
180         return NULL;
181 }
182
183 nfs_export *
184 export_allowed(struct hostent *hp, char *path)
185 {
186         nfs_export              *exp;
187         char                    epath[MAXPATHLEN+1];
188         char                    *p = NULL;
189
190         if (path [0] != '/') return NULL;
191
192         strncpy(epath, path, sizeof (epath) - 1);
193         epath[sizeof (epath) - 1] = '\0';
194
195         /* Try the longest matching exported pathname. */
196         while (1) {
197                 exp = export_allowed_internal (hp, epath);
198                 if (exp)
199                         return exp;
200                 /* We have to treat the root, "/", specially. */
201                 if (p == &epath[1]) break;
202                 p = strrchr(epath, '/');
203                 if (p == epath) p++;
204                 *p = '\0';
205         }
206
207         return NULL;
208 }
209
210 nfs_export *
211 export_lookup(char *hname, char *path, int canonical)
212 {
213         nfs_client      *clp;
214         nfs_export      *exp;
215
216         if (!(clp = client_lookup(hname, canonical)))
217                 return NULL;
218         for (exp = exportlist[clp->m_type]; exp; exp = exp->m_next)
219                 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path))
220                         return exp;
221         return NULL;
222 }
223
224 static int
225 export_check(nfs_export *exp, struct hostent *hp, char *path)
226 {
227         if (strcmp(path, exp->m_export.e_path))
228                 return 0;
229
230         return client_check(exp->m_client, hp);
231 }
232
233 void
234 export_freeall(void)
235 {
236         nfs_export      *exp, *nxt;
237         int             i;
238
239         for (i = 0; i < MCL_MAXTYPES; i++) {
240                 for (exp = exportlist[i]; exp; exp = nxt) {
241                         nxt = exp->m_next;
242                         client_release(exp->m_client);
243                         if (exp->m_export.e_squids)
244                                 xfree(exp->m_export.e_squids);
245                         if (exp->m_export.e_sqgids)
246                                 xfree(exp->m_export.e_sqgids);
247                         if (exp->m_export.e_mountpoint)
248                                 free(exp->m_export.e_mountpoint);
249                         if (exp->m_export.e_fslocdata)
250                                 xfree(exp->m_export.e_fslocdata);
251                         xfree(exp->m_export.e_hostname);
252                         xfree(exp);
253                 }
254                 exportlist[i] = NULL;
255         }
256         client_freeall();
257 }