lib/VOMS/Lite.pm

filter: Statementstmt 35.9% 60 / 167 Branchbran 17.7% 17 / 96 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 75.0% 12 / 16 Podpod 0.0% 0 / 7 totaltotal 28.2% 89 / 315 CC 30 SCAR 51.9
CC
30
Cov
27%
GetGridMapping
342.0
GetDBAttribs
156.0
Issue
2.0
CRAP 178.8
« lib/VOMS/Lite/SAMLHelper.pm ↑ Summary lib/VOMS/Lite/CertKeyHelper.pm »
1 package VOMS::Lite;
2  
3 #This package issues VOMS credentials
4 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use VOMS::Lite::PEMHelper qw(readCert readPrivateKey);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use VOMS::Lite::CertKeyHelper qw(buildchain);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use VOMS::Lite::ASN1Helper qw(Hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use VOMS::Lite::AC;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 require Exporter;
13 1 ▶use vars qw($VERSION @EXPORT_OK @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 @EXPORT = qw( %conf );
16 1 @EXPORT_OK = qw( Issue );
17 }
18  
19 1 $VERSION = '0.20';
20  
21 ###########################################################
22  
23 1 my $configfile;
24 my %conf;
25 1 ▶if (defined $ENV{'VOMS_CONFIG_FILE'}) { $configfile=$ENV{'VOMS_CONFIG_FILE'}; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ENV{'VOMS_CONFIG_FILE'}) { } 1 0 1
elsif ($< == 0) { } 0 0 0
26 0 elsif ( $< == 0 ) { $configfile="/etc/grid-security/voms.config"; }
27 0 else { $configfile=$ENV{'HOME'}."/.grid-security/voms.config"; }
28  
29 # Check for config file
30 1 ▶if ( -r $configfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $configfile) { } 1 0 1
31 1 ▶ if ( (stat($configfile))[2] & 077 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((stat $configfile)[2] & 63) 0 1 1
32 0 ▶ if ( $^O =~ /^MSWin/ ) { print STDERR "WARNING: VOMS::Lite: Inapropriate permissions on config file: $configfile\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/) { } 0 0 0
33 0 else { die "VOMS::Lite: Inapropriate permissions on config file: $configfile"; }
34 }
35 1 ▶ if ( open(CONF,"<$configfile") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open CONF, "<$configfile") { } 1 0 1
36 1 ▶ while (<CONF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CONF)) 8 1 9
37 8 chomp;
38 8 ▶ if ( /^\s*([a-zA-Z0-9_-]+)\s*=\s*(.+?)\s*$/ ) { $conf{"$1"} = $2; }
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([a-zA-Z0-9_-]+)\s*=\s*(.+?)\s*$/) 8 0 8
39 }
40 1 close CONF;
41 }
42 else {
43 0 die "VOMS::Lite: Unable to open config file: $configfile";
44 }
45 }
46 0 else { die "VOMS::Lite: Unable to open config file: $configfile"; }
47  
48 # Set CertDir (where the CAs are stored
49 1 ▶if (! defined $conf{CertDir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"CertDir"}) 0 1 1
50 0 ▶ if (-d "$ENV{HOME}/.grid-security/certificates" ) { $conf{CertDir} = "$ENV{HOME}/.grid-security/certificates"; }
Branch: 2 branches
BranchTrueFalseTotal
if (-d "$ENV{'HOME'}/.grid-security/certificates") { } 0 0 0
elsif (-d '/etc/grid-security/certificates') { } 0 0 0
51 0 elsif (-d "/etc/grid-security/certificates" ) { $conf{CertDir} = "/etc/grid-security/certificates"; }
52 }
53  
54 # Set VOMS Certificate
55 1 ▶if (defined $conf{VOMSCert}) { $conf{VOMSCert} = readCert("$conf{VOMSCert}"); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf{'VOMSCert'}) { } 1 0 1
56 else {
57 0 ▶ if (-d "$ENV{HOME}/.grid-security/vomscert.pem" ) { $conf{VOMSCert} = readCert("$ENV{HOME}/.grid-security/vomscert.pem"); }
Branch: 2 branches
BranchTrueFalseTotal
if (-d "$ENV{'HOME'}/.grid-security/vomscert.pem") { } 0 0 0
elsif (-d '/etc/grid-security/vomscert.pem') { } 0 0 0
58 0 elsif (-d "/etc/grid-security/vomscert.pem" ) { $conf{VOMSCert} = readCert("/etc/grid-security/vomscert.pem"); }
59 }
60  
61 # Set VOMS Key
62 1 ▶if (defined $conf{VOMSKey}) { $conf{VOMSKey} = readPrivateKey("$conf{VOMSKey}"); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf{'VOMSKey'}) { } 1 0 1
63 else {
64 0 ▶ if (-d "$ENV{HOME}/.grid-security/vomskey.pem" ) { $conf{VOMSKey} = readPrivateKey("$ENV{HOME}/.grid-security/vomskey.pem"); }
Branch: 2 branches
BranchTrueFalseTotal
if (-d "$ENV{'HOME'}/.grid-security/vomskey.pem") { } 0 0 0
elsif (-d '/etc/grid-security/vomskey.pem') { } 0 0 0
65 0 elsif (-d "/etc/grid-security/vomskey.pem" ) { $conf{VOMSKey} = readPrivateKey("/etc/grid-security/vomskey.pem"); }
66 }
67  
68 # Set Type of VO user database
69 1 my $AttribCodeRef;
70 1 ▶if (defined $conf{'AttribType'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf{'AttribType'}) { } 1 0 1
71 1 ▶ if ($conf{'AttribType'} eq "Database") {
Branch: 5 branches
BranchTrueFalseTotal
if ($conf{'AttribType'} eq 'Database') { } 0 1 1
elsif ($conf{'AttribType'} eq 'GridMap') { } 0 1 1
elsif ($conf{'AttribType'} eq 'GridSite') { } 0 1 1
elsif ($conf{'AttribType'} eq 'Shibboleth') { } 0 1 1
elsif ($conf{'AttribType'} eq 'Dummy') { } 1 0 1
72 0 require VOMS::Lite::Attribs::DBHelper;
73 0 $AttribCodeRef = \&GetDBAttribs;
74 0 ▶ if (!defined $conf{'DBHost'}) { $conf{'DBHost'}="localhost"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"DBHost"}) 0 0 0
75 0 ▶ if (!defined $conf{'DBPort'}) { $conf{'DBPort'}="3306"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"DBPort"}) 0 0 0
76 0 ▶ if (!defined $conf{'DBUser'}) { die "VOMS::Lite: Database username not specified."}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"DBUser"}) 0 0 0
77 0 ▶ if (!defined $conf{'DBPass'}) { die "VOMS::Lite: Database password not specified."}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"DBPass"}) 0 0 0
78 }
79 elsif ($conf{'AttribType'} eq "GridMap") {
80 0 $AttribCodeRef = \&GetGridMapping;
81 0 ▶ if (!defined $conf{'grid-mapfiles'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"grid-mapfiles"}) 0 0 0
82 0 ▶ if (-d "$ENV{HOME}/.grid-security/grid-mapfile.d" ) { $conf{'grid-mapfiles'}="$ENV{HOME}/.grid-security/grid-mapfile.d"; }
Branch: 2 branches
BranchTrueFalseTotal
if (-d "$ENV{'HOME'}/.grid-security/grid-mapfile.d") { } 0 0 0
elsif (-d '/etc/grid-security/grid-mapfile.d') { } 0 0 0
83 0 elsif (-d "/etc/grid-security/grid-mapfile.d" ) { $conf{'grid-mapfiles'}="/etc/grid-security/grid-mapfile.d"; }
84 0 else { die "VOMS::Lite: grid-mapfile method specified but no grid-mapfile.d directory found"; }
85 }
86 }
87 elsif ($conf{'AttribType'} eq "GridSite") {
88 0 $AttribCodeRef = \&GetGridSiteAttribs;
89 0 ▶ if (!defined $conf{'GridSiteURI'}) { die "VOMS::Lite: GridSite method specified but no GridSiteURI specified."; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"GridSiteURI"}) 0 0 0
90 }
91 elsif ($conf{'AttribType'} eq "Shibboleth") {
92 0 require VOMS::Lite::Attribs::SHIBHelper;
93 0 $AttribCodeRef = \&GetShibAttribs;
94 }
95 1 elsif ($conf{'AttribType'} eq "Dummy") { $AttribCodeRef = \&GetDummyAttribs; }
96 0 else { die "VOMS::Lite: Attribute Method unknown."; }
97 }
98 else {
99 0 die "VOMS::Lite: Attribute method unspecified in config file $configfile";
100 }
101  
102 ###########################################################
103  
104 1 ▶sub UserCert {
Subroutine UserCert: called CC=1 SCAR=0.0
Pod undocumented
105 #This function takes in an array of certificates, completes the chain if necessary and returns
106 #The reference to the chain
107 1 my @certs=@_;
108 1 my %Chain = %{ buildchain( { trustedCAdirs => [ $conf{CertDir} ], suppliedcerts => \@certs} ) };
109 1 return ($Chain{Certs},$Chain{EndEntityDN},$Chain{EndEntityIssuerDN},$Chain{EndEntityCert});
110 }
111  
112 ###########################################################
113  
114 1 ▶sub Issue {
Subroutine Issue: called CC=2 SCAR=7.1
Pod undocumented
115  
116 1 my ($CertsRef,$ReqAttribs)=@_;
117 1 my @Certs=@$CertsRef;
118 1 my ($CertChainRef,$DN,$CA,$CERT)=UserCert(@Certs);
119  
120 # Get the attributes
121 1 my $AttribRef=&$AttribCodeRef($DN,$CA,$ReqAttribs);
122 1 my %Attribs=%$AttribRef;
123  
124 0 ▶ if ( defined $Attribs{'Errors'} ) { return $AttribRef; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Attribs{"Errors"}) 0 1 1
125  
126 # Get AC
127 return VOMS::Lite::AC::Create( { Cert => $CERT,
128 VOMSCert => $conf{'VOMSCert'},
129 VOMSKey => $conf{'VOMSKey'},
130 Lifetime => $conf{'Lifetime'},
131 Server => $conf{'Server'},
132 Port => $conf{'Port'},
133 Serial => $Attribs{'Serial'},
134 Code => $conf{'Code'},
135 1 Attribs => $Attribs{'Attribs'},
136 Broken => 1 } );
137 }
138  
139 ###########################################################
140  
141 0 ▶sub GetDBAttribs {
Subroutine GetDBAttribs: not called CC=12 SCAR=50.5
Pod undocumented
142 0 my ($DN,$CA,$ReqAttrib)=@_;
143  
144 0 my $DB=undef;
145 0 my ($VO,$subGroup,$Role,$Capability) = $ReqAttrib =~ m#^(/[^/]+)(/.*)?(/Role=[^/]*)?(/Capability=[^/]*)?$#;
146 0 ▶ if (! defined $VO ) { return { Errors => [ "VOMS::Lite: No VO specified" ] }; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $VO) 0 0 0
147  
148 0 my $function;
149  
150 0 ▶ if ( defined $subGroup && defined $Role ) { $function="groupandrole"; }
Branch: 3 branches
BranchTrueFalseTotal
if (defined $subGroup and defined $Role) { } 0 0 0
elsif (defined $Role) { } 0 0 0
elsif (defined $subGroup) { } 0 0 0
Condition0%0/3
defined $subGroup and defined $Role
!ll&&!rl&&r
000
MC/DC0%0/2
defined $subGroup and defined $Role
defined $subGroup defined $Role
Truth table0%0/3
A and B
A= defined $subGroupB= defined $Role
ABresult
0X0
100
111
151 0 elsif ( defined $Role ) { $function="role"; }
152 0 elsif ( defined $subGroup ) { $function="attributes"; } # or maybe all
153 0 else { $function="group";}
154  
155 0 $DB=$VO;
156 0 $DB=~s/^\///;
157  
158 0 foreach (keys %conf) {
159 0 ▶ if ( /^(DBMapping_[0-9]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(DBMapping_[0-9]+)$/) 0 0 0
160 0 ▶ if ( $conf{$1} =~ /^$DB\s+(\S*)\s*$/ ) { $DB = $1; print "------ $1\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($conf{$1} =~ /^$DB\s+(\S*)\s*$/) 0 0 0
161 }
162 }
163  
164 0 ▶ if ( $DB =~ /^[\000\377\\\/.]$/ || $DB =~/ $/ || length($DB) > 64) { return { Errors => [ "VOMS::Lite: Bad Database name $DB" ] }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($DB =~ m[^[\000\377\\/.]$] or $DB =~ / $/ or length $DB > 64) 0 0 0
Condition0%0/6
$DB =~ m[^[\000\377\\/.]$] or $DB =~ / $/ or length $DB > 64
l!l&&r!l&&!r
000
$DB =~ m[^[\000\377\\/.]$] or $DB =~ / $/
l!l&&r!l&&!r
000
MC/DC0%0/3
$DB =~ m[^[\000\377\\/.]$] or $DB =~ / $/ or length $DB > 64
$DB =~ m[^[\000\377\\/.]$] $DB =~ / $/ length $DB > 64
Truth table0%0/4
A or B or C
A= $DB =~ m[^[\000\377\\/.]$]B= $DB =~ / $/C= length $DB > 64
ABCresult
1XX1
01X1
0011
0000
165  
166 # Get VO data from Database
167 0 my @Attribs=VOMS::Lite::Attribs::DBHelper::GetAttrib($DB,$conf{'DBHost'},$conf{'DBPort'},$conf{'DBUser'},$conf{'DBPass'},$Role,"$VO$subGroup",$CA,$DN,$function);
168 0 my $Serial=hex(shift @Attribs); #stored as hex in DB
169  
170 0 return { Serial => $Serial, Attribs=>\@Attribs};
171 }
172  
173 ###########################################################
174  
175 0 ▶sub GetGridMapping {
Subroutine GetGridMapping: not called CC=18 SCAR=58.3
Pod undocumented
176 0 my ($DN,$CA,$ReqAttrib)=@_;
177  
178 0 my @Attribs;
179 0 my ($VO,$subGroup,$Role,$Capability) = $ReqAttrib =~ m#^(/[^/]+)(/.*)?(/Role=[^/]*)?(/Capability=[^/]*)?$#;
180 0 ▶ if (! defined $VO ) { return { Errors => [ "VOMS::Lite: No VO specified" ] }; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $VO) 0 0 0
181 0 ▶ open GRIDMAP,"<$conf{'grid-mapfiles'}$VO" or return { Errors => [ "VOMS::Lite: No Gridmapfile for VO" ] };
Branch: 1 branch
BranchTrueFalseTotal
open GRIDMAP, qq[<$conf{"grid-mapfiles"}$VO] or return {"Errors", ["VOMS::Lite: No Gridmapfile for VO"]} 0 0 0
182 0 ▶ foreach (<GRIDMAP>) { if ( m|^\s*"$DN"\s+(.*)| ) { @Attribs= split(/,/,$1); last; } }
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*"$DN"\s+(.*)/) 0 0 0
183 0 close GRIDMAP;
184  
185 0 my $serial=0;
186 0 ▶ open(SERIAL, "+>> $conf{'grid-mapfiles'}$VO.serial") or return { Errors => [ "VOMS::Lite: Unable to open/create serial file $conf{'grid-mapfiles'}/$VO.serial, Check Permissions" ] };
Branch: 1 branch
BranchTrueFalseTotal
open SERIAL, qq[+>> $conf{"grid-mapfiles"}$VO.serial] or return {"Errors", [qq[VOMS::Lite: Unable to open/create serial file $conf{"grid-mapfiles"}/$VO.serial, Check Permissions]]} 0 0 0
187 0 seek(SERIAL,0,0);
188 0 my @lines=<SERIAL>;
189  
190 0 ▶ if ( $lines[0] =~ /^[0-9]+$/ ) { $serial=$1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] =~ /^[0-9]+$/) 0 0 0
191 0 seek(SERIAL,0,0);
192 0 print SERIAL ++$serial;
193 0 close SERIAL;
194  
195 0 my @Roles;
196 my @Capabilities;
197 0 my @Groups;
198 0 foreach (@Attribs) {
199 0 s/^\s*//;
200 0 s/\s*$//;
201 0 ▶ if ( defined $Role && /Role=$Role/o ) { push @Roles,$Role; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Role and /Role=$Role/o) 0 0 0
Condition0%0/3
defined $Role and /Role=$Role/o
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Role and /Role=$Role/o
defined $Role /Role=$Role/o
Truth table0%0/3
A and B
A= defined $RoleB= /Role=$Role/o
ABresult
0X0
100
111
202 0 ▶ if ( defined $Capability && /Capability=$Capability/o ) { push @Capabilities,$Capability; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Capability and /Capability=$Capability/o) 0 0 0
Condition0%0/3
defined $Capability and /Capability=$Capability/o
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Capability and /Capability=$Capability/o
defined $Capability /Capability=$Capability/o
Truth table0%0/3
A and B
A= defined $CapabilityB= /Capability=$Capability/o
ABresult
0X0
100
111
203 0 ▶ if ( defined $subGroup && /$VO$subGroup^/o ) { push @Groups,$_; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $subGroup and ) { } 0 0 0
elsif (m[^$VO(?:/|$)]) { } 0 0 0
Condition0%0/3
defined $subGroup and
!ll&&!rl&&r
000
MC/DC0%0/2
defined $subGroup and
defined $subGroup
Truth table0%0/3
A and B
A= defined $subGroupB=
ABresult
0X0
100
111
204 0 elsif ( /^$VO(?:\/|$)/ ) { push @Groups,$_; }
205 }
206 0 push @Roles,"/Role=NULL";
207 0 push @Capabilities,"/Capability=NULL";
208 0 my @RetAttribs=();
209 0 foreach my $group (@Groups) {
210 0 foreach my $role (@Roles) {
211 0 foreach my $capability (@Capabilities) { push @RetAttribs,"$group$role$capability"; }
212 }
213 }
214  
215 0 return { Serial => $serial, Attribs=>\@RetAttribs};
216 }
217  
218 ###########################################################
219  
220 0 ▶sub GetGridSiteAttribs {
Subroutine GetGridSiteAttribs: not called CC=1 SCAR=6.9
Pod undocumented
221 # eval "use use LWP::UserAgent;";
222 # voms-proxy-init.pl:
223 # my $agent = LWP::UserAgent->new;
224 #
225 ### Cert,
226 ### get https://www.blah/primary.group/subgroup/Role=nuff
227 #
228 #
229 #
230 0 return { Serial => 01, Attribs=> [ "/GridSiteDummy/Role=NULL/Capability=NULL" ] };
231 }
232  
233 ###########################################################
234  
235 0 ▶sub GetShibAttribs {
Subroutine GetShibAttribs: not called CC=1 SCAR=6.9
Pod undocumented
236 # If ShibExportAssertion is on in your http.conf The following Env variable should be set it will be XML
237 0 my @Attribs=VOMS::Lite::Attribs::SHIBHelper::GetAttrib($ENV{'HTTP_SHIB_ATTRIBUTES'});
238 0 my $Serial=shift @Attribs;
239 0 return { Serial => 01, Attribs=> [ "/ShibDummy/Role=NULL/Capability=NULL" ] };
240 }
241  
242 ###########################################################
243  
244 1 ▶sub GetDummyAttribs {
Subroutine GetDummyAttribs: called CC=1 SCAR=0.0
Pod undocumented
245 1 return { Serial => 01, Attribs => [ "/Dummy/Role=NULL/Capability=NULL" ] };
246 }
247  
248  
249 1 1;
250  
251  
252 __END__
« lib/VOMS/Lite/SAMLHelper.pm ↑ Summary lib/VOMS/Lite/CertKeyHelper.pm »