Utilities.pm untested

filter: Statementstmt 0.0% 0 / 170 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 251 CC 23 SCAR 29.1
CC
23
Cov
0%
cleanupcookie
30.0
get_my_timeout
30.0
rewrite_cookie
30.0
CRAP 18.3
1 package Lemonldap::Handlers::Utilities;
2 use Apache2::Const;
3 use Apache::Session::Memorycached;
4 use BerkeleyDB;
5 use MIME::Base64;
6 use Crypt::CBC;
7 use URI::Escape;
8 use Data::Dumper;
9 use Template;
10 use CGI ':cgi-lib';
11 use Sys::Hostname;
12 use strict;
13 our ( @ISA, $VERSION, @EXPORTS );
14 $VERSION = '3.5.3';
15 our $VERSION_LEMONLDAP = "3.5.3";
16 our $VERSION_INTERNAL = "3.5.3";
17 my %STACK;
18 ###########################################################
19 # cleanupcookie function (config,cookie line) #
20 # return $id storing in lemonldap cookie #
21 # and remove lemonldap cookie of header cookie #
22 # if STOPCCOKIE is actived #
23 # Should return undef,undef wihtout $id and cookie #
24 # #
25 ###########################################################
26 sub cleanupcookie {
27 ( my $config, my $cookie_line ) = @_;
28 return ( undef, undef ) unless $cookie_line;
29 my $local_cookie = $config->{'COOKIE'};
30 my @tab = split /;/, $cookie_line;
31 my @tmp;
32 my $id;
33 foreach (@tab) {
34 if (/$local_cookie=([^; ]+)/) {
35 push @tmp, $_ unless ( $config->{STOPCOOKIE} );
36 $id = $1;
37 $id =~ s/\s//g; # remove space
38 }
39 else { push @tmp, $_; }
40 }
41 my $ret;
42  
43 if (@tmp) {
44 $ret = join ";", @tmp;
45 }
46 return ( $id, $ret );
47 }
48  
49 sub get_my_timeout {
50 ( my $config, my $cookie_line ) = @_;
51 return ( undef, undef ) unless $cookie_line;
52 my $local_cookie = $config->{'COOKIE'};
53 my @tab = split /;/, $cookie_line;
54 my @tmp;
55 my $cookie;
56 my $id;
57 my $sep;
58  
59 foreach (@tab) {
60 if (/$local_cookie=([^; ]+)/) {
61 $cookie = $_;
62 }
63 else {
64 push @tmp, $_;
65 }
66  
67 }
68 $sep = "_";
69  
70 #On separe le time_end et l'id de session
71 my @tab_tmp = split( $sep, $cookie );
72 my $id = $tab_tmp[0];
73 my $timeout = $tab_tmp[1];
74 if ( defined( $config->{ENCRYPTIONKEY} ) ) {
75 my $clef = $config->{ENCRYPTIONKEY};
76 my $cipher = new Crypt::CBC(
77 -key => $clef,
78 -cipher => 'Blowfish',
79 -iv => 'lemonlda',
80 -header => 'none'
81 );
82 $timeout = $cipher->decrypt_hex($timeout);
83 }
84 push @tmp, $id;
85  
86 my $ret;
87  
88 if (@tmp) {
89 $ret = join ";", @tmp;
90  
91 }
92  
93 return ( $ret, $timeout );
94 }
95  
96 sub rewrite_cookie {
97 ( my $cookie_line, my $config ) = @_;
98 my $local_domain = $config->{'DOMAIN'};
99 my @tab = split /;/, $cookie_line;
100 my @tmp;
101 my $flag;
102 foreach (@tab) {
103 next if /path/;
104  
105 # $date = $_ if /expire/i;
106  
107 ( push @tmp, $_ ) and (next) unless /domain/;
108 ( my $domain ) = /domain\s?=\s?([^; ]+)/;
109 if ( $domain =~ /$local_domain/i ) {
110 push @tmp, $_;
111 }
112 else {
113 $flag = 1;
114 my $l = 'domain = .' . $local_domain;
115 push @tmp, $l;
116 }
117  
118 }
119 my $ret = join ";", @tmp;
120 if ($flag) {
121 return $ret;
122 }
123 else { return $cookie_line; }
124  
125 }
126  
127 sub cache2 {
128 my ( $path, $pid, $id ) = @_;
129 my $message;
130 my $ligne_h;
131 tie %STACK, 'BerkeleyDB::Btree',
132 -Filename => "$path/$pid.db",
133 -Flags => DB_CREATE;
134 $ligne_h = $STACK{$id};
135 if ($ligne_h) { ## match in ipc
136 $message = "match in cache level 2 for $id";
137 untie %STACK;
138 }
139 else {
140 $message = "No match in cache level 2 for $id";
141 }
142  
143 return ( $ligne_h, $message );
144  
145 }
146  
147 sub goPortal {
148 my ( $r, $conf, $op, $id ) = @_;
149 my $log = $r->log;
150 my %CONFIG = %$conf;
151 my $test = $r->construct_url();
152  
153 #ATTENTION : ne valide que les http et https
154 my $prot;
155  
156 if ( $test =~ /^https/ ) {
157 $prot = "https://";
158 }
159 else {
160 $prot = "http://";
161 }
162  
163 my $urlc_init = $prot . $r->headers_in->{Host} . $r->uri;
164 $urlc_init .= "?" . $r->args if $r->args;
165 my $urlc_initenc = encode_base64( $urlc_init, "" );
166 $r->err_headers_out->add( Pragma => 'no-cache' );
167 if ($CONFIG{URLCDATIMEOUT} && $op eq 'x' ) {
168 $r->headers_out->add(
169 Location => $CONFIG{URLCDATIMEOUT} . "?op=$op&url=$urlc_initenc" );
170 } else {
171 $r->headers_out->add(
172 Location => $CONFIG{PORTAL} . "?op=$op&url=$urlc_initenc" );
173 }
174 $r->err_headers_out->add( Connection => 'close' );
175 $log->error(
176 "SERVER MEMCACHED UNREACHABLE. PLEASE CHECK IF YOUR SERVER IS ON OR IF YOUR CONFIGURATION FILE IS CORRECT"
177 )
178 if ( $op eq 'm' );
179 my $messagelog =
180 "$CONFIG{HANDLERID} : Redirect to portal (url was " . $urlc_init . ")";
181 $log->info($messagelog);
182 return REDIRECT;
183  
184 }
185  
186 sub save_session {
187 my $id = shift;
188 my $trace = shift;
189 $STACK{$id} = $trace;
190 untie %STACK;
191 }
192  
193 sub fake_refresh_ldap {
194  
195 my $HashSession = shift;
196 my $config = shift;
197 my $ttl = shift;
198 my $new_SessExp;
199 my $central = $config->{SERVERS};
200 my $refresh = $config->{SESSCACHEREFRESHPERIOD};
201 $central->{timeout} = $ttl;
202  
203 #No insertion in Memcached before untie
204 $central->{updateOnly} = 1;
205  
206 my %Session;
207 tie %Session, 'Apache::Session::Memorycached', undef, $central;
208 foreach ( keys %{$HashSession} ) {
209 if ( "SessExpTime" eq $_ ) {
210 $new_SessExp = time() + $refresh;
211 $HashSession->{$_} = $new_SessExp;
212 }
213 $Session{$_} = $HashSession->{$_} if $HashSession->{$_};
214 }
215 untie %Session;
216 return $new_SessExp;
217  
218 }
219  
220 sub save_memcached_local {
221  
222 my $HashSession = shift;
223 my $local = shift;
224 my $ttl = shift;
225 my $safe = $local->{'servers'};
226  
227 if ( $local->{'servers'} ) {
228 delete $local->{'servers'};
229 }
230 $local->{timeout} = $ttl;
231  
232 #No insertion in Memcached before untie
233 $local->{updateOnly} = 1;
234  
235 my %Session;
236 tie %Session, 'Apache::Session::Memorycached', undef, $local;
237 foreach ( keys %{$HashSession} ) {
238  
239 $Session{$_} = $HashSession->{$_} if $HashSession->{$_};
240 }
241  
242 untie %Session;
243 if ($safe) {
244 $local->{'servers'} = $safe;
245 }
246  
247 }
248  
249 1;