]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/auth.c
mountd: kill unnecessary m_mayexport check
[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_INSECURE_PORT) &&
186                      ntohs(caller->sin_port) >= IPPORT_RESERVED) {
187                 *error = illegal_port;
188                 return NULL;
189         }
190         *error = success;
191
192         return exp;
193 }
194
195 nfs_export *
196 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
197 {
198         nfs_export      *exp = NULL;
199         char            epath[MAXPATHLEN+1];
200         char            *p = NULL;
201         struct hostent  *hp = NULL;
202         struct in_addr  addr = caller->sin_addr;
203         enum auth_error error = bad_path;
204
205         if (path [0] != '/') {
206                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
207                      what, inet_ntoa(addr), path);
208                 return exp;
209         }
210
211         strncpy(epath, path, sizeof (epath) - 1);
212         epath[sizeof (epath) - 1] = '\0';
213         auth_fixpath(epath); /* strip duplicate '/' etc */
214
215         hp = client_resolve(caller->sin_addr);
216         if (!hp)
217                 return exp;
218
219         /* Try the longest matching exported pathname. */
220         while (1) {
221                 exp = auth_authenticate_internal(what, caller, epath,
222                                                  hp, &error);
223                 if (exp || (error != not_exported && error != no_entry))
224                         break;
225                 /* We have to treat the root, "/", specially. */
226                 if (p == &epath[1]) break;
227                 p = strrchr(epath, '/');
228                 if (p == epath) p++;
229                 *p = '\0';
230         }
231
232         switch (error) {
233         case bad_path:
234                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
235                      what, inet_ntoa(addr), path);
236                 break;
237
238         case unknown_host:
239                 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
240                      what, inet_ntoa(addr), path, epath);
241                 break;
242
243         case no_entry:
244                 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
245                      what, hp->h_name, path, epath);
246                 break;
247
248         case not_exported:
249                 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
250                      what, hp->h_name, path, epath);
251                 break;
252
253         case illegal_port:
254                 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
255                      what, hp->h_name, path, epath, ntohs(caller->sin_port));
256                 break;
257
258         case success:
259                 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
260                      what, hp->h_name, ntohs(caller->sin_port), path, epath);
261                 break;
262         default:
263                 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
264                      what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
265         }
266
267         if (hp)
268                 free (hp);
269
270         return exp;
271 }
272
273 static void
274 auth_fixpath(char *path)
275 {
276         char    *sp, *cp;
277
278         for (sp = cp = path; *sp; sp++) {
279                 if (*sp != '/' || sp[1] != '/')
280                         *cp++ = *sp;
281         }
282         while (cp > path+1 && cp[-1] == '/')
283                 cp--;
284         *cp = '\0';
285 }