File: mount.fedfs.c

package info (click to toggle)
autofs 5.1.9-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,724 kB
  • sloc: ansic: 36,118; yacc: 1,821; lex: 1,126; sh: 627; makefile: 541
file content (485 lines) | stat: -rw-r--r-- 11,586 bytes parent folder | download | duplicates (3)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Copyright 2011 Oracle.  All rights reserved.
 *
 * This file is part of fedfs-utils.
 *
 * fedfs-utils is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2.0 as
 * published by the Free Software Foundation.
 *
 * fedfs-utils 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 version 2.0 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2.0 along with fedfs-utils.  If not, see:
 *
 *	http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/mount.h>
#include <sys/wait.h>

#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <libgen.h>
#include <errno.h>
#include <getopt.h>
#include <locale.h>
#include <netdb.h>
#include <langinfo.h>

#include "fedfs-nls.h"
#include "fedfs-getsrvinfo.h"
#include "fedfs-gpl-boiler.h"

/**
 * Top-level directory on client under which we mount NFSv4 domain roots
 */
#define FEDFS_NFS4_TLDIR		"nfs4"

/**
 * Name of SRV record containing NFSv4 FedFS root
 */
#define FEDFS_NFS_DOMAINROOT	"_nfs-domainroot._tcp"

/**
 * Export path of NFSv4 FedFS root
 */
#define FEDFS_NFS_EXPORTPATH	"/.domainroot"

/**
 * Pathname to mount.nfs
 */
#define MOUNT_NFS_EXECUTABLE		"/sbin/mount.nfs"

/**
 * Mount status values, lifted from util-linux
 */
enum {
	EX_SUCCESS	= 0,
	EX_USAGE	= 1,
	EX_FAIL		= 32,
};

static char *progname;
static int nomtab;
static int verbose;
static _Bool readonly;
static _Bool sloppy;
static _Bool fake;

/**
 * Short form command line options
 */
static const char fedfs_opts[] = "fhno:rsvVw";

/**
 * Long form command line options
 */
static const struct option fedfs_longopts[] =
{
	{ "fake", 0, NULL, 'f' },
	{ "help", 0, NULL, 'h' },
	{ "no-mtab", 0, NULL, 'n' },
	{ "options", 1, NULL, 'o' },
	{ "read-only", 0, NULL, 'r' },
	{ "read-write", 0, NULL, 'w' },
	{ "ro", 0, NULL, 'r' },
	{ "rw", 0, NULL, 'w' },
	{ "sloppy", 0, NULL, 's' },
	{ "verbose", 0, NULL, 'v' },
	{ "version", 0, NULL, 'V' },
	{ NULL, 0, NULL, 0 }
};

/**
 * Display mount.fedfs usage message
 */
static void
mount_usage(void)
{
	printf(_("\nUsage: %s remotedir localdir [-fhnrsvVw]\n\n"), progname);
	printf(_("options:\n"));
	printf(_("\t-f\t\tFake mount, do not actually mount\n"));
	printf(_("\t-h\t\tPrint this help\n"));
	printf(_("\t-n\t\tDo not update /etc/mtab\n"));
	printf(_("\t-r\t\tMount file system readonly\n"));
	printf(_("\t-s\t\tTolerate sloppy mount options\n"));
	printf(_("\t-v\t\tVerbose\n"));
	printf(_("\t-V\t\tPrint version\n"));
	printf(_("\t-w\t\tMount file system read-write\n"));

	printf("%s", fedfs_gpl_boilerplate);
}

/**
 * Concatenate three strings
 *
 * @param s NUL-terminated C string
 * @param t NUL-terminated C string
 * @param u NUL-terminated C string
 * @return pointer to NUL-terminated C string or NULL
 *
 * Caller must free the returned string with free(3).  Always frees
 * its first arg - typical use: s = xstrconcat3(s,t,u);
 *
 * Lifted from util-linux.
 */
static char *
xstrconcat3(char *s, const char *t, const char *u)
{
	_Bool free_s = true;
	char *result;

	if (s == NULL) {
		s = "";
		free_s = false;
	}
	if (t == NULL)
		t = "";
	if (u == NULL)
		u = "";
	result = malloc(strlen(s) + strlen(t) + strlen(u) + 1);
	if (result == NULL)
		goto out;

	strcpy(result, s);
	strcat(result, t);
	strcat(result, u);

out:
	if (free_s)
		free(s);
	return result;
}

