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 - read entries from /etc/exports
63 * @fname: name of file to read from
67 export_read(char *fname)
69 struct exportent *eep;
72 setexportent(fname, "r");
73 while ((eep = getexportent(0,1)) != NULL) {
74 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
76 export_create(eep, 0);
78 warn_duplicated_exports(exp, eep);
84 * Create an in-core export struct from an export entry.
87 export_create(struct exportent *xep, int canonical)
92 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
93 /* bad export entry; complaint already logged */
96 exp = (nfs_export *) xmalloc(sizeof(*exp));
97 export_init(exp, clp, xep);
104 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
106 struct exportent *e = &exp->m_export;
108 dupexportent(e, nep);
110 e->e_hostname = xstrdup(nep->e_hostname);
114 exp->m_mayexport = 0;
122 * Duplicate exports data. The in-core export struct retains the
123 * original hostname from /etc/exports, while the in-core client struct
124 * gets the newly found FQDN.
127 export_dup(nfs_export *exp, const struct addrinfo *ai)
132 new = (nfs_export *) xmalloc(sizeof(*new));
133 memcpy(new, exp, sizeof(*new));
134 dupexportent(&new->m_export, &exp->m_export);
135 if (exp->m_export.e_hostname)
136 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
137 clp = client_dup(exp->m_client, ai);
144 new->m_mayexport = exp->m_mayexport;
155 * Add export entry to hash table
158 export_add(nfs_export *exp)
160 exp_hash_table *p_tbl;
161 exp_hash_entry *p_hen;
164 int type = exp->m_client->m_type;
167 pos = export_hash(exp->m_export.e_path);
168 p_tbl = &(exportlist[type]); /* pointer to hash table */
169 p_hen = &(p_tbl->entries[pos]); /* pointer to hash table entry */
171 if (!(p_hen->p_first)) { /* hash table entry is empty */
172 p_hen->p_first = exp;
175 exp->m_next = p_tbl->p_head;
177 } else { /* hash table entry is NOT empty */
178 p_next = p_hen->p_last->m_next;
179 p_hen->p_last->m_next = exp;
180 exp->m_next = p_next;
186 * export_find - find or create a suitable nfs_export for @ai and @path
187 * @ai: pointer to addrinfo for client
188 * @path: '\0'-terminated ASCII string containing export path
190 * Returns a pointer to nfs_export data matching @ai and @path,
191 * or NULL if an error occurs.
194 export_find(const struct addrinfo *ai, const char *path)
199 for (i = 0; i < MCL_MAXTYPES; i++) {
200 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
201 if (!export_check(exp, ai, path))
203 if (exp->m_client->m_type == MCL_FQDN)
205 return export_dup(exp, ai);
213 export_allowed_internal(const struct addrinfo *ai, const char *path)
218 for (i = 0; i < MCL_MAXTYPES; i++) {
219 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
220 if (!exp->m_mayexport ||
221 !export_check(exp, ai, path))
231 * export_allowed - determine if this export is allowed
232 * @ai: pointer to addrinfo for client
233 * @path: '\0'-terminated ASCII string containing export path
235 * Returns a pointer to nfs_export data matching @ai and @path,
236 * or NULL if the export is not allowed.
239 export_allowed(const struct addrinfo *ai, const char *path)
242 char epath[MAXPATHLEN+1];
245 if (path [0] != '/') return NULL;
247 strncpy(epath, path, sizeof (epath) - 1);
248 epath[sizeof (epath) - 1] = '\0';
250 /* Try the longest matching exported pathname. */
252 exp = export_allowed_internal(ai, epath);
255 /* We have to treat the root, "/", specially. */
256 if (p == &epath[1]) break;
257 p = strrchr(epath, '/');
266 * Search hash table for export entry.
269 export_lookup(char *hname, char *path, int canonical)
273 exp_hash_entry *p_hen;
277 clp = client_lookup(hname, canonical);
281 pos = export_hash(path);
282 p_hen = &(exportlist[clp->m_type].entries[pos]);
283 for(exp = p_hen->p_first; exp && (exp != p_hen->p_last->m_next);
285 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
293 export_check(const nfs_export *exp, const struct addrinfo *ai, const char *path)
295 if (strcmp(path, exp->m_export.e_path))
298 return client_check(exp->m_client, ai);
302 * export_freeall - deallocate all nfs_export records
308 nfs_export *exp, *nxt;
311 for (i = 0; i < MCL_MAXTYPES; i++) {
312 for (exp = exportlist[i].p_head; exp; exp = nxt) {
314 client_release(exp->m_client);
317 for (j = 0; j < HASH_TABLE_SIZE; j++) {
318 exportlist[i].entries[j].p_first = NULL;
319 exportlist[i].entries[j].p_last = NULL;
321 exportlist[i].p_head = NULL;
327 * Compute and returns integer from string.
328 * Note: Its understood the smae integers can be same for
329 * different strings, but it should not matter.
337 while ( str[i] != '\0') {
348 export_hash(char *str)
350 int num = strtoint(str);
352 return num % HASH_TABLE_SIZE;