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;
33 static nfs_export my_exp;
34 static nfs_client my_client;
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 memset(&my_client, 0, sizeof(my_client));
61 // export_read(export_file);
68 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
69 char *path, struct hostent *hp,
70 enum auth_error *error)
76 /* return static nfs_export with details filled in */
77 if (my_client.m_naddr != 1 ||
78 my_client.m_addrlist[0].s_addr != caller->sin_addr.s_addr) {
79 /* different client to last time, so do a lookup */
81 my_client.m_naddr = 0;
82 my_client.m_addrlist[0] = caller->sin_addr;
83 n = client_compose(caller->sin_addr);
84 *error = unknown_host;
87 strcpy(my_client.m_hostname, *n?n:"DEFAULT");
89 my_client.m_naddr = 1;
92 my_exp.m_client = &my_client;
95 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
96 for (exp = exportlist[i]; exp; exp = exp->m_next) {
97 if (!client_member(my_client.m_hostname, exp->m_client->m_hostname))
99 if (strcmp(path, exp->m_export.e_path))
103 *error = not_exported;
107 my_exp.m_export = exp->m_export;
111 if (!(exp = export_find(hp, path))) {
115 if (!exp->m_mayexport) {
116 *error = not_exported;
120 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
121 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
122 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
123 *error = illegal_port;
132 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
134 nfs_export *exp = NULL;
135 char epath[MAXPATHLEN+1];
137 struct hostent *hp = NULL;
138 struct in_addr addr = caller->sin_addr;
139 enum auth_error error;
141 if (path [0] != '/') {
142 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
143 what, inet_ntoa(addr), path);
147 strncpy(epath, path, sizeof (epath) - 1);
148 epath[sizeof (epath) - 1] = '\0';
149 auth_fixpath(epath); /* strip duplicate '/' etc */
151 hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr, sizeof(struct in_addr),
154 hp = get_hostent((const char*)&caller->sin_addr, sizeof(struct in_addr),
159 /* Try the longest matching exported pathname. */
161 exp = auth_authenticate_internal(what, caller, epath,
163 if (exp || (error != not_exported && error != no_entry))
165 /* We have to treat the root, "/", specially. */
166 if (p == &epath[1]) break;
167 p = strrchr(epath, '/');
174 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
175 what, inet_ntoa(addr), path);
179 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
180 what, inet_ntoa(addr), path, epath);
184 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
185 what, hp->h_name, path, epath);
189 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
190 what, hp->h_name, path, epath);
194 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
195 what, hp->h_name, path, epath, ntohs(caller->sin_port));
199 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
200 what, hp->h_name, ntohs(caller->sin_port), path, epath);
203 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
204 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
214 auth_fixpath(char *path)
218 for (sp = cp = path; *sp; sp++) {
219 if (*sp != '/' || sp[1] != '/')
222 while (cp > path+1 && cp[-1] == '/')