summaryrefslogtreecommitdiff
path: root/utils/mount/configfile.c
blob: 1d88cbfcc659ce25e10e10f6d401bfe5dd899b8c (plain)
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
/*
 * configfile.c -- mount configuration file manipulation
 * Copyright (C) 2008 Red Hat, Inc <nfs@redhat.com>
 *
 * - Routines use to create mount options from the mount
 *   configuration file.
 *
 * 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, 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.
 *
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "xlog.h"
#include "mount.h"
#include "parse_opt.h"
#include "network.h"
#include "conffile.h"
#include "mount_config.h"

#define KBYTES(x)     ((x) * (1024))
#define MEGABYTES(x)  ((x) * (1048576))
#define GIGABYTES(x)  ((x) * (1073741824))

#ifndef NFSMOUNT_GLOBAL_OPTS
#define NFSMOUNT_GLOBAL_OPTS "NFSMount_Global_Options"
#endif

#ifndef NFSMOUNT_MOUNTPOINT
#define NFSMOUNT_MOUNTPOINT "MountPoint"
#endif

#ifndef NFSMOUNT_SERVER
#define NFSMOUNT_SERVER "Server"
#endif

enum {
	MNT_NOARG=0,
	MNT_INTARG,
	MNT_STRARG,
	MNT_SPEC,
	MNT_UNSET
};
struct mnt_alias {
	char *alias;
	char *opt;
	int  argtype;
} mnt_alias_tab[] = {
	{"background", "bg", MNT_NOARG},
	{"foreground", "fg", MNT_NOARG},
	{"sloppy", "sloppy", MNT_NOARG},
};
int mnt_alias_sz = (sizeof(mnt_alias_tab)/sizeof(mnt_alias_tab[0]));

static const char *version_keys[] = {
	"v3", "v4", "vers", "nfsvers", "minorversion", NULL
};

static int strict;

static int is_version(const char *field)
{
	int i;
	for (i = 0; version_keys[i] ; i++)
		if (strcmp(version_keys[i], field) == 0)
			return 1;
	if (strncmp(field, "v4.", 3) == 0)
		return 1;
	return 0;
}

/*
 * See if the option is an alias, if so return the
 * real mount option along with the argument type.
 */
inline static
char *mountopts_alias(char *opt, int *argtype)
{
	int i;

	*argtype = MNT_UNSET;
	for (i=0; i < mnt_alias_sz; i++) {
		if (strcasecmp(opt, mnt_alias_tab[i].alias) != 0)
			continue;
		*argtype = mnt_alias_tab[i].argtype;
		return mnt_alias_tab[i].opt;
	}
	/* Make option names case-insensitive */
	upper2lower(opt);

	return opt;
}
/*
 * Convert numeric strings that end with 'k', 'm' or 'g'
 * into numeric strings with the real value.
 * Meaning '8k' becomes '8094'.
 */
static char *mountopts_convert(char *value)
{
	unsigned long long factor, num;
	static char buf[64];
	char *ch;

	ch = &value[strlen(value)-1];
	switch (tolower(*ch)) {
	case 'k':
		factor = KBYTES(1);
		break;
	case 'm':
		factor = MEGABYTES(1);
		break;
	case 'g':
		factor = GIGABYTES(1);
		break;
	default:
		return value;
	}
	*ch = '\0';
	if (strncmp(value, "0x", 2) == 0) {
		num = strtol(value, (char **)NULL, 16);
	} else if (strncmp(value, "0", 1) == 0) {
		num = strtol(value, (char **)NULL, 8);
	} else {
		num = strtol(value, (char **)NULL, 10);
	}
	num *= factor;
	snprintf(buf, 64, "%lld", num);

	return buf;
}

struct nfs_version config_default_vers;
unsigned long config_default_proto;
extern sa_family_t config_default_family;

/*
 * Check to see if a default value is being set.
 * If so, set the appropriate global value which will
 * be used as the initial value in the server negation.
 */
static int
default_value(char *mopt)
{
	struct mount_options *options = NULL;
	int dftlen = strlen("default");
	char *field;

	if (strncasecmp(mopt, "default", dftlen) != 0)
		return 0;

	field = mopt + dftlen;
	if (strncasecmp(field, "proto", strlen("proto")) == 0) {
		if ((options = po_split(field)) != NULL) {
			if (!nfs_nfs_protocol(options, &config_default_proto)) {
				xlog_warn("Unable to set default protocol : %s",
					strerror(errno));
			}
			if (!nfs_nfs_proto_family(options, &config_default_family)) {
				xlog_warn("Unable to set default family : %s",
					strerror(errno));
			}
		} else {
			xlog_warn("Unable to alloc memory for default protocol");
		}
	} else if (strncasecmp(field, "vers", strlen("vers")) == 0) {
		if ((options = po_split(field)) != NULL) {
			if (!nfs_nfs_version("nfs", options, &config_default_vers)) {
				xlog_warn("Unable to set default version: %s",
					strerror(errno));
			}
		} else {
			xlog_warn("Unable to alloc memory for default version");
		}
	} else
		xlog_warn("Invalid default setting: '%s'", mopt);

	if (options)
		po_destroy(options);

	return 1;
}
/*
 * Parse the given section of the configuration
 * file to if there are any mount options set.
 * If so, added them to link list.
 */
