ValidateCAS.pm untested

filter: Statementstmt 0.0% 0 / 241 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 357 CC 23 SCAR 56.7
CC
23
Cov
0%
handler
552.0
BEGIN
2.0
BEGIN
2.0
CRAP 289.5
1 package Lemonldap::Handlers::ValidateCAS;
2 use strict;
3  
4 ##### use ######
5 use Apache2::URI();
6 use Apache2::Const;
7 use Apache2::Connection;
8 use Apache2::ServerUtil ();
9 use MIME::Base64;
10 use LWP::UserAgent;
11 use Lemonldap::Handlers::MemsessionCAS;
12 use Lemonldap::Config::Parameters;
13 use Lemonldap::Config::Initparam;
14 use Lemonldap::Handlers::Utilities;
15 use Lemonldap::Handlers::CoreCAS;
16 use Apache2::Log();
17 use Apache2::ServerRec();
18 use CGI ':cgi-lib';
19 use CGI::Cookie;
20 use Crypt::CBC;
21 use URI::Escape;
22 use Template;
23 use Sys::Hostname;
24  
25 #A retirer en prod
26 use Data::Dumper;
27 #### common declaration #######
28 our ( @ISA, $VERSION, @EXPORTS );
29 $VERSION = '4.0.0';
30 our $VERSION_LEMONLDAP = "3.1.0";
31 our $VERSION_INTERNAL = "3.1.0";
32  
33 ####
34 ####
35 #### my declaration #########
36 my %CONFIG;
37 my %CLIENT;
38 my $SAVE_MHURI;
39 my $NOM;
40 my $UA;
41 @ISA = qw(LWP::UserAgent );
42 my $ID_COLLECTED;
43 my $__STACK;
44 my %STACK;
45 my $ID_SAVE;
46 my $s = Apache2::ServerUtil->server;
47  
48 ### this anonymous function will be call when child dead , it will delete berkeley db file
49 my $cleanup = sub {
50 my $s = Apache2::ServerUtil->server;
51 my $srv_cfg = $s->dir_config;
52  
53 my $vhosts = 0;
54 my $path_other;
55 for ( my $ser = $s->next ; $ser ; $ser = $ser->next ) {
56 $vhosts++;
57 $path_other = $ser->dir_config('cachedbpath') unless $path_other;
58  
59 }
60  
61 my $path = $srv_cfg->{'cachedbpath'} || $path_other;
62 unlink "$path/$$.db" if $path;
63  
64 };
65  
66 Apache2::ServerUtil->server->push_handlers( PerlChildExitHandler => $cleanup );
67  
68 sub handler {
69 my $r = shift;
70  
71 # URL des pages d'erreur a ne pas traiter
72 if ( $r->uri =~ /^\/LemonErrorPages/ ) {
73 return DECLINED;
74 }
75 ########################
76 ## log initialization
77 ########################
78 my $log = $r->log;
79 my $messagelog;
80 my $cache2file;
81 my $APACHE_CODE;
82  
83 # Url a ne pas traiter meme sans conf
84 # exemple
85 # PerlSetVar ExcludeRegex
86 #(?i)(\.smi|\.swf|\.vrml|\.ico|\.tif|\.gif|\.jpg|\.jpeg|\.js|\.css|\.jpeg|\.png|\.avi|ajaxaction|pngbehavior\.jsp)
87  
88 my $regex = $r->dir_config('excluderegex');
89 if ( defined $regex ) {
90 $log->debug("REGEXP : $regex\n");
91 my $uri_input = $r->uri;
92 if ( $uri_input =~ /$regex/o ) {
93 $log->debug("$uri_input : EXCLUDED\n");
94 return DECLINED;
95 }
96 }
97  
98 ########################
99 ## collect httpd param
100 ########################
101 $ID_COLLECTED = '';
102 my $con = $r->dir_config();
103  
104 $log->info($messagelog);
105  
106 my $in_process = $r->dir_config('handlerid');
107 if ( $CONFIG{$in_process} ) {
108 $log->info(
109 "$CONFIG{$in_process}->{HANDLERID} XML $CONFIG{$in_process}->{XML} config already in use"
110 );
111 $ID_COLLECTED = $in_process;
112 }
113 else {
114 my $conf =
115 &Lemonldap::Config::Initparam::init_param_httpd( $log, $con );
116  
117 #Domain insensible a la casse
118 $conf->{DOMAIN} = lc( $conf->{DOMAIN} );
119  
120 #/Domain insensible a la casse
121 $ID_COLLECTED = $conf->{HANDLERID};
122 $CONFIG{$ID_COLLECTED} = $conf;
123  
124 ### I will try retieve HANDLERID from httpd conf
125 if ($ID_COLLECTED) {
126 $NOM = $ID_COLLECTED;
127 $messagelog =
128 "$NOM Phase : handler initialization LOAD HANDLERID httpd.conf:$CONFIG{$ID_COLLECTED}->{HANDLERID} : succeded";
129 }
130 else {
131  
132 # I don't find anything for this handler in order to make link with XLM conf section
133 $messagelog =
134 "$NOM: Phase : handler initialization LOAD HANDLERID httpd.conf:failed";
135 }
136 $log->info($messagelog);
137  
138 ############################################
139  
140 # my $ref = $CONFIG{$ID_COLLECTED}->{HANDLERID};
141 # $ref =~ s/\/.+// ;
142 $log->info(
143 "$CONFIG{$ID_COLLECTED}->{HANDLERID}: Phase : handler initialization LOAD XML file $CONFIG{$ID_COLLECTED}->{CONFIGFILE} and $CONFIG{$ID_COLLECTED}->{CONFIGDBPATH}"
144 );
145 $log->info(
146 "$CONFIG{$ID_COLLECTED}->{HANDLERID}: domain matched $CONFIG{$ID_COLLECTED}->{DOMAIN}"
147 );
148 if ( defined( $CONFIG{$ID_COLLECTED}->{CONFIGFILE} ) ) {
149  
150 # my $ref = $CONFIG{$ID_COLLECTED}->{HANDLERID};
151 # $ref =~ s/\/.+// ;
152 $log->info(
153 "$CONFIG{$ID_COLLECTED}->{HANDLERID}: Phase : handler initialization LOAD XML file $CONFIG{$ID_COLLECTED}->{CONFIGFILE} and $CONFIG{$ID_COLLECTED}->{CONFIGDBPATH}"
154 );
155 $log->info(
156 "$CONFIG{$ID_COLLECTED}->{HANDLERID}: domain matched $CONFIG{$ID_COLLECTED}->{DOMAIN}"
157 );
158 $conf =
159 &Lemonldap::Config::Initparam::init_param_xml(
160 $CONFIG{$ID_COLLECTED} );
161 $log->info("$conf->{message}");
162 }
163 else {
164 $conf = {};
165 }
166 my $c =
167 &Lemonldap::Config::Initparam::merge( $CONFIG{$ID_COLLECTED}, $conf );
168 $CONFIG{$ID_COLLECTED} = $c;
169  
170 # $CONFIG{$ID_COLLECTED}->{KEYIPC} .= "$$.db" if ($CONFIG{$ID_COLLECTED}->{KEYIPC});
171  
172 }
173  
174 #############################Test de CONFIG####################################
175 #################################################################################
176  
177 ## now I save the context of handler
178 $log->info("$CONFIG{$ID_COLLECTED}->{HANDLERID} end of initialization");
179  
180 ## addon for FASTPATTERNS
181 if ( ( $CONFIG{$ID_COLLECTED}->{FASTPATTERNS} )
182 && !( $CONFIG{$ID_COLLECTED}->{ANONYMOUSFUNC} ) )
183 {
184 my $sub =
185 &Lemonldap::Config::Initparam::built_functionics(
186 $CONFIG{$ID_COLLECTED}->{FASTPATTERNS} );
187 $CONFIG{$ID_COLLECTED}->{ANONYMOUSFUNC_SRC} = $sub;
188 $CONFIG{$ID_COLLECTED}->{ANONYMOUSFUNC} = eval "$sub";
189 $log->info(
190 "$CONFIG{$ID_COLLECTED}->{HANDLERID}: Phase : FASTPATTERNS TABLE LOADED : $sub"
191 );
192 }
193  
194 ## addon for multihoming
195 if ( ( $CONFIG{$ID_COLLECTED}->{MULTIHOMING} )
196 && !( $CONFIG{$ID_COLLECTED}->{SELECTOR} ) )
197 {
198 my $sub = $CONFIG{$ID_COLLECTED}->{SUB};
199 $CONFIG{$ID_COLLECTED}->{SELECTOR_SRC} = $sub;
200 $CONFIG{$ID_COLLECTED}->{SELECTOR} = eval "$sub";
201 }
202  
203 ################
204  
205 ##
206 foreach ( keys %{ $CONFIG{$ID_COLLECTED} } ) {
207 if ( $_ ne "ENCRYPTIONKEY" ) {
208 $log->info("$ID_COLLECTED:$_ => $CONFIG{$ID_COLLECTED}->{$_}");
209 }
210 }
211  
212 ##### end of initialization
213 ##### begin process request
214 my $uri = $r->uri;
215 my %param = Vars();
216 my $ticket = $param{'ticket'};
217 my $service = $param{'service'};
218 ( my $t_service ) = $service =~ /:\/\/(.+)/;
219 $t_service = "/" . $t_service;
220 $log->info(
221 "$CONFIG{$ID_COLLECTED}->{HANDLERID} :service requested: $t_service");
222  
223 #### multihoming
224 my $MHURI;
225 if ( $CONFIG{$ID_COLLECTED}->{MH} ) {
226  
227 $MHURI = $CONFIG{$ID_COLLECTED}->{SELECTOR}->($t_service);
228  
229 # Stop process if no multihosting
230 if ( ( $MHURI eq '1' ) || ( !($MHURI) ) ) {
231 $log->warn(
232 "$CONFIG{$ID_COLLECTED}->{HANDLERID} :multihoming failed for $t_service"
233 );
234 return DECLINED;
235 }
236  
237 # load combo config
238 ### I switch the context#
239 my $old_collected = $ID_COLLECTED;
240 $ID_COLLECTED = $MHURI;
241  
242 $log->info(
243 "$CONFIG{$old_collected}->{HANDLERID} :SWITCH CONFIG $MHURI" );
244 if ( $CONFIG{$ID_COLLECTED}->{XML} ) {
245 $log->info(
246 "$CONFIG{$ID_COLLECTED}->{HANDLERID} :MULTIHOMING already in use "
247 );
248 }
249 else {
250 my $c =
251 &Lemonldap::Config::Initparam::mergeMH( $CONFIG{$old_collected},
252 $MHURI );
253 $CONFIG{$ID_COLLECTED} = $c;
254 }
255  
256 $log->info("$CONFIG{$ID_COLLECTED}->{HANDLERID} :MULTIHOMING ON");
257 }
258  
259 #########################
260 ##### for developper ###
261 if ( ( $uri =~ /_lemonldap_internal/i ) && ( $con->get('internaldebug') ) )
262 {
263  
264 $r->content_type('text/html');
265 $r->print(<<END);
266 <html>
267 <head><title>lemonldap websso and CAS server</title></head>
268 <body>
269 <h1>Lemonldap websso ans CAS internal table</h1>
270 <p>
271  
272 END
273  
274 foreach ( keys %CONFIG ) {
275 $r->print("$_<br>\n");
276 }
277 $r->print("<hr>\n");
278 my $tmp = $CONFIG{$ID_COLLECTED};
279 $r->print("<h3>$ID_COLLECTED on $$</H3>\n");
280  
281 foreach ( keys %$tmp ) {
282 if ( ref $tmp->{$_} ) {
283 my $t = ref $tmp->{$_};
284 $r->print("$_ => $t reference<br>\n");
285 }
286 else {
287 $r->print("$_ => $tmp->{$_}<br>\n");
288 }
289  
290 }
291 $r->print("</body></html>");
292  
293 return OK;
294 }
295  
296 if ( ( $CONFIG{$ID_COLLECTED}->{FASTPATTERNS} )
297 && ( $CONFIG{$ID_COLLECTED}->{ANONYMOUSFUNC}->($uri) eq 'OK' ) )
298 {
299 $log->info(
300 "$CONFIG{$ID_COLLECTED}->{HANDLERID} :uri FASTPATTERNS matched: $uri"
301 );
302 return DECLINED;
303 }
304 $APACHE_CODE = DECLINED;
305  
306 if ( $CONFIG{$ID_COLLECTED}->{ENABLELWP} ) {
307 $UA = __PACKAGE__->new;
308 $UA->agent( join "/", __PACKAGE__, $VERSION );
309 $log->info(
310 "$CONFIG{$ID_COLLECTED}->{HANDLERID}: Build-in proxy actived");
311 $r->handler("perl-script");
312 $r->push_handlers( PerlHandler => \&proxy_handler );
313 }
314  
315 ### before to enter in protected area
316 ###
317 # return $APACHE_CODE if ( $CONFIG{$ID_COLLECTED}->{DISABLEACCESSCONTROL} );
318  
319 ### raz cache level 1
320 # is this area protected
321 # configuration check
322 #
323  
324 # collect ticket and service
325  
326 $ticket =~ s/ST-//;
327  
328 ( my $serveursession, my $principalsession ) =
329 Lemonldap::Handlers::MemsessionCAS->get(
330 'id' => $ticket,
331 'config' => $CONFIG{$ID_COLLECTED}
332 );
333 my $servicevalid = $serveursession->{service};
334 my $user = $serveursession->{username};
335 my $response;
336 my $ok;
337 my $cont;
338 if ( $servicevalid and $serveursession->{VALID} eq 'TRUE' ) {
339 $ok = 1;
340 }
341  
342 if ( $CONFIG{$ID_COLLECTED}->{DISABLEACCESSCONTROL} ) {
343 $ok=1;
344 $cont = 1;
345 }
346 # mes modif
347
348  
349 else {
350  
351 ################ verif sur les habiliatation #############
352 my $controle = &Lemonldap::Handlers::CoreCAS::locationRules(
353 config => $CONFIG{$ID_COLLECTED},
354 session => $principalsession
355 );
356 if ( ( !$controle == 0 ) and ( defined $controle->{string} ) ) {
357 $cont = 1;
358 }
359 }
360 if (( $ok == 1 ) and ($cont ==1 )) {
361 $log->info(
362 "$CONFIG{$ID_COLLECTED}->{HANDLERID}:$user -- $servicevalid ACCEPTED"
363 );
364 ##### if cas 1 ####
365 if ( $uri !~ /validate/i ) {
366 $response = "yes\n";
367 $response .= "$user\n";
368 }
369 else { # cas v2
370 $response =
371 "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\n";
372 $response .= "\t<cas:authenticationSuccess>\n";
373 $response .= "\t\t<cas:user>$user</cas:user>\n";
374 $response .= "\t</cas:authenticationSuccess>\n";
375 $response .= "</cas:serviceResponse>\n";
376 }
377  
378 $r->content_type('text/html');
379 $r->print;
380 $r->print($response);
381  
382 }
383 else {
384  
385 #delivrer reponse non
386 $log->info(
387 "$CONFIG{$ID_COLLECTED}->{HANDLERID}:$user -- $servicevalid REJETED"
388 );
389  
390 if ( $uri !~ /validate/i ) {
391 $response = "no\n\n";
392 }
393 else { # cas v2
394 $response =
395 "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\n";
396 $response .=
397 "\t<cas:authenticationFailure code=\"INVALID_TICKET\">\n";
398 $response .= "\t\tTicket ST-$ticket not found\n";
399 $response .= "\t</cas:authenticationFailure>\n";
400 $response .= "</cas:serviceResponse>\n";
401 }
402  
403 $r->content_type('text/html');
404 $r->print;
405 $r->print($response);
406 }
407  
408 return OK;
409 }
410  
411 ###########################################################
412 # _lemonldap_internal handler #
413 # add at your url /_lemonldap_internal #
414 # eg : http://appli1.demo.net/_lemonldap_internal #
415 # in order to dump the internal config of apache children #
416 # add ?id=handler append another config handler on output #
417 # #
418 ###########################################################
419  
420 ############################
421 # #
422 # end _lemonldap_internal #
423 # handler #
424 # #
425 ############################
426  
427 1;
428  
429 ###############################
430 ### fin etape 1
431 ##############################
432  
433 =pod
434  
435 =head1 NAME
436  
437 Lemonldap::Handlers::CAS - Perl extension for Lemonldap webSSO
438  
439 =head1 SYNOPSIS
440  
441 use Lemonldap::Handlers::Validate ### Validate service ticket
442 use Lemonldap::Handlers::LoginCASFake ### Fake login : user must be egal to password (like CAS server demo)
443 use Lemonldap::Handlers::LogoutCAS ### logout SSO
444
445 =head1 DESCRIPTION
446
447 Lemonldap is a Reverse-proxy webSSO and CAS (Central Authentification Service) is an another websso from Yales university .
448 CAS acts like Authentification service NOT for authorization service .
449
450 These modules give the capacity at a lemonldap to become CAS server.
451 So , an user will be authenticate on CAS server AND on lemonldap.
452 Then the service ticket is send to serviceValidate the lemonldap can retrieve all session for user and process to authorization like a lemonldap .
453
454 =head1 Compatibility with CAS protocol.
455  
456 Lemonldap manages those parameters :
457  
458 =over 4
459  
460 =item service
461  
462 =item renew
463  
464 =item gateway
465  
466 =back
467  
468  
469 =head1 INSTALLATION
470  
471 You must have an lemonldap websso installed (see doc on lemonldap.objectweb.org)
472  
473 Configures your Apache like this :
474
475 <virtualhost 192.168.204.100>
476 servername authen.demo.net
477 loglevel debug
478 documentroot /usr/local/apache2/htdocs
479 alias /portal /usr/local/monapache/portal/
480 ErrorLog logs/error_log
481 <location /cas/login>
482 setHandler modperl
483 perlresponsehandler Lemonldap::Handlers::LoginCASFake
484 PerlSetVar Domain demo.net
485 PerlSetVar Configfile /usr/local/monapache/conf/application.xml
486 PerlOptions +GlobalRequest
487 </location>
488 <location /cas/serviceValidate>
489 setHandler modperl
490 perlresponsehandler Lemonldap::Handlers::ValidateCAS
491 PerlSetVar Domain demo.net
492 PerlSetVar HandlerID validate
493 PerlSetVar Configfile /usr/local/monapache/conf/application.xml
494 PerlOptions +GlobalRequest
495 </location>
496 <location /cas/logout>
497 setHandler modperl
498 perlresponsehandler Lemonldap::Handlers::LogoutCAS
499 PerlSetVar Domain demo.net
500 PerlSetVar Configfile /usr/local/monapache/conf/application.xml
501 PerlOptions +GlobalRequest
502 </location>
503 </virtualhost>
504  
505 YOU CAN MIXED lemonldap handler et CAS server
506
507 Your application.xml is like this
508 <domain id="demo.net"
509 Cookie="lemondemo"
510 Sessionstore="memcached"
511 portal= "http://authen.demo.net/portail/accueil.pl"
512 LoginCASPage="/tmp/login.htmlcas"
513 LogoutCASPage="/tmp/logout.htmlcas"
514 LoginPage="/tmp/login.html"
515 ldap_server="192.168.247.30"
516 ldap_branch_people="ou=personnes,dc=demo,dc=net"
517 >
518 <handler
519 id="validate"
520 MultiHoming="pied,tete"
521 />
522 <handler id="pied"
523 MotifIn="/192.168.204.108\/caspied"
524 applcode= "mail"
525 pluginpolicy="Lemonldap::Handlers::RowPolicy"
526 />
527 <handler id="tete"
528 MotifIn="/192.168.204.108\/castete"
529 disableaccessControl="1"
530 />
531 #### here normal lemonldap application #####
532 <handler
533 id="appli1"
534 applcode= "APT"
535 pluginpolicy="Lemonldap::Handlers::RowPolicy"
536 enableLWP="1"
537 basepub="http://myappli.demo.net"
538 basepriv="http://www.eyrolles.com"
539 >
540 </handler>
541  
542 etc..
543  
544 Put your login.html and logout.cas in the good directory (here /tmp) and the right name (here /tmp/login.htmlcas )
545  
546 See the caspied and castete php examples (basic and standard CAS application)
547
548 =head1 NOTES
549  
550 =over 4
551  
552 =item Lemonldapcas is just an emulation of CAS server , use the real CAS server if you have only CAS application .
553  
554 =item Lemonldap provides CAS version 1 and version 2 protocol ,if your location of validation contents the word 'Validate' (eg serviceValidation) the hanlder will use CAS version 2 overwise (eg service) it's CAS version 1
555  
556 =item Lemonlap DOESN'T provide 'proxycas' service (in process) .
557
558 =item Lemonldap shares its sessions with other lemonldap (unlike CAS server) .
559  
560 =item YOU MUST use HTTPS (by mod_ssl) in your apache server
561  
562 =back
563  
564 =head2 EXPORT
565  
566 None by default.
567  
568  
569  
570 =head1 SEE ALSO
571  
572 Mention other useful documentation such as the documentation of
573 related modules or operating system documentation (such as man pages
574 in UNIX), or any relevant external documentation such as RFCs or
575 standards.
576  
577  
578  
579 =head1 AUTHOR
580  
581 root, E<lt>germanlinux@yahoo.frE<gt>
582  
583 =head1 COPYRIGHT AND LICENSE
584  
585 Copyright (C) 2007 by germanlinux at yahoo.fr
586  
587 This library is free software; you can redistribute it and/or modify
588 it under the same terms as Perl itself, either Perl version 5.8.5 or,
589 at your option, any later version of Perl 5 you may have available.
590  
591  
592 =cut
593  
594