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>
32 static void auth_fixpath(char *path);
33 static nfs_export* auth_authenticate_internal
34 (char *what, struct sockaddr_in *caller, char *path,
35 struct hostent **hpp, enum auth_error *error);
36 static char *export_file = NULL;
39 auth_init(char *exports)
42 export_file = exports;
51 static time_t last_modified = 0;
53 if (stat(_PATH_ETAB, &stb) < 0)
54 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
55 if (stb.st_mtime == last_modified)
57 last_modified = stb.st_mtime;
60 // export_read(export_file);
67 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
68 char *path, struct hostent **hpp,
69 enum auth_error *error)
71 struct in_addr addr = caller->sin_addr;
80 if (!(*hpp = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET)))
81 *hpp = get_hostent((const char *)&addr, sizeof(addr),
84 /* must make sure the hostent is authorative. */
86 struct hostent *forward;
88 forward = gethostbyname((*hpp)->h_name);
90 /* now make sure the "addr" is in the list */
91 for (sp = forward->h_addr_list ; *sp ; sp++) {
92 if (memcmp(*sp, &addr, forward->h_length)==0)
98 *error = faked_hostent;
99 *hpp = hostent_dup (*hpp);
102 *hpp = hostent_dup (forward);
105 /* never heard of it. misconfigured DNS? */
106 *error = no_forward_dns;
107 *hpp = hostent_dup (*hpp);
112 if (!(exp = export_find(*hpp, path))) {
116 if (!exp->m_mayexport) {
117 *error = not_exported;
121 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
122 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
123 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
124 *error = illegal_port;
134 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
136 nfs_export *exp = NULL;
137 char epath[MAXPATHLEN+1];
139 struct hostent *hp = NULL;
140 struct in_addr addr = caller->sin_addr;
141 enum auth_error error;
143 if (path [0] != '/') return exp;
145 strncpy(epath, path, sizeof (epath) - 1);
146 epath[sizeof (epath) - 1] = '\0';
148 /* Try the longest matching exported pathname. */
154 exp = auth_authenticate_internal(what, caller, epath,
156 if (exp || (error != not_exported && error != no_entry))
158 /* We have to treat the root, "/", specially. */
159 if (p == &epath[1]) break;
160 p = strrchr(epath, '/');
167 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
168 what, inet_ntoa(addr), path);
172 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
173 what, inet_ntoa(addr), path, epath);
177 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
178 what, hp->h_name, path, epath);
182 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
183 what, hp->h_name, path, epath);
187 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
188 what, hp->h_name, path, epath, ntohs(caller->sin_port));
192 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): DNS forward lookup does't match with reverse",
193 what, inet_ntoa(addr), hp->h_name, path, epath);
197 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): no DNS forward lookup",
198 what, inet_ntoa(addr), hp->h_name, path, epath);
202 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
203 what, hp->h_name, ntohs(caller->sin_port), path, epath);
206 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
207 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
217 auth_fixpath(char *path)
221 for (sp = cp = path; *sp; sp++) {
222 if (*sp != '/' || sp[1] != '/')
225 while (cp > path+1 && cp[-1] == '/')