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));
116 get_client_hostname(const struct sockaddr *caller, struct addrinfo *ai,
117 enum auth_error *error)
119 char buf[INET6_ADDRSTRLEN];
123 return strdup(host_ntop(caller, buf, sizeof(buf)));
124 n = client_compose(ai);
125 *error = unknown_host;
131 return strdup("DEFAULT");
134 /* return static nfs_export with details filled in */
136 auth_authenticate_newcache(const struct sockaddr *caller,
137 const char *path, struct addrinfo *ai,
138 enum auth_error *error)
143 free(my_client.m_hostname);
145 my_client.m_hostname = get_client_hostname(caller, ai, error);
146 if (my_client.m_hostname == NULL)
149 my_client.m_naddr = 1;
150 set_addrlist(&my_client, 0, caller);
151 my_exp.m_client = &my_client;
154 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
155 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
156 if (strcmp(path, exp->m_export.e_path))
158 if (!use_ipaddr && !client_member(my_client.m_hostname, exp->m_client->m_hostname))
160 if (use_ipaddr && !client_check(exp->m_client, ai))
164 *error = not_exported;
168 my_exp.m_export = exp->m_export;
174 auth_authenticate_internal(const struct sockaddr *caller, const char *path,
175 struct addrinfo *ai, enum auth_error *error)
180 exp = auth_authenticate_newcache(caller, path, ai, error);
184 exp = export_find(ai, path);
190 if (exp->m_export.e_flags & NFSEXP_V4ROOT) {
194 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
195 nfs_get_port(caller) >= IPPORT_RESERVED) {
196 *error = illegal_port;
205 auth_authenticate(const char *what, const struct sockaddr *caller,
208 nfs_export *exp = NULL;
209 char epath[MAXPATHLEN+1];
211 char buf[INET6_ADDRSTRLEN];
212 struct addrinfo *ai = NULL;
213 enum auth_error error = bad_path;
215 if (path[0] != '/') {
216 xlog(L_WARNING, "Bad path in %s request from %s: \"%s\"",
217 what, host_ntop(caller, buf, sizeof(buf)), path);
221 strncpy(epath, path, sizeof (epath) - 1);
222 epath[sizeof (epath) - 1] = '\0';
223 auth_fixpath(epath); /* strip duplicate '/' etc */
225 ai = client_resolve(caller);
229 /* Try the longest matching exported pathname. */
231 exp = auth_authenticate_internal(caller, epath, ai, &error);
232 if (exp || (error != not_exported && error != no_entry))
234 /* We have to treat the root, "/", specially. */
235 if (p == &epath[1]) break;
236 p = strrchr(epath, '/');
243 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
244 what, host_ntop(caller, buf, sizeof(buf)), path);
248 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
249 what, host_ntop(caller, buf, sizeof(buf)), path, epath);
253 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
254 what, ai->ai_canonname, path, epath);
258 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
259 what, ai->ai_canonname, path, epath);
263 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %u",
264 what, ai->ai_canonname, path, epath, nfs_get_port(caller));
268 xlog(L_NOTICE, "authenticated %s request from %s:%u for %s (%s)",
269 what, ai->ai_canonname, nfs_get_port(caller), path, epath);
272 xlog(L_NOTICE, "%s request from %s:%u for %s (%s) gave %d",
273 what, ai->ai_canonname, nfs_get_port(caller),
282 auth_fixpath(char *path)
286 for (sp = cp = path; *sp; sp++) {
287 if (*sp != '/' || sp[1] != '/')
290 while (cp > path+1 && cp[-1] == '/')