]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/export.c
27d4440c72bb287f32955704d6beda9ad19f17d3
[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
89         exp->m_exported = 0;
90         exp->m_xtabent = 0;
91         exp->m_mayexport = 0;
92         exp->m_changed = 0;
93         exp->m_client = clp;
94         clp->m_count++;
95 }
96
97 /*
98  * Duplicate exports data. The in-core export struct retains the
99  * original hostname from /etc/exports, while the in-core client struct
100  * gets the newly found FQDN.
101  */
102 nfs_export *
103 export_dup(nfs_export *exp, struct hostent *hp)
104 {
105         nfs_export              *new;
106         nfs_client              *clp;
107
108         new = (nfs_export *) xmalloc(sizeof(*new));
109         memcpy(new, exp, sizeof(*new));
110         dupexportent(&new->m_export, &exp->m_export);
111         clp = client_dup(exp->m_client, hp);
112         clp->m_count++;
113         new->m_client = clp;
114         new->m_mayexport = exp->m_mayexport;
115         new->m_exported = 0;
116         new->m_xtabent = 0;
117         new->m_changed = 0;
118         export_add(new);
119
120         return new;
121 }
122
123 void
124 export_add(nfs_export *exp)
125 {
126         nfs_export      **epp;
127         int             type = exp->m_client->m_type;
128         int             slen = strlen(exp->m_export.e_path);
129
130         if (type < 0 || type >= MCL_MAXTYPES)
131                 xlog(L_FATAL, "unknown client type in export_add");
132
133         epp = exportlist + type;
134         while (*epp && slen < strlen((*epp)->m_export.e_path))
135                 epp = &((*epp)->m_next);
136         exp->m_next = *epp;
137         *epp = exp;
138 }
139
140 nfs_export *
141 export_find(struct hostent *hp, char *path)
142 {
143         nfs_export      *exp;
144         int             i;
145
146         for (i = 0; i < MCL_MAXTYPES; i++) {
147                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
148                         if (!export_check(exp, hp, path))
149                                 continue;
150                         if (exp->m_client->m_type == MCL_FQDN)
151                                 return exp;
152                         return export_dup(exp, hp);
153                 }
154         }
155
156         return NULL;
157 }
158
159 static nfs_export *
160 export_allowed_internal (struct hostent *hp, char *path)
161 {
162         nfs_export      *exp;
163         int             i;
164
165         for (i = 0; i < MCL_MAXTYPES; i++) {
166                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
167                         if (!exp->m_mayexport ||
168                             !export_check(exp, hp, path))
169                                 continue;
170                         return exp;
171                 }
172         }
173
174         return NULL;
175 }
176
177 nfs_export *
178 export_allowed(struct hostent *hp, char *path)
179 {
180         nfs_export              *exp;
181         char                    epath[MAXPATHLEN+1];
182         char                    *p = NULL;
183
184         if (path [0] != '/') return NULL;
185
186         strncpy(epath, path, sizeof (epath) - 1);
187         epath[sizeof (epath) - 1] = '\0';
188
189         /* Try the longest matching exported pathname. */
190         while (1) {
191                 exp = export_allowed_internal (hp, epath);
192                 if (exp)
193                         return exp;
194                 /* We have to treat the root, "/", specially. */
195                 if (p == &epath[1]) break;
196                 p = strrchr(epath, '/');
197                 if (p == epath) p++;
198                 *p = '\0';
199         }
200
201         return NULL;
202 }
203
204 nfs_export *
205 export_lookup(char *hname, char *path, int canonical)
206 {
207         nfs_client      *clp;
208         nfs_export      *exp;
209
210         if (!(clp = client_lookup(hname, canonical)))
211                 return NULL;
212         for (exp = exportlist[clp->m_type]; exp; exp = exp->m_next)
213                 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path))
214                         return exp;
215         return NULL;
216 }
217
218 static int
219 export_check(nfs_export *exp, struct hostent *hp, char *path)
220 {
221         if (strcmp(path, exp->m_export.e_path))
222                 return 0;
223
224         return client_check(exp->m_client, hp);
225 }
226
227 void
228 export_freeall(void)
229 {
230         nfs_export      *exp, *nxt;
231         int             i;
232
233         for (i = 0; i < MCL_MAXTYPES; i++) {
234                 for (exp = exportlist[i]; exp; exp = nxt) {
235                         nxt = exp->m_next;
236                         client_release(exp->m_client);
237                         if (exp->m_export.e_squids)
238                                 xfree(exp->m_export.e_squids);
239                         if (exp->m_export.e_sqgids)
240                                 xfree(exp->m_export.e_sqgids);
241                         if (exp->m_export.e_mountpoint)
242                                 free(exp->m_export.e_mountpoint);
243                         xfree(exp);
244                 }
245                 exportlist[i] = NULL;
246         }
247         client_freeall();
248 }
249
250 void
251 export_reset(nfs_export *exp)
252 {
253         if (!exp)
254                 return;
255
256         /* Restore m_path. */
257         strncpy(exp->m_export.m_path, exp->m_export.e_path,
258                 sizeof (exp->m_export.m_path) - 1);
259         exp->m_export.m_path[sizeof (exp->m_export.m_path) - 1] = '\0';
260 }