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].p_head; 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) {
173 *error = illegal_port;
182 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
184 nfs_export *exp = NULL;
185 char epath[MAXPATHLEN+1];
187 struct hostent *hp = NULL;
188 struct in_addr addr = caller->sin_addr;
189 enum auth_error error = bad_path;
191 if (path [0] != '/') {
192 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
193 what, inet_ntoa(addr), path);
197 strncpy(epath, path, sizeof (epath) - 1);
198 epath[sizeof (epath) - 1] = '\0';
199 auth_fixpath(epath); /* strip duplicate '/' etc */
201 hp = client_resolve(caller->sin_addr);
205 /* Try the longest matching exported pathname. */
207 exp = auth_authenticate_internal(what, caller, epath,
209 if (exp || (error != not_exported && error != no_entry))
211 /* We have to treat the root, "/", specially. */
212 if (p == &epath[1]) break;
213 p = strrchr(epath, '/');
220 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
221 what, inet_ntoa(addr), path);
225 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
226 what, inet_ntoa(addr), path, epath);
230 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
231 what, hp->h_name, path, epath);
235 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
236 what, hp->h_name, path, epath);
240 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
241 what, hp->h_name, path, epath, ntohs(caller->sin_port));
245 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
246 what, hp->h_name, ntohs(caller->sin_port), path, epath);
249 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
250 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
260 auth_fixpath(char *path)
264 for (sp = cp = path; *sp; sp++) {
265 if (*sp != '/' || sp[1] != '/')
268 while (cp > path+1 && cp[-1] == '/')