2 * support/export/export.c
4 * Maintain list of exported file systems.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
12 #include <sys/types.h>
13 #include <sys/param.h>
14 #include <netinet/in.h>
20 nfs_export *exportlist[MCL_MAXTYPES] = { NULL, };
22 static void export_init(nfs_export *exp, nfs_client *clp,
23 struct exportent *nep);
24 static int export_check(nfs_export *, struct hostent *, char *);
26 export_allowed_internal(struct hostent *hp, char *path);
29 export_read(char *fname)
31 struct exportent *eep;
34 setexportent(fname, "r");
35 while ((eep = getexportent(0,1)) != NULL) {
36 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
40 if (exp->m_export.e_flags != eep->e_flags) {
41 xlog(L_ERROR, "incompatible duplicated export entries:");
42 xlog(L_ERROR, "\t%s:%s (0x%x) [IGNORED]", eep->e_hostname,
43 eep->e_path, eep->e_flags);
44 xlog(L_ERROR, "\t%s:%s (0x%x)", exp->m_export.e_hostname,
45 exp->m_export.e_path, exp->m_export.e_flags);
48 xlog(L_ERROR, "duplicated export entries:");
49 xlog(L_ERROR, "\t%s:%s", eep->e_hostname, eep->e_path);
50 xlog(L_ERROR, "\t%s:%s", exp->m_export.e_hostname,
51 exp->m_export.e_path);
61 * Create an in-core export struct from an export entry.
64 export_create(struct exportent *xep, int canonical)
69 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
70 /* bad export entry; complaint already logged */
73 exp = (nfs_export *) xmalloc(sizeof(*exp));
74 export_init(exp, clp, xep);
81 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
83 struct exportent *e = &exp->m_export;
96 * Duplicate exports data. The in-core export struct retains the
97 * original hostname from /etc/exports, while the in-core client struct
98 * gets the newly found FQDN.
101 export_dup(nfs_export *exp, struct hostent *hp)
106 new = (nfs_export *) xmalloc(sizeof(*new));
107 memcpy(new, exp, sizeof(*new));
108 dupexportent(&new->m_export, &exp->m_export);
109 clp = client_dup(exp->m_client, hp);
112 new->m_mayexport = exp->m_mayexport;
122 export_add(nfs_export *exp)
125 int type = exp->m_client->m_type;
126 int slen = strlen(exp->m_export.e_path);
128 if (type < 0 || type >= MCL_MAXTYPES)
129 xlog(L_FATAL, "unknown client type in export_add");
131 epp = exportlist + type;
132 while (*epp && slen < strlen((*epp)->m_export.e_path))
133 epp = &((*epp)->m_next);
139 export_find(struct hostent *hp, char *path)
144 for (i = 0; i < MCL_MAXTYPES; i++) {
145 for (exp = exportlist[i]; exp; exp = exp->m_next) {
146 if (!export_check(exp, hp, path))
148 if (exp->m_client->m_type == MCL_FQDN)
150 return export_dup(exp, hp);
158 export_allowed_internal (struct hostent *hp, char *path)
163 for (i = 0; i < MCL_MAXTYPES; i++) {
164 for (exp = exportlist[i]; exp; exp = exp->m_next) {
165 if (!exp->m_mayexport ||
166 !export_check(exp, hp, path))
176 export_allowed(struct hostent *hp, char *path)
178 static struct exportent ee;
180 char epath[MAXPATHLEN+1];
183 if (path [0] != '/') return NULL;
185 strncpy(epath, path, sizeof (epath) - 1);
186 epath[sizeof (epath) - 1] = '\0';
188 /* Try the longest matching exported pathname. */
190 exp = export_allowed_internal (hp, epath);
192 dupexportent(&ee, &exp->m_export);
195 /* We have to treat the root, "/", specially. */
196 if (p == &epath[1]) break;
197 p = strrchr(epath, '/');
206 export_lookup(char *hname, char *path, int canonical)
211 if (!(clp = client_lookup(hname, canonical)))
213 for (exp = exportlist[clp->m_type]; exp; exp = exp->m_next)
214 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path))
220 export_check(nfs_export *exp, struct hostent *hp, char *path)
222 if (strcmp(path, exp->m_export.e_path))
225 return client_check(exp->m_client, hp);
231 nfs_export *exp, *nxt;
234 for (i = 0; i < MCL_MAXTYPES; i++) {
235 for (exp = exportlist[i]; exp; exp = nxt) {
237 client_release(exp->m_client);
238 if (exp->m_export.e_squids)
239 xfree(exp->m_export.e_squids);
240 if (exp->m_export.e_sqgids)
241 xfree(exp->m_export.e_sqgids);
244 exportlist[i] = NULL;
250 export_reset(nfs_export *exp)
255 /* Restore m_path. */
256 strncpy(exp->m_export.m_path, exp->m_export.e_path,
257 sizeof (exp->m_export.m_path) - 1);
258 exp->m_export.m_path[sizeof (exp->m_export.m_path) - 1] = '\0';