]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/auth.c
6fbf72b5374a971fe74c6a5fc74fb5022c312911
[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 static nfs_export *
130 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
131                            char *path, struct hostent *hp,
132                            enum auth_error *error)
133 {
134         nfs_export              *exp;
135
136         if (new_cache) {
137                 int i;
138                 /* return static nfs_export with details filled in */
139                 free(my_client.m_hostname);
140                 my_client.m_hostname = get_client_hostname(caller, hp, error);
141                 if (my_client.m_hostname == NULL)
142                         return NULL;
143                 my_client.m_naddr = 1;
144                 my_client.m_addrlist[0] = caller->sin_addr;
145                 my_exp.m_client = &my_client;
146
147                 exp = NULL;
148                 for (i = 0; !exp && i < MCL_MAXTYPES; i++) 
149                         for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
150                                 if (strcmp(path, exp->m_export.e_path))
151                                         continue;
152                                 if (!use_ipaddr && !client_member(my_client.m_hostname, exp->m_client->m_hostname))
153                                         continue;
154                                 if (use_ipaddr && !client_check(exp->m_client, hp))
155                                         continue;
156                                 break;
157                         }
158                 *error = not_exported;
159                 if (!exp)
160                         return exp;
161
162                 my_exp.m_export = exp->m_export;
163                 exp = &my_exp;
164
165         } else {
166                 if (!(exp = export_find(hp, path))) {
167                         *error = no_entry;
168                         return NULL;
169                 }
170                 if (!exp->m_mayexport) {
171                         *error = not_exported;
172                         return NULL;
173                 }
174         }
175         if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
176                      ntohs(caller->sin_port) >= IPPORT_RESERVED) {
177                 *error = illegal_port;
178                 return NULL;
179         }
180         *error = success;
181
182         return exp;
183 }
184
185 nfs_export *
186 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
187 {
188         nfs_export      *exp = NULL;
189         char            epath[MAXPATHLEN+1];
190         char            *p = NULL;
191         struct hostent  *hp = NULL;
192         struct in_addr  addr = caller->sin_addr;
193         enum auth_error error = bad_path;
194
195         if (path [0] != '/') {
196                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
197                      what, inet_ntoa(addr), path);
198                 return exp;
199         }
200
201         strncpy(epath, path, sizeof (epath) - 1);
202         epath[sizeof (epath) - 1] = '\0';
203         auth_fixpath(epath); /* strip duplicate '/' etc */
204
205         hp = client_resolve(caller->sin_addr);
206         if (!hp)
207                 return exp;
208
209         /* Try the longest matching exported pathname. */
210         while (1) {
211                 exp = auth_authenticate_internal(what, caller, epath,
212                                                  hp, &error);
213                 if (exp || (error != not_exported && error != no_entry))
214                         break;
215                 /* We have to treat the root, "/", specially. */
216                 if (p == &epath[1]) break;
217                 p = strrchr(epath, '/');
218                 if (p == epath) p++;
219                 *p = '\0';
220         }
221
222         switch (error) {
223         case bad_path:
224                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
225                      what, inet_ntoa(addr), path);
226                 break;
227
228         case unknown_host:
229                 xlog(L_WARNING, "refused %s request from %s for %s (%s): unmatched host",
230                      what, inet_ntoa(addr), path, epath);
231                 break;
232
233         case no_entry:
234                 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
235                      what, hp->h_name, path, epath);
236                 break;
237
238         case not_exported:
239                 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
240                      what, hp->h_name, path, epath);
241                 break;
242
243         case illegal_port:
244                 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
245                      what, hp->h_name, path, epath, ntohs(caller->sin_port));
246                 break;
247
248         case success:
249                 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
250                      what, hp->h_name, ntohs(caller->sin_port), path, epath);
251                 break;
252         default:
253                 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
254                      what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
255         }
256
257         if (hp)
258                 free (hp);
259
260         return exp;
261 }
262
263 static void
264 auth_fixpath(char *path)
265 {
266         char    *sp, *cp;
267
268         for (sp = cp = path; *sp; sp++) {
269                 if (*sp != '/' || sp[1] != '/')
270                         *cp++ = *sp;
271         }
272         while (cp > path+1 && cp[-1] == '/')
273                 cp--;
274         *cp = '\0';
275 }