script/voms-proxy-init.pl untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 260 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::VOMS;
4 use VOMS::Lite::PEMHelper qw(readCert readAC readPrivateKey writeCertKey decodeCert);
5 use VOMS::Lite::ASN1Helper qw(ASN1Wrap ASN1Unwrap DecToHex Hex ASN1BitStr);
6 use VOMS::Lite::PROXY;
7 #use LWP::UserAgent; #Now only loaded if required
8 #use HTTP::Request;
9  
10 my $name=$0;
11 my $lname=length($name);
12 $name =~ s#.*/##g;
13 my $usage = "Usage: $name vomss://voms.server:port/VO[/Subgroup...[/Role=...[/Capability=...]]] [ -cert /path/to/cert.pem ]\n".
14 " " x ($lname+8). "[ -key /path/to/cert's/key.pem ]\n".
15 " " x ($lname+8). "[ -out /path/to/save/proxy ]\n".
16 " " x ($lname+8). "[ -lifetime N (hours, default 12 hours) ]\n".
17 " " x ($lname+8). "[ -CApath /path/to/CA/directory (to verify VOMS server against) ]\n".
18 " " x ($lname+8). "[ -pl N ]\n".
19 " " x ($lname+8). "[ -(old|new|rfc|limited) ]\n".
20 " " x ($lname+8). "[ -limited ]\n";
21 " " x ($lname+8). "[ -verbose ]\n";
22  
23 my %Input;
24 $Input{'Type'}="Legacy";
25 my $lifetime=43200;
26 $Input{'Quiet'}="1";
27 my $HolderCert="$ENV{HOME}/.globus/usercert.pem";
28 my $HolderKey="$ENV{HOME}/.globus/userkey.pem";
29 my $CAPath="/etc/grid-security/certificates";
30 if ( defined $ENV{"X509_CERT_PATH"} && $ENV{"X509_CERT_PATH"} =~ /(.*)/ ) { $CAPath=$1; };
31 my $outfile="/tmp/x509up_u$<";
32 if ( defined $ENV{"X509_USER_PROXY"} && $ENV{"X509_USER_PROXY"} =~ /(.*)/ ) { $outfile=$1; };
33 my ($vomsattribfile,$pathlen);
34 my @VOMSURI;
35 my $verbose=0;
36  
37 while ($_=shift @ARGV) {
38 if ( /^--?cert$/ ) {
39 $HolderCert=shift @ARGV;
40 die "$& requires an argument" if ( ! defined $HolderCert );
41 die "cannot open certificate file $HolderCert" if ( ! -r $HolderCert );
42 }
43 elsif ( /^--?key$/ ) {
44 $HolderKey=shift @ARGV;
45 die "$& requires an argument" if ( ! defined $HolderKey );
46 die "cannot open certificate file $HolderKey" if ( ! -r $HolderKey );
47 }
48 elsif ( /^--?CApath$/ ) {
49 $CAPath=shift @ARGV;
50 die "$& requires an argument" if ( ! defined $CAPath );
51 die "$CAPath is not a directory" if ( ! -d $CAPath );
52 die "cannot open CA directory $CAPath" if ( ! -r $CAPath );
53 }
54 elsif ( /^--?out$/ ) {
55 $outfile=shift @ARGV;
56 die "$& requires an argument" if ( ! defined $outfile );
57 }
58 elsif ( /^--?limited$/ ) { $Input{'Type'}="Limited"; }
59 elsif ( /^--?(new|gt3)$/ ) { $Input{'Type'}="Pre-RFC"; print <<EOF;
60 Warning: Although a VOMS enabled proxy certificate in Pre-RFC form is valid,
61 early VOMS implementations may not recognise the link between VOMS AC
62 and the identity assserted by a Pre-RFC proxy certificate.
63 EOF
64 }
65 elsif ( /^--?rfc$/ ) { $Input{'Type'}="RFC"; print <<EOF;
66 Warning: Although a VOMS enabled proxy certificate in RFC form is valid,
67 early VOMS implementations may not recognise the link between VOMS AC
68 and the identity assserted by an RFC proxy certificate.
69 EOF
70 }
71 elsif ( /^--?(old|legacy)$/ ) { $Input{'Type'}="Legacy"; }
72 elsif ( /^--?(pl|pathlength)$/ ) {
73 $pathlen=shift @ARGV;
74 die "$& requires an argument" if ( ! defined $pathlen );
75 die "Bad Pathlength argument, $& requires a positive integer" if ( $pathlen !~ /^[0-9]+$/ );
76 }
77 elsif ( /^--?l(ifetime)?$/ ) {
78 $lifetime=shift @ARGV;
79 die "$& requires an argument" if ( ! defined $lifetime );
80 die "$& requires a positive numeric integer argument." if ( $lifetime !~ /^[0-9]+$/ );
81 }
82 elsif ( /^--?b(?:its)?$/ ) {
83 my $bits=shift @ARGV;
84 die "$& requires an argument" if ( ! defined $bits );
85 die "$& must be a positive integer." if ( $bits !~ /^[0-9]+$/ );
86 $Input{'Bits'}=$bits;
87 }
88 elsif ( /^vomss:\/\/.*$/ ) {
89 push @VOMSURI,$&;
90 }
91 elsif ( /^https:\/\/.*$/ ) {
92 push @VOMSURI,$&;
93 }
94 elsif ( /^--?v(?:erbose)?$/ ) { $Input{'Quiet'}=undef; $verbose=1; }
95 elsif ( /^--?debug$/ ) { $VOMS::Lite::VOMS::DEBUG="yes"; $Input{'Quiet'}=undef; $verbose=1; }
96 else { die "Unrecognised option \"$_\"\n$usage"; }
97 }
98  
99 # Then make proxy cert
100 my @decodedCERTS=readCert($HolderCert);
101 $Input{'Cert'}=$decodedCERTS[0];
102 $Input{'Key'}=readPrivateKey($HolderKey);
103 $Input{'Lifetime'}=$lifetime;
104 $Input{'PathLength'}=$pathlen;
105  
106 $ENV{HTTPS_CA_DIR} = $CAPath;
107 $ENV{HTTPS_CERT_FILE} = $HolderCert;
108 $ENV{HTTPS_KEY_FILE} = $HolderKey;
109 my $AC;
110  
111 my %URI;
112 my @URI;
113 foreach (@VOMSURI) {
114 if ( m|(vomss://[^:]+:[^/]+)(/.+)| ) {
115 if ( defined $URI{$1} ) { push @{ $URI{$1}},$2; }
116 else { $URI{$1}=[$2]; push @URI,"$1"; }
117 } elsif ( m|https://[^:]+(?::[0-9]{1,5})?/.+| ) {
118 eval "use HTTP::Request"; if ($@) { die "HTTP::Request is required for https style URIs"; }
119 eval "use LWP::UserAgent"; if ($@) { die "LWP::UserAgent is required for https style URIs"; }
120 push @URI,$_;
121 }
122 }
123  
124 foreach my $URI (@URI) {
125 if ( $URI =~ m|vomss://([^:]+):([^/]+)| ) {
126 print "Contacting $URI for ".(join(', ',@{ $URI{$URI} }))." using $HolderCert\n";
127 my $ref = VOMS::Lite::VOMS::Get( { Server => "$1",
128 Port => $2,
129 FQANs => $URI{$URI},
130 Lifetime => $lifetime,
131 CAdirs => $CAPath,
132 Cert => $Input{'Cert'},
133 Key => $Input{'Key'} } );
134  
135 if (@{ ${ $ref }{Errors} } ) { print "Errors:\n ".(join "\n ", @{ ${ $ref }{Errors} }).".\n"; die "Failed to get ".join(', ',@{ $URI{$URI} }); }
136 if (@{ ${ $ref }{Warnings} } and $verbose==1) { print "Warnings for $1:$2$3\n ".(join "\n ", @{ ${ $ref }{Warnings} }).".\n"; }
137 $AC.=${ $ref }{'AC'}."\n";
138 }
139 elsif ( m|https://[^:]+(?::[0-9]{1,5})?/.+| ) {
140  
141 # eval test
142 # use LWP::UserAgent;
143 # use HTTP::Request;
144  
145  
146 my $req = HTTP::Request->new( GET => $URI, HTTP::Headers->new('Accept' => "text/plain"));
147 my $agent = LWP::UserAgent->new;
148 my $response = $agent->request( $req );
149 print "Contacting $URI\n";
150 if ( $response->is_success ) {
151 $AC.=$response->content;
152 if ($verbose) { print "Server responded as follows:\n$AC\n\n"; }
153 } else {
154 if ($verbose) { print "Server responded as follows:\n".$response->as_string; }
155 die "Unable to obtain AC from $URI";
156 }
157 }
158 }
159  
160 print "Creating Proxy\n";
161  
162 if ( $AC ) {
163 my @ACDER=decodeCert($AC,"ATTRIBUTE CERTIFICATE");
164 $Input{'AC'}=join('',@ACDER);
165 }
166 else { print "Warning: No attribute certificates were obtained or generated\n"; }
167  
168 my %Output = %{ VOMS::Lite::PROXY::Create(\%Input) };
169 if ( ! defined $Output{ProxyCert} || ! defined $Output{ProxyKey} ) {
170 foreach ( @{ $Output{Errors} } ) { print "Error: $_\n"; }
171 die "Failed to create proxy";
172 }
173 foreach ( @{ $Output{Warnings} } ) { print "Warning: $_\n"; }
174 print "Writing Proxy to $outfile\n";
175 writeCertKey("$outfile", $Output{'ProxyCert'}, $Output{'ProxyKey'}, @decodedCERTS);
176  
177 __END__