summaryrefslogtreecommitdiffstats
path: root/shlibs/mount/src/optmap.c
blob: d5f641096a6619ce79df95069bcacec96dd56a3b (plain) (blame)
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
/*
 * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 */

/**
 * SECTION: optmap
 * @title: Option maps
 * @short_description: description for mount options
 *
 * The mount(2) linux syscall uses two arguments for mount options:
 *
 *	@mountflags: (see MS_* macros in linux/fs.h)
 *
 *	@mountdata: (usully a comma separated string of options)
 *
 * The libmount uses options-map(s) to describe mount options. The number of
 * maps is unlimited. The libmount options parser could be easily extended
 * (e.g. by mnt_optls_add_map()) to work with new options.
 *
 * The option description (map entry) includes:
 *
 *	@name: and argument type (e.g. "loop[=%s]")
 *
 *	@id: (in the map unique identifier or a mountflags, e.g MS_RDONLY)
 *
 *	@mask: (MNT_INVERT, MNT_NOMTAB)
 *
 * The option argument type is defined by:
 *
 *	"=type"   -- required argument
 *
 *	"[=type]" -- optional argument
 *
 * where the 'type' is sscanf() format string or
 *
 *     {item0,item1,...}  -- enum (mnt_option_get_number() converts the value
 *                           to 0..N number)
 *
 * The options argument format is used for parsing only. The library internally
 * stores the option argument as a string. The conversion to the data type is
 * on-demant by mnt_option_get_value_*() functions.
 *
 * The library checks options argument according to 'type' format for simple
 * formats only:
 *
 *	%s, %d, %ld, %lld, %u, %lu, %llu, %x, %o and {enum}
 *
 * Example:
 *
 * <informalexample>
 *   <programlisting>
 *     #define MY_MS_FOO   (1 << 1)
 *     #define MY_MS_BAR   (1 << 2)
 *
 *     mnt_optmap myoptions[] = {
 *       { "foo",   MY_MS_FOO },
 *       { "nofoo", MY_MS_FOO | MNT_INVERT },
 *       { "bar=%s",MY_MS_BAR },
 *       { NULL }
 *     };
 *   </programlisting>
 * </informalexample>
 *
 * The libmount defines two basic built-in options maps:
 *
 *	@MNT_LINUX_MAP: fs-independent kernel mount options (usually MS_* flags)
 *
 *	@MNT_USERSPACE_MAP: userspace specific mount options (e.g. "user", "loop")
 *
 * For more details about option map struct see "struct mnt_optmap" in
 * mount/mount.h.
 */
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>

#include "nls.h"
#include "mountP.h"

/*
 * fs-independent mount flags (built-in MNT_LINUX_MAP)
 */
