LoginCASLDAPFake.pm untested

filter: Statementstmt 0.0% 0 / 283 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 370 CC 28 SCAR 61.2
CC
28
Cov
0%
handler
812.0
my_none
2.0
my_entry
2.0
CRAP 455.6
1 package Lemonldap::Handlers::LoginCASLDAPFake;
2  
3 use strict;
4 use warnings;
5  
6 use Lemonldap::Config::Parameters;
7 use Lemonldap::Portal::Standard;
8 use Apache2::Const qw(DONE FORBIDDEN OK SERVER_ERROR REDIRECT);
9 use Apache2::Log();
10 use APR::Table;
11 use Apache2::RequestRec ();
12 use Apache2::ServerRec();
13 use Data::Dumper;
14 use Template;
15 use URI::Escape;
16 use CGI ':cgi-lib';
17 use Apache::Session::Memorycached;
18 use MIME::Base64;
19 use Encode qw(encode);
20 #use Lemonldap::Portal::Session;
21 use Net::LDAP::Entry;
22 use IO::File;
23 our $VERSION = '3.1.0';
24  
25 my $client_addr;
26 my $sessCacheRefreshPeriod;
27 my $log;
28 my $dump;
29 my $html;
30 my $Stack_User;
31  
32 #my $UserAttributes;
33 my $Major;
34 my $MyApplicationXmlFile;
35 my $MyDomain;
36 my $LoginPage;
37 my $RedirectPage;
38 my $Parameters;
39 my $Conf_Domain;
40 my $Login_Url;
41 my $Cookie_Name;
42  
43 #my $ipCheck;
44 #my $inactivityTimeout;
45 my $Ldap_Server;
46 my $Ldap_Branch_People;
47 my $Ldap_Dn_Manager;
48 my $Ldap_Pass_Manager;
49 my $Ldap_Port;
50 my @attrs;
51 my $Ldap_Search_Bases;
52 my @base;
53 my $MemcachedServer;
54 my $CookieName;
55 my $line_session;
56 my $_username;
57  
58 #my $InactivityTimeout;
59 #my $Encryptionkey;
60 my $page_html;
61 my $Menu;
62 my $Messages = {
63 1 =>
64 'Votre connexion a expiré vous devez vous authentifier de nouveau',
65 2 =>
66 'Les champs "Identifiant" et "Mot de passe&quot doivent être remplis',
67 3 => 'Wrong directory manager account or password',
68 4 => 'n\'a pas été trouvé dans l\'annuaire',
69 5 => 'Mot de passe erroné',
70 6 =>
71 'Votre adresse IP a été modifié. Vous devez vous authentifier de nouveau',
72 7 => 'Serveral Entries found on ldap server for this user',
73 8 => 'Bad connection to ldap server',
74 };
75  
76 sub my_none {
77 return 0;
78 }
79  
80 sub my_entry {
81 }
82  
83 sub my_credential {
84  
85 }
86  
87 sub My_Session {
88 my $self = shift;
89  
90 my %session;
91  
92 my $entry = $self->{entry};
93  
94 $self->{dn} = $entry->dn();
95  
96 my @T = ('uid',
97 'cn',
98 'mail',
99 'fonction',
100 'affectation',
101 'personaltitle',
102 'departement',
103 'grade',
104 'fonction',
105 );
106 for( @T ) {
107 $session{"$_"} = $entry->get_value("$_") ;
108 }
109 $self->{infosession} = \%session;
110  
111 }
112  
113 sub handler {
114 my $r = shift;
115 my $Stack_User;
116 $log = $r->log;
117 ### 1 tester le TGT cookie
118 my $entete2 = $r->headers_in();
119  
120 #Recuperation du Cookie
121 my $idx_tmp = $entete2->{'Cookie'};
122 my $local_cookie = "CASTGC";
123 my @tab = split /;/, $idx_tmp;
124 my $cookie;
125 my $idrecup;
126 my @base;
127 foreach (@tab) {
128 if (/$local_cookie=([^; ]+)/) {
129 $cookie = $_;
130 }
131 }
132 if ($cookie) {
133 ($idrecup) = $cookie =~ /TGT-(.+)/;
134 }
135  
136 if ( $Stack_User->{'AlreadyCreated'} ) {
137 undef $Stack_User->{'error'};
138 }
139 else {
140 $log = $r->log;
141 $MyApplicationXmlFile = $r->dir_config('ConfigFile');
142 $MyDomain = lc( $r->dir_config('Domain') );
143 $LoginPage = $r->dir_config('LoginPage');
144 $Major = $r->dir_config('Organization');
145 if ( !defined($Major) ) {
146 $Major = "authz_headers";
147 }
148 $Parameters =
149 Lemonldap::Config::Parameters->new( file => $MyApplicationXmlFile, );
150 $Conf_Domain = $Parameters->getDomain($MyDomain);
151 $Login_Url = $Conf_Domain->{Portal};
152 $Cookie_Name = $Conf_Domain->{Cookie};
153 $page_html = $Conf_Domain->{LoginCASPage};
154  
155 ################ enlevver ca ################
156  
157 ##############################################
158 if ( !$html ) {
159 my $file;
160 open( $file, "<$page_html" );
161 local $/;
162 $/ = '';
163 $html = <$file>;
164 close $file;
165 }
166  
167 $Ldap_Server = $Conf_Domain->{ldap_server};
168 $Ldap_Branch_People = $Conf_Domain->{ldap_branch_people};
169 $Ldap_Dn_Manager = $Conf_Domain->{DnManager};
170 $Ldap_Pass_Manager = $Conf_Domain->{passwordManager};
171 $Ldap_Port = $Conf_Domain->{ldap_port};
172 $Ldap_Search_Bases = $Ldap_Branch_People;
173 if ( defined( $r->dir_config('LdapSearchBases') ) ) {
174 $Ldap_Search_Bases =
175 $r->dir_config('LdapSearchBases') . ":" . $Ldap_Search_Bases;
176 }
177 @base = split( /\s*:\s*/, $Ldap_Search_Bases );
178 $MemcachedServer =
179 $Parameters->formateLineHash(
180 $Parameters->findParagraph( 'session', 'memcached' )
181 ->{SessionParams} );
182 $CookieName = $Conf_Domain->{Cookie};
183 my $_username;
184 $line_session = $Conf_Domain->{DefinitionSession};
185 $Stack_User = Lemonldap::Portal::Standard->new(
186 'msg' => $Messages,
187 'setSessionInfo' => \&My_Session,
188 );
189  
190 $Stack_User->{'AlreadyCreated'} = "true";
191 $Stack_User->{line_session} = $line_session;
192  
193 }
194 # compatibilité encore meilleure avec lemonldap
195 if (!$idrecup) {
196 my $lemon_cookie = $CookieName;
197 foreach (@tab) {
198 if (/$lemon_cookie=([^; ]+)/) {
199 $cookie = $_;
200 }
201 }
202 if ($cookie) {
203 ($idrecup) = ~ /=(.+)/;
204 }
205 }
206  
207  
208
209
210
211 if ($idrecup) { ## test itupon memcached
212 my %_it;
213 tie %_it, 'Apache::Session::Memorycached', $idrecup, $MemcachedServer;
214 if ( $_it{username} ) { # il n existe pas :
215 $_username = $_it{username};
216 }
217 untie %_it;
218 }
219  
220 my $UrlCode;
221 my $UrlDecode;
222 my $Erreur;
223 my %Params = Vars;
224 my $param_it;
225 my $buf;
226 my $l = Dumper( \%Params );
227 my $Retour;
228 my $service = $Params{service};
229  
230 ###
231 undef $_username if $Params{renew} eq 'true'; ## force re-sign
232 if ( defined $_username ) {
233 my $service = $Params{service};
234  
235 $MemcachedServer->{timeout} = '30';
236 $l = Dumper($MemcachedServer);
237  
238 ########### ticket service ######################
239 my %ticket;
240 tie %ticket, 'Apache::Session::Memorycached', undef, $MemcachedServer;
241 $ticket{service} = $service;
242 $ticket{principal} = $idrecup;
243 $ticket{username} = $_username;
244 my $idt = $ticket{'_session_id'};
245 untie %ticket;
246 ##########################################################################
247 my $redirection = uri_unescape($service);
248  
249 if ($redirection) {
250 $redirection .= "?ticket=ST-" . $idt;
251 print CGI::header( -Refresh => '0; URL=' . $redirection, );
252 return DONE;
253 }
254  
255 }
256 if ( $Params{'gateway'} eq 'true' ) {
257 my $redirection = uri_unescape($service);
258 print CGI::header( -Refresh => '0; URL=' . $redirection, );
259 return DONE;
260 }
261  
262 $Retour = $Stack_User->process(
263 'param' => \%Params,
264 'server' => $Ldap_Server,
265 'base' => \@base,
266 );
267 my $Message = '';
268 if ($Retour) {
269 $Message = $Retour->message;
270 $Erreur = $Retour->error;
271 }
272 print STDERR "MOTPASSE $Message\n";
273 ###########################################
274 if ( !$Erreur ) {
275 ## on regarde si it existe
276 if ( $Retour->{it} ) {
277 ### on le retrouve sur le serveur de session
278 my $it_l = $Retour->{it};
279 my %_it;
280  
281 tie %_it, 'Apache::Session::Memorycached', $it_l, $MemcachedServer;
282 if ( !$_it{STATUS} ) { # il n existe pas :
283 $Message .= "<br>$it_l Probleme de login ticket CAS\n";
284 $Erreur = 21;
285 }
286 else {
287 if ( $_it{STATUS} ne 'RESERVED' ) { # il existe mais it pris :
288 $Message .=
289 "<br> Probleme de ticket login CAS deja utilise \n";
290 $Erreur = 22;
291 }
292 }
293 $_it{username} = 'USED';
294 untie %_it;
295 delete $Retour->{it};
296 }
297 }
298 #########################################################################
299  
300 if ($Erreur) {
301 if ( !$Retour->{it} ) {
302  
303 #generer it token et le reserver
304 $MemcachedServer->{timeout} = '60';
305 my %_it;
306 tie %_it, 'Apache::Session::Memorycached', undef, $MemcachedServer;
307 $_it{STATUS} = 'RESERVED';
308 $param_it = $_it{'_session_id'};
309 untie %_it;
310 }
311 else { $param_it = $Retour->{it}; }
312  
313 if ( $Erreur == 3 ) {
314  
315 # Wrong directory manager account or password
316 $log->error( "LemonLDAP: " . $Message );
317 return Apache2::Const::SERVER_ERROR;
318 }
319 if ( $Erreur == 4 || $Erreur == 5 ) {
320  
321 # If bad login or password, refresh the login page with no information
322 $log->info( "LemonLDAP: " . $Message );
323  
324 #$Message = 'Authentification echou&eacute;e';
325 }
326  
327 # Login Page sending
328 my $Identifiant = $Retour->user;
329 my $secret = $Retour->secret;
330 ( $UrlCode, $UrlDecode ) = $Stack_User->getAllRedirection;
331 if ( $Retour->{CAS} ) {
332 $UrlDecode = $Retour->{service};
333 }
334 my $html_ok = $html;
335 $html_ok =~ s/%user%/$Identifiant/g;
336 $html_ok =~ s/%secret%/$secret/g;
337 $html_ok =~ s/%message%/$Message/g;
338 $html_ok =~ s/%urldc%/$UrlDecode/g;
339 $html_ok =~ s/%urlc%/$UrlCode/g;
340 $html_ok =~ s/%it%/$param_it/g;
341 $r->content_type('text/html');
342 $r->print;
343 $r->print($html_ok);
344 return DONE;
345 ########## ici page en cas d erreur #############
346 }
347 else {
348  
349 # Sending Redirect to Generic
350  
351 my $MyHashSession = $Retour->infoSession;
352 my $l = Dumper($MyHashSession);
353 ###############################################################
354 my $idTGT;
355 my $service = $Retour->{service};
356 ############ TGT ticket ############################"
357 ########### ticket service ######################
358 my %Session;
359 delete $MemcachedServer->{timeout};
360 tie %Session, 'Apache::Session::Memorycached', undef, $MemcachedServer;
361 $Session{init_service} = $service;
362 $Session{username} = $Retour->user;
363 $idTGT = $Session{'_session_id'};
364  
365 foreach ( keys %{$MyHashSession} ) {
366 $Session{$_} = $MyHashSession->{$_} if $MyHashSession->{$_};
367 }
368 untie %Session;
369  
370 ########### ticket service ######################
371 my %ticket;
372 $MemcachedServer->{timeout} = '30';
373 $l = Dumper($MemcachedServer);
374 tie %ticket, 'Apache::Session::Memorycached', undef, $MemcachedServer;
375  
376 $ticket{service} = $service;
377 $ticket{username} = $Retour->user;
378 my $idt = $ticket{'_session_id'};
379 $ticket{principal} = $idTGT;
380 untie %ticket;
381 ##########################################################################
382 my $pathlemon = "/";
383 my $pathCookie = "/cas";
384 my $dotdomain = "." . $MyDomain;
385  
386 $log->info(
387 "Set-Cookie: -name => $CookieName -value => $idt -domain => "
388 . ".$dotdomain -path => $pathCookie" );
389 my $CASCookie = CGI::cookie(
390 -name => "CASTGC",
391 -value => "TGT-" . $idTGT,
392 -domain => $dotdomain,
393 -path => $pathCookie,
394 );
395  
396 my $LemonCookie = CGI::cookie(
397 -name => $CookieName,
398 -value => $idTGT,
399 -domain => $dotdomain,
400 -path => $pathlemon,
401 );
402  
403 my $redirection = $Retour->CASservice;
404 $redirection = uri_unescape($redirection);
405  
406 if ($redirection) {
407 $redirection .= "?ticket=ST-" . $idt;
408 print CGI::header(
409 -Refresh => '0; URL=' . $redirection,
410 -cookie => [ $LemonCookie, $CASCookie ]
411 );
412 return DONE;
413 }
414  
415 else {
416 $r->content_type('text/html');
417 $r->headers_out->add( 'Set-Cookie' => $LemonCookie );
418 $r->headers_out->add( 'Set-Cookie' => $CASCookie );
419 $redirection="/cas/logged.html";
420 print CGI::header(
421 -Refresh => '0; URL='.$redirection,
422 -cookie => [ $LemonCookie, $CASCookie ]
423 );
424 return DONE;
425 }
426 }
427  
428 }
429 1;