summaryrefslogtreecommitdiffstats
path: root/perl-install/fs/remote/davfs.pm
blob: a3595582787999e0d9c0a304c18f474128fba979 (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
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}, $_->{comment}) . "\n" } @$credentials);
}


sub read_credentials_raw {
    from_double_quoted(cat_(secrets_file()));
}

sub read_credentials {
    my ($mntpoint) = @_;
    find { $mntpoint eq $_->{mntpoint} } read_credentials_raw();
}

# Comments are indicated by a '#' character and the rest of the line
# is ignored. Empty lines are ignored too.
#
# Each line consists of two or three items separated by spaces or tabs.
# If an item contains one of the characters space, tab, #, \ or ", this
# character must be escaped by a preceding \. Alternatively, the item
# may be enclosed in double quotes.

sub from_double_quoted {
    my ($file) = @_;
    my @l;
    my @lines = split("\n",$file);
    foreach (@lines){
	my ($mnt, $user, $pass, $comment); 
	if (/^\s*(#.*)?$/) {
	    $comment = $1;
	} else {
            if(/^(?:"((?:\\.|[^"])*)"|((?:\\.|[^"\s#])+))\s+(?:"((?:\\.|[^"])*)"|((?:\\.|[^"\s#])+))(?:\s+(?:"((?:\\.|[^"])*)"|((?:\\.|[^"\s#])+)))?(?:\s*(#.*))?$/) {
	            $mnt = "$1$2";
		    $mnt =~ s/\\(.)/$1/g;
		    $user = "$3$4";
	            $user =~ s/\\(.)/$1/g;
		    $pass = "$5$6";
	            $pass =~ s/\\(.)/$1/g;
		    $comment=$7;
	    } else {
		    die "bad entry $_";
	    }
        }
        push @l, {'mntpoint'=>$mnt, 'username'=>$user, 'password'=>$pass, 'comment'=>$comment};
    }
    @l;
}

sub to_double_quoted {
    my (@l) = @_;
    join(' ', map {
	s/(["\\])/\\$1/g;
	/\s/ ? qq("$_") : $_;
    } @l);
}

1;