| 1 | #============================================================================= | ||
| 2 | # LibWeb::Admin -- User authentication for libweb applications. | ||
| 3 | |||
| 4 | package LibWeb::Admin; | ||
| 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: Admin.pm,v 1.4 2000/07/18 06:33:30 ckyc Exp $ | ||
| 24 | |||
| 25 | #-############################ | ||
| 26 | # Use standard library. | ||
| 27 | use SelfLoader; | ||
| 28 | use strict; | ||
| 29 | use vars qw(@ISA $VERSION); | ||
| 30 | |||
| 31 | #-############################ | ||
| 32 | # Use custom library. | ||
| 33 | require LibWeb::Session; | ||
| 34 | ##require LibWeb::Database; | ||
| 35 | |||
| 36 | #-############################ | ||
| 37 | # Inheritance. | ||
| 38 | @ISA = qw(LibWeb::Session); | ||
| 39 | |||
| 40 | $VERSION = '0.02'; | ||
| 41 | |||
| 42 | #-############################ | ||
| 43 | # Methods. | ||
| 44 | sub new { | ||
| 45 | # | ||
| 46 | # Params: $class [, $rc_file] | ||
| 47 | # | ||
| 48 | # - $class is the class/package name of this package, be it a string | ||
| 49 | # or a reference. | ||
| 50 | # - $rc_file is the absolute path to the rc file for LibWeb. | ||
| 51 | # | ||
| 52 | # Usage: my $object = new LibWeb::Admin([$rc_file]); | ||
| 53 | # | ||
| 54 | my ($class, $Class, $self); | ||
| 55 | $class = shift; | ||
| 56 | $Class = ref($class) || $class; | ||
| 57 | |||
| 58 | # Inherit instance variables from the base class. | ||
| 59 | $self = $Class->SUPER::new(shift); | ||
| 60 | bless($self, $Class); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub DESTROY {} | ||
| 64 | |||
| 65 | sub _authenticateLogin { | ||
| 66 | # | ||
| 67 | # Params: | ||
| 68 | # (\$uid, \$usrName, $db) | ||
| 69 | # | ||
| 70 | # Post: | ||
| 71 | # 1. Send the prepared auth. cookie: session ID, issue time, | ||
| 72 | # expiration time, user name, IP, UID, MAC, where all elements are encrypted | ||
| 73 | # (one-way encrypted; except for the expiration time, user name and UID which | ||
| 74 | # are encrypted as ciphers (two-way encrypted) for later decryption during | ||
| 75 | # authentication check and for generating logout cookies. | ||
| 76 | # | ||
| 77 | # 2. Update database: `LAST_LOGIN' (Internet address, IP and date/time) and | ||
| 78 | # `NUM_LOGIN_ATTEMPT' for that user IS NOT done here since we want to make | ||
| 79 | # sure client Web browser accepts cookie first. This task is delegated | ||
| 80 | # to sub is_login(1) which should be called by client codes when user is | ||
| 81 | # FIRST login. Subsequent authenticate checks should call islogin() instead | ||
| 82 | # of is_login(1); | ||
| 83 | # | ||
| 84 | # 3. Return 1 upon success. | ||
| 85 | # | ||
| 86 | # Note: how a MAC is generated. Could use MD5 or SHA etc. | ||
| 87 | # MAC = MD5("secret key " + | ||
| 88 | # MD5("session ID" + "issue time" + "expiration time" + | ||
| 89 | # "user name" + "IP address" + "user id" + "secret key") | ||
| 90 | # ), | ||
| 91 | # where session ID = PID(which is $$) + UID(which is $$uid). | ||
| 92 | # | ||
| 93 | |||
| 94 | #============== #1 ===================== | ||
| 95 | my ($self, $crypt, $digest, $uid, $usrName, $db, $sid, $issueTime, $expireTime, | ||
| 96 | $user, $ip, $cuid, $macKey, $preMAC, $MAC, $dummyCookie, $auth_cookie); | ||
| 97 | $self = shift; | ||
| 98 | ($uid, $usrName, $db) = @_; | ||
| 99 | $crypt = LibWeb::Crypt->new(); | ||
| 100 | $digest = LibWeb::Digest->new(); | ||
| 101 | my( | ||
| 102 | $cipher_key, $cipher_algorithm, $cipher_format, | ||
| 103 | $digest_key, $digest_algorithm, $digest_format | ||
| 104 | ) | ||
| 105 | = ( | ||
| 106 | $self->{CIPHER_KEY}, $self->{CIPHER_ALGORITHM}, $self->{CIPHER_FORMAT}, | ||
| 107 | $self->{DIGEST_KEY}, $self->{DIGEST_ALGORITHM}, $self->{DIGEST_FORMAT} | ||
| 108 | ); | ||
| 109 | |||
| 110 | ($sid, $issueTime, $expireTime, $user, $ip, $cuid, $macKey) = | ||
| 111 | ( | ||
| 112 | $digest->generate_digest( | ||
| 113 | -data => $$ + $$uid, | ||
| 114 | -key => $digest_key, | ||
| 115 | -algorithm => $digest_algorithm, | ||
| 116 | -format => $digest_format | ||
| 117 | ), | ||
| 118 | $digest->generate_digest( | ||
| 119 | -data => time(), | ||
| 120 | -key => $digest_key, | ||
| 121 | -algorithm => $digest_algorithm, | ||
| 122 | -format => $digest_format | ||
| 123 | ), | ||
| 124 | $crypt->encrypt_cipher( | ||
| 125 | -data => time() + $self->{LOGIN_DURATION_ALLOWED}, | ||
| 126 | -key => $cipher_key, | ||
| 127 | -algorithm => $cipher_algorithm, | ||
| 128 | -format => $cipher_format | ||
| 129 | ), | ||
| 130 | $crypt->encrypt_cipher( | ||
| 131 | -data => $$usrName, | ||
| 132 | -key => $cipher_key, | ||
| 133 | -algorithm => $cipher_algorithm, | ||
| 134 | -format => $cipher_format | ||
| 135 | ), | ||
| 136 | $digest->generate_digest( | ||
| 137 | -data => $ENV{REMOTE_ADDR}, | ||
| 138 | -key => $digest_key, | ||
| 139 | -algorithm => $digest_algorithm, | ||
| 140 | -format => $digest_format | ||
| 141 | ), | ||
| 142 | $crypt->encrypt_cipher( | ||
| 143 | -data => $$uid, | ||
| 144 | -key => $cipher_key, | ||
| 145 | -algorithm => $cipher_algorithm, | ||
| 146 | -format => $cipher_format | ||
| 147 | ), | ||
| 148 | $self->{MAC_KEY} | ||
| 149 | ); | ||
| 150 | $preMAC = | ||
| 151 | $digest->generate_MAC( | ||
| 152 | -data => $sid.$issueTime.$expireTime.$user.$ip.$cuid.$macKey, | ||
| 153 | -key => $macKey, | ||
| 154 | -algorithm => $digest_algorithm, | ||
| 155 | -format => $digest_format | ||
| 156 | ); | ||
| 157 | $MAC = $digest->generate_MAC( | ||
| 158 | -data => $macKey.$preMAC, | ||
| 159 | -key => $macKey, | ||
| 160 | -algorithm => $digest_algorithm, | ||
| 161 | -format => $digest_format | ||
| 162 | ); | ||
| 163 | $dummyCookie = | ||
| 164 | "B=" . | ||
| 165 | $digest->generate_digest( | ||
| 166 | -data => rand( $self->{RAND_RANGE} ), | ||
| 167 | -key => $digest_key, | ||
| 168 | -algorithm => $digest_algorithm, | ||
| 169 | -format => $digest_format | ||
| 170 | ) . | ||
| 171 | "; path=/"; | ||
| 172 | # CGI::Cookie has problem with characters `=' and `&' or a problem with my | ||
| 173 | # understanding of autoescaping? Therefore prepare the auth cookie manually | ||
| 174 | # instead. | ||
| 175 | $auth_cookie = | ||
| 176 | "C=z=$sid&y=$issueTime&x=$expireTime&w=$user&v=$ip&u=$cuid&t=$MAC; path=/"; | ||
| 177 | LibWeb::CGI->new()->send_cookie([$dummyCookie, $auth_cookie]); | ||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub _handleLogin { | ||
| 182 | # | ||
| 183 | # Params: | ||
| 184 | # (\$uid, \$usrname, \$cryptRealPass, \$guess, \$numLoginAttempt, $db) | ||
| 185 | # | ||
| 186 | # Pre: | ||
| 187 | # 1. None. | ||
| 188 | # | ||
| 189 | # Post: | ||
| 190 | # 1. Writing... | ||
| 191 | # | ||
| 192 | my ($self, $uid, $usrName, $cryptRealPass, $guess, $numLoginAttempt, $db); | ||
| 193 | $self = shift; | ||
| 194 | ($uid, $usrName, $cryptRealPass, $guess, $numLoginAttempt, $db) = @_; | ||
| 195 | |||
| 196 | # Prevent further login attempt if reached max login attempt allowed. | ||
| 197 | $self->fatal(-msg => 'Maximum login attempts reached.', | ||
| 198 | -alertMsg => "Max guess ($$guess) for account ($$usrName) reached.", | ||
| 199 | -helpMsg => $self->{HHTML}->exceeded_max_login_attempt(), | ||
| 200 | -cookie => $self->prepare_deauth_cookie()) | ||
| 201 | unless ($$numLoginAttempt < $self->{MAX_LOGIN_ATTEMPT_ALLOWED} || | ||
| 202 | $$numLoginAttempt == $self->{LOGIN_INDICATOR}); | ||
| 203 | |||
| 204 | # Compare crypted guess with crypted real password. | ||
| 205 | my $cryptGuess = crypt($$guess, $$cryptRealPass); | ||
| 206 | if ($cryptGuess eq $$cryptRealPass) { | ||
| 207 | |||
| 208 | # Alert admin if more than one login per account at a time. | ||
| 209 | if ($$numLoginAttempt == $self->{LOGIN_INDICATOR}) { | ||
| 210 | # my $sql_statement = "update $self->{USER_LOG_TABLE} " . | ||
| 211 | # "set $self->{USER_LOG_TABLE_NUM_LOGIN_ATTEMPT}=0 " . | ||
| 212 | # "where $self->{USER_LOG_TABLE_UID}='$$uid'"; | ||
| 213 | # $db->do( -sql => $sql_statement ); | ||
| 214 | $self->fatal( | ||
| 215 | -alertMsg => "Attempt to use guess [$$guess] to log in " . | ||
| 216 | "as user [$$usrName] while account's login " . | ||
| 217 | "indicator is high!", | ||
| 218 | -isDisplay => 0 | ||
| 219 | ); | ||
| 220 | } | ||
| 221 | |||
| 222 | $self->_authenticateLogin($uid, $usrName, $db); | ||
| 223 | } | ||
| 224 | elsif ($cryptGuess ne $$cryptRealPass) { | ||
| 225 | $self->_failLogin($uid, $usrName, $guess, $numLoginAttempt, $db); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | sub is_logout { | ||
| 230 | # | ||
| 231 | # Params: | ||
| 232 | # none. | ||
| 233 | # | ||
| 234 | # Check to see if authentication cookies have been removed from | ||
| 235 | # client Web browser and return true (1). Otherwise, print | ||
| 236 | # $self->{HHTML}->logout_failed() and exit the program. | ||
| 237 | # | ||
| 238 | my ($self, $sid, $issueTime, $expireTime, $cryptUsrname, $guessIP, $guessCUID, | ||
| 239 | $guessMAC); | ||
| 240 | $self = shift; | ||
| 241 | ($sid, $issueTime, $expireTime, $cryptUsrname, $guessIP, $guessCUID, $guessMAC) | ||
| 242 | = $self->_getAuthInfoFromCookie(); | ||
| 243 | $self->fatal(-msg => 'Could not logout.', | ||
| 244 | -alertMsg => 'Auth cookie is still defined after logout()!!!', | ||
| 245 | -helpMsg => $self->{HHTML}->logout_failed(), | ||
| 246 | -cookie => $self->prepare_deauth_cookie()) | ||
| 247 | if ( defined($sid) || defined($issueTime) || defined($expireTime) || | ||
| 248 | defined($cryptUsrname) || defined($guessIP) || defined($guessCUID) || | ||
| 249 | defined($guessMAC) ); | ||
| 250 | return 1; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub login { | ||
| 254 | # | ||
| 255 | # Params: | ||
| 256 | # (user_name, guess). | ||
| 257 | # | ||
| 258 | # Pre: | ||
| 259 | # 1. None. | ||
| 260 | # | ||
| 261 | # Post: | ||
| 262 | # | ||
| 263 | # 1. Check for correct password. If correct, send the special | ||
| 264 | # authentication cookie object to client Web browser and | ||
| 265 | # return 1; alert admin and print out error page and exit ow. | ||
| 266 | # | ||
| 267 | my ($self, $usrName, $guess, $uid, $cryptRealPass, $numLoginAttempt, $db, | ||
| 268 | $bindCols, $sqlStatement, $fetchFunc, $alertMsg); | ||
| 269 | $self = shift; | ||
| 270 | $usrName = shift; | ||
| 271 | $guess = shift; | ||
| 272 | |||
| 273 | require LibWeb::Database; | ||
| 274 | $db = new LibWeb::Database(); | ||
| 275 | |||
| 276 | # Fetch encrypted user password and numLoginAttempt from database. | ||
| 277 | $bindCols = [\$uid, \$cryptRealPass, \$numLoginAttempt]; | ||
| 278 | $sqlStatement = | ||
| 279 | "select $self->{USER_PROFILE_TABLE}.$self->{USER_PROFILE_TABLE_UID}, ". | ||
| 280 | "$self->{USER_PROFILE_TABLE}.$self->{USER_PROFILE_TABLE_PASS}, ". | ||
| 281 | "$self->{USER_LOG_TABLE}.$self->{USER_LOG_TABLE_NUM_LOGIN_ATTEMPT} from ". | ||
| 282 | "$self->{USER_PROFILE_TABLE},$self->{USER_LOG_TABLE} ". | ||
| 283 | "where $self->{USER_PROFILE_TABLE_NAME} = '$usrName' ". | ||
| 284 | "and $self->{USER_LOG_TABLE}.$self->{USER_LOG_TABLE_UID} = ". | ||
| 285 | "$self->{USER_PROFILE_TABLE}.$self->{USER_PROFILE_TABLE_UID}"; | ||
| 286 | $fetchFunc = $db->query( | ||
| 287 | -sql => $sqlStatement, | ||
| 288 | -bind_cols => $bindCols | ||
| 289 | ); | ||
| 290 | &$fetchFunc; | ||
| 291 | if (defined($uid) && defined($cryptRealPass) && defined($numLoginAttempt)) { | ||
| 292 | $self->_handleLogin(\$uid, \$usrName, \$cryptRealPass, | ||
| 293 | \$guess, \$numLoginAttempt, $db); | ||
| 294 | return 1; | ||
| 295 | } | ||
| 296 | else { # No such user. | ||
| 297 | $db->finish(); | ||
| 298 | $alertMsg = "User name ($usrName) and guess ($guess) non-exist.\n"; | ||
| 299 | $self->fatal(-msg => 'Login incorrect.', alertMsg => $alertMsg, | ||
| 300 | -helpMsg => $self->{HHTML}->login_failed(), | ||
| 301 | -cookie => $self->prepare_deauth_cookie()); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | sub logout { | ||
| 306 | # | ||
| 307 | # Params: | ||
| 308 | # None. | ||
| 309 | # | ||
| 310 | # Pre: | ||
| 311 | # 1. None. | ||
| 312 | # | ||
| 313 | # Post: | ||
| 314 | # 1. Check to see if user is logged in. Get user name from cookie on client | ||
| 315 | # Web browser (decrypt). Fatal if not logged in or no auth cookie. | ||
| 316 | # 2. Flush 'NUM_LOGIN_ATTEMPT' to 0 in database. This also indicates that | ||
| 317 | # the user is currently offline (logout). | ||
| 318 | # 3. Send the prepared DeAuth. cookies for nullifying all cookies on client | ||
| 319 | # Web browser by preparing zero/null auth cookies with an expiration date | ||
| 320 | # in the past. Also set dummy cookie's value to zero. | ||
| 321 | # 4. Return 1 upon success. | ||
| 322 | # | ||
| 323 | my ($self, $usrName, $uid, $sqlStatement, $db); | ||
| 324 | $self = shift; | ||
| 325 | #=================== #1 ============================= | ||
| 326 | ($usrName, $uid) = $self->is_login(); | ||
| 327 | #=================== #2 ============================= | ||
| 328 | require LibWeb::Database; | ||
| 329 | $db = new LibWeb::Database(); | ||
| 330 | $sqlStatement = "update $self->{USER_LOG_TABLE} " . | ||
| 331 | "set $self->{USER_LOG_TABLE_NUM_LOGIN_ATTEMPT} = 0 " . | ||
| 332 | "where $self->{USER_LOG_TABLE_UID} = $uid"; | ||
| 333 | $db->do( -sql => $sqlStatement ); | ||
| 334 | $db->finish(); | ||
| 335 | #=================== #3 ============================= | ||
| 336 | # Remove auth. cookie from client Web browser and reset dummy cookie to 0. | ||
| 337 | LibWeb::CGI->new()->send_cookie($self->prepare_deauth_cookie()); | ||
| 338 | return 1; | ||
| 339 | } | ||
| 340 | |||
| 341 | # Selfloading methods declaration. | ||
| 342 | sub LibWeb::Admin::_failLogin ; | ||
| 343 | sub LibWeb::Admin::_is_user_email_registered ; | ||
| 344 | sub LibWeb::Admin::_is_user_name_registered ; | ||
| 345 | sub LibWeb::Admin::add_new_user ; | ||
| 346 | 1; | ||
| 347 | __DATA__ |