blib/lib/Lemonldap/NG/Common/Safelib.pm

filter: Statementstmt 24.8% 37 / 149 Branchbran 9.3% 6 / 64 Conditioncond 4.1% 1 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 34.6% 9 / 26 Podpod 0.0% 0 / 18 totaltotal 17.9% 53 / 295 CC 46 SCAR 35.9
CC
46
Cov
19%
checkLogonHours
72.0
listMatch
72.0
checkDate
72.0
CRAP 36.2
« blib/lib/Lemonldap/NG/Common/EmailTransport.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Util.pm »
1 ##@file
2 # Functions shared in Safe jail
3  
4 ##@class
5 # Functions shared in Safe jail
6 package Lemonldap::NG::Common::Safelib;
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Lemonldap::NG::Common::IPv6;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Net::CIDR;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 our $VERSION = '2.23.0';
18  
19 # Set here all the names of functions that must be available in Safe objects.
20 # Note that only functions, not methods, can be written here
21 3 our $functions =
22 [
23 qw(&checkLogonHours &date &dateToTime &checkDate &basic &unicode2iso &unicode2isoSafe &iso2unicode &iso2unicodeSafe &groupMatch &isInNet6 &varIsInUri &has2f_internal &ipInSubnet &subjectid &inDomain_internal)
24 ];
25  
26 ## @function boolean ipInSubnet(string ip, string network, ... )
27 # Function to check if an IP is part of a network
28 # @param $ip IP address to test
29 # @param $network Network in CIDR notation
30 # You can call the function with multiple networks
31 # @return 1 true, 0 else
32 0 ▶sub ipInSubnet {
Subroutine ipInSubnet: not called CC=1 SCAR=6.9
Pod undocumented
33 0 my ( $ip, @networks ) = @_;
34 0 return Net::CIDR::cidrlookup( $ip, @networks );
35 }
36  
37 ## @function boolean subjectid(string alg, string value, string scope, string salt)
38 # Function to compute an opaque identifier from a value and optional salt
39 # @param $value ID to encrypt
40 # @param $scope Domain used for restricting scope
41 # @param $salt Salt appended to generate hash
42 # @return encrypted ID
43 0 ▶sub subjectid {
Subroutine subjectid: not called CC=3 SCAR=24.8
Pod undocumented
44 0 my ( $value, $scope, $salt ) = @_;
45 0 ▶ $salt //= "";
Condition0%0/2
$salt //= ""
l!l
00
MC/DC0%0/1
$salt //= ""
$salt
Truth table0%0/2
A
A= $salt
Aresult
11
00
46 0 ▶ $scope = $scope ? "\@$scope" : "";
Branch: 1 branch
BranchTrueFalseTotal
$scope ? : 0 0 0
47 return (
48 0 lc( encode_base32( Digest::SHA::sha256( $value . $salt ) ) ) . $scope );
49 }
50  
51 # This function is copied from MIME::Base32 under GPL/Artistic license
52 # by Daniel Peder <Daniel.Peder@InfoSet.COM>
53 0 ▶sub encode_base32 {
Subroutine encode_base32: not called CC=3 SCAR=24.8
Pod undocumented
54 0 my $arg = shift;
55 0 ▶ return '' unless defined($arg); # mimic MIME::Base64
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 0 0
56  
57 0 $arg = unpack( 'B*', $arg );
58 0 $arg =~ s/(.....)/000$1/g;
59 0 my $l = length($arg);
60 0 ▶ if ( $l & 7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l & 7) 0 0 0
61 0 my $e = substr( $arg, $l & ~7 );
62 0 $arg = substr( $arg, 0, $l & ~7 );
63 0 $arg .= "000$e" . '0' x ( 5 - length $e );
64 }
65 0 $arg = pack( 'B*', $arg );
66 0 $arg =~ tr|\0-\37|A-Z2-7|;
67 0 return $arg;
68 }
69  
70 ## @function boolean checkLogonHours(string logon_hours, string syntax, string time_correction, boolean default_access)
71 # Function to check logon hours
72 # @param $logon_hours string representing allowed logon hours (GMT)
73 # @param $syntax optional hexadecimal (default) or octetstring
74 # @param $time_correction optional hours to add or to subtract
75 # @param $default_access optional what result to return for users without logons hours
76 # @return 1 if access allowed, 0 else
77 0 ▶sub checkLogonHours {
Subroutine checkLogonHours: not called CC=8 SCAR=42.8
Pod undocumented
78 0 my ( $logon_hours, $syntax, $time_correction, $default_access ) = @_;
79  
80 # Active Directory - logonHours: $attr_src_syntax = octetstring
81 # Samba - sambaLogonHours: ???
82 # LL::NG - ssoLogonHours: $attr_src_syntax = hexadecimal
83 0 ▶ $syntax ||= "hexadecimal";
Condition0%0/2
$syntax ||= "hexadecimal"
l!l
00
MC/DC0%0/1
$syntax ||= "hexadecimal"
$syntax
Truth table0%0/2
A
A= $syntax
Aresult
11
00
84  
85 # Default access if no value
86 0 ▶ $default_access ||= "0";
Condition0%0/2
$default_access ||= 0
l!l
00
MC/DC0%0/1
$default_access ||= 0
$default_access
Truth table0%0/2
A
A= $default_access
Aresult
11
00
87 0 ▶ return $default_access unless $logon_hours;
Branch: 1 branch
BranchTrueFalseTotal
unless $logon_hours 0 0 0
88  
89 # Get the base2 value of logon_hours
90 # Each byte represent an hour of the week
91 # Begin with sunday at 0h00
92 0 my $base2_logon_hours;
93 0 ▶ if ( $syntax eq "octetstring" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syntax eq "octetstring") 0 0 0
94 0 $base2_logon_hours = unpack( "B*", $logon_hours );
95 }
96 0 ▶ if ( $syntax eq "hexadecimal" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syntax eq "hexadecimal") 0 0 0
97  
98 # Remove white spaces
99 0 $logon_hours =~ s/ //g;
100 0 $base2_logon_hours = unpack( "B*", pack( "H*", $logon_hours ) );
101 }
102  
103 # Get the present day and hour
104 0 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
105 gmtime(time);
106  
107 # Get the hour position
108 0 my $hourpos = $wday * 24 + $hour;
109  
110 # Use time_correction
111 0 ▶ if ($time_correction) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_correction) 0 0 0
112 0 my ( $sign, $time ) = ( $time_correction =~ /([+|-]?)(\d+)/ );
113 0 ▶ if ( $sign =~ /-/ ) { $hourpos -= $time; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sign =~ /-/) { } 0 0 0
114 0 else { $hourpos += $time; }
115 }
116  
117 # Get the corresponding byte
118 0 return substr( $base2_logon_hours, $hourpos, 1 );
119 }
120  
121 ## @function integer listMatch
122 # Test if a value is found in a collection
123 # @param $list Can be a hash, array or string including the separator
124 # @param $values string The values to search for
125 # @param $ignorecase boolean Be case insensitive
126 # @return 1 if the value was found, 0 else
127 # NOTE: this function is not exported directly in this module because we don't
128 # want the user to have to worry about separator. Is is wrapped in a closure in
129 # Jail.pm
130 0 ▶sub listMatch {
Subroutine listMatch: not called CC=8 SCAR=42.8
Pod undocumented
131 0 my ( $sep, $list, @values ) = @_;
132 0 my $flags = '';
133 0 my @a;
134  
135 0 ▶ if ( $values[-1] eq '1' || $values[-1] eq '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($values[-1] eq 1 or $values[-1] eq 0) 0 0 0
Condition0%0/3
$values[-1] eq 1 or $values[-1] eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$values[-1] eq 1 or $values[-1] eq 0
$values[-1] eq 1 $values[-1] eq 0
Truth table0%0/3
A or B
A= $values[-1] eq 1B= $values[-1] eq 0
ABresult
1X1
011
000
136 0 ▶ $flags = pop @values ? 'i' : '';
Branch: 1 branch
BranchTrueFalseTotal
pop @values ? : 0 0 0
137 }
138  
139 0 ▶ if ( ref($list) eq "ARRAY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $list eq 'ARRAY') { } 0 0 0
elsif (ref $list eq 'HASH') { } 0 0 0
140 0 @a = @{$list};
141 }
142 elsif ( ref($list) eq "HASH" ) {
143 0 @a = keys %{$list};
144 }
145 else {
146 0 @a = split $sep, $list;
147 }
148  
149 0 foreach my $value (@values) {
150 0 ▶ return 1 if grep /(?$flags)^\Q$value\E$/, @a;
Branch: 1 branch
BranchTrueFalseTotal
if grep /(?$flags)^\Q$value\E$/, @a 0 0 0
151 }
152  
153 0 return 0;
154 }
155  
156 ## @function integer date
157 # Get current local date
158 # @param $gmt optional boolean To return GMT date (default is local date)
159 # @return current date on format YYYYMMDDHHMMSS
160 0 ▶sub date {
Subroutine date: not called CC=7 SCAR=40.3
Pod undocumented
161 0 my $gmt = shift;
162 0 ▶ my ( $sec, $min, $hour, $mday, $mon, $year ) = $gmt ? gmtime : localtime;
Branch: 1 branch
BranchTrueFalseTotal
$gmt ? : 0 0 0
163  
164 0 $year += 1900;
165 0 $mon += 1;
166 0 ▶ $mon = "0" . $mon if ( $mon < 10 );
Branch: 1 branch
BranchTrueFalseTotal
if $mon < 10 0 0 0
167 0 ▶ $mday = "0" . $mday if ( $mday < 10 );
Branch: 1 branch
BranchTrueFalseTotal
if $mday < 10 0 0 0
168 0 ▶ $hour = "0" . $hour if ( $hour < 10 );
Branch: 1 branch
BranchTrueFalseTotal
if $hour < 10 0 0 0
169 0 ▶ $min = "0" . $min if ( $min < 10 );
Branch: 1 branch
BranchTrueFalseTotal
if $min < 10 0 0 0
170 0 ▶ $sec = "0" . $sec if ( $sec < 10 );
Branch: 1 branch
BranchTrueFalseTotal
if $sec < 10 0 0 0
171  
172 0 return $year . $mon . $mday . $hour . $min . $sec;
173 }
174  
175 ## @function integer dateToTime(string date)
176 # Converts a LDAP date into epoch time or returns undef upon failure.
177 # @param $date string Date in YYYYMMDDHHMMSS[+/-0000] format. It may contain a differential timezone, otherwise default TZ is GMT
178 # @return Date converted to time
179 0 ▶sub dateToTime {
Subroutine dateToTime: not called CC=2 SCAR=17.9
Pod undocumented
180 0 my $date = shift;
181 0 ▶ return undef unless ($date);
Branch: 1 branch
BranchTrueFalseTotal
unless $date 0 0 0
182  
183 # Parse date
184 0 my ( $year, $month, $day, $hour, $min, $sec, $zone ) =
185 ( $date =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})([-+\w]*)/ );
186  
187 # Convert date to epoch time with GMT as default timezone if date contains none
188 0 return str2time(
189 $year . "-"
190 . $month . "-"
191 . $day . "T"
192 . $hour . ":"
193 . $min . ":"
194 . $sec
195 . $zone,
196 "GMT"
197 );
198 }
199  
200 ## @function boolean checkDate(string start, string end, boolean default_access)
201 # Function to check a date
202 # @param $start string Start date in YYYYMMDDHHMMSS[+/-0000] format. It may contain a differential timezone, otherwise default TZ is GMT
203 # @param $end string End date in YYYYMMDDHHMMSS[+/-0000] format. It may contain a differential timezone, otherwise default TZ is GMT
204 # @param $default_access optional what result to return for users without start and end dates
205 # @return 1 if access allowed, 0 else
206 0 ▶sub checkDate {
Subroutine checkDate: not called CC=8 SCAR=42.8
Pod undocumented
207 0 my ( $start, $end, $default_access ) = @_;
208  
209 # Default access if no value
210 0 ▶ $default_access ||= "0";
Condition0%0/2
$default_access ||= 0
l!l
00
MC/DC0%0/1
$default_access ||= 0
$default_access
Truth table0%0/2
A
A= $default_access
Aresult
11
00
211 0 ▶ return $default_access unless ( $start or $end );
Branch: 1 branch
BranchTrueFalseTotal
unless $start or $end 0 0 0
Condition0%0/3
$start or $end
l!l&&r!l&&!r
000
MC/DC0%0/2
$start or $end
$start $end
Truth table0%0/3
A or B
A= $startB= $end
ABresult
1X1
011
000
212  
213 # If no start, set start to 01 01 1970
214 0 ▶ $start ||= "19700101000000";
Condition0%0/2
$start ||= "19700101000000"
l!l
00
MC/DC0%0/1
$start ||= "19700101000000"
$start
Truth table0%0/2
A
A= $start
Aresult
11
00
215  
216 # If no end, set end to the end of the world
217 0 ▶ $end ||= "99991231235959";
Condition0%0/2
$end ||= "99991231235959"
l!l
00
MC/DC0%0/1
$end ||= "99991231235959"
$end
Truth table0%0/2
A
A= $end
Aresult
11
00
218  
219 # Convert dates to epoch time
220 0 my $starttime = &dateToTime($start);
221 0 my $endtime = &dateToTime($end);
222  
223 # Convert current GMT date to epoch time
224 0 my $datetime = &dateToTime( &date(1) );
225  
226 0 ▶ return 1 if ( ( $datetime >= $starttime ) and ( $datetime <= $endtime ) );
Branch: 1 branch
BranchTrueFalseTotal
if $datetime >= $starttime and $datetime <= $endtime 0 0 0
Condition0%0/3
$datetime >= $starttime and $datetime <= $endtime
!ll&&!rl&&r
000
MC/DC0%0/2
$datetime >= $starttime and $datetime <= $endtime
$datetime >= $starttime $datetime <= $endtime
Truth table0%0/3
A and B
A= $datetime >= $starttimeB= $datetime <= $endtime
ABresult
0X0
100
111
227 0 return 0;
228  
229 }
230  
231 ## @function string basic(string login, string password)
232 # Return string that can be used for HTTP-BASIC authentication
233 # @param login User login
234 # @param password User password
235 # @return Authorization header content
236 0 ▶sub basic {
Subroutine basic: not called CC=1 SCAR=6.9
Pod undocumented
237 0 my ( $login, $password ) = @_;
238  
239 # UTF-8 strings should be ISO encoded
240 0 $login = &unicode2iso($login);
241 0 $password = &unicode2iso($password);
242  
243 0 return "Basic " . encode_base64( $login . ":" . $password, '' );
244 }
245  
246 ## @function string unicode2iso(string string)
247 # Convert UTF-8 in ISO-8859-1
248 # @param string UTF-8 string
249 # @return ISO string
250 0 ▶sub unicode2iso {
Subroutine unicode2iso: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my ($string) = @_;
252  
253 0 return encode( "iso-8859-1", decode( "utf-8", $string ) );
254 }
255  
256 ## @function string unicode2isoSafe(string string)
257 ## This function is compliant with the Safe jail
258 ## but not as portable as the original one
259 # Convert UTF-8 in ISO-8859-1
260 # @param string UTF-8 string
261 # @return ISO string
262 0 ▶sub unicode2isoSafe {
Subroutine unicode2isoSafe: not called CC=1 SCAR=6.9
Pod undocumented
263 0 my ($string) = @_;
264  
265 0 my $res = $string;
266 0 utf8::decode($res);
267 0 utf8::downgrade($res);
268 0 return $res;
269 }
270  
271 ## @function string iso2unicode(string string)
272 # Convert ISO-8859-1 in UTF-8
273 # @param string ISO string
274 # @return UTF-8 string
275 0 ▶sub iso2unicode {
Subroutine iso2unicode: not called CC=1 SCAR=6.9
Pod undocumented
276 0 my ($string) = @_;
277  
278 0 return encode( "utf-8", decode( "iso-8859-1", $string ) );
279 }
280  
281 ## @function string iso2unicodeSafe(string string)
282 ## This function is compliant with the Safe jail
283 ## but not as portable as the original one
284 # Convert ISO-8859-1 in UTF-8
285 # @param string ISO string
286 # @return UTF-8 string
287 0 ▶sub iso2unicodeSafe {
Subroutine iso2unicodeSafe: not called CC=1 SCAR=6.9
Pod undocumented
288 0 my ($string) = @_;
289  
290 0 my $res = $string;
291 0 utf8::encode($res);
292 0 return $res;
293 }
294  
295 ## @function int groupMatch(hashref groups, string attribute, string value)
296 # Check in hGroups structure if a group attribute contains a value
297 # @param groups The $hGroups variable
298 # @param attribute Name of the attribute
299 # @param value Value to check
300 # @return int Number of values that match
301 0 ▶sub groupMatch {
Subroutine groupMatch: not called CC=6 SCAR=37.4
Pod undocumented
302 0 my ( $groups, $attribute, $value ) = @_;
303 0 my $match = 0;
304  
305 0 foreach my $group ( keys %$groups ) {
306 0 ▶ if ( ref( $groups->{$group}->{$attribute} ) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $groups->{$group}{$attribute} eq 'ARRAY') { } 0 0 0
307 0 foreach ( @{ $groups->{$group}->{$attribute} } ) {
308 0 ▶ $match++ if ( $_ =~ /$value/ );
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /$value/ 0 0 0
309 }
310 }
311 else {
312 0 ▶ $match++ if ( $groups->{$group}->{$attribute} =~ /$value/ );
Branch: 1 branch
BranchTrueFalseTotal
if $groups->{$group}{$attribute} =~ /$value/ 0 0 0
313 }
314 }
315  
316 0 return $match;
317 }
318  
319 0 ▶sub isInNet6 {
Subroutine isInNet6: not called CC=2 SCAR=17.9
Pod undocumented
320 0 my ( $ip, $net ) = @_;
321 0 my ($bits) = $net =~ s#/(\d+)##;
322 0 ▶ return net6( $ip, $bits ) eq net6( $net, $bits ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
net6($ip, $bits) eq net6($net, $bits) ? : 0 0 0
323 }
324  
325 0 ▶sub varIsInUri {
Subroutine varIsInUri: not called CC=2 SCAR=17.9
Pod undocumented
326 0 my ( $uri, $wanteduri, $attribute, $restricted ) = @_;
327 0 ▶ return $restricted
Branch: 1 branch
BranchTrueFalseTotal
$restricted ? : 0 0 0
328 ? $uri =~ /$wanteduri$attribute$/
329 : $uri =~ /$wanteduri$attribute/;
330 }
331  
332 3 my $json = JSON::XS->new;
333  
334 3 ▶sub has2f_internal {
Subroutine has2f_internal: called CC=7 SCAR=22.8
Pod undocumented
335 3 my ( $session, $type ) = @_;
336 3 ▶ return 0 unless $session->{_2fDevices};
Branch: 1 branch
BranchTrueFalseTotal
unless $session->{"_2fDevices"} 1 2 3
337  
338 2 my $_2fDevices = eval { $json->decode( $session->{_2fDevices} ); };
339 2 ▶ return 0 if ( $@ or ref($_2fDevices) ne "ARRAY" );
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $_2fDevices ne "ARRAY" 0 2 2
Condition33%1/3
$@ or ref $_2fDevices ne "ARRAY"
l!l&&r!l&&!r
002
MC/DC0%0/2
$@ or ref $_2fDevices ne "ARRAY"
$@ ref $_2fDevices ne "ARRAY"
Truth table33%1/3
A or B
A= $@B= ref $_2fDevices ne "ARRAY"
ABresult
1X1
011
000
340  
341 # Empty array
342 2 ▶ return 0 unless scalar @{$_2fDevices};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$_2fDevices;} 1 1 2
343  
344 # Array has one value and we did not specify a type, succeed
345 1 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 0 1 1
346 0 my @found = grep { lc( $_->{type} ) eq lc($type) } @{$_2fDevices};
347 0 ▶ return @found ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@found ? : 0 0 0
348 }
349  
350 1 return 1;
351 }
352  
353 0 ▶sub inDomain_internal {
Subroutine inDomain_internal: not called CC=1 SCAR=6.9
Pod undocumented
354 0 my ( $host, $domain ) = @_;
355 0 my $res = $host =~ /(?:^|\.)\Q$domain\E$/i;
356 0 return $res;
357 }
358  
359 3 1;
« blib/lib/Lemonldap/NG/Common/EmailTransport.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Util.pm »