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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/*
* stropts.c -- NFS mount using C string to pass options to kernel
*
* Copyright (C) 2007 Oracle. All rights reserved.
* Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*
*/
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/mount.h>
#include "xcommon.h"
#include "mount.h"
#include "nls.h"
#include "nfs_mount.h"
#include "mount_constants.h"
#include "stropts.h"
#include "error.h"
#include "network.h"
#ifdef HAVE_RPCSVC_NFS_PROT_H
#include <rpcsvc/nfs_prot.h>
#else
#include <linux/nfs.h>
#define nfsstat nfs_stat
#endif
#ifndef NFS_PORT
#define NFS_PORT 2049
#endif
#ifndef NFS_MAXHOSTNAME
#define NFS_MAXHOSTNAME (255)
#endif
#ifndef NFS_MAXPATHNAME
#define NFS_MAXPATHNAME (1024)
#endif
extern int nfs_mount_data_version;
extern char *progname;
extern int verbose;
static int retry_opt = 10000; /* 10,000 minutes ~= 1 week */
static int bg_opt = 0;
static int addr_opt = 0;
static int ca_opt = 0;
static int parse_devname(const char *spec, char **hostname)
{
int ret = 0;
char *dev, *pathname, *s;
dev = xstrdup(spec);
if (!(pathname = strchr(dev, ':'))) {
nfs_error(_("%s: remote share not in 'host:dir' format"),
progname);
goto out;
}
*pathname = '\0';
pathname++;
/*
* We don't need a copy of the pathname, but let's
* sanity check it anyway.
*/
if (strlen(pathname) > NFS_MAXPATHNAME) {
nfs_error(_("%s: export pathname is too long"),
progname);
goto out;
}
/*
* Ignore all but first hostname in replicated mounts
* until they can be fully supported. (mack@sgi.com)
*/
if ((s = strchr(dev, ','))) {
*s = '\0';
nfs_error(_("%s: warning: multiple hostnames not supported"),
progname);
nfs_error(_("%s: ignoring hostnames that follow the first one"),
progname);
}
*hostname = xstrdup(dev);
if (strlen(*hostname) > NFS_MAXHOSTNAME) {
nfs_error(_("%s: server hostname is too long"),
progname);
free(*hostname);
goto out;
}
ret = 1;
out:
free(dev);
return ret;
}
static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
{
struct hostent *hp;
addr->sin_family = AF_INET;
if (inet_aton(hostname, &addr->sin_addr))
return 1;
if ((hp = gethostbyname(hostname)) == NULL) {
nfs_error(_("%s: can't get address for %s\n"),
progname, hostname);
return 0;
}
if (hp->h_length > sizeof(struct in_addr)) {
nfs_error(_("%s: got bad hp->h_length"), progname);
hp->h_length = sizeof(struct in_addr);
}
memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
return 1;
}
/*
* Walk through our mount options string, and indicate the presence
* of 'bg', 'retry=', 'addr=', and 'clientaddr='.
*/
static void extract_interesting_options(char *opts)
{
char *opt, *opteq;
int val;
opts = xstrdup(opts);
for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ",")) {
if ((opteq = strchr(opt, '='))) {
val = atoi(opteq + 1);
*opteq = '\0';
if (strcmp(opt, "bg") == 0)
bg_opt++;
else if (strcmp(opt, "retry") == 0)
retry_opt = val;
else if (strcmp(opt, "addr") == 0)
addr_opt++;
else if (strcmp(opt, "clientaddr") == 0)
ca_opt++;
} else {
if (strcmp(opt, "bg") == 0)
bg_opt++;
}
}
free(opts);
}
/*
* Append the 'addr=' option to the options string. The server
* address is added to /etc/mtab for use when unmounting.
*
* Returns 1 if 'addr=' option created successfully;
* otherwise zero.
*/
static int append_addr_opt(struct sockaddr_in *saddr, char **extra_opts)
{
static char new_opts[1024];
char *s, *old_opts;
s = inet_ntoa(saddr->sin_addr);
old_opts = *extra_opts;
if (!old_opts)
old_opts = "";
if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
nfs_error(_("%s: too many mount options\n"),
progname);
return 0;
}
snprintf(new_opts, sizeof(new_opts), "%s%saddr=%s",
old_opts, *old_opts ? "," : "", s);
*extra_opts = xstrdup(new_opts);
return 1;
}
/*
* Called if no 'clientaddr=' option was specified in the options string
* to discover our address and append an appropriate 'clientaddr=' option
* to the options string.
*
* Returns 1 if 'clientaddr=' option created successfully;
* otherwise zero.
*/
static int append_clientaddr_opt(struct sockaddr_in *saddr, char **extra_opts)
{
static char new_opts[2048], cbuf[256];
struct sockaddr_in my_addr;
if (!get_client_address(saddr, &my_addr))
return 0;
if (strlen(*extra_opts) + 30 >= sizeof(new_opts)) {
nfs_error(_("%s: too many mount options"),
progname);
return 0;
}
strcat(new_opts, *extra_opts);
snprintf(cbuf, sizeof(cbuf) - 1, "%sclientaddr=%s",
*extra_opts ? "," : "", inet_ntoa(my_addr.sin_addr));
strcat(new_opts, cbuf);
*extra_opts = xstrdup(new_opts);
return 1;
}
/*
* nfsmount_s - Mount an NFSv2 or v3 file system using C string options
*
* @spec: C string hostname:path specifying remoteshare to mount
* @node: C string pathname of local mounted on directory
* @flags: MS_ style flags
* @extra_opts: pointer to C string containing fs-specific mount options
* (possibly also a return argument)
* @fake: flag indicating whether to carry out the whole operation
* @bg: one if this is a backgrounded mount attempt
*
* XXX: need to handle bg, fg, and retry options.
*/
int nfsmount_s(const char *spec, const char *node, int flags,
char **extra_opts, int fake, int bg)
{
struct sockaddr_in saddr;
char *hostname;
int err;
if (!parse_devname(spec, &hostname))
return EX_FAIL;
err = fill_ipv4_sockaddr(hostname, &saddr);
free(hostname);
if (!err)
return EX_FAIL;
extract_interesting_options(*extra_opts);
if (!bg && addr_opt) {
nfs_error(_("%s: Illegal option: 'addr='"), progname);
return EX_FAIL;
}
if (!append_addr_opt(&saddr, extra_opts))
return EX_FAIL;
if (verbose)
printf(_("%s: text-based options: '%s'\n"),
progname, *extra_opts);
if (!fake) {
if (mount(spec, node, "nfs",
flags & ~(MS_USER|MS_USERS), *extra_opts)) {
mount_error(spec, node, errno);
return EX_FAIL;
}
}
return EX_SUCCESS;
}
/*
* nfs4mount_s - Mount an NFSv4 file system using C string options
*
* @spec: C string hostname:path specifying remoteshare to mount
* @node: C string pathname of local mounted on directory
* @flags: MS_ style flags
* @extra_opts: pointer to C string containing fs-specific mount options
* (possibly also a return argument)
* @fake: flag indicating whether to carry out the whole operation
* @child: one if this is a backgrounded mount
*
* XXX: need to handle bg, fg, and retry options.
*
*/
int nfs4mount_s(const char *spec, const char *node, int flags,
char **extra_opts, int fake, int child)
{
struct sockaddr_in saddr;
char *hostname;
int err;
if (!parse_devname(spec, &hostname))
return EX_FAIL;
err = fill_ipv4_sockaddr(hostname, &saddr);
free(hostname);
if (!err)
return EX_FAIL;
extract_interesting_options(*extra_opts);
if (addr_opt) {
nfs_error(_("%s: Illegal option: 'addr='"), progname);
return EX_FAIL;
}
if (!append_addr_opt(&saddr, extra_opts))
return EX_FAIL;
if (!ca_opt && !append_clientaddr_opt(&saddr, extra_opts))
return EX_FAIL;
if (verbose)
printf(_("%s: text-based options: '%s'\n"),
progname, *extra_opts);
if (!fake) {
if (mount(spec, node, "nfs4",
flags & ~(MS_USER|MS_USERS), *extra_opts)) {
mount_error(spec, node, errno);
return EX_FAIL;
}
}
return EX_SUCCESS;
}
|