static const struct mnt_optmap linux_flags_map[] =
{
   { "ro",       MS_RDONLY },                 /* read-only */
   { "rw",       MS_RDONLY, MNT_INVERT },     /* read-write */
   { "exec",     MS_NOEXEC, MNT_INVERT },     /* permit execution of binaries */
   { "noexec",   MS_NOEXEC },                 /* don't execute binaries */
   { "suid",     MS_NOSUID, MNT_INVERT },     /* honor suid executables */
   { "nosuid",   MS_NOSUID },                 /* don't honor suid executables */
   { "dev",      MS_NODEV, MNT_INVERT },      /* interpret device files  */
   { "nodev",    MS_NODEV },                  /* don't interpret devices */

   { "sync",     MS_SYNCHRONOUS },            /* synchronous I/O */
   { "async",    MS_SYNCHRONOUS, MNT_INVERT },/* asynchronous I/O */

   { "dirsync",  MS_DIRSYNC },                /* synchronous directory modifications */
   { "remount",  MS_REMOUNT },                /* Alter flags of mounted FS */
   { "bind",     MS_BIND },                   /* Remount part of tree elsewhere */
   { "rbind",    MS_BIND | MS_REC },          /* Idem, plus mounted subtrees */
#ifdef MS_NOSUB
   { "sub",      MS_NOSUB, MNT_INVERT },      /* allow submounts */
   { "nosub",    MS_NOSUB },                  /* don't allow submounts */
#endif
#ifdef MS_SILENT
   { "quiet",	 MS_SILENT },                 /* be quiet  */
   { "loud",     MS_SILENT, MNT_INVERT },     /* print out messages. */
#endif
#ifdef MS_MANDLOCK
   { "mand",     MS_MANDLOCK },               /* Allow mandatory locks on this FS */
   { "nomand",   MS_MANDLOCK, MNT_INVERT },   /* Forbid mandatory locks on this FS */
#endif
#ifdef MS_NOATIME
   { "atime",    MS_NOATIME, MNT_INVERT },    /* Update access time */
   { "noatime",	 MS_NOATIME },                /* Do not update access time */
#endif
#ifdef MS_I_VERSION
   { "iversion", MS_I_VERSION },              /* Update inode I_version time */
   { "noiversion", MS_I_VERSION,  MNT_INVERT},/* Don't update inode I_version time */
#endif
#ifdef MS_NODIRATIME
   { "diratime", MS_NODIRATIME, MNT_INVERT }, /* Update dir access times */
   { "nodiratime", MS_NODIRATIME },           /* Do not update dir access times */
#endif
#ifdef MS_RELATIME
   { "relatime", MS_RELATIME },               /* Update access times relative to mtime/ctime */
   { "norelatime", MS_RELATIME, MNT_INVERT }, /* Update access time without regard to mtime/ctime */
#endif
#ifdef MS_STRICTATIME
   { "strictatime", MS_STRICTATIME },         /* Strict atime semantics */
   { "nostrictatime", MS_STRICTATIME, MNT_INVERT }, /* kernel default atime */
#endif
   { NULL, 0, 0 }
};

/*
 * userspace mount option (built-in MNT_USERSPACE_MAP)
 *
 * TODO: offset=, sizelimit=, encryption=, vfs=
 */
static const struct mnt_optmap userspace_opts_map[] =
{
   { "defaults", 0, 0 },               /* default options */

   { "auto",    MNT_MS_NOAUTO, MNT_INVERT | MNT_NOMTAB },  /* Can be mounted using -a */
   { "noauto",  MNT_MS_NOAUTO, MNT_NOMTAB },               /* Can  only be mounted explicitly */

   { "user[=%s]", MNT_MS_USER },                           /* Allow ordinary user to mount (mtab) */
   { "nouser",  MNT_MS_USER, MNT_INVERT | MNT_NOMTAB },    /* Forbid ordinary user to mount */

   { "users",   MNT_MS_USERS, MNT_NOMTAB },                /* Allow ordinary users to mount */
   { "nousers", MNT_MS_USERS, MNT_INVERT | MNT_NOMTAB },   /* Forbid ordinary users to mount */

   { "owner",   MNT_MS_OWNER, MNT_NOMTAB },                /* Let the owner of the device mount */
   { "noowner", MNT_MS_OWNER, MNT_INVERT | MNT_NOMTAB },   /* Device owner has no special privs */

   { "group",   MNT_MS_GROUP, MNT_NOMTAB },                /* Let the group of the device mount */
   { "nogroup", MNT_MS_GROUP, MNT_INVERT | MNT_NOMTAB },   /* Device group has no special privs */

   { "_netdev", MNT_MS_NETDEV },                           /* Device requires network */

   { "comment=%s", MNT_MS_COMMENT, MNT_NOMTAB },           /* fstab comment only */

   { "loop[=%s]", MNT_MS_LOOP },                           /* use the loop device */

   { "nofail",  MNT_MS_NOFAIL, MNT_NOMTAB },               /* Do not fail if ENOENT on dev */

   { "uhelper=%s", MNT_MS_UHELPER },			   /* /sbin/umount.<helper> */

   { NULL, 0, 0 }
};

