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