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;
132 * Add export entry to hash table
135 export_add(nfs_export *exp)
137 exp_hash_table *p_tbl;
138 exp_hash_entry *p_hen;
141 int type = exp->m_client->m_type;
144 pos = export_hash(exp->m_export.e_path);
145 p_tbl = &(exportlist[type]); /* pointer to hash table */
146 p_hen = &(p_tbl->entries[pos]); /* pointer to hash table entry */
148 if (!(p_hen->p_first)) { /* hash table entry is empty */
149 p_hen->p_first = exp;
152 exp->m_next = p_tbl->p_head;
154 } else { /* hash table entry is NOT empty */
155 p_next = p_hen->p_last->m_next;
156 p_hen->p_last->m_next = exp;
157 exp->m_next = p_next;
163 export_find(struct hostent *hp, char *path)
168 for (i = 0; i < MCL_MAXTYPES; i++) {
169 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
170 if (!export_check(exp, hp, path))
172 if (exp->m_client->m_type == MCL_FQDN)
174 return export_dup(exp, hp);
182 export_allowed_internal (struct hostent *hp, char *path)
187 for (i = 0; i < MCL_MAXTYPES; i++) {
188 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
189 if (!exp->m_mayexport ||
190 !export_check(exp, hp, path))
200 export_allowed(struct hostent *hp, char *path)
203 char epath[MAXPATHLEN+1];
206 if (path [0] != '/') return NULL;
208 strncpy(epath, path, sizeof (epath) - 1);
209 epath[sizeof (epath) - 1] = '\0';
211 /* Try the longest matching exported pathname. */
213 exp = export_allowed_internal (hp, epath);
216 /* We have to treat the root, "/", specially. */
217 if (p == &epath[1]) break;
218 p = strrchr(epath, '/');
227 * Search hash table for export entry.
230 export_lookup(char *hname, char *path, int canonical)
234 exp_hash_entry *p_hen;
238 clp = client_lookup(hname, canonical);
242 pos = export_hash(path);
243 p_hen = &(exportlist[clp->m_type].entries[pos]);
244 for(exp = p_hen->p_first; exp && (exp != p_hen->p_last->m_next);
246 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
254 export_check(nfs_export *exp, struct hostent *hp, char *path)
256 if (strcmp(path, exp->m_export.e_path))
259 return client_check(exp->m_client, hp);
265 nfs_export *exp, *nxt;
268 for (i = 0; i < MCL_MAXTYPES; i++) {
269 for (exp = exportlist[i].p_head; exp; exp = nxt) {
271 client_release(exp->m_client);
272 if (exp->m_export.e_squids)
273 xfree(exp->m_export.e_squids);
274 if (exp->m_export.e_sqgids)
275 xfree(exp->m_export.e_sqgids);
276 if (exp->m_export.e_mountpoint)
277 free(exp->m_export.e_mountpoint);
278 if (exp->m_export.e_fslocdata)
279 xfree(exp->m_export.e_fslocdata);
280 xfree(exp->m_export.e_hostname);
283 for(j = 0; j < HASH_TABLE_SIZE; j++) {
284 exportlist[i].entries[j].p_first = NULL;
285 exportlist[i].entries[j].p_last = NULL;
287 exportlist[i].p_head = NULL;
293 * Compute and returns integer from string.
294 * Note: Its understood the smae integers can be same for
295 * different strings, but it should not matter.
303 while ( str[i] != '\0') {
314 export_hash(char *str)
316 int num = strtoint(str);
318 return num % HASH_TABLE_SIZE;