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(const nfs_export *exp, const struct addrinfo *ai,
30 export_allowed_internal(const struct addrinfo *ai,
34 export_free(nfs_export *exp)
36 xfree(exp->m_export.e_squids);
37 xfree(exp->m_export.e_sqgids);
38 free(exp->m_export.e_mountpoint);
39 free(exp->m_export.e_fslocdata);
41 xfree(exp->m_export.e_hostname);
45 static void warn_duplicated_exports(nfs_export *exp, struct exportent *eep)
47 if (exp->m_export.e_flags != eep->e_flags) {
48 xlog(L_ERROR, "incompatible duplicated export entries:");
49 xlog(L_ERROR, "\t%s:%s (0x%x) [IGNORED]", eep->e_hostname,
50 eep->e_path, eep->e_flags);
51 xlog(L_ERROR, "\t%s:%s (0x%x)", exp->m_export.e_hostname,
52 exp->m_export.e_path, exp->m_export.e_flags);
54 xlog(L_ERROR, "duplicated export entries:");
55 xlog(L_ERROR, "\t%s:%s", eep->e_hostname, eep->e_path);
56 xlog(L_ERROR, "\t%s:%s", exp->m_export.e_hostname,
57 exp->m_export.e_path);
62 export_read(char *fname)
64 struct exportent *eep;
67 setexportent(fname, "r");
68 while ((eep = getexportent(0,1)) != NULL) {
69 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
71 export_create(eep, 0);
73 warn_duplicated_exports(exp, eep);
80 * Create an in-core export struct from an export entry.
83 export_create(struct exportent *xep, int canonical)
88 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
89 /* bad export entry; complaint already logged */
92 exp = (nfs_export *) xmalloc(sizeof(*exp));
93 export_init(exp, clp, xep);
100 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
102 struct exportent *e = &exp->m_export;
104 dupexportent(e, nep);
106 e->e_hostname = xstrdup(nep->e_hostname);
110 exp->m_mayexport = 0;
118 * Duplicate exports data. The in-core export struct retains the
119 * original hostname from /etc/exports, while the in-core client struct
120 * gets the newly found FQDN.
123 export_dup(nfs_export *exp, const struct addrinfo *ai)
128 new = (nfs_export *) xmalloc(sizeof(*new));
129 memcpy(new, exp, sizeof(*new));
130 dupexportent(&new->m_export, &exp->m_export);
131 if (exp->m_export.e_hostname)
132 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
133 clp = client_dup(exp->m_client, ai);
140 new->m_mayexport = exp->m_mayexport;
151 * Add export entry to hash table
154 export_add(nfs_export *exp)
156 exp_hash_table *p_tbl;
157 exp_hash_entry *p_hen;
160 int type = exp->m_client->m_type;
163 pos = export_hash(exp->m_export.e_path);
164 p_tbl = &(exportlist[type]); /* pointer to hash table */
165 p_hen = &(p_tbl->entries[pos]); /* pointer to hash table entry */
167 if (!(p_hen->p_first)) { /* hash table entry is empty */
168 p_hen->p_first = exp;
171 exp->m_next = p_tbl->p_head;
173 } else { /* hash table entry is NOT empty */
174 p_next = p_hen->p_last->m_next;
175 p_hen->p_last->m_next = exp;
176 exp->m_next = p_next;
182 * export_find - find or create a suitable nfs_export for @ai and @path
183 * @ai: pointer to addrinfo for client
184 * @path: '\0'-terminated ASCII string containing export path
186 * Returns a pointer to nfs_export data matching @ai and @path,
187 * or NULL if an error occurs.
190 export_find(const struct addrinfo *ai, const char *path)
195 for (i = 0; i < MCL_MAXTYPES; i++) {
196 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
197 if (!export_check(exp, ai, path))
199 if (exp->m_client->m_type == MCL_FQDN)
201 return export_dup(exp, ai);
209 export_allowed_internal(const struct addrinfo *ai, const char *path)
214 for (i = 0; i < MCL_MAXTYPES; i++) {
215 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
216 if (!exp->m_mayexport ||
217 !export_check(exp, ai, path))
227 * export_allowed - determine if this export is allowed
228 * @ai: pointer to addrinfo for client
229 * @path: '\0'-terminated ASCII string containing export path
231 * Returns a pointer to nfs_export data matching @ai and @path,
232 * or NULL if the export is not allowed.
235 export_allowed(const struct addrinfo *ai, const char *path)
238 char epath[MAXPATHLEN+1];
241 if (path [0] != '/') return NULL;
243 strncpy(epath, path, sizeof (epath) - 1);
244 epath[sizeof (epath) - 1] = '\0';
246 /* Try the longest matching exported pathname. */
248 exp = export_allowed_internal(ai, epath);
251 /* We have to treat the root, "/", specially. */
252 if (p == &epath[1]) break;
253 p = strrchr(epath, '/');
262 * Search hash table for export entry.
265 export_lookup(char *hname, char *path, int canonical)
269 exp_hash_entry *p_hen;
273 clp = client_lookup(hname, canonical);
277 pos = export_hash(path);
278 p_hen = &(exportlist[clp->m_type].entries[pos]);
279 for(exp = p_hen->p_first; exp && (exp != p_hen->p_last->m_next);
281 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
289 export_check(const nfs_export *exp, const struct addrinfo *ai, const char *path)
291 if (strcmp(path, exp->m_export.e_path))
294 return client_check(exp->m_client, ai);
298 * export_freeall - deallocate all nfs_export records
304 nfs_export *exp, *nxt;
307 for (i = 0; i < MCL_MAXTYPES; i++) {
308 for (exp = exportlist[i].p_head; exp; exp = nxt) {
310 client_release(exp->m_client);
313 for (j = 0; j < HASH_TABLE_SIZE; j++) {
314 exportlist[i].entries[j].p_first = NULL;
315 exportlist[i].entries[j].p_last = NULL;
317 exportlist[i].p_head = NULL;
323 * Compute and returns integer from string.
324 * Note: Its understood the smae integers can be same for
325 * different strings, but it should not matter.
333 while ( str[i] != '\0') {
344 export_hash(char *str)
346 int num = strtoint(str);
348 return num % HASH_TABLE_SIZE;