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 * export_create - create an in-core nfs_export record from an export entry
86 * @xep: export entry to lookup
87 * @canonical: if set, e_hostname is known to be canonical DNS name
89 * Returns a freshly instantiated export record, or NULL if
93 export_create(struct exportent *xep, int canonical)
98 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
99 /* bad export entry; complaint already logged */
102 exp = (nfs_export *) xmalloc(sizeof(*exp));
103 export_init(exp, clp, xep);
110 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
112 struct exportent *e = &exp->m_export;
114 dupexportent(e, nep);
116 e->e_hostname = xstrdup(nep->e_hostname);
120 exp->m_mayexport = 0;
128 * Duplicate exports data. The in-core export struct retains the
129 * original hostname from /etc/exports, while the in-core client struct
130 * gets the newly found FQDN.
133 export_dup(nfs_export *exp, const struct addrinfo *ai)
138 new = (nfs_export *) xmalloc(sizeof(*new));
139 memcpy(new, exp, sizeof(*new));
140 dupexportent(&new->m_export, &exp->m_export);
141 if (exp->m_export.e_hostname)
142 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
143 clp = client_dup(exp->m_client, ai);
150 new->m_mayexport = exp->m_mayexport;
161 export_add(nfs_export *exp)
163 exp_hash_table *p_tbl;
164 exp_hash_entry *p_hen;
167 int type = exp->m_client->m_type;
170 pos = export_hash(exp->m_export.e_path);
171 p_tbl = &(exportlist[type]); /* pointer to hash table */
172 p_hen = &(p_tbl->entries[pos]); /* pointer to hash table entry */
174 if (!(p_hen->p_first)) { /* hash table entry is empty */
175 p_hen->p_first = exp;
178 exp->m_next = p_tbl->p_head;
180 } else { /* hash table entry is NOT empty */
181 p_next = p_hen->p_last->m_next;
182 p_hen->p_last->m_next = exp;
183 exp->m_next = p_next;
189 * export_find - find or create a suitable nfs_export for @ai and @path
190 * @ai: pointer to addrinfo for client
191 * @path: '\0'-terminated ASCII string containing export path
193 * Returns a pointer to nfs_export data matching @ai and @path,
194 * or NULL if an error occurs.
197 export_find(const struct addrinfo *ai, const char *path)
202 for (i = 0; i < MCL_MAXTYPES; i++) {
203 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
204 if (!export_check(exp, ai, path))
206 if (exp->m_client->m_type == MCL_FQDN)
208 return export_dup(exp, ai);
216 export_allowed_internal(const struct addrinfo *ai, const char *path)
221 for (i = 0; i < MCL_MAXTYPES; i++) {
222 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
223 if (!exp->m_mayexport ||
224 !export_check(exp, ai, path))
234 * export_allowed - determine if this export is allowed
235 * @ai: pointer to addrinfo for client
236 * @path: '\0'-terminated ASCII string containing export path
238 * Returns a pointer to nfs_export data matching @ai and @path,
239 * or NULL if the export is not allowed.
242 export_allowed(const struct addrinfo *ai, const char *path)
245 char epath[MAXPATHLEN+1];
248 if (path [0] != '/') return NULL;
250 strncpy(epath, path, sizeof (epath) - 1);
251 epath[sizeof (epath) - 1] = '\0';
253 /* Try the longest matching exported pathname. */
255 exp = export_allowed_internal(ai, epath);
258 /* We have to treat the root, "/", specially. */
259 if (p == &epath[1]) break;
260 p = strrchr(epath, '/');
269 * export_lookup - search hash table for export entry
270 * @hname: '\0'-terminated ASCII string containing client hostname to look for
271 * @path: '\0'-terminated ASCII string containing export path to look for
272 * @canonical: if set, @hname is known to be canonical DNS name
274 * Returns a pointer to nfs_export record matching @hname and @path,
275 * or NULL if the export was not found.
278 export_lookup(char *hname, char *path, int canonical)
282 exp_hash_entry *p_hen;
286 clp = client_lookup(hname, canonical);
290 pos = export_hash(path);
291 p_hen = &(exportlist[clp->m_type].entries[pos]);
292 for(exp = p_hen->p_first; exp && (exp != p_hen->p_last->m_next);
294 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path)) {
302 export_check(const nfs_export *exp, const struct addrinfo *ai, const char *path)
304 if (strcmp(path, exp->m_export.e_path))
307 return client_check(exp->m_client, ai);
311 * export_freeall - deallocate all nfs_export records
317 nfs_export *exp, *nxt;
320 for (i = 0; i < MCL_MAXTYPES; i++) {
321 for (exp = exportlist[i].p_head; exp; exp = nxt) {
323 client_release(exp->m_client);
326 for (j = 0; j < HASH_TABLE_SIZE; j++) {
327 exportlist[i].entries[j].p_first = NULL;
328 exportlist[i].entries[j].p_last = NULL;
330 exportlist[i].p_head = NULL;
336 * Compute and returns integer from string.
337 * Note: Its understood the smae integers can be same for
338 * different strings, but it should not matter.
346 while ( str[i] != '\0') {
357 export_hash(char *str)
359 int num = strtoint(str);
361 return num % HASH_TABLE_SIZE;