/**
 * Exec mount.nfs
 *
 * @param server NUL-terminated C string containing name of NFS server
 * @param port server port to use when mounting
 * @param domainname NUL-terminated C string containing FedFS domain name
 * @param export_path NUL-terminated C string containing server export path
 * @param mounted_on_dir NUL-terminated C string containing local mounted-on directory
 * @param text_options NUL-terminated C string containing user's mount options
 *
 */
static void
exec_mount_nfs4(const char *server, unsigned short port,
		const char *domainname, const char *export_path,
		const char *mounted_on_dir, const char *text_options)
{
	static char special[2048];
	static char options[2048];
	char *args[16];
	int count = 0;

	snprintf(special, sizeof(special), "%s:%s/%s%s", server,
			FEDFS_NFS_EXPORTPATH, domainname, export_path);

	if (text_options != NULL && strcmp(text_options, "") != 0)
		snprintf(options, sizeof(options), "%s,vers=4,fg,port=%u",
				text_options, port);
	else
		snprintf(options, sizeof(options), "vers=4,fg,port=%u", port);

	if (verbose) {
		printf(_("%s: Special device:       %s\n"),
			progname, special);
		printf(_("%s: Mounted-on directory: %s\n"),
			progname, mounted_on_dir);
		printf(_("%s: Mount options:        %s\n"),
			progname, options);
	}

	args[count++] = MOUNT_NFS_EXECUTABLE;
	args[count++] = special;
	args[count++] = (char *)mounted_on_dir;
	if (verbose)
		args[count++] = "-v";
	if (fake)
		args[count++] = "-f";
	if (nomtab)
		args[count++] = "-n";
	if (readonly)
		args[count++] = "-r";
	if (sloppy)
		args[count++] = "-s";
	args[count++] = "-o";
	args[count++] = options;

	args[count] = NULL;
	execv(args[0], args);
}

/**
 * Mount a FedFS domain root via NFSv4
 *
 * @param domainname NUL-terminated C string containing FedFS domain name
 * @param export_path NUL-terminated C string containing server export path
 * @param mounted_on_dir NUL-terminated C string containing local mounted-on directory
 * @param text_options NUL-terminated C string containing user's mount options
 * @return exit status code from the mount.nfs command
 *
 * Construct the server:/export string and the mounted-on directory string
 * based on the DNS SRV query results, then fork and exec mount.nfs to do
 * the actual mount request.
 */
static int
nfs4_mount(const char *domainname, const char *export_path,
		const char *mounted_on_dir, const char *text_options)
{
	struct srvinfo *tmp, *si = NULL;
	int error, status;

	status = EX_FAIL;

	error = getsrvinfo(FEDFS_NFS_DOMAINROOT, domainname, &si);
	switch (error) {
	case ESI_SUCCESS:
		break;
	case ESI_NONAME:
		fprintf(stderr, _("%s: Domain name %s not found\n"),
			progname, domainname);
		goto out;
	case ESI_SERVICE:
		fprintf(stderr, _("%s: No FedFS domain root available for %s\n"),
			progname, domainname);
		goto out;
	default:
		fprintf(stderr, _("%s: Failed to resolve %s: %s\n"),
			progname, domainname, gsi_strerror(error));
		goto out;
	}

	/*
	 * The srvinfo list is already in RFC 2782 sorted order.  Try each
	 * SRV record once, in the foreground.  Go with the first one that
	 * works.
	 */
	for (tmp = si; tmp != NULL; tmp = tmp->si_next) {
		pid_t pid;

		pid = fork();
		switch (pid) {
		case 0:
			exec_mount_nfs4(tmp->si_target, tmp->si_port,
					domainname, export_path, mounted_on_dir,
					text_options);
			/*NOTREACHED*/
			fprintf(stderr, _("%s: Failed to exec: %s\n"),
				progname, strerror(errno));
			exit(EX_FAIL);
		case -1:
			fprintf(stderr, _("%s: Failed to fork: %s\n"),
				progname, strerror(errno));
			goto out;
		default:
			waitpid(pid, &status, 0);
			if (status == EX_SUCCESS)
				goto out;
		}
	}

out:
	freesrvinfo(si);
	return status;
}

/**
 * Try one mount request
 *
 * @param source NUL-terminated C string containing name of "special device"
 * @param target NUL-terminated C string containing local mounted-on directory
 * @param text_options NUL-terminated C string containing user's mount options
 * @return an exit status code
 *
 * Parse the pathname in "source."  It contains the file system protocol
 * and FedFS domain name.  Then pass these arguments to the appropriate
 * mount helper subcommand.
 */
