]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/auth.c
mountd: add trivial helpers for client-matching
[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 bool ipaddr_client_matches(nfs_export *exp, struct addrinfo *ai)
135 {
136         return client_check(exp->m_client, ai);
137 }
138
139 bool namelist_client_matches(nfs_export *exp, char *dom)
140 {
141         return client_member(dom, exp->m_client->m_hostname);
142 }
143
144 bool client_matches(nfs_export *exp, char *dom, struct addrinfo *ai)
145 {
146         if (use_ipaddr)
147                 return ipaddr_client_matches(exp, ai);
148         return namelist_client_matches(exp, dom);
149 }
150
151 /* return static nfs_export with details filled in */
152 static nfs_export *
153 auth_authenticate_newcache(const struct sockaddr *caller,
154                            const char *path, struct addrinfo *ai,
155                            enum auth_error *error)
156 {
157         nfs_export *exp;
158         int i;
159
160         free(my_client.m_hostname);
161
162         my_client.m_hostname = get_client_hostname(caller, ai, error);
163         if (my_client.m_hostname == NULL)
164                 return NULL;
165
166         my_client.m_naddr = 1;
167         set_addrlist(&my_client, 0, caller);
168         my_exp.m_client = &my_client;
169
170         exp = NULL;
171         for (i = 0; !exp && i < MCL_MAXTYPES; i++)
172                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
173                         if (strcmp(path, exp->m_export.e_path))
174                                 continue;
175                         if (!client_matches(exp, my_client.m_hostname, ai))
176                                 continue;
177                         if (exp->m_export.e_flags & NFSEXP_V4ROOT)
178                                 /* not acceptable for v[23] export */
179                                 continue;
180                         break;
181                 }
182         *error = not_exported;
183         if (!exp)
184                 return NULL;
185
186         my_exp.m_export = exp->m_export;
187         exp = &my_exp;
188         return exp;
189 }
190
191 static nfs_export *
192 auth_authenticate_internal(const struct sockaddr *caller, const char *path,
193                 struct addrinfo *ai, enum auth_error *error)
194 {
195         nfs_export *exp;
196
197         if (new_cache) {
198                 exp = auth_authenticate_newcache(caller, path, ai, error);
199                 if (!exp)
200                         return NULL;
201         } else {
202                 exp = export_find(ai, path);
203                 if (exp == NULL) {
204                         *error = no_entry;
205                         return NULL;
206                 }
207         }
208         if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
209                      nfs_get_port(caller) >= IPPORT_RESERVED) {
210                 *error = illegal_port;
211                 return NULL;
212         }
213         *error = success;
214
215         return exp;
216 }
217
218 nfs_export *
219 auth_authenticate(const char *what, const struct sockaddr *caller,
220                 const char *path)
221 {
222         nfs_export      *exp = NULL;
223         char            epath[MAXPATHLEN+1];
224         char            *p = NULL;
225         char            buf[INET6_ADDRSTRLEN];
226         struct addrinfo *ai = NULL;
227         enum auth_error error = bad_path;
228
229         if (path[0] != '/') {
230                 xlog(L_WARNING, "Bad path in %s request from %s: \"%s\"",
231                              what, host_ntop(caller, buf, sizeof(buf)), path);
232                 return exp;
233         }
234
235         strncpy(epath, path, sizeof (epath) - 1);
236         epath[sizeof (epath) - 1] = '\0';
237         auth_fixpath(epath); /* strip duplicate '/' etc */
238
239         ai = client_resolve(caller);
240         if (ai == NULL)
241                 return exp;
242
243         /* Try the longest matching exported pathname. */
244         while (1) {
245                 exp = auth_authenticate_internal(caller, epath, ai, &error);
246                 if (exp || (error != not_exported && error != no_entry))
247                         break;
248                 /* We have to treat the root, "/", specially. */
249                 if (p == &epath[1]) break;
250                 p = strrchr(epath, '/');
251                 if (p == epath) p++;
252                 *p = '\0';
253         }
254
255         switch (error) {
256         case bad_path:
257                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
258                      what, host_ntop(caller, buf, sizeof(buf)), path);
259                 break;
260
261         case unknown_host:
262                 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
263                      what, host_ntop(caller, buf, sizeof(buf)), path, epath);
264                 break;
265
266         case no_entry:
267                 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
268                      what, ai->ai_canonname, path, epath);
269                 break;
270
271         case not_exported:
272                 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
273                      what, ai->ai_canonname, path, epath);
274                 break;
275
276         case illegal_port:
277                 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %u",
278                      what, ai->ai_canonname, path, epath, nfs_get_port(caller));
279                 break;
280
281         case success:
282                 xlog(L_NOTICE, "authenticated %s request from %s:%u for %s (%s)",
283                      what, ai->ai_canonname, nfs_get_port(caller), path, epath);
284                 break;
285         default:
286                 xlog(L_NOTICE, "%s request from %s:%u for %s (%s) gave %d",
287                      what, ai->ai_canonname, nfs_get_port(caller),
288                         path, epath, error);
289         }
290
291         freeaddrinfo(ai);
292         return exp;
293 }
294
295 static void
296 auth_fixpath(char *path)
297 {
298         char    *sp, *cp;
299
300         for (sp = cp = path; *sp; sp++) {
301                 if (*sp != '/' || sp[1] != '/')
302                         *cp++ = *sp;
303         }
304         while (cp > path+1 && cp[-1] == '/')
305                 cp--;
306         *cp = '\0';
307 }