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 nfs_export *exportlist[MCL_MAXTYPES] = { NULL, };
24 static void export_init(nfs_export *exp, nfs_client *clp,
25 struct exportent *nep);
26 static int export_check(nfs_export *, struct hostent *, char *);
28 export_allowed_internal(struct hostent *hp, char *path);
31 export_read(char *fname)
33 struct exportent *eep;
36 setexportent(fname, "r");
37 while ((eep = getexportent(0,1)) != NULL) {
38 exp = export_lookup(eep->e_hostname, eep->e_path, 0);
42 if (exp->m_export.e_flags != eep->e_flags) {
43 xlog(L_ERROR, "incompatible duplicated export entries:");
44 xlog(L_ERROR, "\t%s:%s (0x%x) [IGNORED]", eep->e_hostname,
45 eep->e_path, eep->e_flags);
46 xlog(L_ERROR, "\t%s:%s (0x%x)", exp->m_export.e_hostname,
47 exp->m_export.e_path, exp->m_export.e_flags);
50 xlog(L_ERROR, "duplicated export entries:");
51 xlog(L_ERROR, "\t%s:%s", eep->e_hostname, eep->e_path);
52 xlog(L_ERROR, "\t%s:%s", exp->m_export.e_hostname,
53 exp->m_export.e_path);
63 * Create an in-core export struct from an export entry.
66 export_create(struct exportent *xep, int canonical)
71 if (!(clp = client_lookup(xep->e_hostname, canonical))) {
72 /* bad export entry; complaint already logged */
75 exp = (nfs_export *) xmalloc(sizeof(*exp));
76 export_init(exp, clp, xep);
83 export_init(nfs_export *exp, nfs_client *clp, struct exportent *nep)
85 struct exportent *e = &exp->m_export;
99 * Duplicate exports data. The in-core export struct retains the
100 * original hostname from /etc/exports, while the in-core client struct
101 * gets the newly found FQDN.
104 export_dup(nfs_export *exp, struct hostent *hp)
109 new = (nfs_export *) xmalloc(sizeof(*new));
110 memcpy(new, exp, sizeof(*new));
111 dupexportent(&new->m_export, &exp->m_export);
112 clp = client_dup(exp->m_client, hp);
115 new->m_mayexport = exp->m_mayexport;
126 export_add(nfs_export *exp)
129 int type = exp->m_client->m_type;
130 int slen = strlen(exp->m_export.e_path);
132 if (type < 0 || type >= MCL_MAXTYPES)
133 xlog(L_FATAL, "unknown client type in export_add");
135 epp = exportlist + type;
136 while (*epp && slen <= strlen((*epp)->m_export.e_path))
137 epp = &((*epp)->m_next);
143 export_find(struct hostent *hp, char *path)
148 for (i = 0; i < MCL_MAXTYPES; i++) {
149 for (exp = exportlist[i]; exp; exp = exp->m_next) {
150 if (!export_check(exp, hp, path))
152 if (exp->m_client->m_type == MCL_FQDN)
154 return export_dup(exp, hp);
162 export_allowed_internal (struct hostent *hp, char *path)
167 for (i = 0; i < MCL_MAXTYPES; i++) {
168 for (exp = exportlist[i]; exp; exp = exp->m_next) {
169 if (!exp->m_mayexport ||
170 !export_check(exp, hp, path))
180 export_allowed(struct hostent *hp, char *path)
183 char epath[MAXPATHLEN+1];
186 if (path [0] != '/') return NULL;
188 strncpy(epath, path, sizeof (epath) - 1);
189 epath[sizeof (epath) - 1] = '\0';
191 /* Try the longest matching exported pathname. */
193 exp = export_allowed_internal (hp, epath);
196 /* We have to treat the root, "/", specially. */
197 if (p == &epath[1]) break;
198 p = strrchr(epath, '/');
207 export_lookup(char *hname, char *path, int canonical)
212 if (!(clp = client_lookup(hname, canonical)))
214 for (exp = exportlist[clp->m_type]; exp; exp = exp->m_next)
215 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path))
221 export_check(nfs_export *exp, struct hostent *hp, char *path)
223 if (strcmp(path, exp->m_export.e_path))
226 return client_check(exp->m_client, hp);
232 nfs_export *exp, *nxt;
235 for (i = 0; i < MCL_MAXTYPES; i++) {
236 for (exp = exportlist[i]; exp; exp = nxt) {
238 client_release(exp->m_client);
239 if (exp->m_export.e_squids)
240 xfree(exp->m_export.e_squids);
241 if (exp->m_export.e_sqgids)
242 xfree(exp->m_export.e_sqgids);
243 if (exp->m_export.e_mountpoint)
244 free(exp->m_export.e_mountpoint);
245 if (exp->m_export.e_fslocdata)
246 xfree(exp->m_export.e_fslocdata);
249 exportlist[i] = NULL;
255 export_reset(nfs_export *exp)
260 /* Restore m_path. */
261 strncpy(exp->m_export.m_path, exp->m_export.e_path,
262 sizeof (exp->m_export.m_path) - 1);
263 exp->m_export.m_path[sizeof (exp->m_export.m_path) - 1] = '\0';