static int
try_mount(const char *source, const char *target, const char *text_options)
{
	char *global_name, *topdir, *domainname, *remaining;
	int result;

	remaining = NULL;
	result = EX_FAIL;

	global_name = strdup(source);
	if (global_name == NULL) {
		fprintf(stderr, _("%s: Unable to parse globally useful name\n"),
				progname);
		goto out;
	}

	topdir = strtok(global_name, "/");
	if (topdir == NULL) {
		fprintf(stderr, _("%s: Invalid globally useful name: %s\n"),
			progname, source);
		goto out;
	}
	if (verbose)
		printf(_("%s: Top-level directory:  %s\n"),
			progname, topdir);

	domainname = strtok(NULL, "/");
	if (domainname == NULL) {
		fprintf(stderr, _("%s: Missing domain name in globally "
				"useful name: %s\n"), progname, source);
		goto out;
	}
	if (verbose)
		printf(_("%s: Domain name:          %s\n"),
			progname, domainname);

	remaining = strtok(NULL, "/");
	if (remaining == NULL) {
		remaining = strdup("/");
		if (remaining == NULL) {
			fprintf(stderr, _("%s: No memory\n"), progname);
			goto out;
		}
	} else {
		char *tmp;

		tmp = malloc(strlen(remaining) + 1);
		if (tmp == NULL) {
			fprintf(stderr, _("%s: No memory\n"), progname);
			remaining = NULL;
			goto out;
		}
		strcpy(tmp, "/");
		strcat(tmp, remaining);
		remaining = tmp;
	}
	if (verbose)
		printf(_("%s: Export path:          %s\n"),
			progname, remaining);

	if (strcmp(topdir, FEDFS_NFS4_TLDIR) == 0)
		result = nfs4_mount(domainname, remaining, target, text_options);
#if 0
	/* example: SMB support plugs in here */
	else if (strcmp(topdir, FEDFS_SMB_TLDIR) == 0)
		result = smb_mount(domainname, remaining, target, text_options);
#endif
	else
		fprintf(stderr, _("%s: Unrecognized file system protocol\n"), progname);

out:
	free(global_name);
	free(remaining);

	return result;
}

/**
 * Program entry point
 *
 * @param argc count of command line arguments
 * @param argv array of NUL-terminated C strings containing command line arguments
 * @return program exit status
 */
int main(int argc, char *argv[])
{
	char *source, *target, *text_options;
	int c, mnt_err;

	(void)setlocale(LC_ALL, "");

	progname = basename(argv[0]);

	if (argv[1] && argv[1][0] == '-') {
		if(argv[1][1] == 'V')
			printf("%s (VERSION_STRING)\n", progname);
		else
			mount_usage();
		exit(EX_SUCCESS);
	}

	if (argc < 3) {
		mount_usage();
		exit(EX_USAGE);
	}

	source = argv[1];
	target = argv[2];

	mnt_err = EX_USAGE;
	text_options = NULL;
	readonly = false;
	sloppy = false;
	fake = false;
	argv[2] = argv[0]; /* so that getopt error messages are correct */
	while ((c = getopt_long(argc - 2, argv + 2, fedfs_opts,
				fedfs_longopts, NULL)) != -1) {
		switch (c) {
		case 'f':
			fake = true;
			break;
		case 'n':
			++nomtab;
			break;
		case 'o':
			/* Ugh. */
			if (text_options != NULL)
				text_options = xstrconcat3(text_options, ",", optarg);
			else
				text_options = strdup(optarg);
			if (text_options == NULL) {
				fprintf(stderr, _("%s: No memory\n"), progname);
				goto out;
			}
			break;
		case 'r':
			readonly = true;
			break;
		case 's':
			sloppy = true;
			break;
		case 'v':
			++verbose;
			break;
		case 'V':
			printf("%s: (VERSION_STRING)\n", progname);
			mnt_err = EX_SUCCESS;
			goto out;
		case 'w':
			readonly = false;
			break;
		case 'h':
		default:
			mount_usage();
			goto out;
		}
	}

	/* Extra non-option words at the end are bogus...  */
	if (optind != argc - 2) {
		mount_usage();
		goto out;
	}

	if (getuid() != 0 && geteuid() != 0) {
		fprintf(stderr, _("%s: Not installed setuid - "
			    "\"user\" FedFS mounts are not supported\n"), progname);
		mnt_err = EX_FAIL;
		goto out;
	}

	mnt_err = try_mount(source, target, text_options);

out:
	free(text_options);
	exit(mnt_err);
}