2 * support/export/export.c
4 * Maintain list of exported file systems.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
14 #include <sys/types.h>
15 #include <sys/param.h>
16 #include <netinet/in.h>
22 exp_hash_table exportlist[MCL_MAXTYPES] = {{NULL, {{NULL,NULL}, }}, };
23 static int export_hash(char *);
25 static void export_init(nfs_export *exp, nfs_client *clp,
26 struct exportent *nep);
27 static int export_check(nfs_export *, struct hostent *, char *);
29 export_allowed_internal(struct hostent *hp, char *path);
31 static void warn_duplicated_exports(nfs_export *exp, struct exportent *eep)
33 if (exp->m_export.e_flags != eep->e_flags) {
34 xlog(L_ERROR, "incompatible duplicated export entries:");
35 xlog(L_ERROR, "\t%s:%s (0x%x) [IGNORED]", eep->e_hostname,
36 eep->e_path, eep->e_flags);
37 xlog(L_ERROR, "\t%s:%s (0x%x)", exp->m_export.e_hostname,
38 exp->m_export.e_path, exp->m_export.e_flags);
40 xlog(L_ERROR, "duplicated export entries:");
41 xlog(L_ERROR, "\t%s:%s", eep->e_hostname, eep->e_path);
42 xlog(L_ERROR, "\t%s:%s", exp->m_export.e_hostname,
43 exp->m_export.e_path);
48 export_read(char *fname)
50 struct exportent *eep;
53 setexportent(fname, "r");
54 while ((eep = getexportent(0,1)) != NULL) {
55 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
57 export_create(eep, 0);
59 warn_duplicated_exports(exp, eep);
66 * Create an in-core export struct from an export entry.
69 export_create(struct exportent *xep, int canonical)
74 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
75 /* bad export entry; complaint already logged */
78 exp = (nfs_export *) xmalloc(sizeof(*exp));
79 export_init(exp, clp, xep);
86 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
88 struct exportent *e = &exp->m_export;
92 e->e_hostname = xstrdup(nep->e_hostname);
104 * Duplicate exports data. The in-core export struct retains the
105 * original hostname from /etc/exports, while the in-core client struct
106 * gets the newly found FQDN.
109 export_dup(nfs_export *exp, struct hostent *hp)
114 new = (nfs_export *) xmalloc(sizeof(*new));
115 memcpy(new, exp, sizeof(*new));
116 dupexportent(&new->m_export, &exp->m_export);
117 if (exp->m_export.e_hostname)
118 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
119 clp = client_dup(exp->m_client, hp);
122 new->m_mayexport = exp->m_mayexport;
133 * Add export entry to hash table
136 export_add(nfs_export *exp)
138 exp_hash_table *p_tbl;
139 exp_hash_entry *p_hen;
142 int type = exp->m_client->m_type;
145 pos = export_hash(exp->m_export.e_path);
146 p_tbl = &(exportlist[type]); /* pointer to hash table */
147 p_hen = &(p_tbl->entries[pos]); /* pointer to hash table entry */
149 if (!(p_hen->p_first)) { /* hash table entry is empty */
150 p_hen->p_first = exp;
153 exp->m_next = p_tbl->p_head;
155 } else { /* hash table entry is NOT empty */
156 p_next = p_hen->p_last->m_next;
157 p_hen->p_last->m_next = exp;
158 exp->m_next = p_next;
164 export_find(struct hostent *hp, char *path)
169 for (i = 0; i < MCL_MAXTYPES; i++) {
170 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
171 if (!export_check(exp, hp, path))
173 if (exp->m_client->m_type == MCL_FQDN)
175 return export_dup(exp, hp);
183 export_allowed_internal (struct hostent *hp, char *path)
188 for (i = 0; i < MCL_MAXTYPES; i++) {
189 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
190 if (!exp->m_mayexport ||
191 !export_check(exp, hp, path))
201 export_allowed(struct hostent *hp, char *path)
204 char epath[MAXPATHLEN+1];
207 if (path [0] != '/') return NULL;
209 strncpy(epath, path, sizeof (epath) - 1);
210 epath[sizeof (epath) - 1] = '\0';
212 /* Try the longest matching exported pathname. */
214 exp = export_allowed_internal (hp, epath);
217 /* We have to treat the root, "/", specially. */
218 if (p == &epath[1]) break;
219 p = strrchr(epath, '/');
228 * Search hash table for export entry.
231 export_lookup(char *hname, char *path, int canonical)
235 exp_hash_entry *p_hen;
239 clp = client_lookup(hname, canonical);
243 pos = export_hash(path);
244 p_hen = &(exportlist[clp->m_type].entries[pos]);
245 for(exp = p_hen->p_first; exp && (exp != p_hen->p_last->m_next);
247 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
255 export_check(nfs_export *exp, struct hostent *hp, char *path)
257 if (strcmp(path, exp->m_export.e_path))
260 return client_check(exp->m_client, hp);
266 nfs_export *exp, *nxt;
269 for (i = 0; i < MCL_MAXTYPES; i++) {
270 for (exp = exportlist[i].p_head; exp; exp = nxt) {
272 client_release(exp->m_client);
273 if (exp->m_export.e_squids)
274 xfree(exp->m_export.e_squids);
275 if (exp->m_export.e_sqgids)
276 xfree(exp->m_export.e_sqgids);
277 if (exp->m_export.e_mountpoint)
278 free(exp->m_export.e_mountpoint);
279 if (exp->m_export.e_fslocdata)
280 free(exp->m_export.e_fslocdata);
281 xfree(exp->m_export.e_hostname);
284 for (j = 0; j < HASH_TABLE_SIZE; j++) {
285 exportlist[i].entries[j].p_first = NULL;
286 exportlist[i].entries[j].p_last = NULL;
288 exportlist[i].p_head = NULL;
294 * Compute and returns integer from string.
295 * Note: Its understood the smae integers can be same for
296 * different strings, but it should not matter.
304 while ( str[i] != '\0') {
315 export_hash(char *str)
317 int num = strtoint(str);
319 return num % HASH_TABLE_SIZE;