blib/lib/MYDan/Util/Secure.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 148 CC 20 SCAR 32.6
CC
20
Cov
0%
pull
72.0
add
20.0
push
20.0
CRAP 25.9
1 package MYDan::Util::Secure;
2 use strict;
3 use warnings;
4 use Carp;
5 use Tie::File;
6 use Digest::MD5;
7  
8 $|++;
9  
10 sub new
11 {
12 my ( $class, %self ) = @_;
13 die "path undef\n" unless $self{path};
14 bless \%self, ref $class || $class;
15 }
16  
17 sub pull
18 {
19 my $this = shift;
20 my $path = $this->{path};
21  
22 if( ! -d "$path/secure" )
23 {
24 print "Enter a git project address or GitHub user name:";
25 my $name = <STDIN>;
26 chomp $name;
27 die "input error.\n" unless length $name;
28 my $git = $name =~ /^[a-zA-Z0-9_\.-]+$/ ? "https://github.com/$name/secure" : $name;
29 print "clone $git ...\n";
30 die "die\n" if system "cd $path && git clone $git";
31 }
32 else
33 {
34 die "clone fail:$!.\n" if system "cd $path/secure && git reset --hard HEAD && git clean -f -d && git pull";
35 }
36  
37 return $this unless -f "$path/secure/data";
38  
39 my $pass = _password(0);
40 die "untar fail:$!\n" if system "cd $path/secure && openssl des3 -d -k '$pass' -salt -in data | tar xzf - -C /";
41 return $this;
42 }
43  
44 sub add
45 {
46 my ( $this, $file ) = @_;
47 die "file format error.\n" unless $file =~ /^\//;
48  
49 die "tie list fail: $!!" unless tie my @list, 'Tie::File', "$this->{path}/secure/list";
50 unless(grep{$_ eq $file }@list) { push @list, $file; }
51 untie @list;
52  
53 return $this;
54 }
55  
56 sub del
57 {
58 my ( $this, $file ) = @_;
59  
60 die "tie list fail: $!!" unless tie my @list, 'Tie::File', "$this->{path}/secure/list";
61 @list = grep{ $_ ne $file }@list;
62 untie @list;
63  
64 return $this;
65 }
66  
67 sub show
68 {
69 my $this = shift;
70  
71 die "tie list fail: $!!" unless tie my @list, 'Tie::File', "$this->{path}/secure/list";
72 map{ printf "$_: %s\n", -f $_ ? "file" : "nofile"; }@list;
73 untie @list;
74  
75 return $this;
76 }
77  
78 sub push
79 {
80 my $this = shift;
81 my $path = $this->{path};
82  
83 die "nofind $path/secure\n" unless( -d "$path/secure" );
84  
85 my $pass = _password(1);
86
87 die "tar fail:$!\n" if system "cd $path/secure && tar -czv -T list |openssl des3 -salt -k '$pass' -out data";
88 die "push fail:$!\n" if system "cd $path/secure && git add * && git commit -m update && git push";
89  
90 return $this;
91 }
92  
93 sub _password
94 {
95 my $x = shift;
96 while(1)
97 {
98 my ( $x1, $x2 ) = map{ __password( $_ ) } 0 .. $x;
99 return Digest::MD5->new()->add( $x1 )->hexdigest() if ( !$x || ( $x1 eq $x2 ) ) && length $x1;
100 }
101 }
102  
103 sub __password
104 {
105 my $c = shift;
106 printf "%s secure password:", $c ? 'Retype' : 'Enter';
107 system "stty -echo";
108 my $x = <STDIN>;
109 system "stty echo";
110 print "\n";
111 return $x;
112 }
113  
114 1;
115 __END__