forked from unfs3/unfs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.c
310 lines (255 loc) · 7.46 KB
/
mount.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* UNFS3 mount protocol procedures
* (C) 2004, Pascal Schmidt
* see file LICENSE for license details
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <rpc/rpc.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <syslog.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif /* WIN32 */
#include <fcntl.h>
#include "nfs.h"
#include "mount.h"
#include "daemon.h"
#include "fh.h"
#include "fh_cache.h"
#include "fd_cache.h"
#include "Config/exports.h"
#include "password.h"
#include "backend.h"
#ifndef PATH_MAX
# define PATH_MAX 4096
#endif
#define IS_SECURE(port) ((port) < 1024)
/*
* number of active mounts
*
* only a guess since clients can crash and/or not sent UMNT calls
*/
static int mount_cnt = 0;
/* list of currently mounted directories */
static mountlist mount_list = NULL;
static char nonce[32] = "";
/*
* add entry to mount list
*/
static void add_mount(const char *path, struct svc_req *rqstp)
{
mountlist new;
mountlist iter;
struct in6_addr addr;
char host[INET6_ADDRSTRLEN];
new = malloc(sizeof(struct mountbody));
if (!new) {
logmsg(LOG_CRIT, "add_mount: Unable to allocate memory");
return;
}
if (get_remote(rqstp, &addr)) {
logmsg(LOG_CRIT, "add_mount: Cannot get client address");
return;
}
inet_ntop(AF_INET6, &addr, host, sizeof(host));
new->ml_hostname = malloc(strlen(host) + 1);
if (!new->ml_hostname) {
logmsg(LOG_CRIT, "add_mount: Unable to allocate memory");
free(new);
return;
}
new->ml_directory = malloc(strlen(path) + 1);
if (!new->ml_directory) {
logmsg(LOG_CRIT, "add_mount: Unable to allocate memory");
free(new->ml_hostname);
free(new);
return;
}
/* initialize the new entry */
new->ml_next = NULL;
strcpy(new->ml_hostname, host);
strcpy(new->ml_directory, path);
iter = mount_list;
if (iter) {
while (iter->ml_next)
iter = iter->ml_next;
iter->ml_next = new;
} else
mount_list = new;
mount_cnt++;
}
/*
* remove entries from mount list
*/
static void remove_mount(const char *path, struct svc_req *rqstp)
{
mountlist iter, next, prev = NULL;
struct in6_addr addr;
char host[INET6_ADDRSTRLEN];
if (get_remote(rqstp, &addr)) {
logmsg(LOG_CRIT, "remove_mount: Cannot get client address");
return;
}
inet_ntop(AF_INET6, &addr, host, sizeof(host));
iter = mount_list;
while (iter) {
if (strcmp(iter->ml_hostname, host) == 0 &&
(!path || strcmp(iter->ml_directory, path) == 0)) {
if (prev)
prev->ml_next = iter->ml_next;
else
mount_list = iter->ml_next;
next = iter->ml_next;
free(iter->ml_hostname);
free(iter->ml_directory);
free(iter);
iter = next;
/* adjust mount count */
if (mount_cnt > 0)
mount_cnt--;
} else {
prev = iter;
iter = iter->ml_next;
}
}
}
void *mountproc_null_3_svc(U(void *argp), U(struct svc_req *rqstp))
{
static void *result = NULL;
return &result;
}
mountres3 *mountproc_mnt_3_svc(dirpath * argp, struct svc_req * rqstp)
{
char buf[PATH_MAX];
unfs3_fh_t fh;
nfs_fh3 nfh;
static char fhbuf[FH_MAXBUF];
static mountres3 result;
static int auth = AUTH_UNIX;
int authenticated = 0;
char *password;
struct in6_addr addr;
char host[INET6_ADDRSTRLEN];
/* We need to modify the *argp pointer. Make a copy. */
char *dpath = *argp;
/* error out if not version 3 */
if (rqstp->rq_vers != 3) {
get_remote(rqstp, &addr);
logmsg(LOG_INFO,
"%s attempted mount with unsupported protocol version",
inet_ntop(AF_INET6, &addr, host, sizeof(host)));
result.fhs_status = MNT3ERR_INVAL;
return &result;
}
/* Check for "mount commands" */
if (strncmp(dpath, "@getnonce", sizeof("@getnonce") - 1) == 0) {
if (backend_gen_nonce(nonce) < 0) {
result.fhs_status = MNT3ERR_IO;
} else {
result.fhs_status = MNT3_OK;
result.mountres3_u.mountinfo.fhandle.fhandle3_len = 32;
result.mountres3_u.mountinfo.fhandle.fhandle3_val = nonce;
result.mountres3_u.mountinfo.auth_flavors.auth_flavors_len = 1;
result.mountres3_u.mountinfo.auth_flavors.auth_flavors_val =
&auth;
}
return &result;
} else if (strncmp(dpath, "@password:", sizeof("@password:") - 1) == 0) {
char pw[PASSWORD_MAXLEN + 1];
mnt_cmd_argument(&dpath, "@password:", pw, PASSWORD_MAXLEN);
if (exports_options(dpath, rqstp, &password, NULL) != -1) {
authenticated = !strcmp(password, pw);
}
/* else leave authenticated unchanged */
} else if (strncmp(dpath, "@otp:", sizeof("@otp:") - 1) == 0) {
/* The otp from the client */
char otp[PASSWORD_MAXLEN + 1];
/* Our calculated otp */
char hexdigest[32];
mnt_cmd_argument(&dpath, "@otp:", otp, PASSWORD_MAXLEN);
if (exports_options(dpath, rqstp, &password, NULL) != -1) {
otp_digest(nonce, password, hexdigest);
/* Compare our calculated digest with what the client submitted */
authenticated = !strncmp(hexdigest, otp, 32);
/* Change nonce */
backend_gen_nonce(nonce);
}
/* else leave authenticated unchanged */
}
if ((exports_opts & OPT_REMOVABLE) && export_point(dpath)) {
/* Removable media export point. Do not call realpath; simply copy
path */
strncpy(buf, dpath, PATH_MAX);
} else if (!backend_realpath(dpath, buf)) {
/* the given path does not exist */
result.fhs_status = MNT3ERR_NOENT;
return &result;
}
if (strlen(buf) + 1 > NFS_MAXPATHLEN) {
get_remote(rqstp, &addr);
logmsg(LOG_INFO, "%s attempted to mount jumbo path",
inet_ntop(AF_INET6, &addr, host, sizeof(host)));
result.fhs_status = MNT3ERR_NAMETOOLONG;
return &result;
}
if ((exports_options(buf, rqstp, &password, NULL) == -1)
|| (!authenticated && password[0])
|| (!(exports_opts & OPT_INSECURE) &&
!IS_SECURE(ntohs(get_port(rqstp))))
) {
/* not exported to this host or at all, or a password defined and not
authenticated */
result.fhs_status = MNT3ERR_ACCES;
return &result;
}
fh = fh_comp(buf, rqstp, FH_DIR);
if (!fh_valid(fh)) {
get_remote(rqstp, &addr);
logmsg(LOG_INFO, "%s attempted to mount non-directory",
inet_ntop(AF_INET6, &addr, host, sizeof(host)));
result.fhs_status = MNT3ERR_NOTDIR;
return &result;
}
add_mount(dpath, rqstp);
nfh = fh_encode(&fh, fhbuf);
result.fhs_status = MNT3_OK;
result.mountres3_u.mountinfo.fhandle.fhandle3_len = nfh.data.data_len;
result.mountres3_u.mountinfo.fhandle.fhandle3_val = nfh.data.data_val;
result.mountres3_u.mountinfo.auth_flavors.auth_flavors_len = 1;
result.mountres3_u.mountinfo.auth_flavors.auth_flavors_val = &auth;
return &result;
}
mountlist *mountproc_dump_3_svc(U(void *argp), U(struct svc_req *rqstp))
{
return &mount_list;
}
void *mountproc_umnt_3_svc(dirpath * argp, struct svc_req *rqstp)
{
/* RPC times out if we use a NULL pointer */
static void *result = NULL;
remove_mount(*argp, rqstp);
/* if no more mounts are active, flush all open file descriptors */
if (mount_cnt == 0)
fd_cache_purge();
return &result;
}
void *mountproc_umntall_3_svc(U(void *argp), struct svc_req *rqstp)
{
/* RPC times out if we use a NULL pointer */
static void *result = NULL;
remove_mount(NULL, rqstp);
/* if no more mounts are active, flush all open file descriptors */
if (mount_cnt == 0)
fd_cache_purge();
return &result;
}
exports *mountproc_export_3_svc(U(void *argp), U(struct svc_req *rqstp))
{
return &exports_nfslist;
}