]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/auth.c
98c3944767767744091db7954824643b6a73d1bc
[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 #include "config.h"
10
11 #include <sys/stat.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include <errno.h>
15 #include "misc.h"
16 #include "nfslib.h"
17 #include "exportfs.h"
18 #include "mountd.h"
19 #include "xmalloc.h"
20
21 enum auth_error
22 {
23   bad_path,
24   unknown_host,
25   no_entry,
26   not_exported,
27   illegal_port,
28   faked_hostent,
29   no_forward_dns,
30   success
31 };
32
33 static void             auth_fixpath(char *path);
34 static nfs_export*      auth_authenticate_internal
35   (char *what, struct sockaddr_in *caller, char *path,
36    struct hostent **hpp, enum auth_error *error);
37 static char     *export_file = NULL;
38
39 void
40 auth_init(char *exports)
41 {
42
43         export_file = exports;
44         auth_reload();
45         xtab_mount_write();
46 }
47
48 int
49 auth_reload()
50 {
51         struct stat             stb;
52         static time_t           last_modified = 0;
53
54         if (stat(_PATH_ETAB, &stb) < 0)
55                 xlog(L_FATAL, "couldn't stat %s", _PATH_ETAB);
56         if (stb.st_mtime == last_modified)
57                 return 0;
58         last_modified = stb.st_mtime;
59
60         export_freeall();
61         // export_read(export_file);
62         xtab_export_read();
63
64         return 1;
65 }
66
67 static nfs_export *
68 auth_authenticate_internal(char *what, struct sockaddr_in *caller,
69                            char *path, struct hostent **hpp,
70                            enum auth_error *error)
71 {
72         struct in_addr          addr = caller->sin_addr;
73         nfs_export              *exp;
74
75         if (path[0] != '/') {
76                 *error = bad_path;
77                 return NULL;
78         }
79         auth_fixpath(path);
80
81         if (!(*hpp = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET)))
82                 *hpp = get_hostent((const char *)&addr, sizeof(addr),
83                                    AF_INET);
84         else {
85                 /* must make sure the hostent is authorative. */
86                 char **sp;
87                 struct hostent *forward = NULL;
88                 char *tmpname;
89
90                 tmpname = xstrdup((*hpp)->h_name);
91                 if (tmpname) {
92                         forward = gethostbyname(tmpname);
93                         free(tmpname);
94                 }
95                 if (forward) {
96                         /* now make sure the "addr" is in the list */
97                         for (sp = forward->h_addr_list ; *sp ; sp++) {
98                                 if (memcmp(*sp, &addr, forward->h_length)==0)
99                                         break;
100                         }
101                 
102                         if (!*sp) {
103                                 /* it was a FAKE */
104                                 *error = faked_hostent;
105                                 *hpp = hostent_dup (*hpp);
106                                 return NULL;
107                         }
108                         *hpp = hostent_dup (forward);
109                 }
110                 else {
111                         /* never heard of it. misconfigured DNS? */
112                         *error = no_forward_dns;
113                         *hpp = hostent_dup (*hpp);
114                         return NULL;
115                 }
116         }
117
118         if (!(exp = export_find(*hpp, path))) {
119                 *error = no_entry;
120                 return NULL;
121         }
122         if (!exp->m_mayexport) {
123                 *error = not_exported;
124                 return NULL;
125         }
126
127         if (!(exp->m_export.e_flags & NFSEXP_INSECURE_PORT) &&
128             (ntohs(caller->sin_port) <  IPPORT_RESERVED/2 ||
129              ntohs(caller->sin_port) >= IPPORT_RESERVED)) {
130                 *error = illegal_port;
131                 return NULL;
132         }
133
134         *error = success;
135
136         return exp;
137 }
138
139 nfs_export *
140 auth_authenticate(char *what, struct sockaddr_in *caller, char *path)
141 {
142         nfs_export      *exp = NULL;
143         char            epath[MAXPATHLEN+1];
144         char            *p = NULL;
145         struct hostent  *hp = NULL;
146         struct in_addr  addr = caller->sin_addr;
147         enum auth_error error;
148
149         if (path [0] != '/') return exp;
150
151         strncpy(epath, path, sizeof (epath) - 1);
152         epath[sizeof (epath) - 1] = '\0';
153
154         /* Try the longest matching exported pathname. */
155         while (1) {
156                 if (hp) {
157                         free (hp);
158                         hp = NULL;
159                 }
160                 exp = auth_authenticate_internal(what, caller, epath,
161                                                  &hp, &error);
162                 if (exp || (error != not_exported && error != no_entry))
163                         break;
164                 /* We have to treat the root, "/", specially. */
165                 if (p == &epath[1]) break;
166                 p = strrchr(epath, '/');
167                 if (p == epath) p++;
168                 *p = '\0';
169         }
170
171         switch (error) {
172         case bad_path:
173                 xlog(L_WARNING, "bad path in %s request from %s: \"%s\"",
174                      what, inet_ntoa(addr), path);
175                 break;
176
177         case unknown_host:
178                 xlog(L_WARNING, "%s request from unknown host %s for %s (%s)",
179                      what, inet_ntoa(addr), path, epath);
180                 break;
181
182         case no_entry:
183                 xlog(L_WARNING, "refused %s request from %s for %s (%s): no export entry",
184                      what, hp->h_name, path, epath);
185                 break;
186
187         case not_exported:
188                 xlog(L_WARNING, "refused %s request from %s for %s (%s): not exported",
189                      what, hp->h_name, path, epath);
190                 break;
191
192         case illegal_port:
193                 xlog(L_WARNING, "refused %s request from %s for %s (%s): illegal port %d",
194                      what, hp->h_name, path, epath, ntohs(caller->sin_port));
195                 break;
196
197         case faked_hostent:
198                 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): DNS forward lookup does't match with reverse",
199                      what, inet_ntoa(addr), hp->h_name, path, epath);
200                 break;
201
202         case no_forward_dns:
203                 xlog(L_WARNING, "refused %s request from %s (%s) for %s (%s): no DNS forward lookup",
204                      what, inet_ntoa(addr), hp->h_name, path, epath);
205                 break;
206
207         case success:
208                 xlog(L_NOTICE, "authenticated %s request from %s:%d for %s (%s)",
209                      what, hp->h_name, ntohs(caller->sin_port), path, epath);
210                 break;
211         default:
212                 xlog(L_NOTICE, "%s request from %s:%d for %s (%s) gave %d",
213                      what, hp->h_name, ntohs(caller->sin_port), path, epath, error);
214         }
215
216         if (hp)
217                 free (hp);
218
219         return exp;
220 }
221
222 static void
223 auth_fixpath(char *path)
224 {
225         char    *sp, *cp;
226
227         for (sp = cp = path; *sp; sp++) {
228                 if (*sp != '/' || sp[1] != '/')
229                         *cp++ = *sp;
230         }
231         while (cp > path+1 && cp[-1] == '/')
232                 cp--;
233         *cp = '\0';
234 }