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 *hpp = hostent_dup (*hpp);
91 tmpname = xstrdup((*hpp)->h_name);
93 forward = gethostbyname(tmpname);
97 /* now make sure the "addr" is in the list */
98 for (sp = forward->h_addr_list ; *sp ; sp++) {
99 if (memcmp(*sp, &addr, forward->h_length)==0)
105 *error = faked_hostent;
109 *hpp = hostent_dup (forward);
112 /* never heard of it. misconfigured DNS? */
113 *error = no_forward_dns;
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] != '/') {
150 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
151 what, inet_ntoa(addr), path);
155 strncpy(epath, path, sizeof (epath) - 1);
156 epath[sizeof (epath) - 1] = '\0';
158 /* Try the longest matching exported pathname. */
164 exp = auth_authenticate_internal(what, caller, epath,
166 if (exp || (error != not_exported && error != no_entry))
168 /* We have to treat the root, "/", specially. */
169 if (p == &epath[1]) break;
170 p = strrchr(epath, '/');
177 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
178 what, inet_ntoa(addr), path);
182 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
183 what, inet_ntoa(addr), path, epath);
187 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
188 what, hp->h_name, path, epath);
192 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
193 what, hp->h_name, path, epath);
197 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
198 what, hp->h_name, path, epath, ntohs(caller->sin_port));
202 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): DNS forward lookup does't match with reverse",
203 what, inet_ntoa(addr), hp->h_name, path, epath);
207 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): no DNS forward lookup",
208 what, inet_ntoa(addr), hp->h_name, path, epath);
212 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
213 what, hp->h_name, ntohs(caller->sin_port), path, epath);
216 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
217 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
227 auth_fixpath(char *path)
231 for (sp = cp = path; *sp; sp++) {
232 if (*sp != '/' || sp[1] != '/')
235 while (cp > path+1 && cp[-1] == '/')