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);
32 export_free(nfs_export *exp)
34 xfree(exp->m_export.e_squids);
35 xfree(exp->m_export.e_sqgids);
36 free(exp->m_export.e_mountpoint);
37 free(exp->m_export.e_fslocdata);
39 xfree(exp->m_export.e_hostname);
43 static void warn_duplicated_exports(nfs_export *exp, struct exportent *eep)
45 if (exp->m_export.e_flags != eep->e_flags) {
46 xlog(L_ERROR, "incompatible duplicated export entries:");
47 xlog(L_ERROR, "\t%s:%s (0x%x) [IGNORED]", eep->e_hostname,
48 eep->e_path, eep->e_flags);
49 xlog(L_ERROR, "\t%s:%s (0x%x)", exp->m_export.e_hostname,
50 exp->m_export.e_path, exp->m_export.e_flags);
52 xlog(L_ERROR, "duplicated export entries:");
53 xlog(L_ERROR, "\t%s:%s", eep->e_hostname, eep->e_path);
54 xlog(L_ERROR, "\t%s:%s", exp->m_export.e_hostname,
55 exp->m_export.e_path);
60 export_read(char *fname)
62 struct exportent *eep;
65 setexportent(fname, "r");
66 while ((eep = getexportent(0,1)) != NULL) {
67 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
69 export_create(eep, 0);
71 warn_duplicated_exports(exp, eep);
78 * Create an in-core export struct from an export entry.
81 export_create(struct exportent *xep, int canonical)
86 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
87 /* bad export entry; complaint already logged */
90 exp = (nfs_export *) xmalloc(sizeof(*exp));
91 export_init(exp, clp, xep);
98 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
100 struct exportent *e = &exp->m_export;
102 dupexportent(e, nep);
104 e->e_hostname = xstrdup(nep->e_hostname);
108 exp->m_mayexport = 0;
116 * Duplicate exports data. The in-core export struct retains the
117 * original hostname from /etc/exports, while the in-core client struct
118 * gets the newly found FQDN.
121 export_dup(nfs_export *exp, struct hostent *hp)
126 new = (nfs_export *) xmalloc(sizeof(*new));
127 memcpy(new, exp, sizeof(*new));
128 dupexportent(&new->m_export, &exp->m_export);
129 if (exp->m_export.e_hostname)
130 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
131 clp = client_dup(exp->m_client, hp);
138 new->m_mayexport = exp->m_mayexport;
149 * Add export entry to hash table
152 export_add(nfs_export *exp)
154 exp_hash_table *p_tbl;
155 exp_hash_entry *p_hen;
158 int type = exp->m_client->m_type;
161 pos = export_hash(exp->m_export.e_path);
162 p_tbl = &(exportlist[type]); /* pointer to hash table */
163 p_hen = &(p_tbl->entries[pos]); /* pointer to hash table entry */
165 if (!(p_hen->p_first)) { /* hash table entry is empty */
166 p_hen->p_first = exp;
169 exp->m_next = p_tbl->p_head;
171 } else { /* hash table entry is NOT empty */
172 p_next = p_hen->p_last->m_next;
173 p_hen->p_last->m_next = exp;
174 exp->m_next = p_next;
180 export_find(struct hostent *hp, char *path)
185 for (i = 0; i < MCL_MAXTYPES; i++) {
186 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
187 if (!export_check(exp, hp, path))
189 if (exp->m_client->m_type == MCL_FQDN)
191 return export_dup(exp, hp);
199 export_allowed_internal (struct hostent *hp, char *path)
204 for (i = 0; i < MCL_MAXTYPES; i++) {
205 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
206 if (!exp->m_mayexport ||
207 !export_check(exp, hp, path))
217 export_allowed(struct hostent *hp, char *path)
220 char epath[MAXPATHLEN+1];
223 if (path [0] != '/') return NULL;
225 strncpy(epath, path, sizeof (epath) - 1);
226 epath[sizeof (epath) - 1] = '\0';
228 /* Try the longest matching exported pathname. */
230 exp = export_allowed_internal (hp, epath);
233 /* We have to treat the root, "/", specially. */
234 if (p == &epath[1]) break;
235 p = strrchr(epath, '/');
244 * Search hash table for export entry.
247 export_lookup(char *hname, char *path, int canonical)
251 exp_hash_entry *p_hen;
255 clp = client_lookup(hname, canonical);
259 pos = export_hash(path);
260 p_hen = &(exportlist[clp->m_type].entries[pos]);
261 for(exp = p_hen->p_first; exp && (exp != p_hen->p_last->m_next);
263 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
271 export_check(nfs_export *exp, struct hostent *hp, char *path)
273 if (strcmp(path, exp->m_export.e_path))
276 return client_check(exp->m_client, hp);
280 * export_freeall - deallocate all nfs_export records
286 nfs_export *exp, *nxt;
289 for (i = 0; i < MCL_MAXTYPES; i++) {
290 for (exp = exportlist[i].p_head; exp; exp = nxt) {
292 client_release(exp->m_client);
295 for (j = 0; j < HASH_TABLE_SIZE; j++) {
296 exportlist[i].entries[j].p_first = NULL;
297 exportlist[i].entries[j].p_last = NULL;
299 exportlist[i].p_head = NULL;
305 * Compute and returns integer from string.
306 * Note: Its understood the smae integers can be same for
307 * different strings, but it should not matter.
315 while ( str[i] != '\0') {
326 export_hash(char *str)
328 int num = strtoint(str);
330 return num % HASH_TABLE_SIZE;