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>
34 static void auth_fixpath(char *path);
35 static char *export_file = NULL;
36 static nfs_export my_exp;
37 static nfs_client my_client;
40 extern int use_ipaddr;
43 auth_init(char *exports)
46 export_file = exports;
52 * A client can match many different netgroups and it's tough to know
53 * beforehand whether it will. If the concatenated string of netgroup
54 * m_hostnames is >512 bytes, then enable the "use_ipaddr" mode. This
55 * makes mountd change how it matches a client ip address when a mount
56 * request comes in. It's more efficient at handling netgroups at the
57 * expense of larger kernel caches.
63 int old_use_ipaddr = use_ipaddr;
66 /* add length of m_hostname + 1 for the comma */
67 for (clp = clientlist[MCL_NETGROUP]; clp; clp = clp->m_next)
68 len += (strlen(clp->m_hostname) + 1);
70 if (len > (NFSCLNT_IDMAX / 2))
75 if (use_ipaddr != old_use_ipaddr)
83 static ino_t last_inode;
85 static unsigned int counter;
88 if ((fd = open(_PATH_ETAB, O_RDONLY)) < 0) {
89 xlog(L_FATAL, "couldn't open %s", _PATH_ETAB);
90 } else if (fstat(fd, &stb) < 0) {
91 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
92 } else if (stb.st_ino == last_inode) {
98 last_inode = stb.st_ino;
102 memset(&my_client, 0, sizeof(my_client));
111 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
112 char *path, struct hostent *hp,
113 enum auth_error *error)
119 /* return static nfs_export with details filled in */
121 free(my_client.m_hostname);
123 my_client.m_hostname =
124 strdup(inet_ntoa(caller->sin_addr));
126 n = client_compose(hp);
127 *error = unknown_host;
129 my_client.m_hostname = NULL;
131 my_client.m_hostname = n;
134 my_client.m_hostname = strdup("DEFAULT");
137 if (my_client.m_hostname == NULL)
139 my_client.m_naddr = 1;
140 my_client.m_addrlist[0] = caller->sin_addr;
141 my_exp.m_client = &my_client;
144 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
145 for (exp = exportlist[i]; exp; exp = exp->m_next) {
146 if (strcmp(path, exp->m_export.e_path))
148 if (!use_ipaddr && !client_member(my_client.m_hostname, exp->m_client->m_hostname))
150 if (use_ipaddr && !client_check(exp->m_client, hp))
154 *error = not_exported;
158 my_exp.m_export = exp->m_export;
162 if (!(exp = export_find(hp, path))) {
166 if (!exp->m_mayexport) {
167 *error = not_exported;
171 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
172 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
173 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
174 *error = illegal_port;
183 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
185 nfs_export *exp = NULL;
186 char epath[MAXPATHLEN+1];
188 struct hostent *hp = NULL;
189 struct in_addr addr = caller->sin_addr;
190 enum auth_error error = bad_path;
192 if (path [0] != '/') {
193 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
194 what, inet_ntoa(addr), path);
198 strncpy(epath, path, sizeof (epath) - 1);
199 epath[sizeof (epath) - 1] = '\0';
200 auth_fixpath(epath); /* strip duplicate '/' etc */
202 hp = client_resolve(caller->sin_addr);
206 /* Try the longest matching exported pathname. */
208 exp = auth_authenticate_internal(what, caller, epath,
210 if (exp || (error != not_exported && error != no_entry))
212 /* We have to treat the root, "/", specially. */
213 if (p == &epath[1]) break;
214 p = strrchr(epath, '/');
221 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
222 what, inet_ntoa(addr), path);
226 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
227 what, inet_ntoa(addr), path, epath);
231 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
232 what, hp->h_name, path, epath);
236 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
237 what, hp->h_name, path, epath);
241 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
242 what, hp->h_name, path, epath, ntohs(caller->sin_port));
246 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
247 what, hp->h_name, ntohs(caller->sin_port), path, epath);
250 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
251 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
261 auth_fixpath(char *path)
265 for (sp = cp = path; *sp; sp++) {
266 if (*sp != '/' || sp[1] != '/')
269 while (cp > path+1 && cp[-1] == '/')