static void
conf_parse_mntopts(char *section, char *arg, struct mount_options *options)
{
	struct conf_list *list;
	struct conf_list_node *node;
	char buf[BUFSIZ], *value, *field;
	char *nvalue, *ptr;
	int argtype;
	int have_version = 0;

	if (po_rightmost(options, version_keys) >= 0 ||
	    po_contains_prefix(options, "v4.", NULL, 0) == PO_FOUND)
		have_version = 1;

	list = conf_get_tag_list(section, arg);
	TAILQ_FOREACH(node, &list->fields, link) {
		/*
		 * Do not overwrite options if already exists
		 */
		field = mountopts_alias(node->field, &argtype);
		if (po_contains(options, field) == PO_FOUND)
			continue;
		/* Some options can be inverted by a "no" prefix.
		 * Check for these.
		 * "no" prefixes are unlikely in the config file as
		 * "option=false" is preferred, but still possible.
		 */
		if (strncmp(field, "no", 2) == 0 &&
		    po_contains(options, field+2) == PO_FOUND)
			continue;
		if (strlen(field) < BUFSIZ-3) {
			strcat(strcpy(buf, "no"), field);
			if (po_contains(options, buf) == PO_FOUND)
				continue;
		}

		/* If fg or bg already present, ignore bg or fg */
		if (strcmp(field, "fg") == 0 &&
		    po_contains(options, "bg") == PO_FOUND)
			continue;
		if (strcmp(field, "bg") == 0 &&
		    po_contains(options, "fg") == PO_FOUND)
			continue;

		if (is_version(field)) {
			if (have_version)
				continue;
			have_version = 1;
		}

		buf[0] = '\0';
		value = conf_get_section(section, arg, node->field);
		if (value == NULL)
			continue;
		if (strcasecmp(value, "false") == 0) {
			if (argtype != MNT_NOARG)
				snprintf(buf, BUFSIZ, "no%s", field);
			else if (strcasecmp(field, "bg") == 0)
				snprintf(buf, BUFSIZ, "fg");
			else if (strcasecmp(field, "fg") == 0)
				snprintf(buf, BUFSIZ, "bg");
			else if (strcasecmp(field, "sloppy") == 0)
				strict = 1;
		} else if (strcasecmp(value, "true") == 0) {
			if ((strcasecmp(field, "sloppy") == 0) && strict)
				continue;
			snprintf(buf, BUFSIZ, "%s", field);
		} else {
			nvalue = strdup(value);
			ptr = mountopts_convert(nvalue);
			snprintf(buf, BUFSIZ, "%s=%s", field, ptr);
			free(nvalue);
		}
		if (buf[0] == '\0')
			continue;

		po_append(options, buf);
		default_value(buf);
	}
	conf_free_list(list);
}

/*
 * Concatenate options from the configuration file with the
 * given options by building a link list of options from the
 * different sections in the conf file. Options that exists
 * in the either the given options or link list are not
 * overwritten so it matter which when each section is
 * parsed.
 */
char *conf_get_mntopts(char *spec, char *mount_point,
			      char *mount_opts)
{
	struct mount_options *options;
	char *ptr, *server;

	strict = 0;
	options = po_split(mount_opts);
	if (!options) {
		xlog_warn("conf_get_mountops: Unable calloc memory for options");
		return mount_opts;
	}
	/*
	 * First see if there are any mount options relative
	 * to the mount point.
	 */
	conf_parse_mntopts(NFSMOUNT_MOUNTPOINT, mount_point, options);

	/*
	 * Next, see if there are any mount options relative
	 * to the server
	 */
	server = strdup(spec);
	if (server == NULL) {
		xlog_warn("conf_get_mountops: Unable calloc memory for server");
		po_destroy(options);
		return mount_opts;
	}
	if ((ptr = strchr(server, ':')) != NULL)
		*ptr='\0';
	conf_parse_mntopts(NFSMOUNT_SERVER, server, options);
	free(server);

	/*
	 * Finally process all the global mount options.
	 */
	conf_parse_mntopts(NFSMOUNT_GLOBAL_OPTS, NULL, options);

	/*
	 * Strip out defaults, which have already been handled,
	 * then join the rest and return.
	 */
	while (po_contains_prefix(options, "default", &ptr, 0) == PO_FOUND) {
		ptr = strdup(ptr);
		po_remove_all(options, ptr);
		free(ptr);
	}

	po_join(options, &mount_opts);
	po_destroy(options);

	return mount_opts;
}