forked from storaged-project/udisks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudiskslinuxmountoptions.c
More file actions
1322 lines (1151 loc) · 40.8 KB
/
udiskslinuxmountoptions.c
File metadata and controls
1322 lines (1151 loc) · 40.8 KB
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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
* Copyright (C) 2020 Tomas Bzatek <tbzatek@redhat.com>
*
* 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 of the License, 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <stdlib.h>
#include <glib/gstdio.h>
#include <libmount/libmount.h>
#include "udiskslogging.h"
#include "udiskslinuxmountoptions.h"
#include "udiskslinuxblockobject.h"
#include "udisksdaemon.h"
#include "udisksstate.h"
#include "udisksdaemonutil.h"
#include "udiskslinuxdevice.h"
#include "udisksconfigmanager.h"
#include "udisks-daemon-resources.h"
/* ---------------------------------------------------------------------------------------------------- */
static GHashTable * mount_options_parse_config_file (const gchar *filename, GError **error);
static GHashTable * mount_options_get_from_udev (UDisksLinuxDevice *device, GError **error);
/* ---------------------------------------------------------------------------------------------------- */
typedef struct
{
gchar **defaults;
gchar **allow;
gchar **drivers;
} FSMountOptions;
static void
free_fs_mount_options (FSMountOptions *options)
{
if (options)
{
g_strfreev (options->defaults);
g_strfreev (options->allow);
g_strfreev (options->drivers);
g_free (options);
}
}
static void
strv_append_unique (gchar **src, gchar ***dest)
{
guint src_len;
guint dest_len;
gchar **s;
gchar **l;
guint l_len = 0;
g_warn_if_fail (dest != NULL);
if (!src || g_strv_length (src) == 0)
return;
if (!*dest)
{
*dest = g_strdupv (src);
return;
}
src_len = g_strv_length (src);
dest_len = g_strv_length (*dest);
l = g_malloc (src_len * sizeof (gpointer));
for (s = src; *s; s++)
if (!g_strv_contains ((const gchar * const *) *dest, *s))
l[l_len++] = g_strdup (*s);
if (l_len > 0)
{
*dest = g_realloc (*dest, (dest_len + l_len + 1) * sizeof (gpointer));
memcpy (*dest + dest_len, l, l_len * sizeof (gpointer));
(*dest)[dest_len + l_len] = NULL;
}
g_free (l);
}
static void
append_fs_mount_options (const FSMountOptions *src, FSMountOptions *dest)
{
if (!src)
return;
strv_append_unique (src->defaults, &dest->defaults);
strv_append_unique (src->allow, &dest->allow);
/* appending not allowed for 'drivers' */
}
/* Similar to append_fs_mount_options() but replaces the member data instead of appending */
static void
override_fs_mount_options (const FSMountOptions *src, FSMountOptions *dest)
{
if (!src)
return;
if (src->defaults)
{
g_strfreev (dest->defaults);
dest->defaults = g_strdupv (src->defaults);
}
if (src->allow)
{
g_strfreev (dest->allow);
dest->allow = g_strdupv (src->allow);
}
if (src->drivers)
{
g_strfreev (dest->drivers);
dest->drivers = g_strdupv (src->drivers);
}
}
/*
* udisks_mount_options_entry_free:
* @entry: A #UDisksMountOptionsEntry.
*
* Frees the mount options entry.
*/
void
udisks_mount_options_entry_free (UDisksMountOptionsEntry *entry)
{
if (entry)
{
g_free (entry->fs_type);
g_free (entry->options);
g_free (entry);
}
}
/* ---------------------------------------------------------------------------------------------------- */
#define MOUNT_OPTIONS_GLOBAL_CONFIG_FILE_NAME "mount_options.conf"
#define MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS "defaults"
#define MOUNT_OPTIONS_KEY_DEFAULTS "defaults"
#define MOUNT_OPTIONS_KEY_ALLOW "allow"
#define MOUNT_OPTIONS_KEY_DRIVERS "drivers"
#define MOUNT_OPTIONS_ARG_UID_SELF "$UID"
#define MOUNT_OPTIONS_ARG_GID_SELF "$GID"
#define UDEV_MOUNT_OPTIONS_PREFIX "UDISKS_MOUNT_OPTIONS_"
#define FS_SIGNATURE_DRIVER_SEP ":"
/* transfer none */
static GHashTable *
get_options_for_block (GHashTable *opts,
UDisksBlock *block)
{
GHashTable *block_options = NULL;
const gchar *block_device;
const gchar * const *block_symlinks;
GList *keys;
GList *l;
if (!block)
return NULL;
block_device = udisks_block_get_device (block);
block_symlinks = udisks_block_get_symlinks (block);
keys = g_hash_table_get_keys (opts);
g_warn_if_fail (keys != NULL);
for (l = keys; l != NULL; l = l->next)
{
if (!l->data || g_str_equal (l->data, MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS))
continue;
if (g_str_equal (l->data, block_device) ||
(block_symlinks && g_strv_contains (block_symlinks, l->data)))
{
block_options = g_hash_table_lookup (opts, l->data);
break;
}
}
g_list_free (keys);
return block_options;
}
/*
* compute_block_level_mount_options: <internal>
* @daemon: A #UDisksDaemon.
* @block: A #UDisksBlock.
* @fstype: The filesystem type to match or %NULL.
* @fsmo: Filesystem type specific #FSMountOptions.
* @fsmo_any: General ("any") #FSMountOptions.
*
* Calculate mount options for the given level of overrides. Matches the block
* device-specific options on top of the defaults.
*
* Returns: %TRUE when mount options were overridden, %FALSE otherwise.
*/
static gboolean
compute_block_level_mount_options (GHashTable *opts,
UDisksBlock *block,
const gchar *fstype,
FSMountOptions *fsmo,
FSMountOptions *fsmo_any)
{
GHashTable *general_options;
GHashTable *block_options;
gboolean changed = FALSE;
/* Compute general defaults first */
general_options = g_hash_table_lookup (opts, MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS);
if (general_options)
{
FSMountOptions *o;
o = g_hash_table_lookup (general_options, MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS);
override_fs_mount_options (o, fsmo_any);
changed = changed || o != NULL;
o = fstype ? g_hash_table_lookup (general_options, fstype) : NULL;
override_fs_mount_options (o, fsmo);
changed = changed || o != NULL;
}
/* Match specific block device */
block_options = get_options_for_block (opts, block);
/* Block device specific options should fully override "general" options per-member basis */
if (block_options)
{
FSMountOptions *o;
o = g_hash_table_lookup (block_options, MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS);
override_fs_mount_options (o, fsmo_any);
changed = changed || o != NULL;
o = fstype ? g_hash_table_lookup (block_options, fstype) : NULL;
override_fs_mount_options (o, fsmo);
changed = changed || o != NULL;
}
return changed;
}
/*
* compute_block_level_fs_drivers: <internal>
* @daemon: A #UDisksDaemon.
* @block: A #UDisksBlock.
* @fs_signature: The filesystem signature to match.
*
* Calculate filesystem drivers for the given level of overrides. Matches the block
* device-specific options on top of the defaults.
*
* Returns: (transfer full) (array zero-terminated=1): list of filesystem drivers. Free with g_strfreev().
*/
static gchar **
compute_block_level_fs_drivers (GHashTable *opts,
UDisksBlock *block,
const gchar *fs_signature)
{
GHashTable *general_options;
GHashTable *block_options;
gchar **drivers = NULL;
/* Compute general defaults first */
general_options = g_hash_table_lookup (opts, MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS);
if (general_options)
{
FSMountOptions *o;
o = g_hash_table_lookup (general_options, fs_signature);
if (o)
drivers = g_strdupv (o->drivers);
}
/* Match specific block device */
block_options = get_options_for_block (opts, block);
/* Block device specific options should fully override "general" options per-member basis */
if (block_options)
{
FSMountOptions *o;
o = g_hash_table_lookup (block_options, fs_signature);
if (o)
{
g_strfreev (drivers);
drivers = g_strdupv (o->drivers);
}
}
return drivers;
}
/*
* compute_mount_options_for_fs_type: <internal>
* @daemon: A #UDisksDaemon.
* @block: A #UDisksBlock.
* @object: A #UDisksLinuxBlockObject.
* @overrides: Config file overrides.
* @fstype: The filesystem type to use or %NULL.
*
* Calculate mount options across different levels of overrides (builtin,
* global config, local user config).
*
* Returns: (transfer full): Newly allocated #FSMountOptions options. Free with free_fs_mount_options().
*/
static FSMountOptions *
compute_mount_options_for_fs_type (UDisksDaemon *daemon,
UDisksBlock *block,
UDisksLinuxBlockObject *object,
GHashTable *overrides,
const gchar *fstype)
{
UDisksLinuxDevice *device;
GHashTable *builtin_opts;
FSMountOptions *fsmo;
FSMountOptions *fsmo_any;
GHashTable *udev_overrides;
GError *error = NULL;
gboolean changed = FALSE;
/* Builtin options, two-level hashtable */
builtin_opts = g_object_get_data (G_OBJECT (daemon), "mount-options");
g_return_val_if_fail (builtin_opts != NULL, NULL);
fsmo = g_malloc0 (sizeof (FSMountOptions));
fsmo_any = g_malloc0 (sizeof (FSMountOptions));
compute_block_level_mount_options (builtin_opts, block, fstype, fsmo, fsmo_any);
/* Global config file overrides, two-level hashtable */
if (overrides)
changed = compute_block_level_mount_options (overrides, block, fstype, fsmo, fsmo_any);
/* udev properties, single-level hashtable */
device = udisks_linux_block_object_get_device (object);
udev_overrides = mount_options_get_from_udev (device, &error);
if (udev_overrides)
{
FSMountOptions *o;
o = g_hash_table_lookup (udev_overrides, MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS);
override_fs_mount_options (o, fsmo_any);
changed = changed || o != NULL;
o = fstype ? g_hash_table_lookup (udev_overrides, fstype) : NULL;
override_fs_mount_options (o, fsmo);
changed = changed || o != NULL;
g_hash_table_unref (udev_overrides);
}
else
{
udisks_warning ("Error getting udev mount options: %s",
error->message);
g_clear_error (&error);
}
g_object_unref (device);
/* Merge "any" and fstype-specific options */
append_fs_mount_options (fsmo_any, fsmo);
free_fs_mount_options (fsmo_any);
fsmo_any = NULL;
if (changed && fsmo->defaults)
{
gchar *opts = g_strjoinv (",", fsmo->defaults);
udisks_notice ("Using overridden mount options: %s", opts);
g_free (opts);
}
return fsmo;
}
/*
* compute_drivers: <internal>
* @daemon: A #UDisksDaemon.
* @block: A #UDisksBlock.
* @object: A #UDisksLinuxBlockObject.
* @overrides: Config file overrides.
* @fs_signature: Probed filesystem signature or %NULL if unavailable.
* @fs_type: The preferred filesystem type to use or %NULL.
*
* Calculate filesystem drivers for the filesystem signature and preferred filesystem type.
*
* Returns: (transfer full) (array zero-terminated=1): list of filesystem drivers. Free with g_strfreev().
*/
static gchar **
compute_drivers (UDisksDaemon *daemon,
UDisksBlock *block,
UDisksLinuxBlockObject *object,
GHashTable *overrides,
const gchar *fs_signature,
const gchar *fs_type)
{
UDisksLinuxDevice *device;
GHashTable *builtin_opts;
GHashTable *udev_overrides;
GError *error = NULL;
gchar **drivers;
/* No probed filesystem signature available or specific filesystem type is requested */
if (!fs_signature || fs_type)
{
drivers = g_new0 (gchar *, 2);
*drivers = g_strdup (fs_type);
return drivers;
}
/* Builtin options, two-level hashtable */
builtin_opts = g_object_get_data (G_OBJECT (daemon), "mount-options");
g_return_val_if_fail (builtin_opts != NULL, NULL);
drivers = compute_block_level_fs_drivers (builtin_opts, block, fs_signature);
/* Global config file overrides, two-level hashtable */
if (overrides)
{
gchar **d;
d = compute_block_level_fs_drivers (overrides, block, fs_signature);
if (d)
{
g_strfreev (drivers);
drivers = d;
}
}
/* udev properties, single-level hashtable */
device = udisks_linux_block_object_get_device (object);
udev_overrides = mount_options_get_from_udev (device, &error);
if (udev_overrides)
{
FSMountOptions *o;
o = g_hash_table_lookup (udev_overrides, fs_signature);
if (o && o->drivers)
{
g_strfreev (drivers);
drivers = g_strdupv (o->drivers);
}
g_hash_table_unref (udev_overrides);
}
else
{
udisks_warning ("Error getting udev mount options: %s",
error->message);
g_clear_error (&error);
}
g_object_unref (device);
/* No drivers configured for the specific fs_signature, use the signature itself */
if (!drivers)
{
drivers = g_new0 (gchar *, 2);
*drivers = g_strdup (fs_signature);
}
return drivers;
}
/* ---------------------------------------------------------------------------------------------------- */
/* transfer-full */
static gchar **
parse_mount_options_string (const gchar *str, gboolean strip_empty_values)
{
GPtrArray *opts;
char *optstr;
char *name;
size_t namesz;
char *value;
size_t valuesz;
int ret;
if (!str)
return NULL;
opts = g_ptr_array_new_with_free_func (g_free);
optstr = (char *)str;
while ((ret = mnt_optstr_next_option (&optstr, &name, &namesz, &value, &valuesz)) == 0)
{
gchar *opt;
if (value == NULL || (strip_empty_values && valuesz == 0))
{
opt = g_strndup (name, namesz);
}
else
{
opt = g_strdup_printf ("%.*s=%.*s", (int) namesz, name, (int) valuesz, value);
}
g_ptr_array_add (opts, opt);
}
if (ret < 0)
{
udisks_warning ("Malformed mount options string '%s' at position %zd, ignoring",
str, optstr - str + 1);
g_ptr_array_free (opts, TRUE);
return NULL;
}
g_ptr_array_add (opts, NULL);
return (gchar **) g_ptr_array_free (opts, FALSE);
}
/* transfer-full */
static gchar *
extract_fs_type (const gchar *key, const gchar **group)
{
if (g_str_equal (key, MOUNT_OPTIONS_KEY_DEFAULTS) ||
g_str_equal (key, MOUNT_OPTIONS_KEY_ALLOW))
{
*group = key;
return g_strdup (MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS);
}
if (g_str_has_suffix (key, "_" MOUNT_OPTIONS_KEY_DEFAULTS))
{
*group = MOUNT_OPTIONS_KEY_DEFAULTS;
return g_strndup (key, strlen (key) - strlen (MOUNT_OPTIONS_KEY_DEFAULTS) - 1);
}
if (g_str_has_suffix (key, "_" MOUNT_OPTIONS_KEY_ALLOW))
{
*group = MOUNT_OPTIONS_KEY_ALLOW;
return g_strndup (key, strlen (key) - strlen (MOUNT_OPTIONS_KEY_ALLOW) - 1);
}
if (g_str_has_suffix (key, "_" MOUNT_OPTIONS_KEY_DRIVERS))
{
*group = MOUNT_OPTIONS_KEY_DRIVERS;
return g_strndup (key, strlen (key) - strlen (MOUNT_OPTIONS_KEY_DRIVERS) - 1);
}
/* invalid key name */
*group = NULL;
return NULL;
}
static void
parse_key_value_pair (GHashTable *mount_options, const gchar *key, const gchar *value)
{
FSMountOptions *ent;
gchar *fs_type;
const gchar *group = NULL;
gchar **opts;
fs_type = extract_fs_type (key, &group);
if (!fs_type)
{
/* invalid or malformed key detected, do not parse and ignore */
udisks_debug ("parse_key_value_pair: garbage key found: %s", key);
return;
}
g_warn_if_fail (group != NULL);
/* Trim equal 'fs_signature:fs_type' strings */
if (strstr (fs_type, FS_SIGNATURE_DRIVER_SEP))
{
gchar **split = g_strsplit (fs_type, FS_SIGNATURE_DRIVER_SEP, 2);
if (g_strv_length (split) == 2 && g_strcmp0 (split[0], split[1]) == 0)
{
g_free (fs_type);
fs_type = g_strdup (split[0]);
}
g_strfreev (split);
}
ent = g_hash_table_lookup (mount_options, fs_type);
if (!ent)
{
ent = g_malloc0 (sizeof (FSMountOptions));
g_hash_table_replace (mount_options, g_strdup (fs_type), ent);
}
if (g_str_equal (group, MOUNT_OPTIONS_KEY_DRIVERS))
{
opts = g_strsplit (value, ",", -1);
}
else
{
opts = parse_mount_options_string (value,
/* strip empty values for _allow groups for easier matching */
!g_str_equal (group, MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS));
}
#define ASSIGN_OPTS(g,p) \
if (g_str_equal (group, g)) \
{ \
if (ent->p) \
{ \
g_warning ("mount_options_parse_group: Duplicate key '%s' detected", key); \
g_strfreev (ent->p); \
} \
ent->p = opts; \
} \
else
ASSIGN_OPTS (MOUNT_OPTIONS_KEY_ALLOW, allow)
ASSIGN_OPTS (MOUNT_OPTIONS_KEY_DEFAULTS, defaults)
ASSIGN_OPTS (MOUNT_OPTIONS_KEY_DRIVERS, drivers)
{
/* should be caught by extract_fs_type() already */
g_warning ("parse_key_value_pair: Unmatched key '%s' found, ignoring", key);
}
g_free (fs_type);
}
static GHashTable *
mount_options_parse_group (GKeyFile *key_file, const gchar *group_name, GError **error)
{
GHashTable *mount_options;
gchar **keys;
gsize keys_len = 0;
keys = g_key_file_get_keys (key_file, group_name, &keys_len, error);
g_warn_if_fail (keys != NULL);
mount_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) free_fs_mount_options);
for (; keys_len > 0; keys_len--)
{
gchar *key;
gchar *value;
GError *e = NULL;
key = g_ascii_strdown (keys[keys_len - 1], -1);
value = g_key_file_get_string (key_file, group_name, keys[keys_len - 1], &e);
if (!value)
{
udisks_warning ("mount_options_parse_group: cannot retrieve value for key '%s': %s",
key, e->message);
g_error_free (e);
}
else
{
parse_key_value_pair (mount_options, key, value);
}
g_free (value);
g_free (key);
}
g_strfreev (keys);
return mount_options;
}
static GHashTable *
mount_options_parse_key_file (GKeyFile *key_file, GError **error)
{
GHashTable *mount_options = NULL;
gchar **groups;
gsize groups_len = 0;
groups = g_key_file_get_groups (key_file, &groups_len);
if (groups == NULL || groups_len == 0)
{
g_set_error_literal (error, UDISKS_ERROR, UDISKS_ERROR_NOT_SUPPORTED,
"Failed to parse mount options: No sections found.");
g_strfreev (groups);
return NULL;
}
mount_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
for (; groups_len > 0; groups_len--)
{
GHashTable *opts;
GError *local_error = NULL;
gchar *group = groups[groups_len - 1];
opts = mount_options_parse_group (key_file, group, &local_error);
if (! opts)
{
udisks_warning ("Failed to parse mount options section %s: %s",
group, local_error->message);
g_error_free (local_error);
/* ignore the whole section, continue with the rest */
}
else
{
g_hash_table_replace (mount_options, g_strdup (group), opts);
}
}
g_strfreev (groups);
return mount_options;
}
/* returns two-level hashtable with block specifics at the first level */
static GHashTable *
mount_options_parse_config_file (const gchar *filename, GError **error)
{
GKeyFile *key_file;
GHashTable *mount_options;
key_file = g_key_file_new ();
if (! g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, error))
{
g_key_file_free (key_file);
return NULL;
}
mount_options = mount_options_parse_key_file (key_file, error);
g_key_file_free (key_file);
return mount_options;
}
/* returns second level of mount options (not block-specific) */
static GHashTable *
mount_options_get_from_udev (UDisksLinuxDevice *device, GError **error)
{
GHashTable *mount_options;
const gchar * const *keys;
g_warn_if_fail (device != NULL);
if (!device->udev_device)
{
g_set_error_literal (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"'device' is not a valid UDisksLinuxDevice");
return NULL;
}
mount_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) free_fs_mount_options);
keys = g_udev_device_get_property_keys (device->udev_device);
for (; *keys; keys++)
if (g_str_has_prefix (*keys, UDEV_MOUNT_OPTIONS_PREFIX))
{
gchar *key;
const gchar *value;
key = g_ascii_strdown (*keys + strlen (UDEV_MOUNT_OPTIONS_PREFIX), -1);
value = g_udev_device_get_property (device->udev_device, *keys);
if (!value)
{
udisks_warning ("mount_options_get_from_udev: cannot retrieve value for udev property %s",
*keys);
}
else
{
parse_key_value_pair (mount_options, key, value);
}
g_free (key);
}
return mount_options;
}
/*
* udisks_linux_mount_options_get_builtin: <internal>
*
* Get built-in set of default mount options. This function will never
* fail, the process is aborted in case of a parse error.
*
* Returns: (transfer full): A two-level #GHashTable.
*/
GHashTable *
udisks_linux_mount_options_get_builtin (void)
{
GResource *daemon_resource;
GBytes *builtin_opts_bytes;
GKeyFile *key_file;
GHashTable *mount_options;
GError *error = NULL;
daemon_resource = udisks_daemon_resources_get_resource ();
builtin_opts_bytes = g_resource_lookup_data (daemon_resource,
"/org/freedesktop/UDisks2/data/builtin_mount_options.conf",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
if (builtin_opts_bytes == NULL)
{
udisks_error ("Failed to read built-in mount options resource: %s", error->message);
g_error_free (error);
return NULL;
}
key_file = g_key_file_new ();
if (! g_key_file_load_from_bytes (key_file, builtin_opts_bytes, G_KEY_FILE_NONE, &error))
{
/* should never happen */
udisks_error ("Failed to read built-in mount options: %s", error->message);
g_error_free (error);
g_key_file_free (key_file);
g_bytes_unref (builtin_opts_bytes);
return NULL;
}
mount_options = mount_options_parse_key_file (key_file, &error);
g_key_file_free (key_file);
g_bytes_unref (builtin_opts_bytes);
if (mount_options == NULL)
{
/* should never happen either */
udisks_error ("Failed to parse built-in mount options: %s", error->message);
g_error_free (error);
}
else if (!g_hash_table_contains (mount_options, MOUNT_OPTIONS_CONFIG_GROUP_DEFAULTS))
{
g_hash_table_destroy (mount_options);
mount_options = NULL;
udisks_error ("Failed to parse built-in mount options: No global `defaults` section found.");
}
return mount_options;
}
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
is_uid_in_gid (uid_t uid,
gid_t gid)
{
GError *error = NULL;
gid_t primary_gid = -1;
gchar *user_name = NULL;
static gid_t supplementary_groups[128];
int num_supplementary_groups = 128;
int n;
/* TODO: use some #define instead of hardcoding some random number like 128 */
if (! udisks_daemon_util_get_user_info (uid, &primary_gid, &user_name, &error))
{
udisks_warning ("%s", error->message);
g_error_free (error);
return FALSE;
}
if (primary_gid == gid)
{
g_free (user_name);
return TRUE;
}
if (getgrouplist (user_name, primary_gid, supplementary_groups, &num_supplementary_groups) < 0)
{
udisks_warning ("Error getting supplementary groups for uid %u: %m", uid);
g_free (user_name);
return FALSE;
}
g_free (user_name);
for (n = 0; n < num_supplementary_groups; n++)
{
if (supplementary_groups[n] == gid)
return TRUE;
}
return FALSE;
}
/* extracts option names from @allow that carry the @arg string as an argument */
static gchar **
extract_opts_with_arg (gchar **allow,
const gchar *arg)
{
GPtrArray *opts;
if (allow == NULL)
return NULL;
opts = g_ptr_array_new ();
for (; *allow; allow++)
{
gchar *eq;
eq = g_strrstr (*allow, arg);
if (eq && eq != *allow && *(eq - 1) == '=')
g_ptr_array_add (opts, g_strndup (*allow, eq - *allow - 1));
}
g_ptr_array_add (opts, NULL);
return (gchar **) g_ptr_array_free (opts, FALSE);
}
#define VARIANT_NULL_STRING "\1"
static gboolean
is_mount_option_allowed (const FSMountOptions *fsmo,
const gchar * const *allow_uid_self,
const gchar * const *allow_gid_self,
const gchar *option,
const gchar *value,
uid_t caller_uid)
{
gchar *endp;
uid_t uid;
gid_t gid;
gchar *s;
/* match the exact option=value string within allowed options */
if (fsmo && fsmo->allow && value && strlen (value) > 0)
{
s = g_strdup_printf ("%s=%s", option, value);
if (g_strv_contains ((const gchar * const *) fsmo->allow, s))
{
g_free (s);
/* not checking whether the option is in UID/GID_self as
* this is what was explicitly allowed by sysadmin (overrides) */
return TRUE;
}
g_free (s);
}
/* .. then check for mount options where the caller is allowed to pass
* in his own uid
*/
if (fsmo && allow_uid_self && g_strv_contains (allow_uid_self, option))
{
if (value == NULL || strlen (value) == 0)
{
udisks_warning ("is_mount_option_allowed: option '%s' is listed within allow_uid_self but has no value",
option);
return FALSE;
}
uid = strtol (value, &endp, 10);
if (*endp != '\0')
/* malformed value string */
return FALSE;
return (uid == caller_uid);
}
/* .. ditto for gid
*/
if (fsmo && allow_gid_self && g_strv_contains (allow_gid_self, option))
{
if (value == NULL || strlen (value) == 0)
{
udisks_warning ("is_mount_option_allowed: option '%s' is listed within allow_gid_self but has no value",
option);
return FALSE;
}
gid = strtol (value, &endp, 10);
if (*endp != '\0')
/* malformed value string */
return FALSE;
return is_uid_in_gid (caller_uid, gid);
}
/* the above UID/GID checks also assure that none of the options
* would be checked again against the _allow array */
/* match within allowed mount options */
if (fsmo && fsmo->allow)
{
/* simple 'option' match */
if (g_strv_contains ((const gchar * const *) fsmo->allow, option))
return TRUE;
}
if (g_str_has_prefix (option, "x-"))
{
return TRUE;
}
return FALSE;
}
static GVariant *
prepend_default_mount_options (const FSMountOptions *fsmo,
const gchar * const *allow_uid_self,
const gchar * const *allow_gid_self,
uid_t caller_uid,
GVariant *given_options,
gboolean shared_fs)
{
GVariantBuilder builder;
gint n;
gchar *s;
gid_t gid;
const gchar *option_string;