| 1 | package Lemonldap::Handlers::LoginCAS; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Lemonldap::Config::Parameters; | ||
| 5 | use Lemonldap::Portal::Standard; | ||
| 6 | use Apache2::Const qw(DONE FORBIDDEN OK SERVER_ERROR REDIRECT); | ||
| 7 | use Apache2::Log(); | ||
| 8 | use APR::Table; | ||
| 9 | use Apache2::RequestRec (); | ||
| 10 | use Apache2::ServerRec(); | ||
| 11 | use Data::Dumper; | ||
| 12 | use Template; | ||
| 13 | use URI::Escape; | ||
| 14 | use CGI ':cgi-lib'; | ||
| 15 | use Apache::Session::Memorycached; | ||
| 16 | use MIME::Base64; | ||
| 17 | use Encode qw(encode); | ||
| 18 | use Lemonldap::Portal::Session; | ||
| 19 | use Net::LDAP::Entry; | ||
| 20 | use IO::File; | ||
| 21 | our $VERSION = '1.0.0'; | ||
| 22 | use AuthCAS; | ||
| 23 | |||
| 24 | my $client_addr; | ||
| 25 | my $sessCacheRefreshPeriod; | ||
| 26 | my $log; | ||
| 27 | my $dump; | ||
| 28 | my $html; | ||
| 29 | my $Stack_User; | ||
| 30 | |||
| 31 | #my $UserAttributes; | ||
| 32 | my $Major; | ||
| 33 | my $MyApplicationXmlFile; | ||
| 34 | my $MyDomain; | ||
| 35 | my $LoginPage; | ||
| 36 | my $RedirectPage; | ||
| 37 | my $Parameters; | ||
| 38 | my $Conf_Domain; | ||
| 39 | my $Login_Url; | ||
| 40 | my $Cookie_Name; | ||
| 41 | |||
| 42 | #my $ipCheck; | ||
| 43 | #my $inactivityTimeout; | ||
| 44 | my $Ldap_Server; | ||
| 45 | my $Ldap_Branch_People; | ||
| 46 | my $Ldap_Dn_Manager; | ||
| 47 | my $Ldap_Pass_Manager; | ||
| 48 | my $Ldap_Port; | ||
| 49 | my @attrs; | ||
| 50 | my $Ldap_Search_Bases; | ||
| 51 | my @base; | ||
| 52 | my $MemcachedServer; | ||
| 53 | my $CookieName; | ||
| 54 | my $line_session; | ||
| 55 | my $_username; | ||
| 56 | |||
| 57 | #my $InactivityTimeout; | ||
| 58 | #my $Encryptionkey; | ||
| 59 | my $page_html; | ||
| 60 | my $Menu; | ||
| 61 | my $Messages = { | ||
| 62 | 1 => | ||
| 63 | 'Votre connexion a expiré vous devez vous authentifier de nouveau', | ||
| 64 | 2 => | ||
| 65 | 'Les champs "Identifiant" et "Mot de passe" doivent être remplis', | ||
| 66 | 3 => 'Wrong directory manager account or password', | ||
| 67 | 4 => 'n\'a pas été trouvé dans l\'annuaire', | ||
| 68 | 5 => 'Mot de passe erroné', | ||
| 69 | 6 => | ||
| 70 | 'Votre adresse IP a été modifié. Vous devez vous authentifier de nouveau', | ||
| 71 | 7 => 'Serveral Entries found on ldap server for this user', | ||
| 72 | 8 => 'Bad connection to ldap server', | ||
| 73 | }; | ||
| 74 | |||
| 75 | sub my_none { | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | ######## only use if you don't have ldap server | ||
| 79 | |||
| 80 | sub my_entry { | ||
| 81 | my $self =shift; | ||
| 82 | my $entry = Net::LDAP::Entry->new(); | ||
| 83 | my $user =$self->user; | ||
| 84 | $entry->dn("uid=$user,dc=demo,dc=net") ; | ||
| 85 | $entry->add(uid =>$user ); | ||
| 86 | $entry->add(mail =>"$user\@demo.net" ); | ||
| 87 | $entry->add(roleprofil =>'APPLIX;my_role' ); | ||
| 88 | $self->{entry}=$entry; | ||
| 89 | my $fh = IO::File->new_tmpfile ; | ||
| 90 | $entry->dump($fh); | ||
| 91 | my @a; | ||
| 92 | seek ($fh,0,0); | ||
| 93 | for (<$fh>) { | ||
| 94 | push @a,$_; | ||
| 95 | } | ||
| 96 | my $a =join '<br>', @a; | ||
| 97 | $self->{dump} =$a; | ||
| 98 | undef $fh; | ||
| 99 | return 1; | ||
| 100 | } | ||
| 101 | ############################# | ||
| 102 | sub my_credential { | ||
| 103 | 1; | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | sub My_Session { | ||
| 108 | my $self = shift; | ||
| 109 | my $paramxml = $self->{line_session}; | ||
| 110 | my %Session; | ||
| 111 | |||
| 112 | my $entry = $self->{entry}; | ||
| 113 | my $l = Dumper ($paramxml) ; | ||
| 114 | my $obj = Lemonldap::Portal::Session->init( $paramxml, 'entry' => $entry ); | ||
| 115 | $self->{infosession} = $obj; | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | sub handler { | ||
| 120 | my $r = shift; | ||
| 121 | my $Stack_User; | ||
| 122 | $log = $r->log; | ||
| 123 | ### 1 tester le TGT cookie | ||
| 124 | my %Param = Vars(); | ||
| 125 | my $origine = $Param{'url'}; | ||
| 126 | $MyApplicationXmlFile = $r->dir_config('ConfigFile'); | ||
| 127 | $MyDomain = lc( $r->dir_config('Domain') ); | ||
| 128 | $Major = $r->dir_config('Organization'); | ||
| 129 | if ( !defined($Major) ) { | ||
| 130 | $Major = "authz_headers"; | ||
| 131 | } | ||
| 132 | $Parameters = | ||
| 133 | Lemonldap::Config::Parameters->new( file => $MyApplicationXmlFile, ); | ||
| 134 | $Conf_Domain = $Parameters->getDomain($MyDomain); | ||
| 135 | my $myaddress = $Conf_Domain->{Portal}; | ||
| 136 | my $Login_Url = $Conf_Domain->{PortalCAS}; | ||
| 137 | my $CASUrl = $Conf_Domain->{CASUrl}; | ||
| 138 | my $CASValidate = $Conf_Domain->{CASValidate}; | ||
| 139 | my $CAFile = $Conf_Domain->{CAFile}; | ||
| 140 | $Cookie_Name = $Conf_Domain->{Cookie}; | ||
| 141 | |||
| 142 | my $CookieNamep = $Cookie_Name . "_prov"; | ||
| 143 | $Ldap_Server = $Conf_Domain->{ldap_server}; | ||
| 144 | $Ldap_Branch_People = $Conf_Domain->{ldap_branch_people}; | ||
| 145 | $Ldap_Dn_Manager = $Conf_Domain->{DnManager}; | ||
| 146 | $Ldap_Pass_Manager = $Conf_Domain->{passwordManager}; | ||
| 147 | $Ldap_Port = $Conf_Domain->{ldap_port}; | ||
| 148 | $Ldap_Search_Bases = $Ldap_Branch_People; | ||
| 149 | @base = split(/\s*:\s*/,$Ldap_Search_Bases); | ||
| 150 | $line_session = $Conf_Domain->{DefinitionSession}; | ||
| 151 | |||
| 152 | |||
| 153 | $MemcachedServer = | ||
| 154 | $Parameters->formateLineHash( | ||
| 155 | $Parameters->findParagraph( 'session', 'memcached' )->{SessionParams} ); | ||
| 156 | ################ recup cookie prov ###################### | ||
| 157 | my $entete2 = $r->headers_in(); | ||
| 158 | |||
| 159 | #Recuperation du Cookie | ||
| 160 | my $idx_tmp = $entete2->{'Cookie'}; | ||
| 161 | my $local_cookie = $CookieNamep; | ||
| 162 | my @tab = split /;/, $idx_tmp; | ||
| 163 | my $cookie; | ||
| 164 | my $idrecup; | ||
| 165 | my $needticket=1; | ||
| 166 | |||
| 167 | foreach (@tab) { | ||
| 168 | if (/$local_cookie=([^; ]+)/) { | ||
| 169 | $cookie = $1; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | my $etat; | ||
| 174 | if ($cookie) { # il peut peut etre deja ok ? ###### | ||
| 175 | my %_it; | ||
| 176 | my $Memcachedfast= $MemcachedServer; | ||
| 177 | $Memcachedfast->{timeout}= 100 ; | ||
| 178 | tie %_it, 'Apache::Session::Memorycached', $cookie, $Memcachedfast; | ||
| 179 | my $or = $_it{origine}; | ||
| 180 | untie %_it; | ||
| 181 | if ($or) { | ||
| 182 | #### il est de retour avec son tiket | ||
| 183 | my $ticket = $Param{'ticket'}; | ||
| 184 | $etat = 1; | ||
| 185 | if ($ticket) { ### un petit coup de cas ? | ||
| 186 | $needticket = 0; | ||
| 187 | my $cas = new AuthCAS( | ||
| 188 | 'casUrl' => $CASUrl, | ||
| 189 | 'serviceValidatePath' => $CASValidate, | ||
| 190 | 'CAFile' => $CAFile, | ||
| 191 | ); | ||
| 192 | my $user = $cas->validateST( $myaddress, $ticket ); | ||
| 193 | if ( !$user ) { # erreur il faut arreter | ||
| 194 | return FORBIDDEN; | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | ### tout est ok | ||
| 198 | ## je retrouve le service initial et je continue la pile | ||
| 199 | $Param{identifiant} = $user; | ||
| 200 | $Param{secret} = $user; | ||
| 201 | $Param{url} = $or; | ||
| 202 | |||
| 203 | } | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | return FORBIDDEN; | ||
| 207 | |||
| 208 | } | ||
| 209 | |||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | if ($needticket) { | ||
| 214 | ######## attibution d un numero provisoire ############### | ||
| 215 | |||
| 216 | my %_it; | ||
| 217 | tie %_it, 'Apache::Session::Memorycached', undef, $MemcachedServer; | ||
| 218 | $_it{origine} = $origine || 'Tk9ORQ=='; #NONE in base64 representation | ||
| 219 | my $idprov = $_it{'_session_id'}; | ||
| 220 | untie %_it; | ||
| 221 | #### won genere un cookie lemonldap avec le numero de session prov | ||
| 222 | my $dotdomain = "." . $MyDomain; | ||
| 223 | my $pathlemon = '/'; | ||
| 224 | my $LemonCookie = CGI::cookie( | ||
| 225 | -name => $CookieNamep, | ||
| 226 | -value => $idprov, | ||
| 227 | -domain => $dotdomain, | ||
| 228 | -path => $pathlemon, | ||
| 229 | ); | ||
| 230 | |||
| 231 | my $redirection = $Login_Url; | ||
| 232 | $redirection .= "?service=" . uri_escape($myaddress); | ||
| 233 | print CGI::header( | ||
| 234 | -Refresh => '0; URL=' . $redirection, | ||
| 235 | -cookie => $LemonCookie | ||
| 236 | ); | ||
| 237 | return DONE; | ||
| 238 | } else | ||
| 239 | { | ||
| 240 | ### tout est ok il faut deroule la pile | ||
| 241 | $Stack_User = Lemonldap::Portal::Standard->new( | ||
| 242 | 'msg' => $Messages, | ||
| 243 | 'credentials' => \&my_credential, | ||
| 244 | 'setSessionInfo' => \&My_Session, | ||
| 245 | 'line_session' => $line_session, | ||
| 246 | ); | ||
| 247 | |||
| 248 | |||
| 249 | my $Retour = $Stack_User->process( 'param' => \%Param, | ||
| 250 | 'server' => $Ldap_Server, | ||
| 251 | 'base' => \@base, | ||
| 252 | ); | ||
| 253 | my $Message = ''; | ||
| 254 | my $Erreur; | ||
| 255 | if ($Retour) { | ||
| 256 | $Message = $Retour->message; | ||
| 257 | $Erreur = $Retour->error; | ||
| 258 | } | ||
| 259 | if ($Erreur) { | ||
| 260 | return FORBIDDEN; | ||
| 261 | } | ||
| 262 | my $MyHashSession = $Retour->infoSession; | ||
| 263 | my $l = Dumper($MyHashSession) ; | ||
| 264 | my %Session; | ||
| 265 | tie %Session, 'Apache::Session::Memorycached', undef, $MemcachedServer; | ||
| 266 | foreach (keys %{$MyHashSession}){ | ||
| 267 | $Session{$_}= $MyHashSession->{$_} if $MyHashSession->{$_} ; | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | my $Session_Id = $Session{'_session_id'}; | ||
| 272 | my $l =Dumper (\%Session); | ||
| 273 | $l =~ s/\n/\<br\>/g; | ||
| 274 | $l=~ s/\$var\d+/Item/ig; | ||
| 275 | untie %Session; | ||
| 276 | |||
| 277 | # Cookie creation | ||
| 278 | my $PathCookie = "/"; | ||
| 279 | my $LemonldapCookie = CGI::cookie( | ||
| 280 | -name => $Cookie_Name, | ||
| 281 | -value => $Session_Id, | ||
| 282 | -domain => ".".$MyDomain, | ||
| 283 | -path => $PathCookie, | ||
| 284 | ); | ||
| 285 | my $LemonldapCookieprov = CGI::cookie( | ||
| 286 | -name => "$CookieNamep", | ||
| 287 | -value => 0, | ||
| 288 | -domain => ".".$MyDomain, | ||
| 289 | -path => $PathCookie, | ||
| 290 | ); | ||
| 291 | |||
| 292 | my $UrlDecode = $Retour->getRedirection(); | ||
| 293 | undef $UrlDecode if $UrlDecode=~ /none/i ; | ||
| 294 | |||
| 295 | if ($UrlDecode) { | ||
| 296 | print CGI::header( -Refresh => '0; URL='.$UrlDecode, -cookie => [$LemonldapCookie, $LemonldapCookieprov] ); | ||
| 297 | return OK; | ||
| 298 | } else { | ||
| 299 | $r->content_type('text/html'); | ||
| 300 | $r->headers_out->add( 'Set-Cookie' => $LemonldapCookieprov ); | ||
| 301 | $r->headers_out->add( 'Set-Cookie' => $LemonldapCookie ); | ||
| 302 | $r->print ; | ||
| 303 | $r->print(<<END1); | ||
| 304 | <html> | ||
| 305 | <head> | ||
| 306 | <title>lemonldap websso</title> | ||
| 307 | <style type="text/css" media="all"> | ||
| 308 | p { color: #060; | ||
| 309 | font-size: 1.0em; | ||
| 310 | font-family: Verdana, Arial, sans-serif; | ||
| 311 | font-weight: bold; | ||
| 312 | } | ||
| 313 | h1 { color: #000; | ||
| 314 | font-size: 1.5em; | ||
| 315 | font-family: Verdana, Arial, sans-serif; | ||
| 316 | font-weight: bold; | ||
| 317 | border-bottom: 3px solid #019733; | ||
| 318 | margin: 40px 0 20px 0px; | ||
| 319 | padding: 0 0.5em 0.5em 0.5em; | ||
| 320 | } | ||
| 321 | body { background-color: #77E0AB; } | ||
| 322 | label { background-color: #94DBB7; | ||
| 323 | color: #018023; | ||
| 324 | font-weight: bold; | ||
| 325 | padding: 4px; | ||
| 326 | font-family: Verdana, Arial, sans-serif; | ||
| 327 | font-size: 0.9em; | ||
| 328 | width: 500px; | ||
| 329 | } | ||
| 330 | |||
| 331 | br { display: none; | ||
| 332 | } | ||
| 333 | .nodisp { display: none; } | ||
| 334 | </style> | ||
| 335 | </head> | ||
| 336 | <body> | ||
| 337 | <h1>Hello on lemonldap and CAS websso 'world</h1> | ||
| 338 | <p> | ||
| 339 | <center><label>Your are Authenticate on lemondap webSSO and CAS server</label> | ||
| 340 | </center> | ||
| 341 | </body> | ||
| 342 | </html> | ||
| 343 | END1 | ||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | |||
| 348 | |||
| 349 | return OK; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | } | ||
| 353 | 1; | ||
| 354 | |||
| 355 | =pod | ||
| 356 | |||
| 357 | =head1 NAME | ||
| 358 | |||
| 359 | Lemonldap::Portal::CAS - Perl extension for lemonldap websso | ||
| 360 | |||
| 361 | =head1 SYNOPSIS | ||
| 362 | |||
| 363 | use Lemonldap::Portal::LoginCAS | ||
| 364 | |||
| 365 | |||
| 366 | |||
| 367 | =head1 DESCRIPTION | ||
| 368 | |||
| 369 | Lemonldap is a Reverse-proxy webSSO and CAS (Central Authentification Service) is an another websso from Yales university . | ||
| 370 | CAS acts like Authentification service NOT for authorization service . | ||
| 371 | |||
| 372 | These modules give the capacity at a lemonldap to resquest authentication upon CAS server. | ||
| 373 | So , an user will be authenticate on CAS server AND authozized by lemonldap | ||
| 374 | |||
| 375 | Need AuthCAS module . | ||
| 376 | |||
| 377 | =head1 INSTALLATION | ||
| 378 | |||
| 379 | First intall lemonlap (see http://lemonldap.objecweb.org) ) | ||
| 380 | |||
| 381 | Configures your Apache like this : | ||
| 382 | |||
| 383 | PerlSetVar Domain demo.net | ||
| 384 | PerlSetVar Configfile /usr/local/monapache/conf/application.xml | ||
| 385 | <location /portail> | ||
| 386 | setHandler modperl | ||
| 387 | perlresponsehandler Lemonldap::Handlers::LoginCAS | ||
| 388 | PerlOptions +GlobalRequest | ||
| 389 | </location> | ||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | Your application.xml is like this | ||
| 394 | |||
| 395 | <domain id="demo.net" | ||
| 396 | Cookie="lemondemo" | ||
| 397 | Sessionstore="memcached" | ||
| 398 | Portal= "http://authen.demo.net/portail/accueil.pl" | ||
| 399 | PortalCAS= "https://10.75.204.108:8443/esup/login" | ||
| 400 | CASUrl ="https://10.75.204.108:8443" | ||
| 401 | CASValidate ="/esup/serviceValidate" | ||
| 402 | CASFile ="/certificate/ca.cer" | ||
| 403 | ldap_server="ldap.demo.net" | ||
| 404 | ldap_branch_people="ou=personnes,dc=demo,dc=net" | ||
| 405 | > | ||
| 406 | |||
| 407 | |||
| 408 | =head2 EXPORT | ||
| 409 | |||
| 410 | None by default. | ||
| 411 | |||
| 412 | |||
| 413 | |||
| 414 | =head1 SEE ALSO | ||
| 415 | |||
| 416 | lemonldap websso framework | ||
| 417 | |||
| 418 | =head1 AUTHOR | ||
| 419 | |||
| 420 | Eric German , E<lt>germanlinux@yahoo.frE<gt> | ||
| 421 | |||
| 422 | =head1 COPYRIGHT AND LICENSE | ||
| 423 | |||
| 424 | Copyright (C) 2007 by lemonasso | ||
| 425 | |||
| 426 | This library is free software; you can redistribute it and/or modify | ||
| 427 | it under the same terms as Perl itself, either Perl version 5.8.5 or, | ||
| 428 | at your option, any later version of Perl 5 you may have available. | ||
| 429 | |||
| 430 | |||
| 431 | =cut |