]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/export.c
libexport.a: Make export_add() static
[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 exp_hash_table exportlist[MCL_MAXTYPES] = {{NULL, {{NULL,NULL}, }}, }; 
23 static int export_hash(char *);
24
25 static void     export_init(nfs_export *exp, nfs_client *clp,
26                                         struct exportent *nep);
27 static void     export_add(nfs_export *exp);
28 static int      export_check(const nfs_export *exp, const struct addrinfo *ai,
29                                 const char *path);
30 static nfs_export *
31                 export_allowed_internal(const struct addrinfo *ai,
32                                 const char *path);
33
34 static void
35 export_free(nfs_export *exp)
36 {
37         xfree(exp->m_export.e_squids);
38         xfree(exp->m_export.e_sqgids);
39         free(exp->m_export.e_mountpoint);
40         free(exp->m_export.e_fslocdata);
41
42         xfree(exp->m_export.e_hostname);
43         xfree(exp);
44 }
45
46 static void warn_duplicated_exports(nfs_export *exp, struct exportent *eep)
47 {
48         if (exp->m_export.e_flags != eep->e_flags) {
49                 xlog(L_ERROR, "incompatible duplicated export entries:");
50                 xlog(L_ERROR, "\t%s:%s (0x%x) [IGNORED]", eep->e_hostname,
51                                 eep->e_path, eep->e_flags);
52                 xlog(L_ERROR, "\t%s:%s (0x%x)", exp->m_export.e_hostname,
53                                 exp->m_export.e_path, exp->m_export.e_flags);
54         } else {
55                 xlog(L_ERROR, "duplicated export entries:");
56                 xlog(L_ERROR, "\t%s:%s", eep->e_hostname, eep->e_path);
57                 xlog(L_ERROR, "\t%s:%s", exp->m_export.e_hostname,
58                                 exp->m_export.e_path);
59         }
60 }
61
62 /**
63  * export_read - read entries from /etc/exports
64  * @fname: name of file to read from
65  *
66  */
67 void
68 export_read(char *fname)
69 {
70         struct exportent        *eep;
71         nfs_export              *exp;
72
73         setexportent(fname, "r");
74         while ((eep = getexportent(0,1)) != NULL) {
75                 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
76                 if (!exp)
77                         export_create(eep, 0);
78                 else
79                         warn_duplicated_exports(exp, eep);
80         }
81         endexportent();
82 }
83
84 /*
85  * Create an in-core export struct from an export entry.
86  */
87 nfs_export *
88 export_create(struct exportent *xep, int canonical)
89 {
90         nfs_client      *clp;
91         nfs_export      *exp;
92
93         if (!(clp = client_lookup(xep->e_hostname, canonical))) {
94                 /* bad export entry; complaint already logged */
95                 return NULL;
96         }
97         exp = (nfs_export *) xmalloc(sizeof(*exp));
98         export_init(exp, clp, xep);
99         export_add(exp);
100
101         return exp;
102 }
103
104 static void
105 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
106 {
107         struct exportent        *e = &exp->m_export;
108
109         dupexportent(e, nep);
110         if (nep->e_hostname)
111                 e->e_hostname = xstrdup(nep->e_hostname);
112
113         exp->m_exported = 0;
114         exp->m_xtabent = 0;
115         exp->m_mayexport = 0;
116         exp->m_changed = 0;
117         exp->m_warned = 0;
118         exp->m_client = clp;
119         clp->m_count++;
120 }
121
122 /*
123  * Duplicate exports data. The in-core export struct retains the
124  * original hostname from /etc/exports, while the in-core client struct
125  * gets the newly found FQDN.
126  */
127 static nfs_export *
128 export_dup(nfs_export *exp, const struct addrinfo *ai)
129 {
130         nfs_export              *new;
131         nfs_client              *clp;
132
133         new = (nfs_export *) xmalloc(sizeof(*new));
134         memcpy(new, exp, sizeof(*new));
135         dupexportent(&new->m_export, &exp->m_export);
136         if (exp->m_export.e_hostname)
137                 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
138         clp = client_dup(exp->m_client, ai);
139         if (clp == NULL) {
140                 export_free(new);
141                 return NULL;
142         }
143         clp->m_count++;
144         new->m_client = clp;
145         new->m_mayexport = exp->m_mayexport;
146         new->m_exported = 0;
147         new->m_xtabent = 0;
148         new->m_changed = 0;
149         new->m_warned = 0;
150         export_add(new);
151
152         return new;
153 }
154
155 static void
156 export_add(nfs_export *exp)
157 {
158         exp_hash_table *p_tbl;
159         exp_hash_entry *p_hen;
160         nfs_export *p_next;
161
162         int type = exp->m_client->m_type;
163         int pos;
164
165         pos = export_hash(exp->m_export.e_path);
166         p_tbl = &(exportlist[type]); /* pointer to hash table */
167         p_hen = &(p_tbl->entries[pos]); /* pointer to hash table entry */
168
169         if (!(p_hen->p_first)) { /* hash table entry is empty */ 
170                 p_hen->p_first = exp;
171                 p_hen->p_last  = exp;
172
173                 exp->m_next = p_tbl->p_head;
174                 p_tbl->p_head = exp;
175         } else { /* hash table entry is NOT empty */
176                 p_next = p_hen->p_last->m_next;
177                 p_hen->p_last->m_next = exp;
178                 exp->m_next = p_next;
179                 p_hen->p_last = exp;
180         }
181 }
182
183 /**
184  * export_find - find or create a suitable nfs_export for @ai and @path
185  * @ai: pointer to addrinfo for client
186  * @path: '\0'-terminated ASCII string containing export path
187  *
188  * Returns a pointer to nfs_export data matching @ai and @path,
189  * or NULL if an error occurs.
190  */
191 nfs_export *
192 export_find(const struct addrinfo *ai, const char *path)
193 {
194         nfs_export      *exp;
195         int             i;
196
197         for (i = 0; i < MCL_MAXTYPES; i++) {
198                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
199                         if (!export_check(exp, ai, path))
200                                 continue;
201                         if (exp->m_client->m_type == MCL_FQDN)
202                                 return exp;
203                         return export_dup(exp, ai);
204                 }
205         }
206
207         return NULL;
208 }
209
210 static nfs_export *
211 export_allowed_internal(const struct addrinfo *ai, const char *path)
212 {
213         nfs_export      *exp;
214         int             i;
215
216         for (i = 0; i < MCL_MAXTYPES; i++) {
217                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
218                         if (!exp->m_mayexport ||
219                             !export_check(exp, ai, path))
220                                 continue;
221                         return exp;
222                 }
223         }
224
225         return NULL;
226 }
227
228 /**
229  * export_allowed - determine if this export is allowed
230  * @ai: pointer to addrinfo for client
231  * @path: '\0'-terminated ASCII string containing export path
232  *
233  * Returns a pointer to nfs_export data matching @ai and @path,
234  * or NULL if the export is not allowed.
235  */
236 nfs_export *
237 export_allowed(const struct addrinfo *ai, const char *path)
238 {
239         nfs_export              *exp;
240         char                    epath[MAXPATHLEN+1];
241         char                    *p = NULL;
242
243         if (path [0] != '/') return NULL;
244
245         strncpy(epath, path, sizeof (epath) - 1);
246         epath[sizeof (epath) - 1] = '\0';
247
248         /* Try the longest matching exported pathname. */
249         while (1) {
250                 exp = export_allowed_internal(ai, epath);
251                 if (exp)
252                         return exp;
253                 /* We have to treat the root, "/", specially. */
254                 if (p == &epath[1]) break;
255                 p = strrchr(epath, '/');
256                 if (p == epath) p++;
257                 *p = '\0';
258         }
259
260         return NULL;
261 }
262
263 /*
264  * Search hash table for export entry. 
265  */  
266 nfs_export *
267 export_lookup(char *hname, char *path, int canonical) 
268 {
269         nfs_client *clp;
270         nfs_export *exp;
271         exp_hash_entry *p_hen;
272
273         int pos;
274
275         clp = client_lookup(hname, canonical);
276         if(clp == NULL)
277                 return NULL;
278
279         pos = export_hash(path);
280         p_hen = &(exportlist[clp->m_type].entries[pos]); 
281         for(exp = p_hen->p_first; exp && (exp != p_hen->p_last->m_next); 
282                         exp = exp->m_next) {
283                 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
284                         return exp;
285                 }
286         }
287         return NULL;
288 }
289
290 static int
291 export_check(const nfs_export *exp, const struct addrinfo *ai, const char *path)
292 {
293         if (strcmp(path, exp->m_export.e_path))
294                 return 0;
295
296         return client_check(exp->m_client, ai);
297 }
298
299 /**
300  * export_freeall - deallocate all nfs_export records
301  *
302  */
303 void
304 export_freeall(void)
305 {
306         nfs_export      *exp, *nxt;
307         int             i, j;
308
309         for (i = 0; i < MCL_MAXTYPES; i++) {
310                 for (exp = exportlist[i].p_head; exp; exp = nxt) {
311                         nxt = exp->m_next;
312                         client_release(exp->m_client);
313                         export_free(exp);
314                 }
315                 for (j = 0; j < HASH_TABLE_SIZE; j++) {
316                         exportlist[i].entries[j].p_first = NULL;
317                         exportlist[i].entries[j].p_last = NULL;
318                 }
319                 exportlist[i].p_head = NULL;
320         }
321         client_freeall();
322 }
323
324 /*
325  * Compute and returns integer from string. 
326  * Note: Its understood the smae integers can be same for 
327  *       different strings, but it should not matter.
328  */
329 static unsigned int 
330 strtoint(char *str)
331 {
332         int i = 0;
333         unsigned int n = 0;
334
335         while ( str[i] != '\0') {
336                 n+=((int)str[i])*i;
337                 i++;
338         }
339         return n;
340 }
341
342 /*
343  * Hash function
344  */
345 static int 
346 export_hash(char *str)
347 {
348         int num = strtoint(str);
349
350         return num % HASH_TABLE_SIZE;
351 }