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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libmount from util-linux project.
*
* Copyright (C) 2009-2018 Karel Zak <kzak@redhat.com>
*
* libmount is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*/
/**
* SECTION: optstr
* @title: Options string
* @short_description: low-level API for working with mount options
*
* This is a simple and low-level API to working with mount options that are stored
* in a string.
*/
#include <ctype.h>
#ifdef HAVE_LIBSELINUX
#include <selinux/selinux.h>
#include <selinux/context.h>
#endif
#include "strutils.h"
#include "mountP.h"
#include "buffer.h"
/*
* Option location
*/
struct libmnt_optloc {
char *begin;
char *end;
char *value;
size_t valsz;
size_t namesz;
};
#define MNT_INIT_OPTLOC { .begin = NULL }
#define mnt_optmap_entry_novalue(e) \
(e && (e)->name && !strchr((e)->name, '=') && !((e)->mask & MNT_PREFIX))
/*
* Parses the first option from @optstr. The @optstr pointer is set to the beginning
* of the next option.
*
* Returns -EINVAL on parse error, 1 at the end of optstr and 0 on success.
*/
static int mnt_optstr_parse_next(char **optstr, char **name, size_t *namesz,
char **value, size_t *valsz)
{
int open_quote = 0;
char *start = NULL, *stop = NULL, *p, *sep = NULL;
char *optstr0;
assert(optstr);
assert(*optstr);
optstr0 = *optstr;
if (name)
*name = NULL;
if (namesz)
*namesz = 0;
if (value)
*value = NULL;
if (valsz)
*valsz = 0;
/* trim leading commas as to not invalidate option
* strings with multiple consecutive commas */
while (optstr0 && *optstr0 == ',')
optstr0++;
for (p = optstr0; p && *p; p++) {
if (!start)
start = p; /* beginning of the option item */
if (*p == '"')
open_quote ^= 1; /* reverse the status */
if (open_quote)
continue; /* still in quoted block */
if (!sep && p > start && *p == '=')
sep = p; /* name and value separator */
if (*p == ',')
stop = p; /* terminate the option item */
else if (*(p + 1) == '\0')
stop = p + 1; /* end of optstr */
if (!start || !stop)
continue;
if (stop <= start)
goto error;
if (name)
*name = start;
if (namesz)
*namesz = sep ? sep - start : stop - start;
*optstr = *stop ? stop + 1 : stop;
if (sep) {
if (value)
*value = sep + 1;
if (valsz)
*valsz = stop - sep - 1;
}
return 0;
}
return 1; /* end of optstr */
error:
DBG(OPTIONS, ul_debug("parse error: \"%s\"", optstr0));
return -EINVAL;
}
/*
* Locates the first option that matches @name. The @end is set to the
* char behind the option (it means ',' or \0).
*
* Returns negative number on parse error, 1 when not found and 0 on success.
*/
static int mnt_optstr_locate_option(char *optstr, const char *name,
struct libmnt_optloc *ol)
{
char *n;
size_t namesz, nsz;
int rc;
if (!optstr)
return 1;
assert(name);
namesz = strlen(name);
do {
rc = mnt_optstr_parse_next(&optstr, &n, &nsz,
&ol->value, &ol->valsz);
if (rc)
break;
if (namesz == nsz && strncmp(n, name, nsz) == 0) {
ol->begin = n;
ol->end = *(optstr - 1) == ',' ? optstr - 1 : optstr;
ol->namesz = nsz;
return 0;
}
} while(1);
return rc;
}
/**
* mnt_optstr_next_option:
* @optstr: option string, returns the position of the next option
* @name: returns the option name
* @namesz: returns the option name length
* @value: returns the option value or NULL
* @valuesz: returns the option value length or zero
*
* Parses the first option in @optstr.
*
* Returns: 0 on success, 1 at the end of @optstr or negative number in case of
* error.
*/
int mnt_optstr_next_option(char **optstr, char **name, size_t *namesz,
char **value, size_t *valuesz)
{
if (!optstr || !*optstr)
return -EINVAL;
return mnt_optstr_parse_next(optstr, name, namesz, value, valuesz);
}
static int __buffer_append_option(struct ul_buffer *buf,
const char *name, size_t namesz,
const char *val, size_t valsz)
{
int rc = 0;
if (!ul_buffer_is_empty(buf))
rc = ul_buffer_append_data(buf, ",", 1);
if (!rc)
rc = ul_buffer_append_data(buf, name, namesz);
if (val && !rc) {
/* we need to append '=' is value is empty string, see
* 727c689908c5e68c92aa1dd65e0d3bdb6d91c1e5 */
rc = ul_buffer_append_data(buf, "=", 1);
if (!rc && valsz)
rc = ul_buffer_append_data(buf, val, valsz);
}
return rc;
}
/**
* mnt_optstr_append_option:
* @optstr: option string or NULL, returns a reallocated string
* @name: value name
* @value: value
*
* Returns: 0 on success or <0 in case of error. After an error the @optstr should
* be unmodified.
*/
int mnt_optstr_append_option(char **optstr, const char *name, const char *value)
{
struct ul_buffer buf = UL_INIT_BUFFER;
int rc;
size_t nsz, vsz, osz;
if (!optstr)
return -EINVAL;
if (!name || !*name)
return 0;
nsz = strlen(name);
osz = *optstr ? strlen(*optstr) : 0;
vsz = value ? strlen(value) : 0;
ul_buffer_refer_string(&buf, *optstr);
ul_buffer_set_chunksize(&buf, osz + nsz + vsz + 3); /* to call realloc() only once */
rc = __buffer_append_option(&buf, name, nsz, value, vsz);
*optstr = ul_buffer_get_data(&buf, NULL, NULL);
return rc;
}
/**
* mnt_optstr_prepend_option:
* @optstr: option string or NULL, returns a reallocated string
* @name: value name
* @value: value
*
* Returns: 0 on success or <0 in case of error. After an error the @optstr should
* be unmodified.
*/
int mnt_optstr_prepend_option(char **optstr, const char *name, const char *value)
{
struct ul_buffer buf = UL_INIT_BUFFER;
size_t nsz, vsz, osz;
int rc;
if (!optstr)
return -EINVAL;
if (!name || !*name)
return 0;
nsz = strlen(name);
osz = *optstr ? strlen(*optstr) : 0;
vsz = value ? strlen(value) : 0;
ul_buffer_set_chunksize(&buf, osz + nsz + vsz + 3); /* to call realloc() only once */
rc = __buffer_append_option(&buf, name, nsz, value, vsz);
if (*optstr && !rc) {
rc = ul_buffer_append_data(&buf, ",", 1);
if (!rc)
rc = ul_buffer_append_data(&buf, *optstr, osz);
free(*optstr);
}
*optstr = ul_buffer_get_data(&buf, NULL, NULL);
return rc;
}
/**
* mnt_optstr_get_option:
* @optstr: string with a comma separated list of options
* @name: requested option name
* @value: returns a pointer to the beginning of the value (e.g. name=VALUE) or NULL
* @valsz: returns size of the value or 0
*
* Returns: 0 on success, 1 when not found the @name or negative number in case
* of error.
*/
int mnt_optstr_get_option(const char *optstr, const char *name,
char **value, size_t *valsz)
{
struct libmnt_optloc ol = MNT_INIT_OPTLOC;
int rc;
if (!optstr || !name)
return -EINVAL;
rc = mnt_optstr_locate_option((char *) optstr, name, &ol);
if (!rc) {
if (value)
*value = ol.value;
if (valsz)
*valsz = ol.valsz;
}
return rc;
}
/**
* mnt_optstr_deduplicate_option:
* @optstr: string with a comma separated list of options
* @name: requested option name
*
* Removes all instances of @name except the last one.
*
* Returns: 0 on success, 1 when not found the @name or negative number in case
* of error.
*/
int mnt_optstr_deduplicate_option(char **optstr, const char *name)
{
int rc;
char *begin = NULL, *end = NULL, *opt;
if (!optstr || !name)
return -EINVAL;
opt = *optstr;
do {
struct libmnt_optloc ol = MNT_INIT_OPTLOC;
rc = mnt_optstr_locate_option(opt, name, &ol);
if (!rc) {
if (begin) {
/* remove the previous instance */
size_t shift = strlen(*optstr);
mnt_optstr_remove_option_at(optstr, begin, end);
/* now all the offsets are not valid anymore - recount */
shift -= strlen(*optstr);
ol.begin -= shift;
ol.end -= shift;
}
begin = ol.begin;
end = ol.end;
opt = end && *end ? end + 1 : NULL;
}
if (opt == NULL)
break;
} while (rc == 0 && *opt);
return rc < 0 ? rc : begin ? 0 : 1;
}
/*
* The result never starts or ends with a comma or contains two commas
* (e.g. ",aaa,bbb" or "aaa,,bbb" or "aaa,")
*/
int mnt_optstr_remove_option_at(char **optstr, char *begin, char *end)
{
size_t sz;
if (!optstr || !begin || !end)
return -EINVAL;
if ((begin == *optstr || *(begin - 1) == ',') && *end == ',')
end++;
sz = strlen(end);
memmove(begin, end, sz + 1);
if (!*begin && (begin > *optstr) && *(begin - 1) == ',')
*(begin - 1) = '\0';
return 0;
}
/* insert 'substr' or '=substr' to @str on position @pos */
static int __attribute__((nonnull(1,2,3)))
insert_value(char **str, char *pos, const char *substr, char **next)
{
size_t subsz = strlen(substr); /* substring size */
size_t strsz = strlen(*str);
size_t possz = strlen(pos);
size_t posoff;
char *p;
int sep;
/* is it necessary to prepend '=' before the substring ? */
sep = !(pos > *str && *(pos - 1) == '=');
/* save an offset of the place where we need to add substr */
posoff = pos - *str;
p = realloc(*str, strsz + sep + subsz + 1);
if (!p)
return -ENOMEM;
/* zeroize the newly allocated memory -- valgrind loves us... */
memset(p + strsz, 0, sep + subsz + 1);
/* set pointers to the reallocated string */
*str = p;
pos = p + posoff;
if (possz)
/* create a room for the new substring */
memmove(pos + subsz + sep, pos, possz + 1);
if (sep)
*pos++ = '=';
memcpy(pos, substr, subsz);
if (next) {
/* set pointer to the next option */
*next = pos + subsz;
if (**next == ',')
(*next)++;
}
return 0;
}
/**
* mnt_optstr_set_option:
* @optstr: string with a comma separated list of options
* @name: requested option
* @value: new value or NULL
*
* Set or unset the option @value.
*
* Returns: 0 on success, 1 when not found the @name or negative number in case
* of error.
*/
int mnt_optstr_set_option(char **optstr, const char *name, const char *value)
{
struct libmnt_optloc ol = MNT_INIT_OPTLOC;
char *nameend;
int rc = 1;
if (!optstr || !name)
return -EINVAL;
if (*optstr)
rc = mnt_optstr_locate_option(*optstr, name, &ol);
if (rc < 0)
return rc; /* parse error */
if (rc == 1)
return mnt_optstr_append_option(optstr, name, value); /* not found */
nameend = ol.begin + ol.namesz;
if (value == NULL && ol.value && ol.valsz)
/* remove unwanted "=value" */
mnt_optstr_remove_option_at(optstr, nameend, ol.end);
else if (value && ol.value == NULL)
/* insert "=value" */
rc = insert_value(optstr, nameend, value, NULL);
else if (value && ol.value && strlen(value) == ol.valsz)
/* simply replace =value */
memcpy(ol.value, value, ol.valsz);
else if (value && ol.value) {
mnt_optstr_remove_option_at(optstr, nameend, ol.end);
rc = insert_value(optstr, nameend, value, NULL);
}
return rc;
}
/**
* mnt_optstr_remove_option:
* @optstr: string with a comma separated list of options
* @name: requested option name
*
* Returns: 0 on success, 1 when not found the @name or negative number in case
* of error.
*/
int mnt_optstr_remove_option(char **optstr, const char *name)
{
struct libmnt_optloc ol = MNT_INIT_OPTLOC;
int rc;
if (!optstr || !name)
return -EINVAL;
rc = mnt_optstr_locate_option(*optstr, name, &ol);
if (rc != 0)
return rc;
mnt_optstr_remove_option_at(optstr, ol.begin, ol.end);
return 0;
}
/**
* mnt_split_optstr:
* @optstr: string with comma separated list of options
* @user: returns newly allocated string with userspace options
* @vfs: returns newly allocated string with VFS options
* @fs: returns newly allocated string with FS options
* @ignore_user: option mask for options that should be ignored
* @ignore_vfs: option mask for options that should be ignored
*
* For example:
*
* mnt_split_optstr(optstr, &u, NULL, NULL, MNT_NOMTAB, 0);
*
* returns all userspace options, the options that do not belong to
* mtab are ignored.
*
* Note that FS options are all options that are undefined in MNT_USERSPACE_MAP
* or MNT_LINUX_MAP.
*
* Returns: 0 on success, or a negative number in case of error.
*/
int mnt_split_optstr(const char *optstr, char **user, char **vfs,
char **fs, int ignore_user, int ignore_vfs)
{
int rc = 0;
char *name, *val, *str = (char *) optstr;
size_t namesz, valsz, chunsz;
struct libmnt_optmap const *maps[2];
struct ul_buffer xvfs = UL_INIT_BUFFER,
xfs = UL_INIT_BUFFER,
xuser = UL_INIT_BUFFER;
if (!optstr)
return -EINVAL;
maps[0] = mnt_get_builtin_optmap(MNT_LINUX_MAP);
maps[1] = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
chunsz = strlen(optstr) / 2;
while (!mnt_optstr_next_option(&str, &name, &namesz, &val, &valsz)) {
struct ul_buffer *buf = NULL;
const struct libmnt_optmap *ent = NULL;
const struct libmnt_optmap *m =
mnt_optmap_get_entry(maps, 2, name, namesz, &ent);
if (ent && !ent->id)
continue; /* ignore undefined options (comments) */
/* ignore name=<value> if options map expects <name> only */
if (valsz && mnt_optmap_entry_novalue(ent))
m = NULL;
if (ent && m && m == maps[0] && vfs) {
if (ignore_vfs && (ent->mask & ignore_vfs))
continue;
if (vfs)
buf = &xvfs;
} else if (ent && m && m == maps[1] && user) {
if (ignore_user && (ent->mask & ignore_user))
continue;
if (user)
buf = &xuser;
} else if (!m && fs) {
if (fs)
buf = &xfs;
}
if (buf) {
if (ul_buffer_is_empty(buf))
ul_buffer_set_chunksize(buf, chunsz);
rc = __buffer_append_option(buf, name, namesz, val, valsz);
}
if (rc)
break;
}
if (vfs)
*vfs = rc ? NULL : ul_buffer_get_data(&xvfs, NULL, NULL);
if (fs)
*fs = rc ? NULL : ul_buffer_get_data(&xfs, NULL, NULL);
if (user)
*user = rc ? NULL : ul_buffer_get_data(&xuser, NULL, NULL);
if (rc) {
ul_buffer_free_data(&xvfs);
ul_buffer_free_data(&xfs);
ul_buffer_free_data(&xuser);
}
return rc;
}
/**
* mnt_optstr_get_options
* @optstr: string with a comma separated list of options
* @subset: returns newly allocated string with options
* @map: options map
* @ignore: mask of the options that should be ignored
*
* Extracts options from @optstr that belong to the @map, for example:
*
* mnt_optstr_get_options(optstr, &p,
* mnt_get_builtin_optmap(MNT_LINUX_MAP),
* MNT_NOMTAB);
*
* the 'p' returns all VFS options, the options that do not belong to mtab
* are ignored.
*
* Returns: 0 on success, or a negative number in case of error.
*/
int mnt_optstr_get_options(const char *optstr, char **subset,
const struct libmnt_optmap *map, int ignore)
{
struct libmnt_optmap const *maps[1];
struct ul_buffer buf = UL_INIT_BUFFER;
char *name, *val, *str = (char *) optstr;
size_t namesz, valsz;
int rc = 0;
if (!optstr || !subset)
return -EINVAL;
maps[0] = map;
ul_buffer_set_chunksize(&buf, strlen(optstr)/2);
while (!mnt_optstr_next_option(&str, &name, &namesz, &val, &valsz)) {
const struct libmnt_optmap *ent;
mnt_optmap_get_entry(maps, 1, name, namesz, &ent);
if (!ent || !ent->id)
continue; /* ignore undefined options (comments) */
if (ignore && (ent->mask & ignore))
continue;
/* ignore name=<value> if options map expects <name> only */
if (valsz && mnt_optmap_entry_novalue(ent))
continue;
rc = __buffer_append_option(&buf, name, namesz, val, valsz);
if (rc)
break;
}
*subset = rc ? NULL : ul_buffer_get_data(&buf, NULL, NULL);
if (rc)
ul_buffer_free_data(&buf);
return rc;
}
/**
* mnt_optstr_get_flags:
* @optstr: string with comma separated list of options
* @flags: returns mount flags
* @map: options map
*
* Returns in @flags IDs of options from @optstr as defined in the @map.
*
* For example:
*
* "bind,exec,foo,bar" --returns-> MS_BIND
*
* "bind,noexec,foo,bar" --returns-> MS_BIND|MS_NOEXEC
*
* Note that @flags are not zeroized by this function! This function sets/unsets
* bits in the @flags only.
*
* Returns: 0 on success or negative number in case of error
*/
int mnt_optstr_get_flags(const char *optstr, unsigned long *flags,
const struct libmnt_optmap *map)
{
struct libmnt_optmap const *maps[2];
char *name, *str = (char *) optstr;
size_t namesz = 0, valsz = 0;
int nmaps = 0;
if (!optstr || !flags || !map)
return -EINVAL;
maps[nmaps++] = map;
if (map == mnt_get_builtin_optmap(MNT_LINUX_MAP))
/*
* Add userspace map -- the "user" is interpreted as
* MS_NO{EXEC,SUID,DEV}.
*/
maps[nmaps++] = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
while(!mnt_optstr_next_option(&str, &name, &namesz, NULL, &valsz)) {
const struct libmnt_optmap *ent;
const struct libmnt_optmap *m;
m = mnt_optmap_get_entry(maps, nmaps, name, namesz, &ent);
if (!m || !ent || !ent->id)
continue;
/* ignore name=<value> if options map expects <name> only */
if (valsz && mnt_optmap_entry_novalue(ent))
continue;
if (m == map) { /* requested map */
if (ent->mask & MNT_INVERT)
*flags &= ~ent->id;
else
*flags |= ent->id;
} else if (nmaps == 2 && m == maps[1] && valsz == 0) {
/*
* Special case -- translate "user" (but no user=) to
* MS_ options
*/
if (ent->mask & MNT_INVERT)
continue;
if (ent->id & (MNT_MS_OWNER | MNT_MS_GROUP))
*flags |= MS_OWNERSECURE;
else if (ent->id & (MNT_MS_USER | MNT_MS_USERS))
*flags |= MS_SECURE;
}
}
return 0;
}
/**
* mnt_optstr_apply_flags:
* @optstr: string with comma separated list of options
* @flags: returns mount flags
* @map: options map
*
* Removes/adds options to the @optstr according to flags. For example:
*
* MS_NOATIME and "foo,bar,noexec" --returns-> "foo,bar,noatime"
*
* Returns: 0 on success or negative number in case of error.
*/
int mnt_optstr_apply_flags(char **optstr, unsigned long flags,
const struct libmnt_optmap *map)
{
struct libmnt_optmap const *maps[1];
char *name, *next, *val;
size_t namesz = 0, valsz = 0, multi = 0;
unsigned long fl;
int rc = 0;
if (!optstr || !map)
return -EINVAL;
DBG(CXT, ul_debug("applying 0x%08lx flags to '%s'", flags, *optstr));
maps[0] = map;
next = *optstr;
fl = flags;
/*
* There is a convention that 'rw/ro' flags are always at the beginning of
* the string (although the 'rw' is unnecessary).
*/
if (map == mnt_get_builtin_optmap(MNT_LINUX_MAP)) {
const char *o = (fl & MS_RDONLY) ? "ro" : "rw";
if (next &&
(!strncmp(next, "rw", 2) || !strncmp(next, "ro", 2)) &&
(*(next + 2) == '\0' || *(next + 2) == ',')) {
/* already set, be paranoid and fix it */
memcpy(next, o, 2);
} else {
rc = mnt_optstr_prepend_option(optstr, o, NULL);
if (rc)
goto err;
next = *optstr; /* because realloc() */
}
fl &= ~MS_RDONLY;
next += 2;
if (*next == ',')
next++;
}
if (next && *next) {
/*
* scan @optstr and remove options that are missing in
* @flags
*/
while(!mnt_optstr_next_option(&next, &name, &namesz,
&val, &valsz)) {
const struct libmnt_optmap *ent;
if (mnt_optmap_get_entry(maps, 1, name, namesz, &ent)) {
/*
* remove unwanted option (rw/ro is already set)
*/
if (!ent || !ent->id)
continue;
/* ignore name=<value> if options map expects <name> only */
if (valsz && mnt_optmap_entry_novalue(ent))
continue;
if (ent->id == MS_RDONLY ||
(ent->mask & MNT_INVERT) ||
(fl & ent->id) != (unsigned long) ent->id) {
char *end = val ? val + valsz :
name + namesz;
next = name;
rc = mnt_optstr_remove_option_at(
optstr, name, end);
if (rc)
goto err;
}
if (!(ent->mask & MNT_INVERT)) {
/* allow options with prefix (X-mount.foo,X-mount.bar) more than once */
if (ent->mask & MNT_PREFIX)
multi |= ent->id;
else
fl &= ~ent->id;
if (ent->id & MS_REC)
fl |= MS_REC;
}
}
}
}
/* remove from flags options which are allowed more than once */
fl &= ~multi;
/* add missing options (but ignore fl if contains MS_REC only) */
if (fl && fl != MS_REC) {
const struct libmnt_optmap *ent;
struct ul_buffer buf = UL_INIT_BUFFER;
size_t sz;
char *p;
ul_buffer_refer_string(&buf, *optstr);
for (ent = map; ent && ent->name; ent++) {
if ((ent->mask & MNT_INVERT)
|| ent->id == 0
|| (fl & ent->id) != (unsigned long) ent->id)
continue;
/* don't add options which require values (e.g. offset=%d) */
p = strchr(ent->name, '=');
if (p) {
if (p > ent->name && *(p - 1) == '[')
p--; /* name[=] */
else
continue; /* name= */
sz = p - ent->name;
} else
sz = strlen(ent->name);
rc = __buffer_append_option(&buf, ent->name, sz, NULL, 0);
if (rc)
goto err;
}
*optstr = ul_buffer_get_data(&buf, NULL, NULL);
}
DBG(CXT, ul_debug("new optstr '%s'", *optstr));
return rc;
err:
DBG(CXT, ul_debug("failed to apply flags [rc=%d]", rc));
return rc;
}
/*
* @optstr: string with comma separated list of options
* @value: pointer to the begin of the context value
* @valsz: size of the value
* @next: returns pointer to the next option (optional argument)
*
* Translates SELinux context from human to raw format. The function does not
* modify @optstr and returns zero if libmount is compiled without SELinux
* support.
*
* Returns: 0 on success, a negative number in case of error.
*/
#ifndef HAVE_LIBSELINUX
int mnt_optstr_fix_secontext(char **optstr __attribute__ ((__unused__)),
char *value __attribute__ ((__unused__)),
size_t valsz __attribute__ ((__unused__)),
char **next __attribute__ ((__unused__)))
{
return 0;
}
#else
int mnt_optstr_fix_secontext(char **optstr,
char *value,
size_t valsz,
char **next)
{
int rc = 0;
char *p, *val, *begin, *end, *raw = NULL;
size_t sz;
if (!optstr || !*optstr || !value || !valsz)
return -EINVAL;
DBG(CXT, ul_debug("fixing SELinux context"));
begin = value;
end = value + valsz;
/* the selinux contexts are quoted */
if (*value == '"') {
if (valsz <= 2 || *(value + valsz - 1) != '"')
return -EINVAL; /* improperly quoted option string */
value++;
valsz -= 2;
}
p = strndup(value, valsz);
if (!p)
return -ENOMEM;
/* translate the context */
rc = selinux_trans_to_raw_context(p, &raw);
DBG(CXT, ul_debug("SELinux context '%s' translated to '%s'",
p, rc == -1 ? "FAILED" : (char *) raw));
free(p);
if (rc == -1 || !raw)
return -EINVAL;
/* create a quoted string from the raw context */
sz = strlen((char *) raw);
if (!sz) {
freecon(raw);
return -EINVAL;
}
p = val = malloc(valsz + 3);
if (!val) {
freecon(raw);
return -ENOMEM;
}
*p++ = '"';
memcpy(p, raw, sz);
p += sz;
*p++ = '"';
*p = '\0';
freecon(raw);
/* set new context */
mnt_optstr_remove_option_at(optstr, begin, end);
rc = insert_value(optstr, begin, val, next);
free(val);
return rc;
}
#endif
static int set_uint_value(char **optstr, unsigned int num,
char *begin, char *end, char **next)
{
char buf[40];
snprintf(buf, sizeof(buf), "%u", num);
mnt_optstr_remove_option_at(optstr, begin, end);
return insert_value(optstr, begin, buf, next);
}
/*
* @optstr: string with a comma separated list of options
* @value: pointer to the beginning of the uid value
* @valsz: size of the value
* @next: returns pointer to the next option (optional argument)
* Translates "username" or "useruid" to the real UID.
*
* For example:
* if (!mnt_optstr_get_option(optstr, "uid", &val, &valsz))
* mnt_optstr_fix_uid(&optstr, val, valsz, NULL);
*
* Returns: 0 on success, a negative number in case of error.
*/
int mnt_optstr_fix_uid(char **optstr, char *value, size_t valsz, char **next)
{
char *end;
if (!optstr || !*optstr || !value || !valsz)
return -EINVAL;
DBG(CXT, ul_debug("fixing uid"));
end = value + valsz;
if (valsz == 7 && !strncmp(value, "useruid", 7) &&
(*(value + 7) == ',' || !*(value + 7)))
return set_uint_value(optstr, getuid(), value, end, next);
if (!isdigit(*value)) {
uid_t id;
int rc;
char *p = strndup(value, valsz);
if (!p)
return -ENOMEM;
rc = mnt_get_uid(p, &id);
free(p);
if (!rc)
return set_uint_value(optstr, id, value, end, next);
}
if (next) {
/* no change, let's keep the original value */
*next = value + valsz;
if (**next == ',')
(*next)++;
}
return 0;
}
/*
* @optstr: string with a comma separated list of options
* @value: pointer to the beginning of the uid value
* @valsz: size of the value
* @next: returns pointer to the next option (optional argument)
* Translates "groupname" or "usergid" to the real GID.
*
* Returns: 0 on success, a negative number in case of error.
*/
int mnt_optstr_fix_gid(char **optstr, char *value, size_t valsz, char **next)
{
char *end;
if (!optstr || !*optstr || !value || !valsz)
return -EINVAL;
DBG(CXT, ul_debug("fixing gid"));
end = value + valsz;
if (valsz == 7 && !strncmp(value, "usergid", 7) &&
(*(value + 7) == ',' || !*(value + 7)))
return set_uint_value(optstr, getgid(), value, end, next);
if (!isdigit(*value)) {
int rc;
gid_t id;
char *p = strndup(value, valsz);
if (!p)
return -ENOMEM;
rc = mnt_get_gid(p, &id);
free(p);
if (!rc)
return set_uint_value(optstr, id, value, end, next);
}
if (next) {
/* nothing */
*next = value + valsz;
if (**next == ',')
(*next)++;
}
return 0;
}
/*
* Converts "user" to "user=<username>".
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_optstr_fix_user(char **optstr)
{
char *username;
struct libmnt_optloc ol = MNT_INIT_OPTLOC;
int rc = 0;
DBG(CXT, ul_debug("fixing user"));
rc = mnt_optstr_locate_option(*optstr, "user", &ol);
if (rc)
return rc == 1 ? 0 : rc; /* 1: user= not found */
username = mnt_get_username(getuid());
if (!username)
return -ENOMEM;
if (!ol.valsz || (ol.value && strncmp(ol.value, username, ol.valsz) != 0)) {
if (ol.valsz)
/* remove old value */
mnt_optstr_remove_option_at(optstr, ol.value, ol.end);
rc = insert_value(optstr, ol.value ? ol.value : ol.end,
username, NULL);
}
free(username);
return rc;
}
/*
* Converts value from @optstr addressed by @name to uid.
*
* Returns: 0 on success, 1 if not found, <0 on error
*/
int mnt_optstr_get_uid(const char *optstr, const char *name, uid_t *uid)
{
char *value = NULL;
size_t valsz = 0;
char buf[sizeof(stringify_value(UINT64_MAX))];
int rc;
uint64_t num;
assert(optstr);
assert(name);
assert(uid);
rc = mnt_optstr_get_option(optstr, name, &value, &valsz);
if (rc != 0)
goto fail;
if (valsz > sizeof(buf) - 1) {
rc = -ERANGE;
goto fail;
}
mem2strcpy(buf, value, valsz, sizeof(buf));
rc = ul_strtou64(buf, &num, 10);
if (rc != 0)
goto fail;
if (num > ULONG_MAX || (uid_t) num != num) {
rc = -ERANGE;
goto fail;
}
*uid = (uid_t) num;
return 0;
fail:
DBG(UTILS, ul_debug("failed to convert '%s'= to number [rc=%d]", name, rc));
return rc;
}
/**
* mnt_match_options:
* @optstr: options string
* @pattern: comma delimited list of options
*
* The "no" could be used for individual items in the @options list. The "no"
* prefix does not have a global meaning.
*
* Unlike fs type matching, nonetdev,user and nonetdev,nouser have
* DIFFERENT meanings; each option is matched explicitly as specified.
*
* The "no" prefix interpretation could be disabled by the "+" prefix, for example
* "+noauto" matches if @optstr literally contains the "noauto" string.
*
* "xxx,yyy,zzz" : "nozzz" -> False
*
* "xxx,yyy,zzz" : "xxx,noeee" -> True
*
* "bar,zzz" : "nofoo" -> True (does not contain "foo")
*
* "nofoo,bar" : "nofoo" -> True (does not contain "foo")
*
* "nofoo,bar" : "+nofoo" -> True (contains "nofoo")
*
* "bar,zzz" : "+nofoo" -> False (does not contain "nofoo")
*
*
* Returns: 1 if pattern is matching, else 0. This function also returns 0
* if @pattern is NULL and @optstr is non-NULL.
*/
int mnt_match_options(const char *optstr, const char *pattern)
{
char *name, *pat = (char *) pattern;
char *buf, *patval;
size_t namesz = 0, patvalsz = 0;
int match = 1;
if (!pattern && !optstr)
return 1;
if (!pattern)
return 0;
buf = malloc(strlen(pattern) + 1);
if (!buf)
return 0;
/* walk on pattern string
*/
while (match && !mnt_optstr_next_option(&pat, &name, &namesz,
&patval, &patvalsz)) {
char *val;
size_t sz;
int no = 0, rc;
if (*name == '+')
name++, namesz--;
else if ((no = (startswith(name, "no") != NULL)))
name += 2, namesz -= 2;
xstrncpy(buf, name, namesz + 1);
rc = mnt_optstr_get_option(optstr, buf, &val, &sz);
/* check also value (if the pattern is "foo=value") */
if (rc == 0 && patvalsz > 0 &&
(patvalsz != sz || strncmp(patval, val, sz) != 0))
rc = 1;
switch (rc) {
case 0: /* found */
match = no == 0 ? 1 : 0;
break;
case 1: /* not found */
match = no == 1 ? 1 : 0;
break;
default: /* parse error */
match = 0;
break;
}
}
free(buf);
return match;
}
#ifdef TEST_PROGRAM
#include "xalloc.h"
static int test_append(struct libmnt_test *ts, int argc, char *argv[])
{
const char *value = NULL, *name;
char *optstr;
int rc;
if (argc < 3)
return -EINVAL;
optstr = xstrdup(argv[1]);
name = argv[2];
if (argc == 4)
value = argv[3];
rc = mnt_optstr_append_option(&optstr, name, value);
if (!rc)
printf("result: >%s<\n", optstr);
free(optstr);
return rc;
}
static int test_prepend(struct libmnt_test *ts, int argc, char *argv[])
{
const char *value = NULL, *name;
char *optstr;
int rc;
if (argc < 3)
return -EINVAL;
optstr = xstrdup(argv[1]);
name = argv[2];
if (argc == 4)
value = argv[3];
rc = mnt_optstr_prepend_option(&optstr, name, value);
if (!rc)
printf("result: >%s<\n", optstr);
free(optstr);
return rc;
}
static int test_split(struct libmnt_test *ts, int argc, char *argv[])
{
char *optstr, *user = NULL, *fs = NULL, *vfs = NULL;
int rc;
if (argc < 2)
return -EINVAL;
optstr = xstrdup(argv[1]);
rc = mnt_split_optstr(optstr, &user, &vfs, &fs, 0, 0);
if (!rc) {
printf("user : %s\n", user);
printf("vfs : %s\n", vfs);
printf("fs : %s\n", fs);
}
free(user);
free(vfs);
free(fs);
free(optstr);
return rc;
}
static int test_flags(struct libmnt_test *ts, int argc, char *argv[])
{
char *optstr;
int rc;
unsigned long fl = 0;
if (argc < 2)
return -EINVAL;
optstr = xstrdup(argv[1]);
rc = mnt_optstr_get_flags(optstr, &fl, mnt_get_builtin_optmap(MNT_LINUX_MAP));
if (rc)
return rc;
printf("mountflags: 0x%08lx\n", fl);
fl = 0;
rc = mnt_optstr_get_flags(optstr, &fl, mnt_get_builtin_optmap(MNT_USERSPACE_MAP));
if (rc)
return rc;
printf("userspace-mountflags: 0x%08lx\n", fl);
free(optstr);
return rc;
}
static int test_apply(struct libmnt_test *ts, int argc, char *argv[])
{
char *optstr;
int rc, map;
unsigned long flags;
if (argc < 4)
return -EINVAL;
if (!strcmp(argv[1], "--user"))
map = MNT_USERSPACE_MAP;
else if (!strcmp(argv[1], "--linux"))
map = MNT_LINUX_MAP;
else {
fprintf(stderr, "unknown option '%s'\n", argv[1]);
return -EINVAL;
}
optstr = xstrdup(argv[2]);
flags = strtoul(argv[3], NULL, 16);
printf("flags: 0x%08lx\n", flags);
rc = mnt_optstr_apply_flags(&optstr, flags, mnt_get_builtin_optmap(map));
printf("optstr: %s\n", optstr);
free(optstr);
return rc;
}
static int test_set(struct libmnt_test *ts, int argc, char *argv[])
{
const char *value = NULL, *name;
char *optstr;
int rc;
if (argc < 3)
return -EINVAL;
optstr = xstrdup(argv[1]);
name = argv[2];
if (argc == 4)
value = argv[3];
rc = mnt_optstr_set_option(&optstr, name, value);
if (!rc)
printf("result: >%s<\n", optstr);
free(optstr);
return rc;
}
static int test_get(struct libmnt_test *ts, int argc, char *argv[])
{
char *optstr;
const char *name;
char *val = NULL;
size_t sz = 0;
int rc;
if (argc < 2)
return -EINVAL;
optstr = argv[1];
name = argv[2];
rc = mnt_optstr_get_option(optstr, name, &val, &sz);
if (rc == 0) {
printf("found; name: %s", name);
if (sz) {
printf(", argument: size=%zd data=", sz);
if (fwrite(val, 1, sz, stdout) != sz)
return -1;
}
printf("\n");
} else if (rc == 1)
printf("%s: not found\n", name);
else
printf("parse error: %s\n", optstr);
return rc;
}
static int test_remove(struct libmnt_test *ts, int argc, char *argv[])
{
const char *name;
char *optstr;
int rc;
if (argc < 3)
return -EINVAL;
optstr = xstrdup(argv[1]);
name = argv[2];
rc = mnt_optstr_remove_option(&optstr, name);
if (!rc)
printf("result: >%s<\n", optstr);
free(optstr);
return rc;
}
static int test_dedup(struct libmnt_test *ts, int argc, char *argv[])
{
const char *name;
char *optstr;
int rc;
if (argc < 3)
return -EINVAL;
optstr = xstrdup(argv[1]);
name = argv[2];
rc = mnt_optstr_deduplicate_option(&optstr, name);
if (!rc)
printf("result: >%s<\n", optstr);
free(optstr);
return rc;
}
static int test_fix(struct libmnt_test *ts, int argc, char *argv[])
{
char *optstr;
int rc = 0;
char *name, *val, *next;
size_t valsz, namesz;
if (argc < 2)
return -EINVAL;
next = optstr = xstrdup(argv[1]);
printf("optstr: %s\n", optstr);
while (!mnt_optstr_next_option(&next, &name, &namesz, &val, &valsz)) {
if (!strncmp(name, "uid", 3))
rc = mnt_optstr_fix_uid(&optstr, val, valsz, &next);
else if (!strncmp(name, "gid", 3))
rc = mnt_optstr_fix_gid(&optstr, val, valsz, &next);
else if (!strncmp(name, "context", 7))
rc = mnt_optstr_fix_secontext(&optstr, val, valsz, &next);
if (rc)
break;
}
if (rc)
rc = mnt_optstr_fix_user(&optstr);
printf("fixed: %s\n", optstr);
free(optstr);
return rc;
}
int main(int argc, char *argv[])
{
struct libmnt_test tss[] = {
{ "--append", test_append, "<optstr> <name> [<value>] append value to optstr" },
{ "--prepend",test_prepend,"<optstr> <name> [<value>] prepend value to optstr" },
{ "--set", test_set, "<optstr> <name> [<value>] (un)set value" },
{ "--get", test_get, "<optstr> <name> search name in optstr" },
{ "--remove", test_remove, "<optstr> <name> remove name in optstr" },
{ "--dedup", test_dedup, "<optstr> <name> deduplicate name in optstr" },
{ "--split", test_split, "<optstr> split into FS, VFS and userspace" },
{ "--flags", test_flags, "<optstr> convert options to MS_* flags" },
{ "--apply", test_apply, "--{linux,user} <optstr> <mask> apply mask to optstr" },
{ "--fix", test_fix, "<optstr> fix uid=, gid=, user, and context=" },
{ NULL }
};
return mnt_run_test(tss, argc, argv);
}
#endif /* TEST_PROGRAM */
|