From a0f2da8ac9ffe8c53b7935d4ad7ff3bf3d14d6ba Mon Sep 17 00:00:00 2001 From: Pascal Rigaux Date: Thu, 6 Nov 2008 08:18:51 +0000 Subject: - diskdrake o --dav: handle davfs2 credentials in /etc/davfs2/secrets (#44190) --- perl-install/NEWS | 3 ++ perl-install/fs.pm | 26 ++++++++++++---- perl-install/fs/remote/davfs.pm | 68 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 perl-install/fs/remote/davfs.pm diff --git a/perl-install/NEWS b/perl-install/NEWS index d79871833..a0f702bf3 100644 --- a/perl-install/NEWS +++ b/perl-install/NEWS @@ -1,3 +1,6 @@ +- diskdrake + o --dav: handle davfs2 credentials in /etc/davfs2/secrets (#44190) + Version 11.70 - 16 October 2008 - handle new driver: diff --git a/perl-install/fs.pm b/perl-install/fs.pm index 1969d8f05..4fc6fa7a2 100644 --- a/perl-install/fs.pm +++ b/perl-install/fs.pm @@ -102,6 +102,13 @@ sub read_fstab { $options->{"$_="} = $credentials->{$_} foreach qw(username password domain); fs::mount_options::pack($h, $options, $unknown); } + } elsif ($h->{fs_type} eq 'davfs2' && !member('verbatim_credentials', @reading_options)) { + require fs::remote::davfs; + if (my $credentials = fs::remote::davfs::read_credentials($h->{mntpoint})) { + my ($options, $unknown) = fs::mount_options::unpack($h); + $options->{"$_="} = $credentials->{$_} foreach qw(username password); + fs::mount_options::pack($h, $options, $unknown); + } } $h; @@ -206,11 +213,11 @@ sub get_info_from_fstab { } sub prepare_write_fstab { - my ($fstab, $o_prefix, $b_keep_smb_credentials) = @_; + my ($fstab, $o_prefix, $b_keep_credentials) = @_; $o_prefix ||= ''; my %new; - my @smb_credentials; + my (@smb_credentials, @davfs_credentials); my @l = map { my $device = isLoopback($_) ? @@ -239,12 +246,18 @@ sub prepare_write_fstab { my $options = $_->{options} || 'defaults'; - if ($_->{fs_type} eq 'smbfs' && $options =~ /password=/ && !$b_keep_smb_credentials) { + if ($_->{fs_type} eq 'smbfs' && $options =~ /password=/ && !$b_keep_credentials) { require fs::remote::smb; if (my ($opts, $smb_credentials) = fs::remote::smb::fstab_entry_to_credentials($_)) { $options = $opts; push @smb_credentials, $smb_credentials; } + } elsif ($_->{fs_type} eq 'davfs2' && $options =~ /password=/ && !$b_keep_credentials) { + require fs::remote::davfs; + if (my ($opts, $davfs_credentials) = fs::remote::davfs::fstab_entry_to_credentials($_)) { + $options = $opts || 'defaults'; + push @davfs_credentials, $davfs_credentials; + } } my $fs_type = $_->{fs_type} || 'auto'; @@ -276,13 +289,13 @@ sub prepare_write_fstab { } @l = sort_it(@l); - join('', map { $_->[2] } @l), \@smb_credentials; + join('', map { $_->[2] } @l), \@smb_credentials, \@davfs_credentials; } sub fstab_to_string { my ($all_hds, $o_prefix) = @_; my $fstab = [ fs::get::really_all_fstab($all_hds), @{$all_hds->{special}} ]; - my ($s, undef) = prepare_write_fstab($fstab, $o_prefix, 'keep_smb_credentials'); + my ($s, undef) = prepare_write_fstab($fstab, $o_prefix, 'keep_credentials'); $s; } @@ -290,10 +303,11 @@ sub write_fstab { my ($all_hds, $o_prefix) = @_; log::l("writing $o_prefix/etc/fstab"); my $fstab = [ fs::get::really_all_fstab($all_hds), @{$all_hds->{special}} ]; - my ($s, $smb_credentials) = prepare_write_fstab($fstab, $o_prefix, ''); + my ($s, $smb_credentials, $davfs_credentials) = prepare_write_fstab($fstab, $o_prefix, ''); renamef("$o_prefix/etc/fstab", "$o_prefix/etc/fstab.old"); output("$o_prefix/etc/fstab", $s); fs::remote::smb::save_credentials($_) foreach @$smb_credentials; + fs::remote::davfs::save_credentials($davfs_credentials); fs::dmcrypt::save_crypttab($all_hds) if @{$all_hds->{dmcrypts}}; } diff --git a/perl-install/fs/remote/davfs.pm b/perl-install/fs/remote/davfs.pm new file mode 100644 index 000000000..ce708f0c0 --- /dev/null +++ b/perl-install/fs/remote/davfs.pm @@ -0,0 +1,68 @@ +package fs::remote::davfs; # $Id: smb.pm 231184 2007-10-24 14:36:29Z pixel $ + +use strict; +use diagnostics; + +use common; +use fs::mount_options; + +sub secrets_file { "$::prefix/etc/davfs2/secrets" } + +sub fstab_entry_to_credentials { + my ($part) = @_; + + my ($options, $unknown) = fs::mount_options::unpack($part); + $options->{'username='} && $options->{'password='} or return; + my %h = map { $_ => delete $options->{"$_="} } qw(username password); + $h{mntpoint} = $part->{mntpoint} or return; + fs::mount_options::pack_($part, $options, $unknown), \%h; +} + +sub save_credentials { + my ($credentials) = @_; + @$credentials or return; + + output_with_perm(secrets_file(), 0600, + map { to_double_quoted($_->{mntpoint}, $_->{username}, $_->{password}) . "\n" } @$credentials); +} + + +sub read_credentials_raw { + my ($file) = @_; + map { + my %h; + @h{'mntpoint', 'username', 'password'} = from_double_quoted($_); + \%h; + } cat_(secrets_file()); +} + +sub read_credentials { + my ($mntpoint) = @_; + find { $mntpoint eq $_->{mntpoint} } read_credentials_raw(); +} + +sub from_double_quoted { + my ($s) = @_; + my @l; + while (1) { + (my $e1, my $e2, $s) = + $s =~ /^( "((?:\\.|[^"])*)" | (?:\\.|[^"\s])+ ) (.*)$/x or die "bad entry $_[0]\n"; + my $entry = defined $e2 ? $e2 : $e1; + $entry =~ s/\\(.)/$1/g; + push @l, $entry; + last if $s eq ''; + $s =~ s/^\s+// or die "bad entry $_[0]\n"; + last if $s eq ''; + } + @l; +} + +sub to_double_quoted { + my (@l) = @_; + join(' ', map { + s/(["\\])/\\$1/g; + /\s/ ? qq("$_") : $_; + } @l); +} + +1; -- cgit v1.2.1