]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/auth.c
384375612a4d3dbb710621e5d02c3289b51cec52
[nfs-utils.git] / utils / mountd / auth.c
1 /*
2  * utils/mountd/auth.c
3  *
4  * Authentication procedures for mountd.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <sys/stat.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <errno.h>
17 #include <unistd.h>
18
19 #include "sockaddr.h"
20 #include "misc.h"
21 #include "nfslib.h"
22 #include "exportfs.h"
23 #include "mountd.h"
24 #include "xmalloc.h"
25 #include "v4root.h"
26
27 enum auth_error
28 {
29   bad_path,
30   unknown_host,
31   no_entry,
32   not_exported,
33   illegal_port,
34   success
35 };
36
37 static void             auth_fixpath(char *path);
38 static char     *export_file = NULL;
39 static nfs_export my_exp;
40 static nfs_client my_client;
41
42 extern int new_cache;
43 extern int use_ipaddr;
44
45 void
46 auth_init(char *exports)
47 {
48
49         export_file = exports;
50         auth_reload();
51         xtab_mount_write();
52 }
53
54 /*
55  * A client can match many different netgroups and it's tough to know
56  * beforehand whether it will. If the concatenated string of netgroup
57  * m_hostnames is >512 bytes, then enable the "use_ipaddr" mode. This
58  * makes mountd change how it matches a client ip address when a mount
59  * request comes in. It's more efficient at handling netgroups at the
60  * expense of larger kernel caches.
61  */
62 static void
63 check_useipaddr(void)
64 {
65         nfs_client *clp;
66         int old_use_ipaddr = use_ipaddr;
67         unsigned int len = 0;
68
69         /* add length of m_hostname + 1 for the comma */
70         for (clp = clientlist[MCL_NETGROUP]; clp; clp = clp->m_next)
71                 len += (strlen(clp->m_hostname) + 1);
72
73         if (len > (NFSCLNT_IDMAX / 2))
74                 use_ipaddr = 1;
75         else
76                 use_ipaddr = 0;
77
78         if (use_ipaddr != old_use_ipaddr)
79                 cache_flush(1);
80 }
81
82 unsigned int
83 auth_reload()
84 {
85         struct stat             stb;
86         static ino_t            last_inode;
87         static int              last_fd;
88         static unsigned int     counter;
89         int                     fd;
90
91         if ((fd = open(_PATH_ETAB, O_RDONLY)) < 0) {
92                 xlog(L_FATAL, "couldn't open %s", _PATH_ETAB);
93         } else if (fstat(fd, &stb) < 0) {
94                 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
95         } else if (stb.st_ino == last_inode) {
96                 close(fd);
97                 return counter;
98         } else {
99                 close(last_fd);
100                 last_fd = fd;
101                 last_inode = stb.st_ino;
102         }
103
104         export_freeall();
105         memset(&my_client, 0, sizeof(my_client));
106         xtab_export_read();
107         check_useipaddr();
108         v4root_set();
109
110         ++counter;
111
112         return counter;
113 }
114
115 static char *
116 get_client_hostname(const struct sockaddr *caller, struct addrinfo *ai,
117                 enum auth_error *error)
118 {
119         char buf[INET6_ADDRSTRLEN];
120         char *n;
121
122         if (use_ipaddr)
123                 return strdup(host_ntop(caller, buf, sizeof(buf)));
124         n = client_compose(ai);
125         *error = unknown_host;
126         if (!n)
127                 return NULL;
128         if (*n)
129                 return n;
130         free(n);
131         return strdup("DEFAULT");
132 }
133
134 /* return static nfs_export with details filled in */
135 static nfs_export *
136 auth_authenticate_newcache(const struct sockaddr *caller,
137                            const char *path, struct addrinfo *ai,
138                            enum auth_error *error)
139 {
140         nfs_export *exp;
141         int i;
142
143         free(my_client.m_hostname);
144
145         my_client.m_hostname = get_client_hostname(caller, ai, error);
146         if (my_client.m_hostname == NULL)
147                 return NULL;
148
149         my_client.m_naddr = 1;
150         set_addrlist(&my_client, 0, caller);
151         my_exp.m_client = &my_client;
152
153         exp = NULL;
154         for (i = 0; !exp && i < MCL_MAXTYPES; i++)
155                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
156                         if (strcmp(path, exp->m_export.e_path))
157                                 continue;
158                         if (!use_ipaddr && !client_member(my_client.m_hostname, exp->m_client->m_hostname))
159                                 continue;
160                         if (use_ipaddr && !client_check(exp->m_client, ai))
161                                 continue;
162                         if (exp->m_export.e_flags & NFSEXP_V4ROOT)
163                                 /* not acceptable for v[23] export */
164                                 continue;
165                         break;
166                 }
167         *error = not_exported;
168         if (!exp)
169                 return NULL;
170
171         my_exp.m_export = exp->m_export;
172         exp = &my_exp;
173         return exp;
174 }
175
176 static nfs_export *
177 auth_authenticate_internal(const struct sockaddr *caller, const char *path,
178                 struct addrinfo *ai, enum auth_error *error)
179 {
180         nfs_export *exp;
181
182         if (new_cache) {
183                 exp = auth_authenticate_newcache(caller, path, ai, error);
184                 if (!exp)
185                         return NULL;
186         } else {
187                 exp = export_find(ai, path);
188                 if (exp == NULL) {
189                         *error = no_entry;
190                         return NULL;
191                 }
192         }
193         if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
194                      nfs_get_port(caller) >= IPPORT_RESERVED) {
195                 *error = illegal_port;
196                 return NULL;
197         }
198         *error = success;
199
200         return exp;
201 }
202
203 nfs_export *
204 auth_authenticate(const char *what, const struct sockaddr *caller,
205                 const char *path)
206 {
207         nfs_export      *exp = NULL;
208         char            epath[MAXPATHLEN+1];
209         char            *p = NULL;
210         char            buf[INET6_ADDRSTRLEN];
211         struct addrinfo *ai = NULL;
212         enum auth_error error = bad_path;
213
214         if (path[0] != '/') {
215                 xlog(L_WARNING, "Bad path in %s request from %s: \"%s\"",
216                              what, host_ntop(caller, buf, sizeof(buf)), path);
217                 return exp;
218         }
219
220         strncpy(epath, path, sizeof (epath) - 1);
221         epath[sizeof (epath) - 1] = '\0';
222         auth_fixpath(epath); /* strip duplicate '/' etc */
223
224         ai = client_resolve(caller);
225         if (ai == NULL)
226                 return exp;
227
228         /* Try the longest matching exported pathname. */
229         while (1) {
230                 exp = auth_authenticate_internal(caller, epath, ai, &error);
231                 if (exp || (error != not_exported && error != no_entry))
232                         break;
233                 /* We have to treat the root, "/", specially. */
234                 if (p == &epath[1]) break;
235                 p = strrchr(epath, '/');
236                 if (p == epath) p++;
237                 *p = '\0';
238         }
239
240         switch (error) {
241         case bad_path:
242                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
243                      what, host_ntop(caller, buf, sizeof(buf)), path);
244                 break;
245
246         case unknown_host:
247                 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
248                      what, host_ntop(caller, buf, sizeof(buf)), path, epath);
249                 break;
250
251         case no_entry:
252                 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
253                      what, ai->ai_canonname, path, epath);
254                 break;
255
256         case not_exported:
257                 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
258                      what, ai->ai_canonname, path, epath);
259                 break;
260
261         case illegal_port:
262                 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %u",
263                      what, ai->ai_canonname, path, epath, nfs_get_port(caller));
264                 break;
265
266         case success:
267                 xlog(L_NOTICE, "authenticated %s request from %s:%u for %s (%s)",
268                      what, ai->ai_canonname, nfs_get_port(caller), path, epath);
269                 break;
270         default:
271                 xlog(L_NOTICE, "%s request from %s:%u for %s (%s) gave %d",
272                      what, ai->ai_canonname, nfs_get_port(caller),
273                         path, epath, error);
274         }
275
276         freeaddrinfo(ai);
277         return exp;
278 }
279
280 static void
281 auth_fixpath(char *path)
282 {
283         char    *sp, *cp;
284
285         for (sp = cp = path; *sp; sp++) {
286                 if (*sp != '/' || sp[1] != '/')
287                         *cp++ = *sp;
288         }
289         while (cp > path+1 && cp[-1] == '/')
290                 cp--;
291         *cp = '\0';
292 }