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 */
80 my_client.m_addrlist[0] = caller->sin_addr;
81 n = client_compose(caller->sin_addr);
82 *error = unknown_host;
85 strcpy(my_client.m_hostname, *n?n:"DEFAULT");
87 my_client.m_naddr = 1;
88 my_exp.m_client = &my_client;
91 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
92 for (exp = exportlist[i]; exp; exp = exp->m_next) {
93 if (!client_member(my_client.m_hostname, exp->m_client->m_hostname))
95 if (strcmp(path, exp->m_export.e_path))
99 *error = not_exported;
103 my_exp.m_export = exp->m_export;
107 if (!(exp = export_find(hp, path))) {
111 if (!exp->m_mayexport) {
112 *error = not_exported;
116 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
117 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
118 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
119 *error = illegal_port;
128 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
130 nfs_export *exp = NULL;
131 char epath[MAXPATHLEN+1];
133 struct hostent *hp = NULL;
134 struct in_addr addr = caller->sin_addr;
135 enum auth_error error;
137 if (path [0] != '/') {
138 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
139 what, inet_ntoa(addr), path);
143 strncpy(epath, path, sizeof (epath) - 1);
144 epath[sizeof (epath) - 1] = '\0';
145 auth_fixpath(epath); /* strip duplicate '/' etc */
147 hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr, sizeof(struct in_addr),
150 hp = get_hostent((const char*)&caller->sin_addr, sizeof(struct in_addr),
155 /* Try the longest matching exported pathname. */
157 exp = auth_authenticate_internal(what, caller, epath,
159 if (exp || (error != not_exported && error != no_entry))
161 /* We have to treat the root, "/", specially. */
162 if (p == &epath[1]) break;
163 p = strrchr(epath, '/');
170 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
171 what, inet_ntoa(addr), path);
175 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
176 what, inet_ntoa(addr), path, epath);
180 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
181 what, hp->h_name, path, epath);
185 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
186 what, hp->h_name, path, epath);
190 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
191 what, hp->h_name, path, epath, ntohs(caller->sin_port));
195 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
196 what, hp->h_name, ntohs(caller->sin_port), path, epath);
199 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
200 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
210 auth_fixpath(char *path)
214 for (sp = cp = path; *sp; sp++) {
215 if (*sp != '/' || sp[1] != '/')
218 while (cp > path+1 && cp[-1] == '/')