summaryrefslogtreecommitdiff
path: root/tools/hal-storage-mount.c
blob: 32d68e57fc666a5263052ff01e42a9b5e568c124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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
/***************************************************************************
 * CVSID: $Id$
 *
 * hal-storage-mount.c : Mount wrapper
 *
 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
 *
 * 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
 *
 **************************************************************************/


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#ifdef __FreeBSD__
#include <fstab.h>
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <limits.h>
#include <pwd.h>
#elif sun
#include <sys/mnttab.h>
#include <sys/vfstab.h>
#else
#include <mntent.h>
#endif
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <syslog.h>

#include <libhal.h>
#include <libhal-storage.h>

#include "hal-storage-shared.h"

#ifdef __FreeBSD__
#define MOUNT		"/sbin/mount"
#define MOUNT_OPTIONS	"noexec,nosuid"
#define MOUNT_TYPE_OPT	"-t"
#elif sun
#define MOUNT		"/sbin/mount"
#define MOUNT_OPTIONS	"noexec,nosuid"
#define MOUNT_TYPE_OPT	"-F"
#else
#define MOUNT		"/bin/mount"
#define MOUNT_OPTIONS	"nosuid,nodev"
#define MOUNT_TYPE_OPT	"-t"
#endif

static void
usage (void)
{
	fprintf (stderr, "This program should only be started by hald.\n");
	exit (1);
}

static void
permission_denied_volume_ignore (const char *device)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Volume.PermissionDenied\n");
	fprintf (stderr, "Device has %s volume.ignore set to TRUE. Refusing to mount.\n", device);
	exit (1);
}

static void
permission_denied_etc_fstab (const char *device)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Volume.PermissionDenied\n");
	fprintf (stderr, "Device %s is listed in /etc/fstab. Refusing to mount.\n", device);
	exit (1);
}

static void
already_mounted (const char *device)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Volume.AlreadyMounted\n");
	fprintf (stderr, "Device %s is already mounted.\n", device);
	exit (1);
}

static void
invalid_mount_option (const char *option, const char *uid)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Volume.InvalidMountOption\n");
	fprintf (stderr, "The option '%s' is not allowed for uid=%s\n", option, uid);
	exit (1);
}

static void
unknown_filesystem (const char *filesystem)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Volume.UnknownFilesystemType\n");
	fprintf (stderr, "Unknown file system '%s'\n", filesystem);
	exit (1);
}

static void
invalid_mount_point (const char *mount_point)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Volume.InvalidMountpoint\n");
	fprintf (stderr, "The mount point '%s' is invalid\n", mount_point);
	exit (1);
}

static void
mount_point_not_available (const char *mount_point)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Volume.MountPointNotAvailable\n");
	fprintf (stderr, "The mount point '%s' is already occupied\n", mount_point);
	exit (1);
}


static void
cannot_remount (const char *device)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Volume.CannotRemount\n");
	fprintf (stderr, "%s not mounted already\n", device);
	exit (1);
}

#ifdef HAVE_POLKIT
static void
permission_denied_action (const char *action, const char *result)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.PermissionDeniedByPolicy\n");
	fprintf (stderr, "%s %s <-- (action, result)\n", action, result);
	exit (1);
}
#endif

/* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
static void
canonicalize_filename (gchar *filename)
{
	gchar *p, *q;
	gboolean last_was_slash = FALSE;
	
	p = filename;
	q = filename;
	
	while (*p)
	{
		if (*p == G_DIR_SEPARATOR)
		{
			if (!last_was_slash)
				*q++ = G_DIR_SEPARATOR;
			
			last_was_slash = TRUE;
		}
		else
		{
			if (last_was_slash && *p == '.')
			{
				if (*(p + 1) == G_DIR_SEPARATOR ||
				    *(p + 1) == '\0')
				{
					if (*(p + 1) == '\0')
						break;
					
					p += 1;
				}
				else if (*(p + 1) == '.' &&
					 (*(p + 2) == G_DIR_SEPARATOR ||
					  *(p + 2) == '\0'))
				{
					if (q > filename + 1)
					{
						q--;
						while (q > filename + 1 &&
						       *(q - 1) != G_DIR_SEPARATOR)
							q--;
					}
					
					if (*(p + 2) == '\0')
						break;
					
					p += 2;
				}
				else
				{
					*q++ = *p;
					last_was_slash = FALSE;
				}
			}
			else
			{
				*q++ = *p;
				last_was_slash = FALSE;
			}
		}
		
		p++;
	}
	
	if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
		q--;
	
	*q = '\0';
}

static char *
resolve_symlink (const char *file)
{
	GError *error;
	char *dir;
	char *link;
	char *f;
	char *f1;

	f = g_strdup (file);

	while (g_file_test (f, G_FILE_TEST_IS_SYMLINK)) {
		link = g_file_read_link (f, &error);
		if (link == NULL) {
			g_warning ("Cannot resolve symlink %s: %s", f, error->message);
			g_error_free (error);
			g_free (f);
			f = NULL;
			goto out;
		}
		
		dir = g_path_get_dirname (f);
		f1 = g_strdup_printf ("%s/%s", dir, link);
		g_free (dir);
		g_free (link);
		g_free (f);
		f = f1;
	}

out:
	if (f != NULL)
		canonicalize_filename (f);
	return f;
}

static LibHalVolume *
volume_findby (LibHalContext *hal_ctx, const char *property, const char *value)
{
	int i;
	char **hal_udis;
	int num_hal_udis;
	LibHalVolume *result = NULL;
	char *found_udi = NULL;
	DBusError error;

	dbus_error_init (&error);
	if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, property, 
								 value, &num_hal_udis, &error)) == NULL) {
		if (dbus_error_is_set (&error)) {
			dbus_error_free (&error);
		}
		goto out;
	}
	for (i = 0; i < num_hal_udis; i++) {
		char *udi;
		udi = hal_udis[i];
		if (libhal_device_query_capability (hal_ctx, udi, "volume", &error)) {
			found_udi = strdup (udi);
			break;
		}
	}

	libhal_free_string_array (hal_udis);

	if (found_udi != NULL)
		result = libhal_volume_from_udi (hal_ctx, found_udi);

	free (found_udi);
out:
	return result;
}

static void
bailout_if_in_fstab (LibHalContext *hal_ctx, const char *device, const char *label, const char *uuid)
{
	gpointer handle;
	char *entry;
	char *_mount_point;

	printf (" label '%s'  uuid '%s'\n", label ? label : "" , uuid ? uuid : "");

	/* check if /etc/fstab mentions this device... (with symlinks etc) */
	if (! fstab_open (&handle)) {
		printf ("cannot open /etc/fstab\n");
		unknown_error ("Cannot open /etc/fstab");		
	}
	while ((entry = fstab_next (handle, &_mount_point)) != NULL) {
		char *resolved;

#ifdef DEBUG
		printf ("Looking at /etc/fstab entry '%s'\n", entry);
#endif
		if (label != NULL && g_str_has_prefix (entry, "LABEL=")) {
			if (strcmp (entry + 6, label) == 0) {
				gboolean skip_fstab_entry;

				skip_fstab_entry = FALSE;

				/* (heck, we also do the stuff below in gnome-mount) */

				/* OK, so what's if someone attaches an external disk with the label '/' and
				 * /etc/fstab has
				 *
				 *    LABEL=/    /    ext3    defaults    1 1
				 *
				 * in /etc/fstab as most Red Hat systems do? Bugger, this is a very common use
				 * case; suppose that you take the disk from your Fedora server and attaches it
				 * to your laptop. Bingo, you now have two disks with the label '/'. One must
				 * seriously wonder if using things like LABEL=/ for / is a good idea; just
				 * what happens if you boot in this configuration? (answer: the initrd gets
				 * it wrong most of the time.. sigh)
				 *
				 * To work around this, check if the listed entry in /etc/fstab is already mounted,
				 * if it is, then check if it's the same device_file as the given one...
				 */

				/* see if a volume is mounted at this mount point  */
				if (_mount_point != NULL) {
					LibHalVolume *mounted_vol;

					mounted_vol = volume_findby (hal_ctx, "volume.mount_point", _mount_point);
					if (mounted_vol != NULL) {
						const char *mounted_vol_device_file;

						mounted_vol_device_file = libhal_volume_get_device_file (mounted_vol);
						/* no need to resolve symlinks, hal uses the canonical device file */
						if (mounted_vol_device_file != NULL &&
						    strcmp (mounted_vol_device_file, device) !=0) {
#ifdef DEBUG
							printf ("Wanting to mount %s that has label %s, but /etc/fstab says LABEL=%s is to be mounted at mount point '%s'. However %s (that also has label %s), is already mounted at said mount point. So, skipping said /etc/fstab entry.\n", 
								   device, label, label, _mount_point, mounted_vol_device_file, _mount_point);
#endif
							skip_fstab_entry = TRUE;
						}
						libhal_volume_free (mounted_vol);
					}
				}
				
				if (!skip_fstab_entry) {
					printf ("%s found in /etc/fstab. Not mounting.\n", entry);
					permission_denied_etc_fstab (device);
				}
			}
		} else if (uuid != NULL && g_str_has_prefix (entry, "UUID=")) {
			if (strcmp (entry + 5, uuid) == 0) {
				printf ("%s found in /etc/fstab. Not mounting.\n", entry);
				permission_denied_etc_fstab (device);
			}
		} else {

			resolved = resolve_symlink (entry);
#ifdef DEBUG
			printf ("/etc/fstab: device %s -> %s \n", entry, resolved);
#endif
			if (strcmp (device, resolved) == 0) {
				printf ("%s (-> %s) found in /etc/fstab. Not mounting.\n", entry, resolved);
				permission_denied_etc_fstab (device);
			}

			g_free (resolved);
		}
	}
	fstab_close (handle);
}

