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>
31 static void auth_fixpath(char *path);
32 static nfs_export* auth_authenticate_internal
33 (char *what, struct sockaddr_in *caller, char *path,
34 struct hostent **hpp, enum auth_error *error);
35 static char *export_file = NULL;
38 auth_init(char *exports)
41 export_file = exports;
50 static time_t last_modified = 0;
52 if (stat(_PATH_ETAB, &stb) < 0)
53 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
54 if (stb.st_mtime == last_modified)
56 last_modified = stb.st_mtime;
59 // export_read(export_file);
66 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
67 char *path, struct hostent **hpp,
68 enum auth_error *error)
70 struct in_addr addr = caller->sin_addr;
79 if (!(*hpp = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET)))
80 *hpp = get_hostent((const char *)&addr, sizeof(addr),
83 /* must make sure the hostent is authorative. */
84 char *name = strdup((*hpp)->h_name);
86 *hpp = gethostbyname(name);
87 /* now make sure the "addr" is in the list */
88 for (sp = (*hpp)->h_addr_list ; *sp ; sp++) {
89 if (memcmp(*sp, &addr, (*hpp)->h_length)==0)
96 *error = faked_hostent;
100 *hpp = hostent_dup (*hpp);
104 if (!(exp = export_find(*hpp, path))) {
108 if (!exp->m_mayexport) {
109 *error = not_exported;
113 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
114 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
115 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
116 *error = illegal_port;
126 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
128 nfs_export *exp = NULL;
129 char epath[MAXPATHLEN+1];
131 struct hostent *hp = NULL;
132 struct in_addr addr = caller->sin_addr;
133 enum auth_error error;
135 if (path [0] != '/') return exp;
137 strncpy(epath, path, sizeof (epath) - 1);
138 epath[sizeof (epath) - 1] = '\0';
140 /* Try the longest matching exported pathname. */
146 exp = auth_authenticate_internal(what, caller, epath,
148 if (exp || (error != not_exported && error != no_entry))
150 /* We have to treat the root, "/", specially. */
151 if (p == &epath[1]) break;
152 p = strrchr(epath, '/');
159 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
160 what, inet_ntoa(addr), path);
164 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
165 what, inet_ntoa(addr), path, epath);
169 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
170 what, hp->h_name, path, epath);
174 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
175 what, hp->h_name, path, epath);
179 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
180 what, hp->h_name, path, epath, ntohs(caller->sin_port));
184 xlog(L_WARNING, "refused %s request from %s for %s (%s): faked hostent",
185 what, inet_ntoa(addr), path, epath);
189 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
190 what, hp->h_name, ntohs(caller->sin_port), path, epath);
193 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
194 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
204 auth_fixpath(char *path)
208 for (sp = cp = path; *sp; sp++) {
209 if (*sp != '/' || sp[1] != '/')
212 while (cp > path+1 && cp[-1] == '/')