4 * Authentication procedures for mountd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
33 static void auth_fixpath(char *path);
34 static char *export_file = NULL;
35 static nfs_export my_exp;
36 static nfs_client my_client;
41 auth_init(char *exports)
44 export_file = exports;
53 static time_t last_modified = 0;
55 if (stat(_PATH_ETAB, &stb) < 0)
56 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
57 if (stb.st_mtime == last_modified)
59 last_modified = stb.st_mtime;
62 memset(&my_client, 0, sizeof(my_client));
63 // export_read(export_file);
70 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
71 char *path, struct hostent *hp,
72 enum auth_error *error)
78 /* return static nfs_export with details filled in */
79 if (my_client.m_naddr != 1 ||
80 my_client.m_addrlist[0].s_addr != caller->sin_addr.s_addr) {
81 /* different client to last time, so do a lookup */
83 my_client.m_naddr = 0;
84 my_client.m_addrlist[0] = caller->sin_addr;
85 n = client_compose(caller->sin_addr);
86 *error = unknown_host;
89 strcpy(my_client.m_hostname, *n?n:"DEFAULT");
91 my_client.m_naddr = 1;
94 my_exp.m_client = &my_client;
97 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
98 for (exp = exportlist[i]; exp; exp = exp->m_next) {
99 if (!client_member(my_client.m_hostname, exp->m_client->m_hostname))
101 if (strcmp(path, exp->m_export.e_path))
105 *error = not_exported;
109 my_exp.m_export = exp->m_export;
113 if (!(exp = export_find(hp, path))) {
117 if (!exp->m_mayexport) {
118 *error = not_exported;
122 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
123 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
124 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
125 *error = illegal_port;
134 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
136 nfs_export *exp = NULL;
137 char epath[MAXPATHLEN+1];
139 struct hostent *hp = NULL;
140 struct in_addr addr = caller->sin_addr;
141 enum auth_error error;
143 if (path [0] != '/') {
144 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
145 what, inet_ntoa(addr), path);
149 strncpy(epath, path, sizeof (epath) - 1);
150 epath[sizeof (epath) - 1] = '\0';
151 auth_fixpath(epath); /* strip duplicate '/' etc */
153 hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr, sizeof(struct in_addr),
156 hp = get_hostent((const char*)&caller->sin_addr, sizeof(struct in_addr),
161 /* Try the longest matching exported pathname. */
163 exp = auth_authenticate_internal(what, caller, epath,
165 if (exp || (error != not_exported && error != no_entry))
167 /* We have to treat the root, "/", specially. */
168 if (p == &epath[1]) break;
169 p = strrchr(epath, '/');
176 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
177 what, inet_ntoa(addr), path);
181 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
182 what, inet_ntoa(addr), path, epath);
186 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
187 what, hp->h_name, path, epath);
191 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
192 what, hp->h_name, path, epath);
196 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
197 what, hp->h_name, path, epath, ntohs(caller->sin_port));
201 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
202 what, hp->h_name, ntohs(caller->sin_port), path, epath);
205 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
206 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
216 auth_fixpath(char *path)
220 for (sp = cp = path; *sp; sp++) {
221 if (*sp != '/' || sp[1] != '/')
224 while (cp > path+1 && cp[-1] == '/')