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))
162 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
163 /* not acceptable for v[23] export */
167 *error = not_exported;
171 my_exp.m_export = exp->m_export;
177 auth_authenticate_internal(const struct sockaddr *caller, const char *path,
178 struct addrinfo *ai, enum auth_error *error)
183 exp = auth_authenticate_newcache(caller, path, ai, error);
187 exp = export_find(ai, path);
193 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
194 nfs_get_port(caller) >= IPPORT_RESERVED) {
195 *error = illegal_port;
204 auth_authenticate(const char *what, const struct sockaddr *caller,
207 nfs_export *exp = NULL;
208 char epath[MAXPATHLEN+1];
210 char buf[INET6_ADDRSTRLEN];
211 struct addrinfo *ai = NULL;
212 enum auth_error error = bad_path;
214 if (path[0] != '/') {
215 xlog(L_WARNING, "Bad path in %s request from %s: \"%s\"",
216 what, host_ntop(caller, buf, sizeof(buf)), path);
220 strncpy(epath, path, sizeof (epath) - 1);
221 epath[sizeof (epath) - 1] = '\0';
222 auth_fixpath(epath); /* strip duplicate '/' etc */
224 ai = client_resolve(caller);
228 /* Try the longest matching exported pathname. */
230 exp = auth_authenticate_internal(caller, epath, ai, &error);
231 if (exp || (error != not_exported && error != no_entry))
233 /* We have to treat the root, "/", specially. */
234 if (p == &epath[1]) break;
235 p = strrchr(epath, '/');
242 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
243 what, host_ntop(caller, buf, sizeof(buf)), path);
247 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
248 what, host_ntop(caller, buf, sizeof(buf)), path, epath);
252 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
253 what, ai->ai_canonname, path, epath);
257 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
258 what, ai->ai_canonname, path, epath);
262 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %u",
263 what, ai->ai_canonname, path, epath, nfs_get_port(caller));
267 xlog(L_NOTICE, "authenticated %s request from %s:%u for %s (%s)",
268 what, ai->ai_canonname, nfs_get_port(caller), path, epath);
271 xlog(L_NOTICE, "%s request from %s:%u for %s (%s) gave %d",
272 what, ai->ai_canonname, nfs_get_port(caller),
281 auth_fixpath(char *path)
285 for (sp = cp = path; *sp; sp++) {
286 if (*sp != '/' || sp[1] != '/')
289 while (cp > path+1 && cp[-1] == '/')