| 1 | #============================================================================= | ||
| 2 | # LibWeb::Session -- Session management for libweb applications. | ||
| 3 | |||
| 4 | package LibWeb::Session; | ||
| 5 | |||
| 6 | # Copyright (C) 2000 Colin Kong | ||
| 7 | # | ||
| 8 | # This program is free software; you can redistribute it and/or | ||
| 9 | # modify it under the terms of the GNU General Public License | ||
| 10 | # as published by the Free Software Foundation; either version 2 | ||
| 11 | # of the License, or (at your option) any later version. | ||
| 12 | # | ||
| 13 | # This program is distributed in the hope that it will be useful, | ||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | # GNU General Public License for more details. | ||
| 17 | # | ||
| 18 | # You should have received a copy of the GNU General Public License | ||
| 19 | # along with this program; if not, write to the Free Software | ||
| 20 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 21 | #============================================================================= | ||
| 22 | |||
| 23 | # $Id: Session.pm,v 1.7 2000/07/19 20:31:57 ckyc Exp $ | ||
| 24 | |||
| 25 | $VERSION = '0.02'; | ||
| 26 | |||
| 27 | #-############################# | ||
| 28 | # Use standard library. | ||
| 29 | use Carp; | ||
| 30 | use strict; | ||
| 31 | use vars qw(@ISA $VERSION); | ||
| 32 | |||
| 33 | #-############################# | ||
| 34 | # Use custom library. | ||
| 35 | require LibWeb::Core; | ||
| 36 | require LibWeb::Digest; | ||
| 37 | require LibWeb::Crypt; | ||
| 38 | ##require LibWeb::CGI; | ||
| 39 | ##require LibWeb::Database; | ||
| 40 | |||
| 41 | #-############################# | ||
| 42 | # Inheritance. | ||
| 43 | @ISA = qw(LibWeb::Core); | ||
| 44 | |||
| 45 | #-############################# | ||
| 46 | # Methods. | ||
| 47 | sub new { | ||
| 48 | # | ||
| 49 | # Params: $class [, $rc_file] | ||
| 50 | # | ||
| 51 | # - $class is the class/package name of this package, be it a string | ||
| 52 | # or a reference. | ||
| 53 | # - $rc_file is the absolute path to the rc file for LibWeb. | ||
| 54 | # | ||
| 55 | # Usage: my $object = new LibWeb::Admin([$rc_file]); | ||
| 56 | # | ||
| 57 | my ($class, $Class, $self); | ||
| 58 | $class = shift; | ||
| 59 | $Class = ref($class) || $class; | ||
| 60 | |||
| 61 | # Inherit instance variables from the base class. | ||
| 62 | $self = $Class->SUPER::new(shift); | ||
| 63 | bless($self, $Class); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub DESTROY {} | ||
| 67 | |||
| 68 | sub _checkDummyCookieIntegrity { | ||
| 69 | # | ||
| 70 | # Params: | ||
| 71 | # _dummyCookies_ [, _no_auth_ ] | ||
| 72 | # | ||
| 73 | # Pre: | ||
| 74 | # 1. `_dummyCookies_' is a scalar which is the dummy cookie retrieved | ||
| 75 | # from $ENV{HTTP_COOKIE}. | ||
| 76 | # 2. `_no_auth_' is a CODE reference for callback if the viewing browser | ||
| 77 | # does not return an authentication cookie. | ||
| 78 | # | ||
| 79 | my ($self, $dc, $no_auth_callback); | ||
| 80 | ($self,$dc,$no_auth_callback) = (shift,shift,shift); | ||
| 81 | unless ($dc =~ m:^B=.+$:) { | ||
| 82 | if ($no_auth_callback) { | ||
| 83 | return undef; | ||
| 84 | } else { | ||
| 85 | $self->fatal( -alertMsg => 'Dummy cookie has been tampered with.', | ||
| 86 | -isDisplay => 0 ); | ||
| 87 | $self->_redirect_to_login_page(); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _getAuthInfoFromCookie { | ||
| 94 | # | ||
| 95 | # Params: | ||
| 96 | # [ _isJustLogin_, _no_auth_ ] | ||
| 97 | # | ||
| 98 | # Pre: | ||
| 99 | # 1. Parameter `_isJustLogin_' is to indicate whether this is the first login | ||
| 100 | # check. This parameter should be defined in order to check whether the | ||
| 101 | # remote Web browser is cookie enabled when first logging in. | ||
| 102 | # 2. `_no_auth_' is a CODE reference for callback if the viewing browser | ||
| 103 | # does not return an authentication cookie. | ||
| 104 | # 3. The auth. cookie must came after the dummy cookie in the $ENV{HTTP_COOKIE} | ||
| 105 | # string i.e. in the second position. | ||
| 106 | # | ||
| 107 | # Post: | ||
| 108 | # 1. If `_no_auth_' is provided, then call it; otherwise, Output an error message | ||
| 109 | # and exit the program if integrity of the dummy cookies have been tampered | ||
| 110 | # with. This is achieved by checking the positions of the dummy. | ||
| 111 | # | ||
| 112 | # 2. Return encrypted auth. info in an array | ||
| 113 | # (sid, issueTime, expireTime, user, ip, uid, MAC) if auth. cookie is set | ||
| 114 | # properly on client Web browser; ow, return undef (null). | ||
| 115 | # | ||
| 116 | # Since the authentication cookie is stored as one single gigantic string, | ||
| 117 | # need to parse it to get sub info within it. | ||
| 118 | # | ||
| 119 | my ($self, $is_just_login, $no_auth_callback, @cookies, $auth); | ||
| 120 | $self = shift; | ||
| 121 | $is_just_login = shift; | ||
| 122 | $no_auth_callback = shift; | ||
| 123 | |||
| 124 | return undef | ||
| 125 | unless ( $self->is_browser_cookie_enabled( $is_just_login, $no_auth_callback ) ); | ||
| 126 | |||
| 127 | @cookies = split /; /, $ENV{HTTP_COOKIE}; | ||
| 128 | $self->_checkDummyCookieIntegrity( shift(@cookies), $no_auth_callback ); | ||
| 129 | $auth = shift @cookies; | ||
| 130 | if ($auth) { | ||
| 131 | $auth =~ m:^C=z=(.*)&y=(.*)&x=(.*)&w=(.*)&v=(.*)&u=(.*)&t=(.*)$:; | ||
| 132 | return ($1, $2, $3, $4, $5, $6, $7); | ||
| 133 | } | ||
| 134 | |||
| 135 | return undef; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _redirect_to_login_page { | ||
| 139 | my $self = shift; | ||
| 140 | require LibWeb::CGI; | ||
| 141 | LibWeb::CGI->new()->redirect( | ||
| 142 | -url => $self->{LM_IN}, | ||
| 143 | -cookie => $self->prepare_deauth_cookie() | ||
| 144 | ); | ||
| 145 | exit(0); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub is_browser_cookie_enabled { | ||
| 149 | # | ||
| 150 | # Params: | ||
| 151 | # [ _is_just_login_, _no_auth_ ] | ||
| 152 | # | ||
| 153 | # Pre: | ||
| 154 | # 1. _is_just_login_ is either 1 or undef indicating whether the user has | ||
| 155 | # just logged in. | ||
| 156 | # 2. _no_auth_ is a CODE reference for callback if the viewing browser | ||
| 157 | # does not return an authentication cookie. | ||
| 158 | # | ||
| 159 | # Post: | ||
| 160 | # 1. Print out an error to client Web browser telling the user that his/her | ||
| 161 | # browser is not cookie enabled and exit the program if $ENV{HTTP_COOKIE} | ||
| 162 | # is not defined and `_is_just_login_' is defined. | ||
| 163 | # 2. If $ENV{HTTP_COOKIE} is not defined and `_is_just_login_' is undef, | ||
| 164 | # call `_no_auth_' if it is provided; otherwise, redirect remote Web browser | ||
| 165 | # to a login page | ||
| 166 | # 3. Return 1 ow. | ||
| 167 | # | ||
| 168 | # Caveat: $ENV{HTTP_COOKIE} is still null even if the browser is cookie-enabled | ||
| 169 | # if no cookie is available for our domain. Therefore a dummy cookie is | ||
| 170 | # sent to client Web browser (it stays for one browser session) to help | ||
| 171 | # indicate that the browser is still cookie-enabled (i.e. to keep | ||
| 172 | # $ENV{HTTP_COOKIE} defined even after DeAuth. cookie has been issued.) | ||
| 173 | # For details, see LibWeb::Admin::_authenticateLogin() where the dummy | ||
| 174 | # cookie is first set. | ||
| 175 | # | ||
| 176 | my ($self, $is_just_login, $no_auth_callback); | ||
| 177 | $self = shift; | ||
| 178 | $is_just_login = shift; | ||
| 179 | $no_auth_callback = shift; | ||
| 180 | unless ( $ENV{HTTP_COOKIE} ) { | ||
| 181 | |||
| 182 | if ( $is_just_login ) { | ||
| 183 | |||
| 184 | $self->fatal( -msg => 'Your browser is not cookie enabled.', | ||
| 185 | -alertMsg => 'Could not retrieve cookie.', | ||
| 186 | -helpMsg => $self->{HHTML}->cookie_error(), | ||
| 187 | -cookie => $self->prepare_deauth_cookie() ); | ||
| 188 | |||
| 189 | } elsif ($no_auth_callback) { | ||
| 190 | |||
| 191 | return undef; | ||
| 192 | |||
| 193 | } else { | ||
| 194 | |||
| 195 | $self->fatal( | ||
| 196 | -alertMsg => | ||
| 197 | 'LibWeb::Session::is_browser_cookie_enabled: no cookie.', | ||
| 198 | -isDisplay => 0 | ||
| 199 | ); | ||
| 200 | $self->_redirect_to_login_page(); | ||
| 201 | } | ||
| 202 | |||
| 203 | } | ||
| 204 | |||
| 205 | return 1; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub get_user { | ||
| 209 | # | ||
| 210 | # Check to see if the viewing Web browser has logged in by | ||
| 211 | # checking expiration time, IP and MAC in the authentication cookie, and | ||
| 212 | # return user name and user ID if it passes all the authentication checks. | ||
| 213 | # | ||
| 214 | # Params: | ||
| 215 | # [ -no_auth=>, -mac_mismatch=>, -ip_mismatch=>, | ||
| 216 | # -expired=>, -is_update_db=> ] | ||
| 217 | # | ||
| 218 | # Pre: | ||
| 219 | # 1. -no_auth is a CODE reference for callback if the viewing browser | ||
| 220 | # does not return an authentication cookie. | ||
| 221 | # 2. -mac_mismatch is a CODE reference for callback if the authentication | ||
| 222 | # cookie has been tampered with. | ||
| 223 | # 3. -ip_mismatch is a CODE reference for callback if the IP in the | ||
| 224 | # authentication cookie does not correspond to the IP address of the | ||
| 225 | # viewing browser. | ||
| 226 | # 4. -expired is a CODE reference for callback if the authentication cookie | ||
| 227 | # is expired. | ||
| 228 | # 5. -is_update_db is either 1 or 0 (default). Use this to indicate whether | ||
| 229 | # this is the first login check. This parameter should be 1 in order to | ||
| 230 | # update database's 'NUM_LOGIN_ATTEMPT' when user first logged in. | ||
| 231 | # | ||
| 232 | # Post: | ||
| 233 | # 1. Retrieve authentication cookies from client Web browser. | ||
| 234 | # | ||
| 235 | # 2. All -no_auth, -mac_mismatch, _ip_mismatch and -expired default to | ||
| 236 | # the following actions if you do not provide the callbacks: | ||
| 237 | # * Nullify and delete all authentication cookies resided on the viewing | ||
| 238 | # Web browser, | ||
| 239 | # * send an alert e-mail to ADMIN_EMAIL, | ||
| 240 | # * log that event in FATAL_LOG, and | ||
| 241 | # * redirect the remote user to the login page (LM_IN). | ||
| 242 | # | ||
| 243 | # 3. Check to see If cookie values are null/zero. | ||
| 244 | # | ||
| 245 | # 4. Check to see If MAC mis-matches (this means possible hacking from remote | ||
| 246 | # host). | ||
| 247 | # | ||
| 248 | # 5. Check to see if If IP mis-matches (this means possible hacking from remote | ||
| 249 | # host). | ||
| 250 | # | ||
| 251 | # 6. Check to see if the login has expired and update database: Set | ||
| 252 | # 'NUM_LOGIN_ATTEMPT' to 0 if this is the case. | ||
| 253 | # | ||
| 254 | # 7. If client has officially logged in and none of #3, #4, #5 and #6 happens, | ||
| 255 | # set database's NUM_LOGIN_ATTEMPT to LOGIN_INDICATOR if parameter | ||
| 256 | # -is_update_db is defined and is equal to 1. This helps indicate that the | ||
| 257 | # user is online (currently login). | ||
| 258 | # | ||
| 259 | # 8. And finally return an array (user name and uid) in plain text. | ||
| 260 | # | ||
| 261 | # Note: | ||
| 262 | # 'NUM_LOGIN_ATTEMPT' != 0 && != 'LOGIN_INDICATOR' means | ||
| 263 | # there were several attempts to login but unsuccessful solely because | ||
| 264 | # cryptGuess != cryptRealPass. Need to re-flush it to 0 manually | ||
| 265 | # after 24 hours of receiving the alert email if this value == the max | ||
| 266 | # login attempt allowed (MAX_LOGIN_ATTEMPT_ALLOWED). | ||
| 267 | # | ||
| 268 | my ($self,$no_auth,$mac_mismatch,$ip_mismatch,$expired,$is_update_db, | ||
| 269 | $sid, $issueTime, $expireTime, $user, $guessIP, $guessCUID, $guessMAC, | ||
| 270 | $crypt, $digest, $alertMsg, $userName, $uid, $macKey, | ||
| 271 | $preRealMAC, $realMAC, $realIP, $decryptExpireTime, $gmcDecryptExpireTime, | ||
| 272 | $db, $log, $sqlStatement); | ||
| 273 | $self = shift; | ||
| 274 | ($no_auth,$mac_mismatch,$ip_mismatch,$expired,$is_update_db) | ||
| 275 | = $self->rearrange( | ||
| 276 | [ | ||
| 277 | 'NO_AUTH','MAC_MISMATCH','IP_MISMATCH','EXPIRED', | ||
| 278 | 'IS_UPDATE_DB' | ||
| 279 | ], @_ | ||
| 280 | ); | ||
| 281 | |||
| 282 | $is_update_db ||= 0; | ||
| 283 | |||
| 284 | $crypt = LibWeb::Crypt->new(); | ||
| 285 | $digest = LibWeb::Digest->new(); | ||
| 286 | |||
| 287 | my( | ||
| 288 | $cipher_key, $cipher_algorithm, $cipher_format, | ||
| 289 | $digest_key, $digest_algorithm, $digest_format | ||
| 290 | ) | ||
| 291 | = ( | ||
| 292 | $self->{CIPHER_KEY}, $self->{CIPHER_ALGORITHM}, $self->{CIPHER_FORMAT}, | ||
| 293 | $self->{DIGEST_KEY}, $self->{DIGEST_ALGORITHM}, $self->{DIGEST_FORMAT} | ||
| 294 | ); | ||
| 295 | |||
| 296 | # Does the viewing browser has the authentication cookie set? | ||
| 297 | ($sid, $issueTime, $expireTime, $user, $guessIP, $guessCUID, $guessMAC) = | ||
| 298 | $self->_getAuthInfoFromCookie( $is_update_db, $no_auth ); | ||
| 299 | unless ( defined($sid) && defined($issueTime) && defined($expireTime) && | ||
| 300 | defined($user) && defined($guessIP) && defined($guessCUID) && | ||
| 301 | defined($guessMAC) ) { | ||
| 302 | |||
| 303 | if ($no_auth) { | ||
| 304 | eval { &{$no_auth}; }; | ||
| 305 | croak "-no_auth must be a CODE reference." if $@; | ||
| 306 | return undef; | ||
| 307 | } else { | ||
| 308 | $self->fatal( -alertMsg => 'No auth. cookies!!!', -isDisplay => 0 ); | ||
| 309 | $self->_redirect_to_login_page(); | ||
| 310 | } | ||
| 311 | |||
| 312 | } | ||
| 313 | |||
| 314 | # Generate a digest of remote IP. | ||
| 315 | # Note: some proxies have rotating IPs, can't check their IP in that case. | ||
| 316 | # How to get the ``true'' IP of remote browser? | ||
| 317 | #if ( $ENV{REMOTE_HOST} =~ m:^proxy: ) { $realIP = $guessIP; } | ||
| 318 | if ( $ENV{HTTP_VIA} ) { | ||
| 319 | $realIP = $guessIP; | ||
| 320 | } else { | ||
| 321 | $realIP = $digest->generate_digest( | ||
| 322 | -data => $ENV{REMOTE_ADDR}, | ||
| 323 | -key => $digest_key, | ||
| 324 | -algorithm => $digest_algorithm, | ||
| 325 | -format => $digest_format | ||
| 326 | ); | ||
| 327 | } | ||
| 328 | |||
| 329 | # Generate a MAC. | ||
| 330 | $macKey = $self->{MAC_KEY}; | ||
| 331 | $preRealMAC = | ||
| 332 | $digest->generate_MAC( | ||
| 333 | -data => $sid.$issueTime.$expireTime.$user.$realIP.$guessCUID.$macKey, | ||
| 334 | -key => $macKey, | ||
| 335 | -algorithm => $digest_algorithm, | ||
| 336 | -format => $digest_format | ||
| 337 | ); | ||
| 338 | $realMAC = $digest->generate_MAC( | ||
| 339 | -data => $macKey.$preRealMAC, | ||
| 340 | -key => $macKey, | ||
| 341 | -algorithm => $digest_algorithm, | ||
| 342 | -format => $digest_format | ||
| 343 | ); | ||
| 344 | |||
| 345 | # MAC check. | ||
| 346 | unless ($guessMAC eq $realMAC) { | ||
| 347 | if ($mac_mismatch) { | ||
| 348 | eval { &{$mac_mismatch}; }; | ||
| 349 | croak "-mac_mismatch must be a CODE reference." if $@; | ||
| 350 | return undef; | ||
| 351 | } else { | ||
| 352 | $alertMsg = "MAC mis-match!!!\nGuessMAC: $guessMAC\nRealMAC: $realMAC\n"; | ||
| 353 | $self->fatal( -alertMsg => $alertMsg, -isDisplay => 0 ); | ||
| 354 | $self->_redirect_to_login_page(); | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | # IP check. | ||
| 359 | unless ($guessIP eq $realIP) { | ||
| 360 | if ($ip_mismatch) { | ||
| 361 | eval { &{$ip_mismatch}; }; | ||
| 362 | croak "-ip_mismatch must be a CODE reference." if $@; | ||
| 363 | return undef; | ||
| 364 | } else { | ||
| 365 | $alertMsg = "IP mis-match!!!\nGuessIP: $guessIP\nRealIP: $realIP\n"; | ||
| 366 | $self->fatal( -alertMsg => $alertMsg, -isDisplay => 0 ); | ||
| 367 | $self->_redirect_to_login_page(); | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | # Decrypt username from the authentication cookie. | ||
| 372 | $userName = $crypt->decrypt_cipher( | ||
| 373 | -cipher => $user, | ||
| 374 | -key => $cipher_key, | ||
| 375 | -algorithm => $cipher_algorithm, | ||
| 376 | -format => $cipher_format | ||
| 377 | ); | ||
| 378 | |||
| 379 | # Decrypt uid from the authentication cookie. | ||
| 380 | $uid = $crypt->decrypt_cipher( | ||
| 381 | -cipher => $guessCUID, | ||
| 382 | -key => $cipher_key, | ||
| 383 | -algorithm => $cipher_algorithm, | ||
| 384 | -format => $cipher_format | ||
| 385 | ); | ||
| 386 | |||
| 387 | # Decrypt the expire time from the authentication cookie. | ||
| 388 | $decryptExpireTime = | ||
| 389 | $crypt->decrypt_cipher( | ||
| 390 | -cipher => $expireTime, | ||
| 391 | -key => $cipher_key, | ||
| 392 | -algorithm => $cipher_algorithm, | ||
| 393 | -format => $cipher_format | ||
| 394 | ); | ||
| 395 | |||
| 396 | # Is the authentication cookie expired? | ||
| 397 | unless ( $decryptExpireTime > time() ) { | ||
| 398 | if ($expired) { | ||
| 399 | eval { &{$expired}; }; | ||
| 400 | croak "-expired must be a CODE reference." if $@; | ||
| 401 | return undef; | ||
| 402 | } else { | ||
| 403 | require LibWeb::Database; | ||
| 404 | $db = new LibWeb::Database(); | ||
| 405 | |||
| 406 | # Flush the database. Set `NUM_LOGIN_ATTEMPT' to 0. | ||
| 407 | $sqlStatement = "update $self->{USER_LOG_TABLE} " . | ||
| 408 | "set $self->{USER_LOG_TABLE_NUM_LOGIN_ATTEMPT}=0 " . | ||
| 409 | "where $self->{USER_LOG_TABLE_UID}=$uid"; | ||
| 410 | $db->do( -sql => $sqlStatement ); | ||
| 411 | $db->finish(); | ||
| 412 | |||
| 413 | # Alert admin and redirect user to login page. | ||
| 414 | $gmcDecryptExpireTime = localtime($decryptExpireTime); | ||
| 415 | $alertMsg = "Login session expired.\n " . | ||
| 416 | "Current time: " . localtime() . "\n " . | ||
| 417 | "Expire time: $gmcDecryptExpireTime\n"; | ||
| 418 | $self->fatal( -alertMsg => $alertMsg, -isDisplay => 0 ); | ||
| 419 | $self->_redirect_to_login_page(); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | # Update database. | ||
| 424 | if ( $is_update_db ) { | ||
| 425 | |||
| 426 | require LibWeb::Database; | ||
| 427 | $db = new LibWeb::Database(); | ||
| 428 | |||
| 429 | my $time = localtime(); | ||
| 430 | my $ip = $ENV{REMOTE_ADDR}; | ||
| 431 | my $host = $ENV{REMOTE_HOST}; | ||
| 432 | $sqlStatement = "update $self->{USER_LOG_TABLE} " . | ||
| 433 | "set $self->{USER_LOG_TABLE_NUM_LOGIN_ATTEMPT}=" . | ||
| 434 | "$self->{LOGIN_INDICATOR}, " . | ||
| 435 | "$self->{USER_LOG_TABLE_LAST_LOGIN}='$time', " . | ||
| 436 | "$self->{USER_LOG_TABLE_IP}='$ip', " . | ||
| 437 | "$self->{USER_LOG_TABLE_HOST}='$host' " . | ||
| 438 | "where $self->{USER_LOG_TABLE_UID}=$uid"; | ||
| 439 | $db->do( -sql => $sqlStatement ); | ||
| 440 | $db->finish(); | ||
| 441 | } | ||
| 442 | |||
| 443 | return ($userName, $uid); | ||
| 444 | } | ||
| 445 | |||
| 446 | sub is_login { | ||
| 447 | # | ||
| 448 | # This is here mainly for backward compatible with client codes | ||
| 449 | # written using LibWeb-0.01. | ||
| 450 | # | ||
| 451 | shift->get_user( -is_update_db => ($_[0]) ? 1 : 0 ); | ||
| 452 | } | ||
| 453 | |||
| 454 | # Selfloading methods declaration. | ||
| 455 | sub LibWeb::Session::prepare_deauth_cookie ; | ||
| 456 | 1; | ||
| 457 | __DATA__ |