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