static gboolean
device_is_mounted (const char *device, char **mount_point)
{
	gpointer handle;
	char *entry;
	gboolean ret;

	ret = FALSE;

	/* check if /proc/mounts mentions this device... (with symlinks etc) */
	if (! mtab_open (&handle)) {
		printf ("cannot open mount list\n");
		unknown_error ("Cannot open /etc/mtab or equivalent");		
	}
	while (((entry = mtab_next (handle, mount_point)) != NULL) && (ret == FALSE)) {
		char *resolved;

		resolved = resolve_symlink (entry);
#ifdef DEBUG
		printf ("/proc/mounts: device %s -> %s \n", entry, resolved);
#endif
		if (strcmp (device, resolved) == 0) {
			printf ("%s (-> %s) found in mount list. Not mounting.\n", entry, resolved);
			ret = TRUE;
		}

		g_free (resolved);
	}
	mtab_close (handle);
	return ret;
}

/* maps volume_id fs types to the appropriate -t mount option */
static const char *
map_fstype (const char *fstype)
{
#ifdef __FreeBSD__
	if (! strcmp (fstype, "iso9660"))
		return "cd9660";
	else if (! strcmp (fstype, "ext2"))
		return "ext2fs";
	else if (! strcmp (fstype, "vfat"))
		return "msdosfs";
#elif sun
	if (! strcmp (fstype, "iso9660"))
		return "hsfs";
	else if (! strcmp (fstype, "vfat"))
		return "pcfs";
#endif

	return fstype;
}

