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>
35 static void auth_fixpath(char *path);
36 static char *export_file = NULL;
37 static nfs_export my_exp;
38 static nfs_client my_client;
41 extern int use_ipaddr;
44 auth_init(char *exports)
47 export_file = exports;
53 * A client can match many different netgroups and it's tough to know
54 * beforehand whether it will. If the concatenated string of netgroup
55 * m_hostnames is >512 bytes, then enable the "use_ipaddr" mode. This
56 * makes mountd change how it matches a client ip address when a mount
57 * request comes in. It's more efficient at handling netgroups at the
58 * expense of larger kernel caches.
64 int old_use_ipaddr = use_ipaddr;
67 /* add length of m_hostname + 1 for the comma */
68 for (clp = clientlist[MCL_NETGROUP]; clp; clp = clp->m_next)
69 len += (strlen(clp->m_hostname) + 1);
71 if (len > (NFSCLNT_IDMAX / 2))
76 if (use_ipaddr != old_use_ipaddr)
84 static ino_t last_inode;
86 static unsigned int counter;
89 if ((fd = open(_PATH_ETAB, O_RDONLY)) < 0) {
90 xlog(L_FATAL, "couldn't open %s", _PATH_ETAB);
91 } else if (fstat(fd, &stb) < 0) {
92 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
93 } else if (stb.st_ino == last_inode) {
99 last_inode = stb.st_ino;
103 memset(&my_client, 0, sizeof(my_client));
114 get_client_hostname(struct sockaddr_in *caller, struct addrinfo *ai,
115 enum auth_error *error)
120 return strdup(inet_ntoa(caller->sin_addr));
121 n = client_compose(ai);
122 *error = unknown_host;
128 return strdup("DEFAULT");
131 /* return static nfs_export with details filled in */
133 auth_authenticate_newcache(struct sockaddr_in *caller,
134 char *path, struct addrinfo *ai,
135 enum auth_error *error)
140 free(my_client.m_hostname);
142 my_client.m_hostname = get_client_hostname(caller, ai, error);
143 if (my_client.m_hostname == NULL)
146 my_client.m_naddr = 1;
147 set_addrlist_in(&my_client, 0, caller);
148 my_exp.m_client = &my_client;
151 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
152 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
153 if (strcmp(path, exp->m_export.e_path))
155 if (!use_ipaddr && !client_member(my_client.m_hostname, exp->m_client->m_hostname))
157 if (use_ipaddr && !client_check(exp->m_client, ai))
161 *error = not_exported;
165 my_exp.m_export = exp->m_export;
171 auth_authenticate_internal(struct sockaddr_in *caller,
172 char *path, struct addrinfo *ai,
173 enum auth_error *error)
178 exp = auth_authenticate_newcache(caller, path, ai, error);
182 exp = export_find(ai, path);
188 if (exp->m_export.e_flags & NFSEXP_V4ROOT) {
192 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
193 ntohs(caller->sin_port) >= IPPORT_RESERVED) {
194 *error = illegal_port;
203 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
205 nfs_export *exp = NULL;
206 char epath[MAXPATHLEN+1];
208 struct addrinfo *ai = NULL;
209 struct in_addr addr = caller->sin_addr;
210 enum auth_error error = bad_path;
212 if (path [0] != '/') {
213 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
214 what, inet_ntoa(addr), path);
218 strncpy(epath, path, sizeof (epath) - 1);
219 epath[sizeof (epath) - 1] = '\0';
220 auth_fixpath(epath); /* strip duplicate '/' etc */
222 ai = client_resolve((struct sockaddr *)caller);
226 /* Try the longest matching exported pathname. */
228 exp = auth_authenticate_internal(caller, epath,
230 if (exp || (error != not_exported && error != no_entry))
232 /* We have to treat the root, "/", specially. */
233 if (p == &epath[1]) break;
234 p = strrchr(epath, '/');
241 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
242 what, inet_ntoa(addr), path);
246 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
247 what, inet_ntoa(addr), path, epath);
251 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
252 what, ai->ai_canonname, path, epath);
256 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
257 what, ai->ai_canonname, path, epath);
261 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
262 what, ai->ai_canonname, path, epath, ntohs(caller->sin_port));
266 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
267 what, ai->ai_canonname, ntohs(caller->sin_port), path, epath);
270 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
271 what, ai->ai_canonname, ntohs(caller->sin_port),
280 auth_fixpath(char *path)
284 for (sp = cp = path; *sp; sp++) {
285 if (*sp != '/' || sp[1] != '/')
288 while (cp > path+1 && cp[-1] == '/')