]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/auth.c
Ensure 'showmount -e' gets current information.
[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 "misc.h"
18 #include "nfslib.h"
19 #include "exportfs.h"
20 #include "mountd.h"
21 #include "xmalloc.h"
22
23 enum auth_error
24 {
25   bad_path,
26   unknown_host,
27   no_entry,
28   not_exported,
29   illegal_port,
30   success
31 };
32
33 static void             auth_fixpath(char *path);
34 static char     *export_file = NULL;
35 static nfs_export my_exp;
36 static nfs_client my_client;
37
38 extern int new_cache;
39
40 void
41 auth_init(char *exports)
42 {
43
44         export_file = exports;
45         auth_reload();
46         xtab_mount_write();
47 }
48
49 time_t
50 auth_reload()
51 {
52         struct stat             stb;
53         static time_t           last_modified = 0;
54
55         if (stat(_PATH_ETAB, &stb) < 0)
56                 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
57         if (stb.st_mtime == last_modified)
58                 return last_modified;
59         last_modified = stb.st_mtime;
60
61         export_freeall();
62         memset(&my_client, 0, sizeof(my_client));
63         // export_read(export_file);
64         xtab_export_read();
65
66         return last_modified;
67 }
68
69 static nfs_export *
70 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
71                            char *path, struct hostent *hp,
72                            enum auth_error *error)
73 {
74         nfs_export              *exp;
75
76         if (new_cache) {
77                 int i;
78                 /* return static nfs_export with details filled in */
79                 char *n;
80                 my_client.m_addrlist[0] = caller->sin_addr;
81                 n = client_compose(caller->sin_addr);
82                 *error = unknown_host;
83                 if (!n)
84                         return NULL;
85                 strcpy(my_client.m_hostname, *n?n:"DEFAULT");
86                 free(n);
87                 my_client.m_naddr = 1;
88                 my_exp.m_client = &my_client;
89
90                 exp = NULL;
91                 for (i = 0; !exp && i < MCL_MAXTYPES; i++) 
92                         for (exp = exportlist[i]; exp; exp = exp->m_next) {
93                                 if (!client_member(my_client.m_hostname, exp->m_client->m_hostname))
94                                         continue;
95                                 if (strcmp(path, exp->m_export.e_path))
96                                         continue;
97                                 break;
98                         }
99                 *error = not_exported;
100                 if (!exp)
101                         return exp;
102
103                 my_exp.m_export = exp->m_export;
104                 exp = &my_exp;
105
106         } else {
107                 if (!(exp = export_find(hp, path))) {
108                         *error = no_entry;
109                         return NULL;
110                 }
111                 if (!exp->m_mayexport) {
112                         *error = not_exported;
113                         return NULL;
114                 }
115         }
116         if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
117                     (ntohs(caller->sin_port) <  IPPORT_RESERVED/2 ||
118                      ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
119                 *error = illegal_port;
120                 return NULL;
121         }
122         *error = success;
123
124         return exp;
125 }
126
127 nfs_export *
128 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
129 {
130         nfs_export      *exp = NULL;
131         char            epath[MAXPATHLEN+1];
132         char            *p = NULL;
133         struct hostent  *hp = NULL;
134         struct in_addr  addr = caller->sin_addr;
135         enum auth_error error;
136
137         if (path [0] != '/') {
138                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
139                      what, inet_ntoa(addr), path);
140                 return exp;
141         }
142
143         strncpy(epath, path, sizeof (epath) - 1);
144         epath[sizeof (epath) - 1] = '\0';
145         auth_fixpath(epath); /* strip duplicate '/' etc */
146
147         hp = get_reliable_hostbyaddr((const char*)&caller->sin_addr, sizeof(struct in_addr),
148                                      AF_INET);
149         if (!hp)
150                 hp = get_hostent((const char*)&caller->sin_addr, sizeof(struct in_addr),
151                                      AF_INET);
152         if (!hp)
153                 return exp;
154
155         /* Try the longest matching exported pathname. */
156         while (1) {
157                 exp = auth_authenticate_internal(what, caller, epath,
158                                                  hp, &error);
159                 if (exp || (error != not_exported && error != no_entry))
160                         break;
161                 /* We have to treat the root, "/", specially. */
162                 if (p == &epath[1]) break;
163                 p = strrchr(epath, '/');
164                 if (p == epath) p++;
165                 *p = '\0';
166         }
167
168         switch (error) {
169         case bad_path:
170                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
171                      what, inet_ntoa(addr), path);
172                 break;
173
174         case unknown_host:
175                 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
176                      what, inet_ntoa(addr), path, epath);
177                 break;
178
179         case no_entry:
180                 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
181                      what, hp->h_name, path, epath);
182                 break;
183
184         case not_exported:
185                 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
186                      what, hp->h_name, path, epath);
187                 break;
188
189         case illegal_port:
190                 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
191                      what, hp->h_name, path, epath, ntohs(caller->sin_port));
192                 break;
193
194         case success:
195                 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
196                      what, hp->h_name, ntohs(caller->sin_port), path, epath);
197                 break;
198         default:
199                 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
200                      what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
201         }
202
203         if (hp)
204                 free (hp);
205
206         return exp;
207 }
208
209 static void
210 auth_fixpath(char *path)
211 {
212         char    *sp, *cp;
213
214         for (sp = cp = path; *sp; sp++) {
215                 if (*sp != '/' || sp[1] != '/')
216                         *cp++ = *sp;
217         }
218         while (cp > path+1 && cp[-1] == '/')
219                 cp--;
220         *cp = '\0';
221 }