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 void export_add(nfs_export *exp);
28 static int export_check(const nfs_export *exp, const struct addrinfo *ai,
31 export_allowed_internal(const struct addrinfo *ai,
35 export_free(nfs_export *exp)
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);
42 xfree(exp->m_export.e_hostname);
46 static void warn_duplicated_exports(nfs_export *exp, struct exportent *eep)
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);
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);
63 * export_read - read entries from /etc/exports
64 * @fname: name of file to read from
68 export_read(char *fname)
70 struct exportent *eep;
73 setexportent(fname, "r");
74 while ((eep = getexportent(0,1)) != NULL) {
75 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
77 export_create(eep, 0);
79 warn_duplicated_exports(exp, eep);
85 * Create an in-core export struct from an export entry.
88 export_create(struct exportent *xep, int canonical)
93 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
94 /* bad export entry; complaint already logged */
97 exp = (nfs_export *) xmalloc(sizeof(*exp));
98 export_init(exp, clp, xep);
105 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
107 struct exportent *e = &exp->m_export;
109 dupexportent(e, nep);
111 e->e_hostname = xstrdup(nep->e_hostname);
115 exp->m_mayexport = 0;
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.
128 export_dup(nfs_export *exp, const struct addrinfo *ai)
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);
145 new->m_mayexport = exp->m_mayexport;
156 export_add(nfs_export *exp)
158 exp_hash_table *p_tbl;
159 exp_hash_entry *p_hen;
162 int type = exp->m_client->m_type;
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 */
169 if (!(p_hen->p_first)) { /* hash table entry is empty */
170 p_hen->p_first = exp;
173 exp->m_next = p_tbl->p_head;
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;
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
188 * Returns a pointer to nfs_export data matching @ai and @path,
189 * or NULL if an error occurs.
192 export_find(const struct addrinfo *ai, const char *path)
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))
201 if (exp->m_client->m_type == MCL_FQDN)
203 return export_dup(exp, ai);
211 export_allowed_internal(const struct addrinfo *ai, const char *path)
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))
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
233 * Returns a pointer to nfs_export data matching @ai and @path,
234 * or NULL if the export is not allowed.
237 export_allowed(const struct addrinfo *ai, const char *path)
240 char epath[MAXPATHLEN+1];
243 if (path [0] != '/') return NULL;
245 strncpy(epath, path, sizeof (epath) - 1);
246 epath[sizeof (epath) - 1] = '\0';
248 /* Try the longest matching exported pathname. */
250 exp = export_allowed_internal(ai, epath);
253 /* We have to treat the root, "/", specially. */
254 if (p == &epath[1]) break;
255 p = strrchr(epath, '/');
264 * Search hash table for export entry.
267 export_lookup(char *hname, char *path, int canonical)
271 exp_hash_entry *p_hen;
275 clp = client_lookup(hname, canonical);
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);
283 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
291 export_check(const nfs_export *exp, const struct addrinfo *ai, const char *path)
293 if (strcmp(path, exp->m_export.e_path))
296 return client_check(exp->m_client, ai);
300 * export_freeall - deallocate all nfs_export records
306 nfs_export *exp, *nxt;
309 for (i = 0; i < MCL_MAXTYPES; i++) {
310 for (exp = exportlist[i].p_head; exp; exp = nxt) {
312 client_release(exp->m_client);
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;
319 exportlist[i].p_head = NULL;
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.
335 while ( str[i] != '\0') {
346 export_hash(char *str)
348 int num = strtoint(str);
350 return num % HASH_TABLE_SIZE;