| 1 | package Apache::AuthCookieURL; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use mod_perl qw(1.24 StackedHandlers MethodHandlers Authen Authz); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use Apache::Constants qw(:common M_GET REDIRECT MOVED); | ||
| 5 | use vars qw($VERSION); | ||
| 6 | use Apache::URI (); | ||
| 7 | use Apache::Cookie; | ||
| 8 | |||
| 9 | use constant DEBUG => 'AuthCookieURLDebug'; | ||
| 10 | |||
| 11 | # $Id: AuthCookieURL.pm,v 1.3 2000/11/21 00:46:01 lii Exp $ | ||
| 12 | $VERSION = sprintf '%d.%03d', q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/; | ||
| 13 | |||
| 14 | #======================== NOTE ============================================ | ||
| 15 | |||
| 16 | ### This module is a modification of Ken Williams <ken@forum.swarthmore.edu> | ||
| 17 | ### Apache::AuthCookie apache module. | ||
| 18 | |||
| 19 | # Modified July 14, 2000 to handle munged urls and sessions w/o login | ||
| 20 | # - use cookies or munged urls for sessions | ||
| 21 | # - can be used with a login script, or without for simple session management | ||
| 22 | # - Will create sessions without overriding, if you don't care how unique they are. | ||
| 23 | # Comments to: Bill Moseley moseley@hank.org | ||
| 24 | |||
| 25 | #======================== NOTE ============================================ | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | # These should be overridden in your own module | ||
| 31 | # Purpose: to provide a default session ID when not using a login script | ||
| 32 | # Must use with URLsession enabled so initail redirect will see a different url | ||
| 33 | # in the Location header from the original request. | ||
| 34 | |||
| 35 | sub authen_cred ($$\@) { | ||
| 36 | my $self = shift; | ||
| 37 | my $r = shift; | ||
| 38 | my @creds = @_; | ||
| 39 | |||
| 40 | # Normall this would convert credentials into a session key | ||
| 41 | |||
| 42 | |||
| 43 | # A really silly session key. | ||
| 44 | return time . $$ . int rand $$; | ||
| 45 | |||
| 46 | # Or return a flag that authen_ses_key can look for | ||
| 47 | return 'invalid:account_expired'; | ||
| 48 | |||
| 49 | # Or return a message that will be placed in a 'Reason' cookie | ||
| 50 | return ('','User Blocked'); | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | sub authen_ses_key ($$$) { | ||
| 55 | my ($self, $r, $session) = @_; | ||
| 56 | |||
| 57 | # Validate the session and convert it into REMOTE_USER | ||
| 58 | |||
| 59 | |||
| 60 | # This is using the session key as the REMOTE_USER | ||
| 61 | return $session; | ||
| 62 | |||
| 63 | # This returns undef so no REMOTE_USER is set sending back to login form | ||
| 64 | # Make sure there IS a login form before doing this. | ||
| 65 | return undef; | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | sub recognize_user ($$) { | ||
| 72 | my ($self, $r) = @_; | ||
| 73 | my $debug = $r->dir_config( DEBUG ) || 0; | ||
| 74 | my ($auth_type, $auth_name) = ($r->auth_type, $r->auth_name); | ||
| 75 | return unless $auth_type && $auth_name; | ||
| 76 | # return unless $r->header_in('Cookie'); | ||
| 77 | |||
| 78 | |||
| 79 | my ($cookie) = ($r->header_in( 'Cookie' ) || '') =~ /${auth_type}_$auth_name=([^;]+)/; | ||
| 80 | |||
| 81 | # Get session from URI if not set in a cookie | ||
| 82 | # (won't likely be here if this isn't a protected doc) | ||
| 83 | $cookie ||= $r->notes( 'URI_Session' ) || ''; | ||
| 84 | |||
| 85 | |||
| 86 | $r->log_error("session provided = '$cookie'" ) if $debug >= 1; | ||
| 87 | |||
| 88 | return OK unless $cookie; | ||
| 89 | |||
| 90 | if (my ($user) = $auth_type->authen_ses_key($r, $cookie)) { | ||
| 91 | $r->log_error("recognize user = '$user'") if $debug >= 2; | ||
| 92 | $r->connection->user($user); | ||
| 93 | } | ||
| 94 | return OK; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | # Transhandler to strip the session from the URL | ||
| 99 | # | ||
| 100 | # $r->notes('Session_prefix') session prefix found in httpd.conf | ||
| 101 | # also used to idicate to login() that trans handler in use | ||
| 102 | # | ||
| 103 | # $r-notes('URI_Session') is the extracted session. | ||
| 104 | # authenticate() uses it if no cookie | ||
| 105 | # | ||
| 106 | # $r->subprocess_env( 'SESSION' ) is set so cgi scripts can prefix to href links | ||
| 107 | # | ||
| 108 | # $r->notes( 'SESSION' ) is set for ErrorDocument fixups - prefix | ||
| 109 | # Location: headers if exists (might as well use the $ENV{SESSION}) | ||
| 110 | |||
| 111 | |||
| 112 | sub URLsession ( $$ ) { | ||
| 113 | my ($self, $r) = @_; | ||
| 114 | |||
| 115 | return DECLINED unless $r->is_initial_req; | ||
| 116 | |||
| 117 | # I'd like to be able to do this so don't need all that httpd.conf config | ||
| 118 | # $r->custom_response( MOVED , \&error_document ); | ||
| 119 | # $r->custom_response( REDIRECT , \&error_document ); | ||
| 120 | |||
| 121 | |||
| 122 | my $debug = $r->dir_config( DEBUG ) || 0; | ||
| 123 | $r->log_error('TRANS:Requested URI = \'' . $r->the_request() . "'" ) if $debug >= 3; | ||
| 124 | |||
| 125 | |||
| 126 | my ( undef, $session, $rest ) = split m[/+], $r->uri, 3; | ||
| 127 | $rest ||= ''; | ||
| 128 | |||
| 129 | my $prefix = $r->dir_config('SessionPrefix') || 'Session-'; | ||
| 130 | |||
| 131 | # This way simply adding the PerlTransHandler is enough to enable URL munging | ||
| 132 | $r->notes( Session_prefix => $prefix ); | ||
| 133 | |||
| 134 | |||
| 135 | return DECLINED unless $session && $session =~ /^$prefix(.+)$/; | ||
| 136 | |||
| 137 | |||
| 138 | # Session found. Extract and make it available in notes(); | ||
| 139 | |||
| 140 | $session = $1; | ||
| 141 | |||
| 142 | $r->log_error("Found session '$session' in url") if $debug >= 1; | ||
| 143 | |||
| 144 | $r->notes( URI_Session => $session ); | ||
| 145 | |||
| 146 | |||
| 147 | # Make the prefix and session available to CGI scripts for use in absolute | ||
| 148 | # links or redirects | ||
| 149 | |||
| 150 | $r->subprocess_env( SESSION => "/$prefix$session" ); | ||
| 151 | |||
| 152 | $r->notes( SESSION => "/$prefix$session" ); # for error document fixup | ||
| 153 | |||
| 154 | |||
| 155 | # Remove the session from the URI | ||
| 156 | $r->uri( "/$rest" ); | ||
| 157 | |||
| 158 | return DECLINED; | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | # Error document | ||
| 165 | |||
| 166 | # Add the session ID to location headers on redirects | ||
| 167 | # iff not using cookies and host matches | ||
| 168 | |||
| 169 | sub error_document ($$) { | ||
| 170 | my ( $self, $r ) = @_; | ||
| 171 | |||
| 172 | my $uri; | ||
| 173 | |||
| 174 | # unlikely, but might as well make sure there's a location header available | ||
| 175 | |||
| 176 | if ( $r->prev->header_out('Location') ) { | ||
| 177 | |||
| 178 | $uri = Apache::URI->parse($r, $r->prev->header_out('Location') ); | ||
| 179 | |||
| 180 | |||
| 181 | my $same_host = 1; | ||
| 182 | my $hostname = $uri->hostname || ''; | ||
| 183 | $same_host = 0 if $hostname && $hostname ne $r->get_server_name; | ||
| 184 | |||
| 185 | |||
| 186 | my $session = $r->prev->notes( 'SESSION' ) || ''; | ||
| 187 | |||
| 188 | if ( $same_host && $session && $uri->path !~ /^$session/ ) { | ||
| 189 | $uri->path( $session . $uri->path ); | ||
| 190 | $r->header_out('Location', $uri->unparse ); | ||
| 191 | |||
| 192 | } else { | ||
| 193 | $r->header_out('Location', $r->prev->header_out('Location') ); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | my $status = $r->prev->status; | ||
| 199 | my $location = $uri ? $uri->unparse : 'unknown'; | ||
| 200 | my $description = ( $status == MOVED ) ? 'Moved Permanently' : 'Found'; | ||
| 201 | |||
| 202 | my $message = <<EOF; | ||
| 203 | |||
| 204 | <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | ||
| 205 | <HTML> | ||
| 206 | <HEAD> | ||
| 207 | <TITLE>$status $description</TITLE> | ||
| 208 | </HEAD> | ||
| 209 | <BODY> | ||
| 210 | <H1>$description</H1> | ||
| 211 | The document has moved <A HREF="$location">$location</A>.<P> | ||
| 212 | </BODY> | ||
| 213 | </HTML> | ||
| 214 | EOF | ||
| 215 | |||
| 216 | $r->content_type('text/html'); | ||
| 217 | $r->send_http_header; | ||
| 218 | |||
| 219 | $r->print( $message ); | ||
| 220 | |||
| 221 | return OK; | ||
| 222 | |||
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | # Action of the login.pl script, or called directly from authenticate if login script is 'NONE' | ||
| 227 | sub login ($$) { | ||
| 228 | my ($self, $r, $destination ) = @_; | ||
| 229 | |||
| 230 | |||
| 231 | my $debug = $r->dir_config( DEBUG ) || 0; | ||
| 232 | $r->log_error( "** In login **" ) if $debug >= 3; | ||
| 233 | |||
| 234 | my ($auth_type, $auth_name) = ($r->auth_type, $r->auth_name); | ||
| 235 | |||
| 236 | |||
| 237 | my %args = $r->method eq 'POST' ? $r->content : $r->args; | ||
| 238 | |||
| 239 | |||
| 240 | $destination ||= $args{destination} || ''; | ||
| 241 | |||
| 242 | unless ( $destination ) { | ||
| 243 | $r->log_error("No key 'destination' found in posted data"); | ||
| 244 | return SERVER_ERROR; | ||
| 245 | } else { | ||
| 246 | $r->log_error("'destination' in posted data = '$destination'") if $debug >= 1; | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | # Get the credentials from the data posted by the client, if any. | ||
| 251 | |||
| 252 | my @credentials; | ||
| 253 | |||
| 254 | while (exists $args{"credential_" . ($#credentials + 1)}) { | ||
| 255 | |||
| 256 | $r->log_error("credential_" . ($#credentials + 1) . "= '" . | ||
| 257 | $args{"credential_" . ($#credentials + 1)} . "'") if $debug >= 2; | ||
| 258 | |||
| 259 | push(@credentials, $args{"credential_" . ($#credentials + 1)}); | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | # convert post to get | ||
| 264 | |||
| 265 | if ($r->method eq 'POST') { | ||
| 266 | $r->method('GET'); | ||
| 267 | $r->method_number(M_GET); | ||
| 268 | $r->headers_in->unset('Content-Length'); | ||
| 269 | } | ||
| 270 | |||
| 271 | $r->no_cache(1) unless $r->dir_config( $auth_name . 'Cache' ); | ||
| 272 | |||
| 273 | |||
| 274 | # Exchange the credentials for a session key. | ||
| 275 | my ($ses_key, $error_message ) = $self->authen_cred($r, @credentials); | ||
| 276 | |||
| 277 | # Would be nice if could somehow go back to original request yet pass info | ||
| 278 | # from authen_cred about a failed authentication | ||
| 279 | # two ideas: 1) return a session key that authen_ses_key can identify as invalid | ||
| 280 | # 2) return a message and place that in a cookie | ||
| 281 | |||
| 282 | |||
| 283 | |||
| 284 | # Get the uri so can adjust path, and to redirect including the query string | ||
| 285 | my $uri = Apache::URI->parse($r, $destination ); | ||
| 286 | |||
| 287 | |||
| 288 | unless ( $ses_key ) { | ||
| 289 | $r->log_error("No session returned from authen_cred" ) if $debug >= 2; | ||
| 290 | |||
| 291 | # Pass a cookie with the error reason that can be read after the redirect. | ||
| 292 | # Use a cookie with no time limit | ||
| 293 | $self->send_cookie( name=>'Reason', value=>$error_message, expires=>undef ) if $error_message; | ||
| 294 | |||
| 295 | } else { | ||
| 296 | |||
| 297 | # Delete error message cookie, if found | ||
| 298 | $self->send_cookie( value=>'none', name=>'Reason', expires=>'-1d' ) | ||
| 299 | if ($r->header_in( 'Cookie' ) || '') =~ /${auth_type}_${auth_name}Reason/; | ||
| 300 | |||
| 301 | |||
| 302 | $r->log_error("ses_key returned from authen_cred = '$ses_key'" ) if $debug >= 2; | ||
| 303 | |||
| 304 | |||
| 305 | # Send cookie if a session was returned from authen_cred | ||
| 306 | |||
| 307 | $self->send_cookie(value=>$ses_key); | ||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | # add the session to the URI - if trans handler not installed prefix will be empty | ||
| 312 | |||
| 313 | if ( my $prefix = $r->notes('Session_Prefix') || '' ) { | ||
| 314 | |||
| 315 | $uri->path( "/$prefix$ses_key" . $uri->path ); | ||
| 316 | |||
| 317 | # And save info for ErrorDocument handler | ||
| 318 | $r->notes( SESSION => "/$prefix$ses_key" ); | ||
| 319 | } | ||
| 320 | |||
| 321 | |||
| 322 | } | ||
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | # See if destination is a directory and add a slash (might as well save a redirect) | ||
| 327 | |||
| 328 | if ( $uri->path !~ m[/$] && -d $r->lookup_uri( $destination )->filename ) { | ||
| 329 | $r->log_error("Destination '$destination' is a directory: add slash before redirect") if $debug >= 3; | ||
| 330 | |||
| 331 | $uri->path( $uri->path . '/' ); | ||
| 332 | } | ||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | $r->log_error("login() Redirecting to " . $uri->unparse ) if $debug >= 2; | ||
| 338 | |||
| 339 | |||
| 340 | $r->header_out( Location => $uri->unparse ); | ||
| 341 | return REDIRECT; | ||
| 342 | } | ||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | # Note -- should redirect without adding the session for URL-based logout? | ||
| 347 | # Might be smart to override this method so that the session can be marked as logged out | ||
| 348 | # in whatever database you are using | ||
| 349 | |||
| 350 | sub logout($$) { | ||
| 351 | my ($self,$r) = @_; | ||
| 352 | my $debug = $r->dir_config( DEBUG ) || 0; | ||
| 353 | |||
| 354 | my ($auth_type, $auth_name) = ($r->auth_type, $r->auth_name); | ||
| 355 | |||
| 356 | # Send the Set-Cookie header to expire the auth cookie. | ||
| 357 | $self->send_cookie(value=>'none', expires=>'-1d'); | ||
| 358 | |||
| 359 | return FORBIDDEN; | ||
| 360 | |||
| 361 | # This doesn't seem to work. | ||
| 362 | |||
| 363 | $r->no_cache(1) unless $r->dir_config( $auth_name . 'Cache' ); | ||
| 364 | |||
| 365 | my $location = $r->dir_config( $auth_name . 'LogoutURI' ) || '/'; | ||
| 366 | |||
| 367 | $r->notes('SESSION', undef ); # so error doc doesn't fixup. | ||
| 368 | |||
| 369 | $r->header_out( Location => $location ); | ||
| 370 | return REDIRECT; | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | sub authenticate ($$) { | ||
| 375 | my ($auth_type, $r) = @_; | ||
| 376 | my ($authen_script, $auth_user); | ||
| 377 | |||
| 378 | |||
| 379 | |||
| 380 | my $debug = $r->dir_config( DEBUG ) || 0; | ||
| 381 | $r->log_error( '** In authenticate **' ) if $debug >= 3; | ||
| 382 | $r->log_error( "auth_type= '$auth_type'" ) if $debug >= 3; | ||
| 383 | |||
| 384 | # This is a way to open up some documents/directories | ||
| 385 | return OK if lc $r->auth_name eq 'none'; | ||
| 386 | return OK if $r->dir_config('DisableAuthCookieURL'); | ||
| 387 | |||
| 388 | |||
| 389 | # Only authenticate the first internal request | ||
| 390 | return OK unless $r->is_initial_req; | ||
| 391 | |||
| 392 | |||
| 393 | # How to solve the problem of protecting from the root downward yet allow access to login script? | ||
| 394 | # Don't really want to set another PerlSetVar, so currently the action of the login script must | ||
| 395 | # be '/LOGIN' if protecting from the root down. | ||
| 396 | return OK if $r->uri =~ m[^/LOGIN]; | ||
| 397 | |||
| 398 | |||
| 399 | if ($r->auth_type ne $auth_type) { | ||
| 400 | # This location requires authentication because we are being called, | ||
| 401 | # but we don't handle this AuthType. | ||
| 402 | $r->log_error("AuthType mismatch: $auth_type =/= ".$r->auth_type) if $debug >= 3; | ||
| 403 | |||
| 404 | return DECLINED; | ||
| 405 | } | ||
| 406 | |||
| 407 | # Ok, the AuthType is $auth_type which we handle, what's the authentication | ||
| 408 | # realm's name? | ||
| 409 | |||
| 410 | my $auth_name = $r->auth_name; | ||
| 411 | |||
| 412 | $r->log_error( "auth_name= '$auth_name'" ) if $debug >= 2; | ||
| 413 | |||
| 414 | unless ($auth_name) { | ||
| 415 | $r->log_reason("AuthName not set, AuthType=$auth_type", $r->uri); | ||
| 416 | return SERVER_ERROR; | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | ## Check and get session from cookie or URL ## | ||
| 422 | |||
| 423 | # Get the Cookie header. If there is a session key for this realm, strip | ||
| 424 | # off everything but the value of the cookie. | ||
| 425 | |||
| 426 | my ($ses_key_cookie) = ($r->header_in( 'Cookie' ) || "") =~ /${auth_type}_$auth_name=([^;]+)/; | ||
| 427 | |||
| 428 | my $cookie = $ses_key_cookie || ''; | ||
| 429 | |||
| 430 | |||
| 431 | # Get session from URI if not set in a cookie | ||
| 432 | $ses_key_cookie ||= $r->notes( 'URI_Session' ) || ''; | ||
| 433 | |||
| 434 | |||
| 435 | |||
| 436 | $r->log_error("session provided = '$ses_key_cookie'" ) if $debug >= 1; | ||
| 437 | $r->log_error("requested uri = '" . $r->uri . "'" ) if $debug >= 2; | ||
| 438 | |||
| 439 | |||
| 440 | |||
| 441 | unless ($ses_key_cookie) { | ||
| 442 | $r->subprocess_env( AuthCookieURLReason => 'no_session_provided' ); | ||
| 443 | |||
| 444 | } else { | ||
| 445 | |||
| 446 | # Check and convert the session key into a user | ||
| 447 | |||
| 448 | if ($auth_user = $auth_type->authen_ses_key($r, $ses_key_cookie)) { | ||
| 449 | |||
| 450 | # We have a valid session key, so we return with an OK value. | ||
| 451 | # Tell the rest of Apache what the authentication method and | ||
| 452 | # user is. | ||
| 453 | |||
| 454 | $r->connection->auth_type($auth_type); | ||
| 455 | $r->connection->user($auth_user); | ||
| 456 | $r->log_error("user authenticated as $auth_user. Exiting Authen") if $debug >= 1; | ||
| 457 | |||
| 458 | $r->log_error( "Cookie: '$cookie' and URI_Session: '" . ($r->notes( 'URI_Session' ) || '') ."'") if $debug >= 4; | ||
| 459 | |||
| 460 | # Clean up the path by redirecting if cookies are in use | ||
| 461 | |||
| 462 | if ( $cookie && $r->notes( 'URI_Session' ) ) { | ||
| 463 | |||
| 464 | my $query = $r->args ? '?' . $r->args : ''; | ||
| 465 | $query = $r->uri . $query; | ||
| 466 | |||
| 467 | $r->log_error("Cookies are in use -- redirecting to '$query'") if $debug >= 3; | ||
| 468 | |||
| 469 | # prevent the error_document from adding the session back in. | ||
| 470 | $r->notes('SESSION', undef ); | ||
| 471 | |||
| 472 | $r->header_out( Location => $query ); | ||
| 473 | return REDIRECT; | ||
| 474 | } | ||
| 475 | |||
| 476 | return OK; | ||
| 477 | |||
| 478 | } else { | ||
| 479 | # There was a session key set, but it's invalid for some reason. So, | ||
| 480 | # remove it from the client now so when the credential data is posted | ||
| 481 | # we act just like it's a new session starting. | ||
| 482 | |||
| 483 | # Do this even if no cookie was sent -- can't hurt. | ||
| 484 | $auth_type->send_cookie( value=>'vanish', expires=>'-1d'); | ||
| 485 | |||
| 486 | |||
| 487 | $r->subprocess_env( AuthCookieURLReason => 'bad_session_provided' ); | ||
| 488 | } | ||
| 489 | } | ||
| 490 | |||
| 491 | |||
| 492 | # Invalid or no session was provided -- send to the login form | ||
| 493 | |||
| 494 | |||
| 495 | # If the LoginScript is set to 'NONE' then only generating a session | ||
| 496 | # So call login() directly instead of calling the login form. | ||
| 497 | |||
| 498 | if ($r->dir_config($auth_name . 'LoginScript' ) && | ||
| 499 | $r->dir_config($auth_name . 'LoginScript' ) eq 'NONE' ) | ||
| 500 | { | ||
| 501 | $r->log_error('LoginScript=NONE -- passing to login()' ) if $debug >= 2; | ||
| 502 | |||
| 503 | # Call login script, passing the original URI | ||
| 504 | |||
| 505 | my $query = $r->args ? '?' . $r->args : ''; | ||
| 506 | |||
| 507 | return $auth_type->login( $r, $r->uri . $query ); | ||
| 508 | } | ||
| 509 | |||
| 510 | |||
| 511 | |||
| 512 | # They aren't authenticated, and they tried to get a protected | ||
| 513 | # document. Send them the authen form. | ||
| 514 | return $auth_type->login_form; | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 518 | sub login_form { | ||
| 519 | my $r = Apache->request or die "no request"; | ||
| 520 | my $auth_name = $r->auth_name; | ||
| 521 | |||
| 522 | |||
| 523 | my $debug = $r->dir_config( DEBUG ) || 0; | ||
| 524 | $r->log_error( "** In login_form **" ) if $debug >= 3; | ||
| 525 | |||
| 526 | # There should be a PerlSetVar directive that gives us the URI of | ||
| 527 | # the script to execute for the login form. | ||
| 528 | |||
| 529 | my $authen_script; | ||
| 530 | |||
| 531 | unless ($authen_script = $r->dir_config($auth_name . 'LoginScript' )) { | ||
| 532 | $r->log_reason("PerlSetVar '${auth_name}LoginScript' not set", $r->uri); | ||
| 533 | return SERVER_ERROR; | ||
| 534 | } | ||
| 535 | |||
| 536 | $r->log_error("Internally redirecting to $authen_script") if $debug >= 3; | ||
| 537 | |||
| 538 | $r->custom_response(FORBIDDEN, $authen_script); | ||
| 539 | |||
| 540 | return FORBIDDEN; | ||
| 541 | } | ||
| 542 | |||
| 543 | |||
| 544 | |||
| 545 | sub authorize ($$) { | ||
| 546 | my ($auth_type, $r) = @_; | ||
| 547 | my $debug = $r->dir_config( DEBUG ) || 0; | ||
| 548 | |||
| 549 | $r->log_error( "** In authorize **" ) if $debug >= 3; | ||
| 550 | |||
| 551 | # This is a way to open up some documents/directories | ||
| 552 | return OK if lc $r->auth_name eq 'none'; | ||
| 553 | return OK if $r->dir_config('DisableAuthCookieURL'); | ||
| 554 | |||
| 555 | |||
| 556 | return OK unless $r->is_initial_req; #only the first internal request | ||
| 557 | |||
| 558 | # How to solve the problem of protecting from the root downward? | ||
| 559 | # Don't really want to set another PerlSetVar. | ||
| 560 | return OK if $r->uri =~ m[^/LOGIN]; | ||
| 561 | |||
| 562 | |||
| 563 | if ($r->auth_type ne $auth_type) { | ||
| 564 | $r->log_error($auth_type . " auth type is " . $r->auth_type) if $debug >= 3; | ||
| 565 | return DECLINED; | ||
| 566 | } | ||
| 567 | |||
| 568 | my $reqs_arr = $r->requires or return DECLINED; | ||
| 569 | |||
| 570 | my $user = $r->connection->user; | ||
| 571 | |||
| 572 | unless ($user) { | ||
| 573 | # user is either undef or =0 which means the authentication failed | ||
| 574 | $r->log_reason("No user authenticated", $r->uri); | ||
| 575 | return FORBIDDEN; | ||
| 576 | } | ||
| 577 | |||
| 578 | my ($forbidden); | ||
| 579 | |||
| 580 | foreach my $req (@$reqs_arr) { | ||
| 581 | my ($requirement, $args) = split /\s+/, $req->{requirement}, 2; | ||
| 582 | $args = '' unless defined $args; | ||
| 583 | $r->log_error("requirement := $requirement, $args") if $debug >= 2; | ||
| 584 | |||
| 585 | next if $requirement eq 'valid-user'; | ||
| 586 | |||
| 587 | if($requirement eq 'user') { | ||
| 588 | next if $args =~ m/\b$user\b/; | ||
| 589 | $forbidden = 1; | ||
| 590 | next; | ||
| 591 | } | ||
| 592 | |||
| 593 | # Call a custom method | ||
| 594 | my $ret_val = $auth_type->$requirement($r, $args); | ||
| 595 | $r->log_error("$auth_type->$requirement returned $ret_val") if $debug >= 3; | ||
| 596 | next if $ret_val == OK; | ||
| 597 | |||
| 598 | # Nothing succeeded, deny access to this user. | ||
| 599 | $forbidden = 1; | ||
| 600 | last; | ||
| 601 | } | ||
| 602 | |||
| 603 | |||
| 604 | return $forbidden ? FORBIDDEN : OK; | ||
| 605 | } | ||
| 606 | |||
| 607 | sub send_cookie { | ||
| 608 | my ($self, %settings ) = @_; | ||
| 609 | my $r = Apache->request(); | ||
| 610 | |||
| 611 | my ( $auth_name, $auth_type ) = ( $r->auth_name, $r->auth_type ); | ||
| 612 | |||
| 613 | return if $r->dir_config( $auth_name . 'NoCookie' ); | ||
| 614 | |||
| 615 | $settings{name} = $settings{name} | ||
| 616 | ? "${auth_type}_$auth_name$settings{name}" | ||
| 617 | : "${auth_type}_$auth_name"; | ||
| 618 | |||
| 619 | |||
| 620 | for (qw/Path Expires Domain Secure/) { | ||
| 621 | next if exists $settings{ lc() }; | ||
| 622 | |||
| 623 | if (my $value = $r->dir_config( $auth_name . $_ )) { | ||
| 624 | $settings{ lc() } = $value; | ||
| 625 | } | ||
| 626 | } | ||
| 627 | |||
| 628 | # Remove any undef settings | ||
| 629 | my @deletes = grep { ! defined $settings{$_} } keys %settings; | ||
| 630 | delete $settings{$_} for @deletes; | ||
| 631 | |||
| 632 | $settings{path} ||= '/'; # need to do this so will return cookie when url is munged. | ||
| 633 | |||
| 634 | my $cookie = Apache::Cookie->new( $r, %settings ); | ||
| 635 | $cookie->bake; | ||
| 636 | |||
| 637 | my $debug = $r->dir_config( DEBUG ) || 0; | ||
| 638 | $r->log_error('Send cookie: ' . $cookie->as_string ) if $debug >= 3; | ||
| 639 | |||
| 640 | |||
| 641 | } | ||
| 642 | |||
| 643 | sub key { | ||
| 644 | my $self = shift; | ||
| 645 | my $r = Apache->request; | ||
| 646 | |||
| 647 | my ( $auth_name, $auth_type ) = ( $r->auth_name, $r->auth_type ); | ||
| 648 | my ( $ses_key_cookie ) = ($r->header_in( 'Cookie' ) || '') =~ /${auth_type}_$auth_name=([^;]+)/; | ||
| 649 | |||
| 650 | return $ses_key_cookie || $r->notes( 'URI_Session' ) || undef; | ||
| 651 | } | ||
| 652 | |||
| 653 | 1; | ||
| 654 | |||
| 655 | __END__ | ||