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 exportent_release(struct exportent *eep)
39 free(eep->e_mountpoint);
40 free(eep->e_fslocdata);
42 xfree(eep->e_hostname);
46 export_free(nfs_export *exp)
48 exportent_release(&exp->m_export);
52 static void warn_duplicated_exports(nfs_export *exp, struct exportent *eep)
54 if (exp->m_export.e_flags != eep->e_flags) {
55 xlog(L_ERROR, "incompatible duplicated export entries:");
56 xlog(L_ERROR, "\t%s:%s (0x%x) [IGNORED]", eep->e_hostname,
57 eep->e_path, eep->e_flags);
58 xlog(L_ERROR, "\t%s:%s (0x%x)", exp->m_export.e_hostname,
59 exp->m_export.e_path, exp->m_export.e_flags);
61 xlog(L_ERROR, "duplicated export entries:");
62 xlog(L_ERROR, "\t%s:%s", eep->e_hostname, eep->e_path);
63 xlog(L_ERROR, "\t%s:%s", exp->m_export.e_hostname,
64 exp->m_export.e_path);
69 * export_read - read entries from /etc/exports
70 * @fname: name of file to read from
74 export_read(char *fname)
76 struct exportent *eep;
79 setexportent(fname, "r");
80 while ((eep = getexportent(0,1)) != NULL) {
81 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
83 export_create(eep, 0);
85 warn_duplicated_exports(exp, eep);
91 * export_create - create an in-core nfs_export record from an export entry
92 * @xep: export entry to lookup
93 * @canonical: if set, e_hostname is known to be canonical DNS name
95 * Returns a freshly instantiated export record, or NULL if
99 export_create(struct exportent *xep, int canonical)
104 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
105 /* bad export entry; complaint already logged */
108 exp = (nfs_export *) xmalloc(sizeof(*exp));
109 export_init(exp, clp, xep);
116 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
118 struct exportent *e = &exp->m_export;
120 dupexportent(e, nep);
122 e->e_hostname = xstrdup(nep->e_hostname);
126 exp->m_mayexport = 0;
134 * Duplicate exports data. The in-core export struct retains the
135 * original hostname from /etc/exports, while the in-core client struct
136 * gets the newly found FQDN.
139 export_dup(nfs_export *exp, const struct addrinfo *ai)
144 new = (nfs_export *) xmalloc(sizeof(*new));
145 memcpy(new, exp, sizeof(*new));
146 dupexportent(&new->m_export, &exp->m_export);
147 if (exp->m_export.e_hostname)
148 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
149 clp = client_dup(exp->m_client, ai);
156 new->m_mayexport = exp->m_mayexport;
167 export_add(nfs_export *exp)
169 exp_hash_table *p_tbl;
170 exp_hash_entry *p_hen;
173 int type = exp->m_client->m_type;
176 pos = export_hash(exp->m_export.e_path);
177 p_tbl = &(exportlist[type]); /* pointer to hash table */
178 p_hen = &(p_tbl->entries[pos]); /* pointer to hash table entry */
180 if (!(p_hen->p_first)) { /* hash table entry is empty */
181 p_hen->p_first = exp;
184 exp->m_next = p_tbl->p_head;
186 } else { /* hash table entry is NOT empty */
187 p_next = p_hen->p_last->m_next;
188 p_hen->p_last->m_next = exp;
189 exp->m_next = p_next;
195 * export_find - find or create a suitable nfs_export for @ai and @path
196 * @ai: pointer to addrinfo for client
197 * @path: '\0'-terminated ASCII string containing export path
199 * Returns a pointer to nfs_export data matching @ai and @path,
200 * or NULL if an error occurs.
203 export_find(const struct addrinfo *ai, const char *path)
208 for (i = 0; i < MCL_MAXTYPES; i++) {
209 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
210 if (!export_check(exp, ai, path))
212 if (exp->m_client->m_type == MCL_FQDN)
214 return export_dup(exp, ai);
222 export_allowed_internal(const struct addrinfo *ai, const char *path)
227 for (i = 0; i < MCL_MAXTYPES; i++) {
228 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
229 if (!exp->m_mayexport ||
230 !export_check(exp, ai, path))
240 * export_allowed - determine if this export is allowed
241 * @ai: pointer to addrinfo for client
242 * @path: '\0'-terminated ASCII string containing export path
244 * Returns a pointer to nfs_export data matching @ai and @path,
245 * or NULL if the export is not allowed.
248 export_allowed(const struct addrinfo *ai, const char *path)
251 char epath[MAXPATHLEN+1];
254 if (path [0] != '/') return NULL;
256 strncpy(epath, path, sizeof (epath) - 1);
257 epath[sizeof (epath) - 1] = '\0';
259 /* Try the longest matching exported pathname. */
261 exp = export_allowed_internal(ai, epath);
264 /* We have to treat the root, "/", specially. */
265 if (p == &epath[1]) break;
266 p = strrchr(epath, '/');
275 * export_lookup - search hash table for export entry
276 * @hname: '\0'-terminated ASCII string containing client hostname to look for
277 * @path: '\0'-terminated ASCII string containing export path to look for
278 * @canonical: if set, @hname is known to be canonical DNS name
280 * Returns a pointer to nfs_export record matching @hname and @path,
281 * or NULL if the export was not found.
284 export_lookup(char *hname, char *path, int canonical)
288 exp_hash_entry *p_hen;
292 clp = client_lookup(hname, canonical);
296 pos = export_hash(path);
297 p_hen = &(exportlist[clp->m_type].entries[pos]);
298 for(exp = p_hen->p_first; exp && (exp != p_hen->p_last->m_next);
300 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
308 export_check(const nfs_export *exp, const struct addrinfo *ai, const char *path)
310 if (strcmp(path, exp->m_export.e_path))
313 return client_check(exp->m_client, ai);
317 * export_freeall - deallocate all nfs_export records
323 nfs_export *exp, *nxt;
326 for (i = 0; i < MCL_MAXTYPES; i++) {
327 for (exp = exportlist[i].p_head; exp; exp = nxt) {
329 client_release(exp->m_client);
332 for (j = 0; j < HASH_TABLE_SIZE; j++) {
333 exportlist[i].entries[j].p_first = NULL;
334 exportlist[i].entries[j].p_last = NULL;
336 exportlist[i].p_head = NULL;
342 * Compute and returns integer from string.
343 * Note: Its understood the smae integers can be same for
344 * different strings, but it should not matter.
352 while ( str[i] != '\0') {
363 export_hash(char *str)
365 unsigned int num = strtoint(str);
367 return num % HASH_TABLE_SIZE;