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;
89 e->e_hostname = xstrdup(nep->e_hostname);
101 * Duplicate exports data. The in-core export struct retains the
102 * original hostname from /etc/exports, while the in-core client struct
103 * gets the newly found FQDN.
106 export_dup(nfs_export *exp, struct hostent *hp)
111 new = (nfs_export *) xmalloc(sizeof(*new));
112 memcpy(new, exp, sizeof(*new));
113 dupexportent(&new->m_export, &exp->m_export);
114 if (exp->m_export.e_hostname)
115 new->m_export.e_hostname = xstrdup(exp->m_export.e_hostname);
116 clp = client_dup(exp->m_client, hp);
119 new->m_mayexport = exp->m_mayexport;
130 export_add(nfs_export *exp)
133 int type = exp->m_client->m_type;
134 int slen = strlen(exp->m_export.e_path);
136 if (type < 0 || type >= MCL_MAXTYPES)
137 xlog(L_FATAL, "unknown client type in export_add");
139 epp = exportlist + type;
140 while (*epp && slen <= strlen((*epp)->m_export.e_path))
141 epp = &((*epp)->m_next);
147 export_find(struct hostent *hp, char *path)
152 for (i = 0; i < MCL_MAXTYPES; i++) {
153 for (exp = exportlist[i]; exp; exp = exp->m_next) {
154 if (!export_check(exp, hp, path))
156 if (exp->m_client->m_type == MCL_FQDN)
158 return export_dup(exp, hp);
166 export_allowed_internal (struct hostent *hp, char *path)
171 for (i = 0; i < MCL_MAXTYPES; i++) {
172 for (exp = exportlist[i]; exp; exp = exp->m_next) {
173 if (!exp->m_mayexport ||
174 !export_check(exp, hp, path))
184 export_allowed(struct hostent *hp, char *path)
187 char epath[MAXPATHLEN+1];
190 if (path [0] != '/') return NULL;
192 strncpy(epath, path, sizeof (epath) - 1);
193 epath[sizeof (epath) - 1] = '\0';
195 /* Try the longest matching exported pathname. */
197 exp = export_allowed_internal (hp, epath);
200 /* We have to treat the root, "/", specially. */
201 if (p == &epath[1]) break;
202 p = strrchr(epath, '/');
211 export_lookup(char *hname, char *path, int canonical)
216 if (!(clp = client_lookup(hname, canonical)))
218 for (exp = exportlist[clp->m_type]; exp; exp = exp->m_next)
219 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path))
225 export_check(nfs_export *exp, struct hostent *hp, char *path)
227 if (strcmp(path, exp->m_export.e_path))
230 return client_check(exp->m_client, hp);
236 nfs_export *exp, *nxt;
239 for (i = 0; i < MCL_MAXTYPES; i++) {
240 for (exp = exportlist[i]; exp; exp = nxt) {
242 client_release(exp->m_client);
243 if (exp->m_export.e_squids)
244 xfree(exp->m_export.e_squids);
245 if (exp->m_export.e_sqgids)
246 xfree(exp->m_export.e_sqgids);
247 if (exp->m_export.e_mountpoint)
248 free(exp->m_export.e_mountpoint);
249 if (exp->m_export.e_fslocdata)
250 xfree(exp->m_export.e_fslocdata);
251 xfree(exp->m_export.e_hostname);
254 exportlist[i] = NULL;