4 * Authentication procedures for mountd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
37 static void auth_fixpath(char *path);
38 static char *export_file = NULL;
39 static nfs_export my_exp;
40 static nfs_client my_client;
43 extern int use_ipaddr;
46 auth_init(char *exports)
49 export_file = exports;
55 * A client can match many different netgroups and it's tough to know
56 * beforehand whether it will. If the concatenated string of netgroup
57 * m_hostnames is >512 bytes, then enable the "use_ipaddr" mode. This
58 * makes mountd change how it matches a client ip address when a mount
59 * request comes in. It's more efficient at handling netgroups at the
60 * expense of larger kernel caches.
66 int old_use_ipaddr = use_ipaddr;
69 /* add length of m_hostname + 1 for the comma */
70 for (clp = clientlist[MCL_NETGROUP]; clp; clp = clp->m_next)
71 len += (strlen(clp->m_hostname) + 1);
73 if (len > (NFSCLNT_IDMAX / 2))
78 if (use_ipaddr != old_use_ipaddr)
86 static ino_t last_inode;
88 static unsigned int counter;
91 if ((fd = open(_PATH_ETAB, O_RDONLY)) < 0) {
92 xlog(L_FATAL, "couldn't open %s", _PATH_ETAB);
93 } else if (fstat(fd, &stb) < 0) {
94 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
95 } else if (stb.st_ino == last_inode) {
101 last_inode = stb.st_ino;
105 memset(&my_client, 0, sizeof(my_client));
115 static char *get_client_ipaddr_name(const struct sockaddr *caller)
117 char buf[INET6_ADDRSTRLEN + 1];
120 host_ntop(caller, buf + 1, sizeof(buf) - 1);
125 get_client_hostname(const struct sockaddr *caller, struct addrinfo *ai,
126 enum auth_error *error)
131 return get_client_ipaddr_name(caller);
132 n = client_compose(ai);
133 *error = unknown_host;
139 return strdup("DEFAULT");
142 bool ipaddr_client_matches(nfs_export *exp, struct addrinfo *ai)
144 return client_check(exp->m_client, ai);
147 bool namelist_client_matches(nfs_export *exp, char *dom)
149 return client_member(dom, exp->m_client->m_hostname);
152 bool client_matches(nfs_export *exp, char *dom, struct addrinfo *ai)
154 if (is_ipaddr_client(dom))
155 return ipaddr_client_matches(exp, ai);
156 return namelist_client_matches(exp, dom);
159 /* return static nfs_export with details filled in */
161 auth_authenticate_newcache(const struct sockaddr *caller,
162 const char *path, struct addrinfo *ai,
163 enum auth_error *error)
168 free(my_client.m_hostname);
170 my_client.m_hostname = get_client_hostname(caller, ai, error);
171 if (my_client.m_hostname == NULL)
174 my_client.m_naddr = 1;
175 set_addrlist(&my_client, 0, caller);
176 my_exp.m_client = &my_client;
179 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
180 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
181 if (strcmp(path, exp->m_export.e_path))
183 if (!client_matches(exp, my_client.m_hostname, ai))
185 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
186 /* not acceptable for v[23] export */
190 *error = not_exported;
194 my_exp.m_export = exp->m_export;
200 auth_authenticate_internal(const struct sockaddr *caller, const char *path,
201 struct addrinfo *ai, enum auth_error *error)
206 exp = auth_authenticate_newcache(caller, path, ai, error);
210 exp = export_find(ai, path);
216 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
217 nfs_get_port(caller) >= IPPORT_RESERVED) {
218 *error = illegal_port;
227 auth_authenticate(const char *what, const struct sockaddr *caller,
230 nfs_export *exp = NULL;
231 char epath[MAXPATHLEN+1];
233 char buf[INET6_ADDRSTRLEN];
234 struct addrinfo *ai = NULL;
235 enum auth_error error = bad_path;
237 if (path[0] != '/') {
238 xlog(L_WARNING, "Bad path in %s request from %s: \"%s\"",
239 what, host_ntop(caller, buf, sizeof(buf)), path);
243 strncpy(epath, path, sizeof (epath) - 1);
244 epath[sizeof (epath) - 1] = '\0';
245 auth_fixpath(epath); /* strip duplicate '/' etc */
247 ai = client_resolve(caller);
251 /* Try the longest matching exported pathname. */
253 exp = auth_authenticate_internal(caller, epath, ai, &error);
254 if (exp || (error != not_exported && error != no_entry))
256 /* We have to treat the root, "/", specially. */
257 if (p == &epath[1]) break;
258 p = strrchr(epath, '/');
265 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
266 what, host_ntop(caller, buf, sizeof(buf)), path);
270 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
271 what, host_ntop(caller, buf, sizeof(buf)), path, epath);
275 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
276 what, ai->ai_canonname, path, epath);
280 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
281 what, ai->ai_canonname, path, epath);
285 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %u",
286 what, ai->ai_canonname, path, epath, nfs_get_port(caller));
290 xlog(L_NOTICE, "authenticated %s request from %s:%u for %s (%s)",
291 what, ai->ai_canonname, nfs_get_port(caller), path, epath);
294 xlog(L_NOTICE, "%s request from %s:%u for %s (%s) gave %d",
295 what, ai->ai_canonname, nfs_get_port(caller),
304 auth_fixpath(char *path)
308 for (sp = cp = path; *sp; sp++) {
309 if (*sp != '/' || sp[1] != '/')
312 while (cp > path+1 && cp[-1] == '/')