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 char *export_file = NULL;
37 auth_init(char *exports)
40 export_file = exports;
49 static time_t last_modified = 0;
51 if (stat(_PATH_ETAB, &stb) < 0)
52 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
53 if (stb.st_mtime == last_modified)
55 last_modified = stb.st_mtime;
58 // export_read(export_file);
65 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
66 char *path, struct hostent *hp,
67 enum auth_error *error)
72 static nfs_export my_exp;
73 static nfs_client my_client;
75 /* return static nfs_export with details filled in */
76 if (my_client.m_naddr != 1 ||
77 my_client.m_addrlist[0].s_addr != caller->sin_addr.s_addr) {
78 /* different client to last time, so do a lookup */
80 my_client.m_naddr = 0;
81 my_client.m_addrlist[0] = caller->sin_addr;
82 n = client_compose(caller->sin_addr);
85 strcpy(my_client.m_hostname, *n?n:"DEFAULT");
87 my_client.m_naddr = 1;
90 my_exp.m_client = &my_client;
93 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
94 for (exp = exportlist[i]; exp; exp = exp->m_next) {
95 if (!client_member(my_client.m_hostname, exp->m_client->m_hostname))
97 if (strcmp(path, exp->m_export.e_path))
101 *error = not_exported;
105 my_exp.m_export = exp->m_export;
109 if (!(exp = export_find(hp, path))) {
113 if (!exp->m_mayexport) {
114 *error = not_exported;
118 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
119 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
120 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
121 *error = illegal_port;
130 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
132 nfs_export *exp = NULL;
133 char epath[MAXPATHLEN+1];
135 struct hostent *hp = NULL;
136 struct in_addr addr = caller->sin_addr;
137 enum auth_error error;
139 if (path [0] != '/') {
140 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
141 what, inet_ntoa(addr), path);
145 strncpy(epath, path, sizeof (epath) - 1);
146 epath[sizeof (epath) - 1] = '\0';
147 auth_fixpath(epath); /* strip duplicate '/' etc */
149 hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr, sizeof(struct in_addr),
152 hp = get_hostent((const char*)&caller->sin_addr, sizeof(struct in_addr),
157 /* Try the longest matching exported pathname. */
159 exp = auth_authenticate_internal(what, caller, epath,
161 if (exp || (error != not_exported && error != no_entry))
163 /* We have to treat the root, "/", specially. */
164 if (p == &epath[1]) break;
165 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_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
199 what, hp->h_name, ntohs(caller->sin_port), path, epath);
202 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
203 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
213 auth_fixpath(char *path)
217 for (sp = cp = path; *sp; sp++) {
218 if (*sp != '/' || sp[1] != '/')
221 while (cp > path+1 && cp[-1] == '/')