| line | stmt | bran | cond | sub | pod | time | code | 
| 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 | 1 |  |  | 1 |  | 22651 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 49 |  | 
| 9 | 1 |  |  | 1 |  | 825 | use Encode; | 
|  | 1 |  |  |  |  | 15232 |  | 
|  | 1 |  |  |  |  | 136 |  | 
| 10 | 1 |  |  | 1 |  | 740 | use MIME::Base64; | 
|  | 1 |  |  |  |  | 1733 |  | 
|  | 1 |  |  |  |  | 1052 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | #use AutoLoader qw(AUTOLOAD); | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our $VERSION = '1.0.0'; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Set here all the names of functions that must be available in Safe objects. | 
| 17 |  |  |  |  |  |  | # Not that only functions, not methods, can be written here | 
| 18 |  |  |  |  |  |  | our $functions = | 
| 19 |  |  |  |  |  |  | [qw(&checkLogonHours &checkDate &basic &unicode2iso &iso2unicode)]; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | ## @function boolean checkLogonHours(string logon_hours, string syntax, string time_correction, boolean default_access) | 
| 22 |  |  |  |  |  |  | # Function to check logon hours | 
| 23 |  |  |  |  |  |  | # @param $logon_hours string representing allowed logon hours (GMT) | 
| 24 |  |  |  |  |  |  | # @param $syntax optional hexadecimal (default) or octetstring | 
| 25 |  |  |  |  |  |  | # @param $time_correction optional hours to add or to subtract | 
| 26 |  |  |  |  |  |  | # @param $default_access optional what result to return for users without logons hours | 
| 27 |  |  |  |  |  |  | # @return 1 if access allowed, 0 else | 
| 28 |  |  |  |  |  |  | sub checkLogonHours { | 
| 29 | 0 |  |  | 0 | 0 |  | my ( $logon_hours, $syntax, $time_correction, $default_access ) = splice @_; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # Active Directory - logonHours: $attr_src_syntax = octetstring | 
| 32 |  |  |  |  |  |  | # Samba - sambaLogonHours: ??? | 
| 33 |  |  |  |  |  |  | # LL::NG - ssoLogonHours: $attr_src_syntax = hexadecimal | 
| 34 | 0 |  | 0 |  |  |  | $syntax ||= "hexadecimal"; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # Default access if no value | 
| 37 | 0 |  | 0 |  |  |  | $default_access ||= "0"; | 
| 38 | 0 | 0 |  |  |  |  | return $default_access unless $logon_hours; | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # Get the base2 value of logon_hours | 
| 41 |  |  |  |  |  |  | # Each byte represent an hour of the week | 
| 42 |  |  |  |  |  |  | # Begin with sunday at 0h00 | 
| 43 | 0 |  |  |  |  |  | my $base2_logon_hours; | 
| 44 | 0 | 0 |  |  |  |  | if ( $syntax eq "octetstring" ) { | 
| 45 | 0 |  |  |  |  |  | $base2_logon_hours = unpack( "B*", $logon_hours ); | 
| 46 |  |  |  |  |  |  | } | 
| 47 | 0 | 0 |  |  |  |  | if ( $syntax eq "hexadecimal" ) { | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | # Remove white spaces | 
| 50 | 0 |  |  |  |  |  | $logon_hours =~ s/ //g; | 
| 51 | 0 |  |  |  |  |  | $base2_logon_hours = unpack( "B*", pack( "H*", $logon_hours ) ); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # Get the present day and hour | 
| 55 | 0 |  |  |  |  |  | my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = | 
| 56 |  |  |  |  |  |  | gmtime(time); | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | # Get the hour position | 
| 59 | 0 |  |  |  |  |  | my $hourpos = $wday * 24 + $hour; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # Use time_correction | 
| 62 | 0 | 0 |  |  |  |  | if ($time_correction) { | 
| 63 | 0 |  |  |  |  |  | my ( $sign, $time ) = ( $time_correction =~ /([+|-]?)(\d+)/ ); | 
| 64 | 0 | 0 |  |  |  |  | if ( $sign =~ /-/ ) { $hourpos -= $time; } | 
|  | 0 |  |  |  |  |  |  | 
| 65 | 0 |  |  |  |  |  | else                { $hourpos += $time; } | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # Get the corresponding byte | 
| 69 | 0 |  |  |  |  |  | return substr( $base2_logon_hours, $hourpos, 1 ); | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | ## @function boolean checkDate(string start, string end, boolean default_access) | 
| 73 |  |  |  |  |  |  | # Function to check a date | 
| 74 |  |  |  |  |  |  | # @param $start string Start date (GMT) | 
| 75 |  |  |  |  |  |  | # @param $end string End date (GMT) | 
| 76 |  |  |  |  |  |  | # @param $default_access optional what result to return for users without start or end start | 
| 77 |  |  |  |  |  |  | # @return 1 if access allowed, 0 else | 
| 78 |  |  |  |  |  |  | sub checkDate { | 
| 79 | 0 |  |  | 0 | 0 |  | my ( $start, $end, $default_access ) = splice @_; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | # Get date in string | 
| 82 | 0 |  |  |  |  |  | $start = substr( $start, 0, 14 ); | 
| 83 | 0 |  |  |  |  |  | $end   = substr( $end,   0, 14 ); | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | # Default access if no value | 
| 86 | 0 |  | 0 |  |  |  | $default_access ||= "0"; | 
| 87 | 0 | 0 | 0 |  |  |  | return $default_access unless ( $start or $end ); | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | # If no start, set start to 0 | 
| 90 | 0 |  | 0 |  |  |  | $start ||= 0; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # If no end, set end to the end of the world | 
| 93 | 0 |  | 0 |  |  |  | $end ||= 999999999999999; | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | # Get the present day and hour | 
| 96 | 0 |  |  |  |  |  | my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = | 
| 97 |  |  |  |  |  |  | gmtime(time); | 
| 98 | 0 |  |  |  |  |  | $year += 1900; | 
| 99 | 0 |  |  |  |  |  | $mon  += 1; | 
| 100 | 0 | 0 |  |  |  |  | $mon  = "0" . $mon  if ( $mon < 10 ); | 
| 101 | 0 | 0 |  |  |  |  | $mday = "0" . $mday if ( $mday < 10 ); | 
| 102 | 0 | 0 |  |  |  |  | $hour = "0" . $hour if ( $hour < 10 ); | 
| 103 | 0 | 0 |  |  |  |  | $min  = "0" . $min  if ( $min < 10 ); | 
| 104 | 0 | 0 |  |  |  |  | $sec  = "0" . $sec  if ( $sec < 10 ); | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 0 |  |  |  |  |  | my $date = $year . $mon . $mday . $hour . $min . $sec; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 0 | 0 | 0 |  |  |  | return 1 if ( ( $date >= $start ) and ( $date <= $end ) ); | 
| 109 | 0 |  |  |  |  |  | return 0; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | ## @function string basic(string login, string password) | 
| 113 |  |  |  |  |  |  | # Return string that can be used for HTTP-BASIC authentication | 
| 114 |  |  |  |  |  |  | # @param login User login | 
| 115 |  |  |  |  |  |  | # @param password User password | 
| 116 |  |  |  |  |  |  | # @return Authorization header content | 
| 117 |  |  |  |  |  |  | sub basic { | 
| 118 | 0 |  |  | 0 | 0 |  | my ( $login, $password ) = splice @_; | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | # UTF-8 strings should be ISO encoded | 
| 121 | 0 |  |  |  |  |  | $login    = &unicode2iso($login); | 
| 122 | 0 |  |  |  |  |  | $password = &unicode2iso($password); | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 |  |  |  |  |  | return "Basic " . encode_base64( $login . ":" . $password ); | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | ## @function string unicode2iso(string string) | 
| 128 |  |  |  |  |  |  | # Convert UTF-8 in ISO-8859-1 | 
| 129 |  |  |  |  |  |  | # @param string UTF-8 string | 
| 130 |  |  |  |  |  |  | # @return ISO string | 
| 131 |  |  |  |  |  |  | sub unicode2iso { | 
| 132 | 0 |  |  | 0 | 0 |  | my ($string) = splice @_; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 0 |  |  |  |  |  | return encode( "iso-8859-1", decode( "utf-8", $string ) ); | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | ## @function string iso2unicode(string string) | 
| 138 |  |  |  |  |  |  | # Convert ISO-8859-1 in UTF-8 | 
| 139 |  |  |  |  |  |  | # @param string ISO string | 
| 140 |  |  |  |  |  |  | # @return UTF-8 string | 
| 141 |  |  |  |  |  |  | sub iso2unicode { | 
| 142 | 0 |  |  | 0 | 0 |  | my ($string) = splice @_; | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 0 |  |  |  |  |  | return encode( "utf-8", decode( "iso-8859-1", $string ) ); | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | 1; | 
| 148 |  |  |  |  |  |  | __END__ |