/**
 * mnt_get_builtin_map:
 * @id: map id -- MNT_LINUX_MAP or MNT_USERSPACE_MAP
 *
 * MNT_LINUX_MAP - Linux kernel fs-independent mount options
 *                 (usually MS_* flags, see linux/fs.h)
 *
 * MNT_USERSPACE_MAP - userpace mount(8) specific mount options
 *                     (e.g user=, _netdev, ...)
 *
 * Returns: static built-in libmount map.
 */
const struct mnt_optmap *mnt_get_builtin_optmap(int id)
{
	assert(id);

	if (id == MNT_LINUX_MAP)
		return linux_flags_map;
	else if (id == MNT_USERSPACE_MAP)
		return userspace_opts_map;
	return NULL;
}

/*
 * Lookups for the @name in @maps and returns a map and in @mapent
 * returns the map entry
 */
const struct mnt_optmap *mnt_optmap_get_entry(
				struct mnt_optmap const **maps,
				int nmaps,
				const char *name,
				size_t namelen,
				const struct mnt_optmap **mapent)
{
	int i;

	assert(maps);
	assert(nmaps);
	assert(name);
	assert(namelen);

	if (mapent)
		*mapent = NULL;

	for (i = 0; i < nmaps; i++) {
		const struct mnt_optmap *map = maps[i];
		const struct mnt_optmap *ent;
		const char *p;

		for (ent = map; ent && ent->name; ent++) {
			if (strncmp(ent->name, name, namelen))
				continue;
			p = ent->name + namelen;
			if (*p == '\0' || *p == '=' || *p == '[') {
				if (mapent)
					*mapent = ent;
				return map;
			}
		}
	}
	return NULL;
}


/*
 * Converts @rawdata to number according to enum definition in the @mapent.
 */
int mnt_optmap_enum_to_number(const struct mnt_optmap *mapent,
			const char *rawdata, size_t len)
{
	const char *p, *end = NULL, *begin = NULL;
	int n = -1;

	if (!rawdata || !*rawdata || !mapent || !len)
		return -EINVAL;

	p = strrchr(mapent->name, '=');
	if (!p || *(p + 1) == '{')
		return -EINVAL;	/* value unexpected or not "enum" */
	p += 2;
	if (!*p || *(p + 1) == '}')
		return -EINVAL;	/* hmm... option <type> is "={" or "={}" */

	/* we cannot use strstr(), @rawdata is not terminated */
	for (; p && *p; p++) {
		if (!begin)
			begin = p;		/* begin of the item */
		if (*p == ',')
			end = p;		/* terminate the item */
		if (*(p + 1) == '}')
			end = p + 1;		/* end of enum definition */
		if (!begin || !end)
			continue;
		if (end <= begin)
			return -EINVAL;
		n++;
		if (len == end - begin && strncasecmp(begin, rawdata, len) == 0)
			return n;
		p = end;
	}

	return -1;
}

/*
 * Returns data type defined in the @mapent.
 */
const char *mnt_optmap_get_type(const struct mnt_optmap *mapent)
{
	char *type;

	assert(mapent);
	assert(mapent->name);

	type = strrchr(mapent->name, '=');
	if (!type)
		return NULL;			/* value is unexpected */
	if (type == mapent->name)
		return NULL;			/* wrong format of type definition */
	type++;
	if (*type != '%' && *type != '{')
		return NULL;			/* wrong format of type definition */
	return type ? : NULL;
}

/*
 * Does the option (that is described by @mntent) require any value? (e.g.
 * uid=<foo>)
 */
int mnt_optmap_require_value(const struct mnt_optmap *mapent)
{
	char *type;

	assert(mapent);
	assert(mapent->name);

	type = strchr(mapent->name, '=');
	if (!type)
		return 0;			/* value is unexpected */
	if (type == mapent->name)
		return 0;			/* wrong format of type definition */
	if (*(type - 1) == '[')
		return 0;			/* optional */
	return 1;
}