script/myproxy-init.pl untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 164 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 use VOMS::Lite::PEMHelper qw(readCert readAC readPrivateKey);
4 use VOMS::Lite::MyProxy;
5 require Term::ReadKey;
6 use strict;
7  
8 my $name=$0;
9 my $lname=length($name);
10 $name =~ s#.*/##g;
11 my $usage = "Usage: $name [username@]myproxy.server[:port]\n".
12 " " x ($lname+8). "[ -cert /path/to/cert.pem ]\n".
13 " " x ($lname+8). "[ -key /path/to/cert's/key.pem ]\n".
14 " " x ($lname+8). "[ -vomsAC /path/to/VOMS/AC ]\n".
15 " " x ($lname+8). "[ -lifetime N (hours, default 24) ]\n".
16 " " x ($lname+8). "[ -releaselifetime N (hours, default 9) ]\n".
17 " " x ($lname+8). "[ -pl N ]\n".
18 " " x ($lname+8). "[ -(old|new|rfc|limited) ]\n";
19  
20 my %Input;
21 my $HolderCert="$ENV{HOME}/.globus/usercert.pem";
22 my $HolderKey="$ENV{HOME}/.globus/userkey.pem";
23 my ($vomsattribfile,$pathlen,$lifetime,$releaselifetime,$outfile,$server,$port,$username);
24 if ( defined $ENV{"X509_USER_PROXY"} && $ENV{"X509_USER_PROXY"} =~ /(.*)/ ) { $outfile=$1; };
25  
26 while ($_=shift @ARGV) {
27 if ( /^--?cert$/ ) {
28 $HolderCert=shift @ARGV;
29 die "$& requires an argument" if ( ! defined $HolderCert );
30 die "cannot open certificate file $HolderCert" if ( ! -r $HolderCert );
31 }
32 elsif ( /^--?key$/ ) {
33 $HolderKey=shift @ARGV;
34 die "$& requires an argument" if ( ! defined $HolderKey );
35 die "cannot open certificate file $HolderKey" if ( ! -r $HolderKey );
36 }
37 elsif ( /^--?vomsAC$/ ) {
38 $vomsattribfile=shift @ARGV;
39 die "$& requires an argument" if ( ! defined $vomsattribfile );
40 die "cannot open certificate file $vomsattribfile" if ( ! -r $vomsattribfile );
41 }
42 elsif ( /^--?limited$/ ) { $Input{'Type'}="Limited"; }
43 elsif ( /^--?(new|gt3)$/ ) { $Input{'Type'}="Pre-RFC"; }
44 elsif ( /^--?rfc$/ ) { $Input{'Type'}="RFC"; }
45 elsif ( /^--?(old|legacy)$/ ) { $Input{'Type'}="Legacy"; }
46 elsif ( /^--?(pl|pathlength)$/ ) {
47 $pathlen=shift @ARGV;
48 die "$& requires an argument" if ( ! defined $pathlen );
49 die "Bad Pathlength argument, $& requires a positive integer" if ( $pathlen !~ /^[0-9]+$/ );
50 }
51 elsif ( /^(--?lifetime)$/ ) {
52 $lifetime=shift @ARGV;
53 die "$& requires an argument" if ( ! defined $lifetime );
54 die "$& requires a positive numeric argument $&." if ( $lifetime !~ /^[0-9]*(?:\.[0-9]+)?$/ );
55 $lifetime*=3600;
56 $lifetime=int($lifetime);
57 }
58 elsif ( /^--?releaselifetime$/ ) {
59 $releaselifetime=shift @ARGV;
60 die "$& requires an argument" if ( ! defined $releaselifetime );
61 die "$& requires a positive numeric argument." if ( $releaselifetime !~ /^[0-9]+(?:\.[0-9]+)?$/ );
62 $releaselifetime*=3600;
63 $releaselifetime=int($releaselifetime);
64 }
65 elsif ( /^([\/a-zA-Z0-9_ =.-]+)@([a-zA-Z0-9_-][a-zA-Z0-9_.-]+[a-zA-Z0-9_-])(?::([0-9]{1,5}))?$/ ) {
66 $username=$1;
67 $server=$2;
68 $port=$3;
69 }
70 elsif ( /^([a-zA-Z0-9_][a-zA-Z0-9_.-]+[a-zA-Z0-9_])(?::([0-9]{1,5}))?$/ ) {
71 $server=$1;
72 $port=$2;
73 }
74 else { die "Unrecognised option \"$_\"\n$usage"; }
75 }
76  
77 my @decodedCERTS=readCert($HolderCert);
78 $Input{'Server'}=$server;
79 $Input{'Username'}=$username;
80 $Input{'Port'}=$port;
81 $Input{'Cert'}=$decodedCERTS[0];
82 $Input{'Key'}=readPrivateKey($HolderKey);
83 $Input{'Lifetime'}=$lifetime;
84 $Input{'ReleaseLifetime'}=$releaselifetime;
85 $Input{'PathLength'}=$pathlen;
86 $Input{'Port'}=$port;
87 if ( defined $vomsattribfile ) { $Input{'AC'}=readAC($vomsattribfile); }
88  
89 # Prompt for password
90 if ( ! defined $Input{"Password"} ) {
91 print "Passphrase to protect the credential on the MyProxy Server: ";
92 my $dummy=Term::ReadKey::ReadMode('noecho');
93 my $passwd = Term::ReadKey::ReadLine(),
94 $dummy=Term::ReadKey::ReadMode('normal');
95 chomp $passwd;
96 print "\nRetype passphrase to protect the credential on the MyProxy Server: ";
97 my $dummy=Term::ReadKey::ReadMode('noecho');
98 my $passwd2 = Term::ReadKey::ReadLine(),
99 $dummy=Term::ReadKey::ReadMode('normal');
100 chomp $passwd2;
101 print "\n";
102 if ( $passwd ne $passwd2 ) { die "Passphrases $passwd and $passwd2 did not match!";}
103 $Input{"Password"}=$passwd;
104 }
105  
106 my %Output = %{ VOMS::Lite::MyProxy::Init(\%Input) };
107  
108 if ( defined $Output{Errors} ) {
109 foreach ( @{ $Output{Errors} } ) { print "Error: $_\n"; }
110 die "Failed to create proxy";
111 }
112 foreach ( @{ $Output{Warnings} } ) { print "Warning: $_\n"; }
113  
114 $lifetime = int($Output{'Lifetime'}/3600) . "h " . int($Output{'Lifetime'} / 60) % 60 . "m " . $Output{'Lifetime'} % 60 . "s";
115 $releaselifetime = int($Output{'ReleaseLifetime'}/3600) . "h " . int($Output{'ReleaseLifetime'} / 60 ) % 60 . "m " . $Output{'ReleaseLifetime'} % 60 . "s";
116  
117 print "Proxy Created $Output{Username}\@$Output{Server}".(($Output{Port} != 7512)?":$Output{Port}":"")."\nValid for $lifetime with a release policy lifetime restriction of $releaselifetime.\n";
118  
119 __END__