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;
98 * Duplicate exports data. The in-core export struct retains the
99 * original hostname from /etc/exports, while the in-core client struct
100 * gets the newly found FQDN.
103 export_dup(nfs_export *exp, struct hostent *hp)
108 new = (nfs_export *) xmalloc(sizeof(*new));
109 memcpy(new, exp, sizeof(*new));
110 dupexportent(&new->m_export, &exp->m_export);
111 clp = client_dup(exp->m_client, hp);
114 new->m_mayexport = exp->m_mayexport;
124 export_add(nfs_export *exp)
127 int type = exp->m_client->m_type;
128 int slen = strlen(exp->m_export.e_path);
130 if (type < 0 || type >= MCL_MAXTYPES)
131 xlog(L_FATAL, "unknown client type in export_add");
133 epp = exportlist + type;
134 while (*epp && slen <= strlen((*epp)->m_export.e_path))
135 epp = &((*epp)->m_next);
141 export_find(struct hostent *hp, char *path)
146 for (i = 0; i < MCL_MAXTYPES; i++) {
147 for (exp = exportlist[i]; exp; exp = exp->m_next) {
148 if (!export_check(exp, hp, path))
150 if (exp->m_client->m_type == MCL_FQDN)
152 return export_dup(exp, hp);
160 export_allowed_internal (struct hostent *hp, char *path)
165 for (i = 0; i < MCL_MAXTYPES; i++) {
166 for (exp = exportlist[i]; exp; exp = exp->m_next) {
167 if (!exp->m_mayexport ||
168 !export_check(exp, hp, path))
178 export_allowed(struct hostent *hp, char *path)
181 char epath[MAXPATHLEN+1];
184 if (path [0] != '/') return NULL;
186 strncpy(epath, path, sizeof (epath) - 1);
187 epath[sizeof (epath) - 1] = '\0';
189 /* Try the longest matching exported pathname. */
191 exp = export_allowed_internal (hp, epath);
194 /* We have to treat the root, "/", specially. */
195 if (p == &epath[1]) break;
196 p = strrchr(epath, '/');
205 export_lookup(char *hname, char *path, int canonical)
210 if (!(clp = client_lookup(hname, canonical)))
212 for (exp = exportlist[clp->m_type]; exp; exp = exp->m_next)
213 if (exp->m_client == clp && !strcmp(exp->m_export.e_path, path))
219 export_check(nfs_export *exp, struct hostent *hp, char *path)
221 if (strcmp(path, exp->m_export.e_path))
224 return client_check(exp->m_client, hp);
230 nfs_export *exp, *nxt;
233 for (i = 0; i < MCL_MAXTYPES; i++) {
234 for (exp = exportlist[i]; exp; exp = nxt) {
236 client_release(exp->m_client);
237 if (exp->m_export.e_squids)
238 xfree(exp->m_export.e_squids);
239 if (exp->m_export.e_sqgids)
240 xfree(exp->m_export.e_sqgids);
241 if (exp->m_export.e_mountpoint)
242 free(exp->m_export.e_mountpoint);
245 exportlist[i] = NULL;
251 export_reset(nfs_export *exp)
256 /* Restore m_path. */
257 strncpy(exp->m_export.m_path, exp->m_export.e_path,
258 sizeof (exp->m_export.m_path) - 1);
259 exp->m_export.m_path[sizeof (exp->m_export.m_path) - 1] = '\0';