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