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>
34 static void auth_fixpath(char *path);
35 static char *export_file = NULL;
36 static nfs_export my_exp;
37 static nfs_client my_client;
42 auth_init(char *exports)
45 export_file = exports;
54 static ino_t last_inode;
56 static unsigned int counter;
59 if ((fd = open(_PATH_ETAB, O_RDONLY)) < 0) {
60 xlog(L_FATAL, "couldn't open %s", _PATH_ETAB);
61 } else if (fstat(fd, &stb) < 0) {
62 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
63 } else if (stb.st_ino == last_inode) {
69 last_inode = stb.st_ino;
73 memset(&my_client, 0, sizeof(my_client));
81 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
82 char *path, struct hostent *hp,
83 enum auth_error *error)
89 /* return static nfs_export with details filled in */
91 my_client.m_addrlist[0] = caller->sin_addr;
92 n = client_compose(caller->sin_addr);
93 *error = unknown_host;
96 strcpy(my_client.m_hostname, *n?n:"DEFAULT");
98 my_client.m_naddr = 1;
99 my_exp.m_client = &my_client;
102 for (i = 0; !exp && i < MCL_MAXTYPES; i++)
103 for (exp = exportlist[i]; exp; exp = exp->m_next) {
104 if (!client_member(my_client.m_hostname, exp->m_client->m_hostname))
106 if (strcmp(path, exp->m_export.e_path))
110 *error = not_exported;
114 my_exp.m_export = exp->m_export;
118 if (!(exp = export_find(hp, path))) {
122 if (!exp->m_mayexport) {
123 *error = not_exported;
127 if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
128 (ntohs(caller->sin_port) < IPPORT_RESERVED/2 ||
129 ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
130 *error = illegal_port;
139 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
141 nfs_export *exp = NULL;
142 char epath[MAXPATHLEN+1];
144 struct hostent *hp = NULL;
145 struct in_addr addr = caller->sin_addr;
146 enum auth_error error;
148 if (path [0] != '/') {
149 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
150 what, inet_ntoa(addr), path);
154 strncpy(epath, path, sizeof (epath) - 1);
155 epath[sizeof (epath) - 1] = '\0';
156 auth_fixpath(epath); /* strip duplicate '/' etc */
158 hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr, sizeof(struct in_addr),
161 hp = get_hostent((const char*)&caller->sin_addr, sizeof(struct in_addr),
166 /* Try the longest matching exported pathname. */
168 exp = auth_authenticate_internal(what, caller, epath,
170 if (exp || (error != not_exported && error != no_entry))
172 /* We have to treat the root, "/", specially. */
173 if (p == &epath[1]) break;
174 p = strrchr(epath, '/');
181 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
182 what, inet_ntoa(addr), path);
186 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
187 what, inet_ntoa(addr), path, epath);
191 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
192 what, hp->h_name, path, epath);
196 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
197 what, hp->h_name, path, epath);
201 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
202 what, hp->h_name, path, epath, ntohs(caller->sin_port));
206 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
207 what, hp->h_name, ntohs(caller->sin_port), path, epath);
210 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
211 what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
221 auth_fixpath(char *path)
225 for (sp = cp = path; *sp; sp++) {
226 if (*sp != '/' || sp[1] != '/')
229 while (cp > path+1 && cp[-1] == '/')