static void
handle_mount (LibHalContext *hal_ctx, 
	      const char *udi,
	      LibHalVolume *volume, LibHalDrive *drive, const char *device, 
	      const char *invoked_by_uid, const char *invoked_by_syscon_name)
{
	int i, j;
	DBusError error;
	char mount_point[256];
	char mount_fstype[256];
	char mount_options[1024];
	char **allowed_options;
	char **given_options;
	gboolean wants_to_change_uid;
	char *mount_dir;
	GError *err = NULL;
	char *sout = NULL;
	char *serr = NULL;
	int exit_status;
	char *args[10];
	int na;
	GString *mount_option_str;
	gboolean pol_is_fixed;
	gboolean pol_change_uid;
	char *action;
	gboolean is_remount;
	gboolean explicit_mount_point_given;
	const char *end;
#ifdef __FreeBSD__
	struct passwd *pw;
	uid_t calling_uid;
	gid_t calling_gid;
#endif
	const char *label;
	const char *uuid;

#ifdef DEBUG
	printf ("device                           = %s\n", device);
	printf ("invoked by uid                   = %s\n", invoked_by_uid);
	printf ("invoked by system bus connection = %s\n", invoked_by_syscon_name);
#endif

	if (volume != NULL) {		
		dbus_error_init (&error);
		if (libhal_device_get_property_bool (hal_ctx, udi, "volume.ignore", &error) || 
		    dbus_error_is_set (&error)) {
			if (dbus_error_is_set (&error)) {
				dbus_error_free (&error);
			}
			permission_denied_volume_ignore (device);
		}

		label = libhal_volume_get_label (volume);
		uuid = libhal_volume_get_uuid (volume);
	} else {
		label = NULL;
		uuid = NULL;
	}

	bailout_if_in_fstab (hal_ctx, device, label, uuid);

        bailout_if_drive_is_locked (hal_ctx, drive, invoked_by_syscon_name);

	/* TODO: sanity check that what hal exports is correct (cf. Martin Pitt's email) */

	/* read from stdin */
	if (strlen (fgets (mount_point, sizeof (mount_point), stdin)) > 0)
		mount_point   [strlen (mount_point)   - 1] = '\0';
	if (strlen (fgets (mount_fstype, sizeof (mount_fstype), stdin)) > 0)
		mount_fstype  [strlen (mount_fstype)  - 1] = '\0';
	if (strlen (fgets (mount_options, sizeof (mount_options), stdin)) > 0)
		mount_options [strlen (mount_options) - 1] = '\0';
	/* validate that input from stdin is UTF-8 */
	if (!g_utf8_validate (mount_point, -1, &end))
		unknown_error ("Error validating mount_point as UTF-8");
	if (!g_utf8_validate (mount_fstype, -1, &end))
		unknown_error ("Error validating mount_fstype as UTF-8");
	if (!g_utf8_validate (mount_options, -1, &end))
		unknown_error ("Error validating mount_options as UTF-8");

	for (i = 0; mount_point[i] != '\0'; i++) {
		if (mount_point[i] == '\n' ||
		    mount_point[i] == G_DIR_SEPARATOR) {
			unknown_error ("mount_point cannot contain the following characters: newline, G_DIR_SEPARATOR (usually /)");
		}
	}

#ifdef DEBUG
	printf ("mount_point    = '%s'\n", mount_point);
	printf ("mount_fstype   = '%s'\n", mount_fstype);
	printf ("mount_options  = '%s'\n", mount_options);
#endif

	/* delete any trailing whitespace options from splitting the string */
	given_options = g_strsplit (mount_options, "\t", 0);
	for (i = g_strv_length (given_options) - 1; i >= 0; --i) {
		if (strlen (given_options[i]) > 0)
			break;
		given_options[i] = NULL;
	}

	/* is option 'remount' included? */
	is_remount = FALSE;
	for (i = 0; i < (int) g_strv_length (given_options); i++) {
		if (strcmp (given_options[i], "remount") == 0) {
			is_remount = TRUE;
		}
	}

	mount_dir = NULL;
	if (is_remount) {
		if (volume != NULL) {
			if (!libhal_volume_is_mounted (volume)) {
				cannot_remount (device);
			}
			mount_dir = g_strdup (libhal_volume_get_mount_point (volume));
		} else {
			if (!device_is_mounted (device, &mount_dir)) {
				cannot_remount (device);
			}
		}

		if (mount_dir == NULL) {
			unknown_error ("Cannot get mount_dir for remount even though volume is mounted!");
		}

	} else {
		if (volume != NULL) {
			if (libhal_volume_is_mounted (volume)) {
				already_mounted (device);
			}
		} else {
			if (device_is_mounted (device, NULL)) {
				already_mounted (device);
			}
		}
	}

	if (!is_remount) {
		/* figure out mount point if no mount point is given... */
		explicit_mount_point_given = FALSE;
		if (strlen (mount_point) == 0) {
			char *p;
			const char *label;
			
			if (volume != NULL)
				label = libhal_volume_get_label (volume);
			else
				label = NULL;
			
			if (label != NULL) {
				/* best - use label */
				g_strlcpy (mount_point, label, sizeof (mount_point));
				
				/* TODO: use drive type */
				
			} else {
				/* fallback - use "disk" */
				g_snprintf (mount_point, sizeof (mount_point), "%s", "disk");
			}
			
			/* sanitize computed mount point name, e.g. replace invalid chars with '-' */
			p = mount_point;
			while (TRUE) {
				p = g_utf8_strchr (mount_point, -1, G_DIR_SEPARATOR);
				if (p == NULL)
					break;
				*p = '-';
			};
			
		} else {
			explicit_mount_point_given = TRUE;
		}

		/* check mount point name - only forbid separators */
		if (g_utf8_strchr (mount_point, -1, G_DIR_SEPARATOR) != NULL) {
			printf ("'%s' is an invalid mount point\n", mount_point);
			invalid_mount_point (mount_point);
		}
		
		/* check if mount point is available - append number to mount point */
		i = 0;
		mount_dir = NULL;
		while (TRUE) {
			g_free (mount_dir);
			if (i == 0)
				mount_dir = g_strdup_printf ("/media/%s", mount_point);
			else
				mount_dir = g_strdup_printf ("/media/%s-%d", mount_point, i);
			
#ifdef DEBUG
			printf ("trying dir %s\n", mount_dir);
#endif
			
			if (!g_file_test (mount_dir, G_FILE_TEST_EXISTS)) {
				break;
			}
			
			if (explicit_mount_point_given) {
				mount_point_not_available (mount_dir);
			}
			
			i++;
		}
	}

	dbus_error_init (&error);
	allowed_options = libhal_device_get_property_strlist (hal_ctx, udi, "volume.mount.valid_options", &error);
	if (dbus_error_is_set (&error)) {
		unknown_error ("Cannot get volume.mount.valid_options");
		dbus_error_free (&error);
	}

#ifdef DEBUG
	for (i = 0; given_options[i] != NULL; i++)
		printf ("given_options[%d] = '%s'\n", i, given_options[i]);
	for (i = 0; allowed_options[i] != NULL; i++)
		printf ("allowed_options[%d] = '%s'\n", i, allowed_options[i]);
#endif

	wants_to_change_uid = FALSE;

	/* check mount options */
	for (i = 0; given_options[i] != NULL; i++) {
		char *given = given_options[i];

		for (j = 0; allowed_options[j] != NULL; j++) {
			char *allow = allowed_options[j];
			int allow_len = strlen (allow);

			if (strcmp (given, allow) == 0) {
				goto option_ok;
			}

			if ((allow[allow_len - 1] == '=') && 
			    (strncmp (given, allow, allow_len) == 0) &&
			    (int) strlen (given) > allow_len) {

				/* option matched allowed ending in '=', e.g.
				 * given == "umask=foobar" and allowed == "umask="
				 */
				if (strcmp (allow, "uid=") == 0) {
					uid_t uid;
					char *endp;
					/* check for uid=, it requires special handling */
					uid = (uid_t) strtol (given + allow_len, &endp, 10);
					if (*endp != '\0') {
						printf ("'%s' is not a number?\n", given);
						unknown_error ("option uid is malformed");
					}
#ifdef DEBUG
					printf ("%s with uid %d\n", allow, uid);
#endif
					wants_to_change_uid = TRUE;

					goto option_ok;
				} else {

					goto option_ok;
				}
			}
		}

		/* apparently option was not ok */
		invalid_mount_option (given, invoked_by_uid);

	option_ok:
		;
	}

	/* Check privilege */
	pol_is_fixed = TRUE;
	if (libhal_drive_is_hotpluggable (drive) || libhal_drive_uses_removable_media (drive))
		pol_is_fixed = FALSE;

	pol_change_uid = FALSE;
	/* don't consider uid= on non-pollable drives for the purpose of policy 
	 * (since these drives normally use vfat)
	 */
	if (volume != NULL) {
		/* don't consider uid= on vfat, iso9660, udf change-uid for the purpose of policy
		 * (since these doesn't contain uid/gid bits) 
		 */
		if (strcmp (libhal_volume_get_fstype (volume), "vfat") != 0 &&
		    strcmp (libhal_volume_get_fstype (volume), "iso9660") != 0 &&
		    strcmp (libhal_volume_get_fstype (volume), "udf") != 0) {
			pol_change_uid = wants_to_change_uid;
		}
	}


	char *mount_option_commasep = NULL;
	char *mount_do_fstype = "auto";

	/* construct arguments to mount */
	na = 0;
	args[na++] = MOUNT;
	if (strlen (mount_fstype) > 0) {
		mount_do_fstype = (char *) map_fstype (mount_fstype);
	} else if (volume == NULL) {
		/* non-pollable drive; force auto */
		mount_do_fstype = "auto";
	} else if (libhal_volume_get_fstype (volume) != NULL && strlen (libhal_volume_get_fstype (volume)) > 0) {
		mount_do_fstype = (char *) map_fstype (libhal_volume_get_fstype (volume));
	}
	args[na++] = MOUNT_TYPE_OPT;
	args[na++] = mount_do_fstype;

	args[na++] = "-o";
#ifdef HAVE_UMOUNT_HAL
	mount_option_str = g_string_new (MOUNT_OPTIONS ",uhelper=hal");
#else
	mount_option_str = g_string_new (MOUNT_OPTIONS);
#endif
	for (i = 0; given_options[i] != NULL; i++) {
		g_string_append (mount_option_str, ",");
		g_string_append (mount_option_str, given_options[i]);
	}
	mount_option_commasep = g_string_free (mount_option_str, FALSE); /* leak! */
	args[na++] = mount_option_commasep;
	args[na++] = (char *) device;
	args[na++] = mount_dir;
	args[na++] = NULL;

	if (pol_is_fixed) {
		if (pol_change_uid) {
			action = "hal-storage-mount-fixed-extra-options";
		} else {
			action = "hal-storage-mount-fixed";
		}
	} else {
		if (pol_change_uid) {
			action = "hal-storage-mount-removable-extra-options"; /* TODO: rethink "extra-options" */
		} else {
			action = "hal-storage-mount-removable";
		}
	}

#ifdef DEBUG
	printf ("using action %s for uid %s, system_bus_connection %s\n", action, invoked_by_uid, 
		invoked_by_syscon_name);
#endif

#ifdef HAVE_POLKIT
        if (invoked_by_syscon_name != NULL) {
                char *polkit_result;
                char *action_params[] = {
                        "fstype", "",
                        "mount-point", "",
                        "mount-options", "",
                        NULL};

                action_params[1] = mount_do_fstype;
                action_params[3] = mount_dir;
                action_params[5] = mount_option_commasep;

                dbus_error_init (&error);
                polkit_result = libhal_device_is_caller_privileged (hal_ctx,
                                                                    udi,
                                                                    action,
                                                                    invoked_by_syscon_name,
                                                                    &error);
                if (polkit_result == NULL){
                        unknown_error ("IsCallerPrivileged() failed");
                }
                if (strcmp (polkit_result, "yes") != 0) {
                        permission_denied_action (action, polkit_result);
                }
                libhal_free_string (polkit_result);
        }
#endif

#ifdef DEBUG
	printf ("passed privilege\n");
#endif

	if (!is_remount) {
		/* create directory */
		if (g_mkdir (mount_dir, 0700) != 0) {
			printf ("Cannot create '%s'\n", mount_dir);
			unknown_error ("Cannot create mount directory");
		}
		
#ifdef __FreeBSD__
		calling_uid = (uid_t) strtol (invoked_by_uid, (char **) NULL, 10);
		pw = getpwuid (calling_uid);
		if (pw != NULL) {
			calling_gid = pw->pw_gid;
		} else {
			calling_gid = 0;
		}
		if (chown (mount_dir, calling_uid, calling_gid) != 0) {
			printf ("Cannot chown '%s' to uid: %d, gid: %d\n", mount_dir,
				calling_uid, calling_gid);
			g_rmdir (mount_dir);
			unknown_error ("Failed to chown mount directory");
		}
#endif
	}


	/* TODO FIXME XXX HACK: OK, so we should rewrite the options in /media/.hal-mtab .. 
	 *                      but it doesn't really matter much at this point */
	if (!is_remount) {
		FILE *hal_mtab;
		FILE *hal_mtab_orig;
		int hal_mtab_orig_len;
		int num_read;
		char *hal_mtab_buf;
		char *hal_mtab_buf_old;
		
		/* Maintain a list in /media/.hal-mtab with entries of the following format
		 *
		 *  <device_file>\t<uid>\t<session-id>\t<fstype>\t<options_sep_by_comma>\t<mount point>\n
		 *
		 * where session-id currently is unused and thus set to 0.
		 *
		 * Example:
		 *
		 *  /dev/sda2	500	0	hfsplus	noexec,nosuid,nodev	/media/Macintosh HD
		 *  /dev/sda4	500	0	ntfs	noexec,nosuid,nodev,umask=222	/media/Windows
		 *  /dev/sdb1	500	0	vfat	noexec,nosuid,nodev,shortname=winnt,uid=500	/media/davidz
		 */
		
		
		if (g_file_test ("/media/.hal-mtab", G_FILE_TEST_EXISTS)) {
			hal_mtab_orig = fopen ("/media/.hal-mtab", "r");
			if (hal_mtab_orig == NULL) {
				unknown_error ("Cannot open /media/.hal-mtab");
			}
			if (fseek (hal_mtab_orig, 0L, SEEK_END) != 0) {
				unknown_error ("Cannot seek to end of /media/.hal-mtab");
			}
			hal_mtab_orig_len = ftell (hal_mtab_orig);
			if (hal_mtab_orig_len < 0) {
				unknown_error ("Cannot determine size of /media/.hal-mtab");
			}
			rewind (hal_mtab_orig);
			hal_mtab_buf = g_new0 (char, hal_mtab_orig_len + 1);
			num_read = fread (hal_mtab_buf, 1, hal_mtab_orig_len, hal_mtab_orig);
			if (num_read != hal_mtab_orig_len) {
				unknown_error ("Cannot read from /media/.hal-mtab");
			}
			fclose (hal_mtab_orig);
		} else {
			hal_mtab_buf = g_strdup ("");
		}
		
#ifdef DEBUG
		printf ("%d: XYA creating /media/.hal-mtab~\n", getpid ());
#endif
		hal_mtab = fopen ("/media/.hal-mtab~", "w");
		if (hal_mtab == NULL) {
			unknown_error ("Cannot create /media/.hal-mtab~");
		}
		hal_mtab_buf_old = hal_mtab_buf;
		hal_mtab_buf = g_strdup_printf ("%s%s\t%s\t0\t%s\t%s\t%s\n", 
						hal_mtab_buf_old,
						device, invoked_by_uid, mount_do_fstype, 
						mount_option_commasep, mount_dir);
		g_free (hal_mtab_buf_old);
		if (hal_mtab_buf_old == NULL) {
			unknown_error ("Out of memory appending to /media/.hal-mtab~");
		}
		if (fwrite (hal_mtab_buf, 1, strlen (hal_mtab_buf), hal_mtab) != strlen (hal_mtab_buf)) {
			unknown_error ("Cannot write to /media/.hal-mtab~");
		}
		fclose (hal_mtab);
		g_free (hal_mtab_buf);
#ifdef DEBUG
		printf ("%d: XYA closing /media/.hal-mtab~\n", getpid ());
#endif
	} /* !is_remount */
		
	/* now try to mount */
	if (!g_spawn_sync ("/",
			   args,
			   NULL,
			   0,
			   NULL,
			   NULL,
			   &sout,
			   &serr,
			   &exit_status,
			   &err)) {
		printf ("Cannot execute %s\n", MOUNT);
		g_rmdir (mount_dir);
		unlink ("/media/.hal-mtab~");
		unknown_error ("Cannot spawn " MOUNT);
	}


	if (exit_status != 0) {
		char errstr[]  = "mount: unknown filesystem type";

		printf ("%s error %d, stdout='%s', stderr='%s'\n", MOUNT, exit_status, sout, serr);

		if (!is_remount) {
			g_rmdir (mount_dir);
			unlink ("/media/.hal-mtab~");
		}

		if (strncmp (errstr, serr, sizeof (errstr) - 1) == 0) {
			unknown_filesystem (strlen (mount_fstype) > 0 ? 
					    mount_fstype : 
					    (volume != NULL ? libhal_volume_get_fstype (volume) : "") );
		} else {
			int n;
			for (n = 0; serr[n] != '\0'; n++) {
				if (serr[n] == '\n') {
					serr[n] = ' ';
				}
			}
			unknown_error (serr);
		}
	}

	if (!is_remount) {
		if (rename ("/media/.hal-mtab~", "/media/.hal-mtab") != 0) {
			printf ("rename(2) failed, errno=%d -> '%s'\n", errno, strerror (errno));
			unlink ("/media/.hal-mtab~");
#ifdef DEBUG
			printf ("%d: XYA failed renaming /media/.hal-mtab~ to /media/.hal-mtab\n", getpid ());
#endif
			unknown_error ("Cannot rename /media/.hal-mtab~ to /media/.hal-mtab");
		}
#ifdef DEBUG
		printf ("%d: XYA done renaming /media/.hal-mtab~ to /media/.hal-mtab\n", getpid ());
#endif
	}

	openlog ("hald", 0, LOG_DAEMON);
	if (is_remount) {
		syslog (LOG_INFO, "remounted %s at '%s' on behalf of uid %s", device, mount_dir, invoked_by_uid);
	} else {
		syslog (LOG_INFO, "mounted %s on behalf of uid %s", device, invoked_by_uid);
	}
	closelog ();

	g_free (sout);
	g_free (serr);
	g_free (mount_dir);
	libhal_free_string_array (allowed_options);
	g_strfreev (given_options);
}


