4 * Authentication procedures for mountd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
33 static void auth_fixpath(char *path);
34 static nfs_export* auth_authenticate_internal
35 (char *what, struct sockaddr_in *caller, char *path,
36 struct hostent **hpp, enum auth_error *error);
37 static char *export_file = NULL;
40 auth_init(char *exports)
43 export_file = exports;
52 static time_t last_modified = 0;
54 if (stat(_PATH_ETAB, &stb) < 0)
55 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
56 if (stb.st_mtime == last_modified)
58 last_modified = stb.st_mtime;
61 // export_read(export_file);
68 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
69 char *path, struct hostent **hpp,
70 enum auth_error *error)
72 struct in_addr addr = caller->sin_addr;
81 if (!(*hpp = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET)))
82 *hpp = get_hostent((const char *)&addr, sizeof(addr),
85 /* must make sure the hostent is authorative. */
87 struct hostent *forward = NULL;
90 tmpname = xstrdup((*hpp)->h_name);
92 forward = gethostbyname(tmpname);
96 /* now make sure the "addr" is in the list */
97 for (sp = forward->h_addr_list ; *sp ; sp++) {
98 if (memcmp(*sp, &addr, forward->h_length)==0)
104 *error = faked_hostent;
105 *hpp = hostent_dup (*hpp);
108 *hpp = hostent_dup (forward);
111 /* never heard of it. misconfigured DNS? */
112 *error = no_forward_dns;
113 *hpp = hostent_dup (*hpp);
118 if (!(exp = export_find(*hpp, path))) {
122 if (!exp->m_mayexport) {
123 *error = not_exported;
127 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
128 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
129 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
130 *error = illegal_port;
140 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
142 nfs_export *exp = NULL;
143 char epath[MAXPATHLEN+1];
145 struct hostent *hp = NULL;
146 struct in_addr addr = caller->sin_addr;
147 enum auth_error error;
149 if (path [0] != '/') return exp;
151 strncpy(epath, path, sizeof (epath) - 1);
152 epath[sizeof (epath) - 1] = '\0';
154 /* Try the longest matching exported pathname. */
160 exp = auth_authenticate_internal(what, caller, epath,
162 if (exp || (error != not_exported && error != no_entry))
164 /* We have to treat the root, "/", specially. */
165 if (p == &epath[1]) break;
166 p = strrchr(epath, '/');
173 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
174 what, inet_ntoa(addr), path);
178 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
179 what, inet_ntoa(addr), path, epath);
183 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
184 what, hp->h_name, path, epath);
188 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
189 what, hp->h_name, path, epath);
193 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
194 what, hp->h_name, path, epath, ntohs(caller->sin_port));
198 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): DNS forward lookup does't match with reverse",
199 what, inet_ntoa(addr), hp->h_name, path, epath);
203 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): no DNS forward lookup",
204 what, inet_ntoa(addr), hp->h_name, path, epath);
208 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
209 what, hp->h_name, ntohs(caller->sin_port), path, epath);
212 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
213 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
223 auth_fixpath(char *path)
227 for (sp = cp = path; *sp; sp++) {
228 if (*sp != '/' || sp[1] != '/')
231 while (cp > path+1 && cp[-1] == '/')