2
* Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
4
* This file may be redistributed under the terms of the
5
* GNU Lesser General Public License.
10
* @title: Options string
11
* @short_description: low-level API for work with mount options
13
* This is simple and low-level API to work with mount options that are stored
18
#ifdef HAVE_LIBSELINUX
19
#include <selinux/selinux.h>
20
#include <selinux/context.h>
28
struct libmnt_optloc {
36
#define mnt_init_optloc(_ol) (memset((_ol), 0, sizeof(struct libmnt_optloc)))
39
* Parses the first option from @optstr. The @optstr pointer is set to begin of
42
* Returns -EINVAL on parse error, 1 at the end of optstr and 0 on success.
44
static int mnt_optstr_parse_next(char **optstr, char **name, size_t *namesz,
45
char **value, size_t *valsz)
48
char *start = NULL, *stop = NULL, *p, *sep = NULL;
65
for (p = optstr0; p && *p; p++) {
67
start = p; /* begin of the option item */
69
open_quote ^= 1; /* reverse the status */
71
continue; /* still in quoted block */
72
if (!sep && *p == '=')
73
sep = p; /* name and value separator */
75
stop = p; /* terminate the option item */
76
else if (*(p + 1) == '\0')
77
stop = p + 1; /* end of optstr */
86
*namesz = sep ? sep - start : stop - start;
87
*optstr = *stop ? stop + 1 : stop;
93
*valsz = stop - sep - 1;
98
return 1; /* end of optstr */
101
DBG(OPTIONS, mnt_debug("parse error: \"%s\"", optstr0));
106
* Locates the first option that match with @name. The @end is set to
107
* char behind the option (it means ',' or \0).
109
* Returns negative number on parse error, 1 when not found and 0 on success.
111
static int mnt_optstr_locate_option(char *optstr, const char *name,
112
struct libmnt_optloc *ol)
124
namesz = strlen(name);
127
rc = mnt_optstr_parse_next(&optstr, &n, &nsz,
128
&ol->value, &ol->valsz);
132
if (namesz == nsz && strncmp(n, name, nsz) == 0) {
134
ol->end = *(optstr - 1) == ',' ? optstr - 1 : optstr;
144
* mnt_optstr_next_option:
145
* @optstr: option string, returns position to next option
146
* @name: returns option name
147
* @namesz: returns option name length
148
* @value: returns option value or NULL
149
* @valuesz: returns option value length or zero
151
* Parses the first option in @optstr.
153
* Returns: 0 on success, 1 at the end of @optstr or negative number in case of
156
int mnt_optstr_next_option(char **optstr, char **name, size_t *namesz,
157
char **value, size_t *valuesz)
159
if (!optstr || !*optstr)
161
return mnt_optstr_parse_next(optstr, name, namesz, value, valuesz);
164
static int __mnt_optstr_append_option(char **optstr,
165
const char *name, size_t nsz,
166
const char *value, size_t vsz)
173
osz = *optstr ? strlen(*optstr) : 0;
175
sz = osz + nsz + 1; /* 1: '\0' */
177
sz++; /* ',' options separator */
179
sz += vsz + 1; /* 1: '=' */
181
p = realloc(*optstr, sz);
191
memcpy(p, name, nsz);
196
memcpy(p, value, vsz);
205
* mnt_optstr_append_option:
206
* @optstr: option string or NULL, returns reallocated string
210
* Returns: 0 on success or -1 in case of error. After error the @optstr should
213
int mnt_optstr_append_option(char **optstr, const char *name, const char *value)
221
vsz = value ? strlen(value) : 0;
223
return __mnt_optstr_append_option(optstr, name, nsz, value, vsz);
227
* mnt_optstr_prepend_option:
228
* @optstr: option string or NULL, returns reallocated string
232
* Returns: 0 on success or -1 in case of error. After error the @optstr should
235
int mnt_optstr_prepend_option(char **optstr, const char *name, const char *value)
242
rc = mnt_optstr_append_option(optstr, name, value);
243
if (!rc && tmp && *tmp)
244
rc = mnt_optstr_append_option(optstr, tmp, NULL);
253
DBG(OPTIONS, mnt_debug("failed to prepend '%s[=%s]' to '%s'",
254
name, value, *optstr));
259
* mnt_optstr_get_option:
260
* @optstr: string with comma separated list of options
261
* @name: requested option name
262
* @value: returns pointer to the begin of the value (e.g. name=VALUE) or NULL
263
* @valsz: returns size of the value or 0
265
* Returns: 0 on success, 1 when not found the @name or negative number in case
268
int mnt_optstr_get_option(const char *optstr, const char *name,
269
char **value, size_t *valsz)
271
struct libmnt_optloc ol;
274
mnt_init_optloc(&ol);
276
rc = mnt_optstr_locate_option((char *) optstr, name, &ol);
287
* The result never starts or ends with comma or contains two commas
288
* (e.g. ",aaa,bbb" or "aaa,,bbb" or "aaa,")
290
int mnt_optstr_remove_option_at(char **optstr, char *begin, char *end)
294
if (!optstr || !begin || !end)
297
if ((begin == *optstr || *(begin - 1) == ',') && *end == ',')
302
memmove(begin, end, sz + 1);
303
if (!*begin && *(begin - 1) == ',')
309
/* insert 'substr' or '=substr' to @str on position @pos */
310
static int insert_value(char **str, char *pos, const char *substr, char **next)
312
size_t subsz = strlen(substr); /* substring size */
313
size_t strsz = strlen(*str);
314
size_t possz = strlen(pos);
319
/* is it necessary to prepend '=' before the substring ? */
320
sep = !(pos > *str && *(pos - 1) == '=');
322
/* save an offset of the place where we need add substr */
325
p = realloc(*str, strsz + sep + subsz + 1);
329
/* zeroize new allocated memory -- valgind loves is... */
330
memset(p + strsz, 0, sep + subsz + 1);
332
/* set pointers to the reallocated string */
337
/* create a room for new substring */
338
memmove(pos + subsz + sep, pos, possz + 1);
342
memcpy(pos, substr, subsz);
345
/* set pointer to the next option */
346
*next = pos + subsz + sep + 1;
354
* mnt_optstr_set_option:
355
* @optstr: string with comma separated list of options
356
* @name: requested option
357
* @value: new value or NULL
359
* Set or unset option @value.
361
* Returns: 0 on success, 1 when not found the @name or negative number in case
364
int mnt_optstr_set_option(char **optstr, const char *name, const char *value)
366
struct libmnt_optloc ol;
373
mnt_init_optloc(&ol);
376
rc = mnt_optstr_locate_option(*optstr, name, &ol);
378
return rc; /* parse error */
380
return mnt_optstr_append_option(optstr, name, value); /* not found */
382
nameend = ol.begin + ol.namesz;
384
if (value == NULL && ol.value && ol.valsz)
385
/* remove unwanted "=value" */
386
mnt_optstr_remove_option_at(optstr, nameend, ol.end);
388
else if (value && ol.value == NULL)
389
/* insert "=value" */
390
rc = insert_value(optstr, nameend, value, NULL);
392
else if (value && ol.value && strlen(value) == ol.valsz)
393
/* simply replace =value */
394
memcpy(ol.value, value, ol.valsz);
396
else if (value && ol.value) {
397
mnt_optstr_remove_option_at(optstr, nameend, ol.end);
398
rc = insert_value(optstr, nameend, value, NULL);
404
* mnt_optstr_remove_option:
405
* @optstr: string with comma separated list of options
406
* @name: requested option name
408
* Returns: 0 on success, 1 when not found the @name or negative number in case
411
int mnt_optstr_remove_option(char **optstr, const char *name)
413
struct libmnt_optloc ol;
416
mnt_init_optloc(&ol);
418
rc = mnt_optstr_locate_option(*optstr, name, &ol);
422
mnt_optstr_remove_option_at(optstr, ol.begin, ol.end);
428
* @optstr: string with comma separated list of options
429
* @user: returns newly allocated string with userspace options
430
* @vfs: returns newly allocated string with VFS options
431
* @fs: returns newly allocated string with FS options
432
* @ignore_user: option mask for options that should be ignored
433
* @ignore_vfs: option mask for options that should be ignored
437
* mnt_split_optstr(optstr, &u, NULL, NULL, MNT_NOMTAB, 0);
439
* returns all userspace options, the options that does not belong to
442
* Note that FS options are all options that are undefined in MNT_USERSPACE_MAP
445
* Returns: 0 on success, or negative number in case of error.
447
int mnt_split_optstr(const char *optstr, char **user, char **vfs,
448
char **fs, int ignore_user, int ignore_vfs)
450
char *name, *val, *str = (char *) optstr;
451
size_t namesz, valsz;
452
struct libmnt_optmap const *maps[2];
459
maps[0] = mnt_get_builtin_optmap(MNT_LINUX_MAP);
460
maps[1] = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
469
while(!mnt_optstr_next_option(&str, &name, &namesz, &val, &valsz)) {
471
const struct libmnt_optmap *ent = NULL;
472
const struct libmnt_optmap *m =
473
mnt_optmap_get_entry(maps, 2, name, namesz, &ent);
476
continue; /* ignore undefined options (comments) */
478
if (ent && m && m == maps[0] && vfs) {
479
if (ignore_vfs && (ent->mask & ignore_vfs))
481
rc = __mnt_optstr_append_option(vfs, name, namesz,
483
} else if (ent && m && m == maps[1] && user) {
484
if (ignore_user && (ent->mask & ignore_user))
486
rc = __mnt_optstr_append_option(user, name, namesz,
489
rc = __mnt_optstr_append_option(fs, name, namesz,
506
* mnt_optstr_get_options
507
* @optstr: string with comma separated list of options
508
* @subset: returns newly allocated string with options
510
* @ignore: mask of the options that should be ignored
512
* Extracts options from @optstr that belongs to the @map, for example:
514
* mnt_optstr_get_options(optstr, &p,
515
* mnt_get_builtin_optmap(MNT_LINUX_MAP),
518
* the 'p' returns all VFS options, the options that does not belong to mtab
521
* Returns: 0 on success, or negative number in case of error.
523
int mnt_optstr_get_options(const char *optstr, char **subset,
524
const struct libmnt_optmap *map, int ignore)
526
struct libmnt_optmap const *maps[1];
527
char *name, *val, *str = (char *) optstr;
528
size_t namesz, valsz;
530
if (!optstr || !subset)
536
while(!mnt_optstr_next_option(&str, &name, &namesz, &val, &valsz)) {
538
const struct libmnt_optmap *ent;
540
mnt_optmap_get_entry(maps, 1, name, namesz, &ent);
542
if (!ent || !ent->id)
543
continue; /* ignore undefined options (comments) */
545
if (ignore && (ent->mask & ignore))
547
rc = __mnt_optstr_append_option(subset, name, namesz, val, valsz);
559
* mnt_optstr_get_flags:
560
* @optstr: string with comma separated list of options
561
* @flags: returns mount flags
564
* Returns in @flags IDs of options from @optstr as defined in the @map.
568
* "bind,exec,foo,bar" --returns-> MS_BIND
570
* "bind,noexec,foo,bar" --returns-> MS_BIND|MS_NOEXEC
572
* Note that @flags are not zeroized by this function! This function set/unset
573
* bites in the @flags only.
575
* Returns: 0 on success or negative number in case of error
577
int mnt_optstr_get_flags(const char *optstr, unsigned long *flags,
578
const struct libmnt_optmap *map)
580
struct libmnt_optmap const *maps[2];
581
char *name, *str = (char *) optstr;
587
if (!optstr || !flags || !map)
592
if (map == mnt_get_builtin_optmap(MNT_LINUX_MAP))
594
* Add userspace map -- the "user" is interpreted as
595
* MS_NO{EXEC,SUID,DEV}.
597
maps[nmaps++] = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
599
while(!mnt_optstr_next_option(&str, &name, &namesz, NULL, NULL)) {
600
const struct libmnt_optmap *ent;
601
const struct libmnt_optmap *m;
603
m = mnt_optmap_get_entry(maps, nmaps, name, namesz, &ent);
604
if (!m || !ent || !ent->id)
607
if (m == map) { /* requested map */
608
if (ent->mask & MNT_INVERT)
613
} else if (nmaps == 2 && m == maps[1]) {
615
* Special case -- translate "user" to MS_ options
617
if (ent->mask & MNT_INVERT)
619
if (ent->id & (MNT_MS_OWNER | MNT_MS_GROUP))
620
*flags |= MS_OWNERSECURE;
621
else if (ent->id & (MNT_MS_USER | MNT_MS_USERS))
630
* mnt_optstr_apply_flags:
631
* @optstr: string with comma separated list of options
632
* @flags: returns mount flags
635
* Removes/adds options to the @optstr according to flags. For example:
637
* MS_NOATIME and "foo,bar,noexec" --returns-> "foo,bar,noatime"
639
* Returns: 0 on success or negative number in case of error.
641
int mnt_optstr_apply_flags(char **optstr, unsigned long flags,
642
const struct libmnt_optmap *map)
644
struct libmnt_optmap const *maps[1];
645
char *name, *next, *val;
646
size_t namesz = 0, valsz = 0;
655
DBG(CXT, mnt_debug("appling 0x%08lu flags '%s'", flags, *optstr));
662
* There is a convetion that 'rw/ro' flags is always at the begin of
663
* the string (athough the 'rw' is unnecessary).
665
if (map == mnt_get_builtin_optmap(MNT_LINUX_MAP)) {
666
const char *o = (fl & MS_RDONLY) ? "ro" : "rw";
669
(!strncmp(next, "rw", 2) || !strncmp(next, "ro", 2)) &&
670
(*(next + 2) == '\0' || *(next + 2) == ',')) {
672
/* already set, be paranoid and fix it */
675
rc = mnt_optstr_prepend_option(optstr, o, NULL);
678
next = *optstr; /* because realloc() */
688
* scan @optstr and remove options that are missing in
691
while(!mnt_optstr_next_option(&next, &name, &namesz,
693
const struct libmnt_optmap *ent;
695
if (mnt_optmap_get_entry(maps, 1, name, namesz, &ent)) {
697
* remove unwanted option (rw/ro is already set)
701
if (ent->id == MS_RDONLY ||
702
(ent->mask & MNT_INVERT) ||
705
char *end = val ? val + valsz :
708
rc = mnt_optstr_remove_option_at(
713
if (!(ent->mask & MNT_INVERT))
719
/* add missing options */
721
const struct libmnt_optmap *ent;
724
for (ent = map; ent && ent->name; ent++) {
725
if ((ent->mask & MNT_INVERT) || !(fl & ent->id))
728
/* don't add options which require values (e.g. offset=%d) */
729
p = strchr(ent->name, '=');
734
continue; /* name= */
736
p = strndup(ent->name, p - ent->name);
741
mnt_optstr_append_option(optstr, p, NULL);
744
mnt_optstr_append_option(optstr, ent->name, NULL);
750
DBG(CXT, mnt_debug("failed to apply flags [rc=%d]", rc));
755
* @optstr: string with comma separated list of options
756
* @value: pointer to the begin of the context value
757
* @valsz: size of the value
758
* @next: returns pointer to the next option (optional argument)
760
* Translates SELinux context from human to raw format. The function does not
761
* modify @optstr and returns zero if libmount is compiled without SELinux
764
* Returns: 0 on success, negative number in case of error.
766
int mnt_optstr_fix_secontext(char **optstr, char *value, size_t valsz, char **next)
770
#ifdef HAVE_LIBSELINUX
771
security_context_t raw = NULL;
772
char *p, *val, *begin, *end;
775
if (!optstr || !*optstr || !value || !valsz)
778
DBG(CXT, mnt_debug("fixing SELinux context"));
783
/* the selinux contexts are quoted */
785
if (valsz <= 2 || *(value + valsz - 1) != '"')
786
return -EINVAL; /* improperly quoted option string */
791
p = strndup(value, valsz);
796
/* translate the context */
797
rc = selinux_trans_to_raw_context((security_context_t) p, &raw);
799
DBG(CXT, mnt_debug("SELinux context '%s' translated to '%s'",
800
p, rc == -1 ? "FAILED" : (char *) raw));
803
if (rc == -1 || !raw)
807
/* create quoted string from the raw context */
808
sz = strlen((char *) raw);
812
p = val = malloc(valsz + 3);
824
/* set new context */
825
mnt_optstr_remove_option_at(optstr, begin, end);
826
rc = insert_value(optstr, begin, val, next);
832
static int set_uint_value(char **optstr, unsigned int num,
833
char *begin, char *end, char **next)
836
snprintf(buf, sizeof(buf), "%u", num);
838
mnt_optstr_remove_option_at(optstr, begin, end);
839
return insert_value(optstr, begin, buf, next);
843
* @optstr: string with comma separated list of options
844
* @value: pointer to the begin of the uid value
845
* @valsz: size of the value
846
* @next: returns pointer to the next option (optional argument)
848
* Translates "<username>" or "useruid" to the real UID.
851
* if (!mnt_optstr_get_option(optstr, "uid", &val, &valsz))
852
* mnt_optstr_fix_uid(&optstr, val, valsz, NULL);
854
* Returns: 0 on success, negative number in case of error.
856
int mnt_optstr_fix_uid(char **optstr, char *value, size_t valsz, char **next)
861
if (!optstr || !*optstr || !value || !valsz)
864
DBG(CXT, mnt_debug("fixing uid"));
868
if (valsz == 7 && !strncmp(value, "useruid", 7) &&
869
(*(value + 7) == ',' || !*(value + 7)))
870
rc = set_uint_value(optstr, getuid(), value, end, next);
872
else if (!isdigit(*value)) {
874
char *p = strndup(value, valsz);
877
rc = mnt_get_uid(p, &id);
881
rc = set_uint_value(optstr, id, value, end, next);
885
*next = value + valsz;
894
* @optstr: string with comma separated list of options
895
* @value: pointer to the begin of the uid value
896
* @valsz: size of the value
897
* @next: returns pointer to the next option (optional argument)
899
* Translates "<groupname>" or "usergid" to the real GID.
901
* Returns: 0 on success, negative number in case of error.
903
int mnt_optstr_fix_gid(char **optstr, char *value, size_t valsz, char **next)
908
if (!optstr || !*optstr || !value || !valsz)
911
DBG(CXT, mnt_debug("fixing gid"));
915
if (valsz == 7 && !strncmp(value, "usergid", 7) &&
916
(*(value + 7) == ',' || !*(value + 7)))
917
rc = set_uint_value(optstr, getgid(), value, end, next);
919
else if (!isdigit(*value)) {
921
char *p = strndup(value, valsz);
924
rc = mnt_get_gid(p, &id);
928
rc = set_uint_value(optstr, id, value, end, next);
932
*next = value + valsz;
940
* Converts "user" to "user=<username>".
942
* Returns: 0 on success, negative number in case of error.
944
int mnt_optstr_fix_user(char **optstr)
947
struct libmnt_optloc ol;
950
DBG(CXT, mnt_debug("fixing user"));
952
mnt_init_optloc(&ol);
954
rc = mnt_optstr_locate_option(*optstr, "user", &ol);
956
return rc == 1 ? 0 : rc; /* 1: user= not found */
958
username = mnt_get_username(getuid());
962
if (!ol.valsz || (ol.value && strncmp(ol.value, username, ol.valsz))) {
964
/* remove old value */
965
mnt_optstr_remove_option_at(optstr, ol.value, ol.end);
967
rc = insert_value(optstr, ol.value ? ol.value : ol.end,
977
int test_append(struct libmnt_test *ts, int argc, char *argv[])
979
const char *value = NULL, *name;
985
optstr = strdup(argv[1]);
991
rc = mnt_optstr_append_option(&optstr, name, value);
993
printf("result: >%s<\n", optstr);
997
int test_prepend(struct libmnt_test *ts, int argc, char *argv[])
999
const char *value = NULL, *name;
1005
optstr = strdup(argv[1]);
1011
rc = mnt_optstr_prepend_option(&optstr, name, value);
1013
printf("result: >%s<\n", optstr);
1017
int test_split(struct libmnt_test *ts, int argc, char *argv[])
1019
char *optstr, *user = NULL, *fs = NULL, *vfs = NULL;
1025
optstr = strdup(argv[1]);
1027
rc = mnt_split_optstr(optstr, &user, &vfs, &fs, 0, 0);
1029
printf("user : %s\n", user);
1030
printf("vfs : %s\n", vfs);
1031
printf("fs : %s\n", fs);
1041
int test_flags(struct libmnt_test *ts, int argc, char *argv[])
1045
unsigned long fl = 0;
1050
optstr = strdup(argv[1]);
1052
rc = mnt_optstr_get_flags(optstr, &fl, mnt_get_builtin_optmap(MNT_LINUX_MAP));
1055
printf("mountflags: 0x%08lx\n", fl);
1058
rc = mnt_optstr_get_flags(optstr, &fl, mnt_get_builtin_optmap(MNT_USERSPACE_MAP));
1061
printf("userspace-mountflags: 0x%08lx\n", fl);
1067
int test_apply(struct libmnt_test *ts, int argc, char *argv[])
1071
unsigned long flags;
1076
if (!strcmp(argv[1], "--user"))
1077
map = MNT_USERSPACE_MAP;
1078
else if (!strcmp(argv[1], "--linux"))
1079
map = MNT_LINUX_MAP;
1081
fprintf(stderr, "unknown option '%s'\n", argv[1]);
1085
optstr = strdup(argv[2]);
1086
flags = strtoul(argv[3], NULL, 16);
1088
printf("flags: 0x%08lx\n", flags);
1090
rc = mnt_optstr_apply_flags(&optstr, flags, mnt_get_builtin_optmap(map));
1091
printf("optstr: %s\n", optstr);
1097
int test_set(struct libmnt_test *ts, int argc, char *argv[])
1099
const char *value = NULL, *name;
1105
optstr = strdup(argv[1]);
1111
rc = mnt_optstr_set_option(&optstr, name, value);
1113
printf("result: >%s<\n", optstr);
1118
int test_get(struct libmnt_test *ts, int argc, char *argv[])
1131
rc = mnt_optstr_get_option(optstr, name, &val, &sz);
1133
printf("found; name: %s", name);
1135
printf(", argument: size=%zd data=", sz);
1136
if (fwrite(val, 1, sz, stdout) != sz)
1141
printf("%s: not found\n", name);
1143
printf("parse error: %s\n", optstr);
1147
int test_remove(struct libmnt_test *ts, int argc, char *argv[])
1155
optstr = strdup(argv[1]);
1158
rc = mnt_optstr_remove_option(&optstr, name);
1160
printf("result: >%s<\n", optstr);
1164
int test_fix(struct libmnt_test *ts, int argc, char *argv[])
1168
char *name, *val, *next;
1169
size_t valsz, namesz;
1174
next = optstr = strdup(argv[1]);
1176
printf("optstr: %s\n", optstr);
1178
while (!mnt_optstr_next_option(&next, &name, &namesz, &val, &valsz)) {
1180
if (!strncmp(name, "uid", 3))
1181
rc = mnt_optstr_fix_uid(&optstr, val, valsz, &next);
1182
else if (!strncmp(name, "gid", 3))
1183
rc = mnt_optstr_fix_gid(&optstr, val, valsz, &next);
1184
else if (!strncmp(name, "context", 7))
1185
rc = mnt_optstr_fix_secontext(&optstr, val, valsz, &next);
1190
rc = mnt_optstr_fix_user(&optstr);
1192
printf("fixed: %s\n", optstr);
1199
int main(int argc, char *argv[])
1201
struct libmnt_test tss[] = {
1202
{ "--append", test_append, "<optstr> <name> [<value>] append value to optstr" },
1203
{ "--prepend",test_prepend,"<optstr> <name> [<value>] prepend value to optstr" },
1204
{ "--set", test_set, "<optstr> <name> [<value>] (un)set value" },
1205
{ "--get", test_get, "<optstr> <name> search name in optstr" },
1206
{ "--remove", test_remove, "<optstr> <name> remove name in optstr" },
1207
{ "--split", test_split, "<optstr> split into FS, VFS and userspace" },
1208
{ "--flags", test_flags, "<optstr> convert options to MS_* flags" },
1209
{ "--apply", test_apply, "--{linux,user} <optstr> <mask> apply mask to optstr" },
1210
{ "--fix", test_fix, "<optstr> fix uid=, gid=, user, and context=" },
1214
return mnt_run_test(tss, argc, argv);
1216
#endif /* TEST_PROGRAM */