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 /* First try it w/o doing a hostname lookup... */
82 *hpp = get_hostent((const char *)&addr, sizeof(addr), AF_INET);
83 exp = export_find(*hpp, path);
86 /* Ok, that didn't fly. Try it with a reverse lookup. */
88 *hpp = gethostbyaddr((const char *)&addr, sizeof(addr),
94 /* must make sure the hostent is authorative. */
96 struct hostent *forward = NULL;
99 tmpname = xstrdup((*hpp)->h_name);
101 forward = gethostbyname(tmpname);
105 /* now make sure the "addr" is in the list */
106 for (sp = forward->h_addr_list ; *sp ; sp++) {
107 if (memcmp(*sp, &addr, forward->h_length)==0)
113 *error = faked_hostent;
114 *hpp = hostent_dup (*hpp);
117 *hpp = hostent_dup (forward);
120 /* never heard of it. misconfigured DNS? */
121 *error = no_forward_dns;
122 *hpp = hostent_dup (*hpp);
127 if (!(exp = export_find(*hpp, path))) {
133 if (!exp->m_mayexport) {
134 *error = not_exported;
138 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
139 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
140 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
141 *error = illegal_port;
151 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
153 nfs_export *exp = NULL;
154 char epath[MAXPATHLEN+1];
156 struct hostent *hp = NULL;
157 struct in_addr addr = caller->sin_addr;
158 enum auth_error error;
160 if (path [0] != '/') return exp;
162 strncpy(epath, path, sizeof (epath) - 1);
163 epath[sizeof (epath) - 1] = '\0';
165 /* Try the longest matching exported pathname. */
171 exp = auth_authenticate_internal(what, caller, epath,
173 if (exp || (error != not_exported && error != no_entry))
175 /* We have to treat the root, "/", specially. */
176 if (p == &epath[1]) break;
177 p = strrchr(epath, '/');
184 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
185 what, inet_ntoa(addr), path);
189 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
190 what, inet_ntoa(addr), path, epath);
194 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
195 what, hp->h_name, path, epath);
199 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
200 what, hp->h_name, path, epath);
204 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
205 what, hp->h_name, path, epath, ntohs(caller->sin_port));
209 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): DNS forward lookup does't match with reverse",
210 what, inet_ntoa(addr), hp->h_name, path, epath);
214 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): no DNS forward lookup",
215 what, inet_ntoa(addr), hp->h_name, path, epath);
219 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
220 what, hp->h_name, ntohs(caller->sin_port), path, epath);
223 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
224 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
234 auth_fixpath(char *path)
238 for (sp = cp = path; *sp; sp++) {
239 if (*sp != '/' || sp[1] != '/')
242 while (cp > path+1 && cp[-1] == '/')