]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/auth.c
nfs-utils: Fix source code character encoding
[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                         break;
163                 }
164         *error = not_exported;
165         if (!exp)
166                 return NULL;
167
168         my_exp.m_export = exp->m_export;
169         exp = &my_exp;
170         return exp;
171 }
172
173 static nfs_export *
174 auth_authenticate_internal(const struct sockaddr *caller, const char *path,
175                 struct addrinfo *ai, enum auth_error *error)
176 {
177         nfs_export *exp;
178
179         if (new_cache) {
180                 exp = auth_authenticate_newcache(caller, path, ai, error);
181                 if (!exp)
182                         return NULL;
183         } else {
184                 exp = export_find(ai, path);
185                 if (exp == NULL) {
186                         *error = no_entry;
187                         return NULL;
188                 }
189         }
190         if (exp->m_export.e_flags & NFSEXP_V4ROOT) {
191                 *error = no_entry;
192                 return NULL;
193         }
194         if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
195                      nfs_get_port(caller) >= IPPORT_RESERVED) {
196                 *error = illegal_port;
197                 return NULL;
198         }
199         *error = success;
200
201         return exp;
202 }
203
204 nfs_export *
205 auth_authenticate(const char *what, const struct sockaddr *caller,
206                 const char *path)
207 {
208         nfs_export      *exp = NULL;
209         char            epath[MAXPATHLEN+1];
210         char            *p = NULL;
211         char            buf[INET6_ADDRSTRLEN];
212         struct addrinfo *ai = NULL;
213         enum auth_error error = bad_path;
214
215         if (path[0] != '/') {
216                 xlog(L_WARNING, "Bad path in %s request from %s: \"%s\"",
217                              what, host_ntop(caller, buf, sizeof(buf)), path);
218                 return exp;
219         }
220
221         strncpy(epath, path, sizeof (epath) - 1);
222         epath[sizeof (epath) - 1] = '\0';
223         auth_fixpath(epath); /* strip duplicate '/' etc */
224
225         ai = client_resolve(caller);
226         if (ai == NULL)
227                 return exp;
228
229         /* Try the longest matching exported pathname. */
230         while (1) {
231                 exp = auth_authenticate_internal(caller, epath, ai, &error);
232                 if (exp || (error != not_exported && error != no_entry))
233                         break;
234                 /* We have to treat the root, "/", specially. */
235                 if (p == &epath[1]) break;
236                 p = strrchr(epath, '/');
237                 if (p == epath) p++;
238                 *p = '\0';
239         }
240
241         switch (error) {
242         case bad_path:
243                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
244                      what, host_ntop(caller, buf, sizeof(buf)), path);
245                 break;
246
247         case unknown_host:
248                 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
249                      what, host_ntop(caller, buf, sizeof(buf)), path, epath);
250                 break;
251
252         case no_entry:
253                 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
254                      what, ai->ai_canonname, path, epath);
255                 break;
256
257         case not_exported:
258                 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
259                      what, ai->ai_canonname, path, epath);
260                 break;
261
262         case illegal_port:
263                 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %u",
264                      what, ai->ai_canonname, path, epath, nfs_get_port(caller));
265                 break;
266
267         case success:
268                 xlog(L_NOTICE, "authenticated %s request from %s:%u for %s (%s)",
269                      what, ai->ai_canonname, nfs_get_port(caller), path, epath);
270                 break;
271         default:
272                 xlog(L_NOTICE, "%s request from %s:%u for %s (%s) gave %d",
273                      what, ai->ai_canonname, nfs_get_port(caller),
274                         path, epath, error);
275         }
276
277         freeaddrinfo(ai);
278         return exp;
279 }
280
281 static void
282 auth_fixpath(char *path)
283 {
284         char    *sp, *cp;
285
286         for (sp = cp = path; *sp; sp++) {
287                 if (*sp != '/' || sp[1] != '/')
288                         *cp++ = *sp;
289         }
290         while (cp > path+1 && cp[-1] == '/')
291                 cp--;
292         *cp = '\0';
293 }