int
main (int argc, char *argv[])
{
	char *udi;
	char *device;
	LibHalVolume *volume;
	DBusError error;
	LibHalContext *hal_ctx = NULL;
	DBusConnection *system_bus = NULL;
	char *invoked_by_uid;
	char *invoked_by_syscon_name;

	if (!lock_hal_mtab ()) {
		unknown_error ("Cannot obtain lock on /media/.hal-mtab");
	}

	device = getenv ("HAL_PROP_BLOCK_DEVICE");
	if (device == NULL)
		usage ();

	udi = getenv ("HAL_PROP_INFO_UDI");
	if (udi == NULL)
		usage ();

	invoked_by_uid = getenv ("HAL_METHOD_INVOKED_BY_UID");

	invoked_by_syscon_name = getenv ("HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME");

	dbus_error_init (&error);
	if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) {
		printf ("Cannot connect to hald\n");
		if (dbus_error_is_set (&error)) {
			dbus_error_free (&error);
		}
		usage ();
	}

	dbus_error_init (&error);
	system_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
	if (system_bus == NULL) {
		printf ("Cannot connect to the system bus\n");
		if (dbus_error_is_set (&error)) {
			dbus_error_free (&error);
		}
		usage ();
	}

	volume = libhal_volume_from_udi (hal_ctx, udi);
	if (volume == NULL) {
		LibHalDrive *drive;

		drive = libhal_drive_from_udi (hal_ctx, udi);
		if (drive == NULL) {
			usage ();
		} else {
			handle_mount (hal_ctx, 
				      udi, NULL, drive, device, invoked_by_uid, 
				      invoked_by_syscon_name);
		}

	} else {
		const char *drive_udi;
		LibHalDrive *drive;

		drive_udi = libhal_volume_get_storage_device_udi (volume);
		
		if (drive_udi == NULL)
			unknown_error ("Cannot get drive_udi from volume");
		drive = libhal_drive_from_udi (hal_ctx, drive_udi);
		if (drive == NULL)
			unknown_error ("Cannot get drive from hal");
		
		handle_mount (hal_ctx, 
			      udi, volume, drive, device, invoked_by_uid, 
			      invoked_by_syscon_name);

	}

	unlock_hal_mtab ();

	return 0;
}