| 1 | # $Id: Auth.pm,v 1.10 2007/12/08 03:19:43 atobey Exp $ | ||
| 2 | |||
| 3 | package Apache2::AuthenSecurID::Auth; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use ModPerl::Registry; | ||
| 7 | use Apache2::porting; | ||
| 8 | use Apache2::Request; | ||
| 9 | use Apache2::Const qw(:common); | ||
| 10 | use IO::Socket::INET; | ||
| 11 | use Crypt::CBC; | ||
| 12 | use CGI qw(:standard); | ||
| 13 | use vars qw($VERSION); | ||
| 14 | |||
| 15 | $VERSION = '0.5'; | ||
| 16 | |||
| 17 | sub handler { | ||
| 18 | my $r = shift; | ||
| 19 | my $req = Apache2::Request->new($r); | ||
| 20 | |||
| 21 | # seed the random number generator | ||
| 22 | srand( time ^ $$ ^ unpack "%L*", `ps axww | gzip -f` ); | ||
| 23 | |||
| 24 | #get params | ||
| 25 | my $username = $req->param('username'); | ||
| 26 | my $passcode = $req->param('passcode'); | ||
| 27 | my $type = $req->param('type'); | ||
| 28 | my $uri = $req->param('a'); | ||
| 29 | |||
| 30 | # get ace_initd config directives | ||
| 31 | my $ace_initd_server = $r->dir_config("ace_initd_server") || "localhost"; | ||
| 32 | my $ace_initd_port = $r->dir_config("ace_initd_port") || 1969; | ||
| 33 | |||
| 34 | # grab apache session cookie | ||
| 35 | my ($session_id) = | ||
| 36 | ( ( $r->headers_in->{"Cookie"} || "" ) =~ /Apache=([^;]+)/ ); | ||
| 37 | |||
| 38 | my $client = IO::Socket::INET->new( | ||
| 39 | PeerAddr => $ace_initd_server, | ||
| 40 | PeerPort => $ace_initd_port, | ||
| 41 | Proto => 'udp' | ||
| 42 | ); | ||
| 43 | |||
| 44 | my %ACE; | ||
| 45 | my $request; | ||
| 46 | my $message; | ||
| 47 | my $extra_input; | ||
| 48 | |||
| 49 | if ( ( !$username && !$passcode ) | ||
| 50 | || ( $type ne "pin" && !$passcode ) | ||
| 51 | || ( $passcode =~ /\:/ ) ) | ||
| 52 | { | ||
| 53 | $message = qq{ | ||
| 54 | Please enter your username and passcode<br/> | ||
| 55 | Your passcode is your 4 - 8 digit pin plus<br/> | ||
| 56 | 6 digit SecurID code. If you do not have<br/> | ||
| 57 | a PIN yet just enter the 6 digit SecurID code. | ||
| 58 | }; | ||
| 59 | $extra_input = qq{ | ||
| 60 | <label for="passcode">Passcode :</label> | ||
| 61 | <input type="password" name="passcode" id="passcode"/> | ||
| 62 | <input type="hidden" name="type" value="check"/> | ||
| 63 | <input type="hidden" name="a" value="$uri"/> | ||
| 64 | }; | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | if ( $type eq "pin" ) { | ||
| 68 | ( $passcode, $message, $extra_input ) = check_pin( $r, $req ); | ||
| 69 | if ($passcode) { | ||
| 70 | ( $message, $extra_input ) = | ||
| 71 | Do_ACE( $username, $passcode, $type, $session_id, $client, $r, $req ); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | else { | ||
| 75 | ( $message, $extra_input ) = | ||
| 76 | Do_ACE( $username, $passcode, $type, $session_id, $client, $r, $req ); | ||
| 77 | } | ||
| 78 | |||
| 79 | } | ||
| 80 | |||
| 81 | my $head = qq| | ||
| 82 | <body> | ||
| 83 | <head> | ||
| 84 | <title>SecurID Authentication</title> | ||
| 85 | <style> | ||
| 86 | label { | ||
| 87 | font-family: monospace; | ||
| 88 | } | ||
| 89 | p { | ||
| 90 | font-family: sans-serif; | ||
| 91 | margin-left: auto; | ||
| 92 | margin-right: auto; | ||
| 93 | } | ||
| 94 | </style> | ||
| 95 | </head> | ||
| 96 | <html> | ||
| 97 | <!-- $session_id --> | ||
| 98 | <form method="post"> | ||
| 99 | <fieldset style="width: 500px; margin-left: auto; margin-right: auto; background: #cccccc;"> | ||
| 100 | <legend>SecurID Authorization</legend> | ||
| 101 | <p>$message</p> | ||
| 102 | <br/> | ||
| 103 | <label for="username">Username :</label> | ||
| 104 | <input type="text" name="username" value="$username" id="username"/> | ||
| 105 | <br/> | ||
| 106 | $extra_input | ||
| 107 | <br/> | ||
| 108 | <br/> | ||
| 109 | <input type="submit" name="Submit" value="Enter"/> | ||
| 110 | <input type="reset" name="reset"/> | ||
| 111 | <br/> | ||
| 112 | </fieldset> | ||
| 113 | </form> | ||
| 114 | </html> | ||
| 115 | |; | ||
| 116 | |||
| 117 | $r->content_type('text/html'); | ||
| 118 | |||
| 119 | #$r->send_http_header; | ||
| 120 | $r->print($head); | ||
| 121 | |||
| 122 | return Apache2::Const::OK; | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | sub check_pin { | ||
| 127 | my ( $r, $req ) = @_; | ||
| 128 | |||
| 129 | my $pin1 = $req->param('pin1'); | ||
| 130 | my $pin2 = $req->param('pin2'); | ||
| 131 | my $alphanumeric = $req->param('alphanumeric'); | ||
| 132 | my $min_pin_len = $req->param('min_pin_len'); | ||
| 133 | my $max_pin_len = $req->param('max_pin_len'); | ||
| 134 | my $uri = $req->param('a'); | ||
| 135 | my $message; | ||
| 136 | |||
| 137 | my $extra_info = qq{ | ||
| 138 | <span style="font-weight: bold;">User Authenticated</span> | ||
| 139 | <label for="pin1">PIN :</label> | ||
| 140 | <input type="password" name="pin1" id="pin1"/> | ||
| 141 | <input type="hidden" name="type" value="pin"/> | ||
| 142 | <input type="hidden" name="a" value="$uri"/> | ||
| 143 | <input type="hidden" name="alphanumeric" value="$alphanumeric"/> | ||
| 144 | <input type="hidden" name="min_pin_len" value="$min_pin_len"/> | ||
| 145 | <input type="hidden" name="max_pin_len" value="$max_pin_len"/> | ||
| 146 | <label for="pin1">PIN ( Again ) :</label> | ||
| 147 | <input type="password" name="pin2" id="pin2"/> | ||
| 148 | }; | ||
| 149 | |||
| 150 | if ( $pin1 != $pin2 ) { | ||
| 151 | $message = qq{ | ||
| 152 | <span style="font-weight: bold;">New Pin Required</span> | ||
| 153 | <p> | ||
| 154 | Pins do not match!! | ||
| 155 | </p> | ||
| 156 | <p> | ||
| 157 | Please enter a $min_pin_len to $max_pin_len digit pin. | ||
| 158 | </p> | ||
| 159 | }; | ||
| 160 | return ( 0, $message, $extra_info ); | ||
| 161 | } | ||
| 162 | |||
| 163 | if ($alphanumeric) { | ||
| 164 | if ( $pin1 =~ /[^0-9a-zA-Z]/ ) { | ||
| 165 | $message = qq{ | ||
| 166 | <span style="font-weight: bold;">New Pin Required</span> | ||
| 167 | <p> | ||
| 168 | Pin must be alphanumeric!! | ||
| 169 | </p> | ||
| 170 | <p> | ||
| 171 | Please enter a $min_pin_len to $max_pin_len digit pin. | ||
| 172 | </p> | ||
| 173 | }; | ||
| 174 | return ( 0, $message, $extra_info ); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | if ( $pin1 =~ /[^0-9]/ ) { | ||
| 179 | $message = qq{ | ||
| 180 | <span style="font-weight: bold;">New Pin Required</span> | ||
| 181 | <p> | ||
| 182 | Pin must be numeric!! | ||
| 183 | </p> | ||
| 184 | <p> | ||
| 185 | Please enter a $min_pin_len to $max_pin_len digit pin. | ||
| 186 | </p> | ||
| 187 | }; | ||
| 188 | return ( 0, $message, $extra_info ); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | my $pin_length = length($pin1); | ||
| 193 | |||
| 194 | if ( $pin_length < $min_pin_len || $pin_length > $max_pin_len ) { | ||
| 195 | $message = qq{ | ||
| 196 | <span style="font-weight: bold;">New Pin Required</span> | ||
| 197 | <p> | ||
| 198 | Pin must be the correct length!! | ||
| 199 | </p> | ||
| 200 | <p> | ||
| 201 | Please enter a $min_pin_len to $max_pin_len digit pin. | ||
| 202 | </p> | ||
| 203 | }; | ||
| 204 | return ( 0, $message, $extra_info ); | ||
| 205 | } | ||
| 206 | |||
| 207 | return ( $pin1, 0, 0 ); | ||
| 208 | |||
| 209 | } | ||
| 210 | |||
| 211 | sub Do_ACE { | ||
| 212 | |||
| 213 | my ( $username, $passcode, $type, $session_id, $client, $r, $req ) = @_; | ||
| 214 | |||
| 215 | $ENV{'VAR_ACE'} ||= "/opt/ace/data"; | ||
| 216 | my $message; | ||
| 217 | my $extra_input; | ||
| 218 | my $result; | ||
| 219 | my %info; | ||
| 220 | my $ace; | ||
| 221 | my $mesg; | ||
| 222 | my $my_rand = rand(); | ||
| 223 | my $return_rand; | ||
| 224 | |||
| 225 | my $crypt_key = $r->dir_config("AuthCryptKey"); | ||
| 226 | my $crypt = new Crypt::CBC( $crypt_key, "Blowfish" ); | ||
| 227 | |||
| 228 | $mesg = | ||
| 229 | $crypt->encrypt_hex("$my_rand:$session_id:$type:$username:$passcode"); | ||
| 230 | $client->send($mesg); | ||
| 231 | |||
| 232 | $client->recv( $mesg, 1024 ); | ||
| 233 | $mesg = $crypt->decrypt_hex($mesg); | ||
| 234 | |||
| 235 | ( | ||
| 236 | $return_rand, $result, $info{system_pin}, $info{min_pin_len}, | ||
| 237 | $info{max_pin_len}, $info{alphanumeric}, $info{user_selectable} | ||
| 238 | ) = split /\:/, $mesg; | ||
| 239 | |||
| 240 | if ( $my_rand ne $return_rand ) { | ||
| 241 | $result = 100; | ||
| 242 | } | ||
| 243 | ( $message, $extra_input ) = | ||
| 244 | Ace_Result( $result, \%info, $r, $crypt, $req, $username ); | ||
| 245 | |||
| 246 | return ( $message, $extra_input ); | ||
| 247 | |||
| 248 | } | ||
| 249 | |||
| 250 | sub Ace_Result { | ||
| 251 | |||
| 252 | my ( $result, $info, $r, $crypt, $req, $username ) = @_; | ||
| 253 | my $message; | ||
| 254 | my $uri = $req->param('a'); | ||
| 255 | my $extra_input = qq{ | ||
| 256 | <label for="passcode">Passcode :</label> | ||
| 257 | <input type="password" name="passcode" id="passcode"/> | ||
| 258 | <input type="hidden" name="type" value="check"/> | ||
| 259 | <input type="hidden" name="a" value="$uri"/> | ||
| 260 | }; | ||
| 261 | my $time = time(); | ||
| 262 | |||
| 263 | if ( $result == 0 ) { | ||
| 264 | |||
| 265 | my $auth_cookie = $r->dir_config("AuthCookie") || "SecurID"; | ||
| 266 | my $auth_user_cookie = $r->dir_config("AuthUserCookie") | ||
| 267 | || "SecurID_User"; | ||
| 268 | my $crypt_cookie = $crypt->encrypt_hex("$time:$username"); | ||
| 269 | $r->headers_out->add( "Set-Cookie" => $auth_user_cookie . "=" | ||
| 270 | . $username | ||
| 271 | . "; path=" | ||
| 272 | . "/" ); | ||
| 273 | $r->headers_out->add( "Set-Cookie" => $auth_cookie . "=" | ||
| 274 | . $crypt_cookie | ||
| 275 | . "; path=" | ||
| 276 | . "/" ); | ||
| 277 | |||
| 278 | $uri = $crypt->decrypt_hex($uri); | ||
| 279 | |||
| 280 | # success | ||
| 281 | $message = qq{ | ||
| 282 | <span style="font-weight: bold;">User Authenticated</span> | ||
| 283 | <script language="javascript"> | ||
| 284 | window.location="$uri"; | ||
| 285 | </script> | ||
| 286 | <!-- <p> | ||
| 287 | If you do not have Java Script enabled<br/> | ||
| 288 | please click <a href="$uri">here</a> to go to<br/> | ||
| 289 | the protected page. | ||
| 290 | </p> --> | ||
| 291 | <p> | ||
| 292 | Plase enter your username and passcode<br/> | ||
| 293 | Your passcode is your 4 - 8 digit pin plus<br/> | ||
| 294 | 6 digit SecurID code. If you do not have<br/> | ||
| 295 | a PIN yet just enter the 6 digit SecurID code. | ||
| 296 | </p> | ||
| 297 | }; | ||
| 298 | $extra_input = ''; | ||
| 299 | } | ||
| 300 | elsif ( $result == 1 ) { | ||
| 301 | # failure | ||
| 302 | $message = qq{ | ||
| 303 | <span style="font-weight: bold;">User Authenticated Failed</span> | ||
| 304 | <p> | ||
| 305 | Plase enter your username and passcode<br/> | ||
| 306 | Your passcode is your 4 - 8 digit pin plus<br/> | ||
| 307 | 6 digit SecurID code. If you do not have<br/> | ||
| 308 | a PIN yet just enter the 6 digit SecurID code. | ||
| 309 | </p> | ||
| 310 | }; | ||
| 311 | } | ||
| 312 | elsif ( $result == 100 ) { | ||
| 313 | # failure | ||
| 314 | $message = qq{ | ||
| 315 | <span style="font-weight: bold;">User Authenticated Failed</span> | ||
| 316 | <p> | ||
| 317 | The ACE server is either down or behaving<br/> | ||
| 318 | incorrectly. Please conact your system<br/> | ||
| 319 | administrator. | ||
| 320 | </p> | ||
| 321 | }; | ||
| 322 | } | ||
| 323 | elsif ( $result == 2 ) { | ||
| 324 | # next token code | ||
| 325 | $message = qq{ | ||
| 326 | <span style="font-weight: bold;">Next Token Required</span> | ||
| 327 | <p> | ||
| 328 | Plase wait for you token to change and enter<br/> | ||
| 329 | the 6 digit SecurID token code. | ||
| 330 | </p> | ||
| 331 | }; | ||
| 332 | $extra_input = qq{ | ||
| 333 | <label for="passcode">Passcode :</label> | ||
| 334 | <input type="password" name="passcode" id="passcode"/> | ||
| 335 | <input type="hidden" name="type" value="next"/> | ||
| 336 | <input type="hidden" name="a" value="$uri"/> | ||
| 337 | }; | ||
| 338 | } | ||
| 339 | elsif ( $result == 5 ) { | ||
| 340 | # New PIN required. | ||
| 341 | if ( $$info{user_selectable} >= 1 ) { | ||
| 342 | $message = qq| | ||
| 343 | <span style="font-weight: bold;">New Pin Required</span> | ||
| 344 | <p> | ||
| 345 | Please enter a $$info{min_pin_len} to $$info{max_pin_len} digit pin. | ||
| 346 | </p> | ||
| 347 | |; | ||
| 348 | $extra_input = qq| | ||
| 349 | <label for="pin1">PIN :</label> | ||
| 350 | <input type="password" name="pin1" id="pin1"/> | ||
| 351 | <input type="hidden" name="type" value="pin"/> | ||
| 352 | <input type="hidden" name="a" value="$uri"/> | ||
| 353 | <input type="hidden" name="alphanumeric" value="$$info{alphanumeric}"/> | ||
| 354 | <input type="hidden" name="min_pin_len" value="$$info{min_pin_len}"/> | ||
| 355 | <input type="hidden" name="max_pin_len" value="$$info{max_pin_len}"/> | ||
| 356 | <label for="pin1">PIN ( Again ) :</label> | ||
| 357 | <input type="password" name="pin2" id="pin2"/> | ||
| 358 | |; | ||
| 359 | |||
| 360 | } | ||
| 361 | else { | ||
| 362 | $message = qq| | ||
| 363 | <span style="font-weight: bold;">You have been assigned a new PIN.</span> | ||
| 364 | <p> | ||
| 365 | Your PIN is: <$$info{system_pin}</span><p> | ||
| 366 | Please remember your PIN. Do not share it<br/> | ||
| 367 | with anyone else. | ||
| 368 | </p> | ||
| 369 | <p> | ||
| 370 | Please enter your username and passcode<br/> | ||
| 371 | Your passcode is your 4 - 8 digit pin plus<br/> | ||
| 372 | 6 digit SecurID code. | ||
| 373 | </p> | ||
| 374 | |; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | elsif ( $result == 6 ) { | ||
| 378 | $message = qq{ | ||
| 379 | <span style="font-weight: bold;">PIN Accepted</span> | ||
| 380 | <p> | ||
| 381 | Please remember you PIN. Do not share it<br/> | ||
| 382 | with anyone else. | ||
| 383 | </p> | ||
| 384 | <p> | ||
| 385 | Please enter your username and passcode<br/> | ||
| 386 | Your passcode is your 4 - 8 digit pin plus<br/> | ||
| 387 | 6 digit SecurID code. | ||
| 388 | </p> | ||
| 389 | }; | ||
| 390 | } | ||
| 391 | elsif ( $result == 7 ) { | ||
| 392 | $message = qq{ | ||
| 393 | <span style="font-weight: bold;">PIN Rejected</span> | ||
| 394 | <p> | ||
| 395 | If you're having difficulties, please contact<br/> | ||
| 396 | the system administrator.<br/><br/> | ||
| 397 | Please enter your username and passcode<br/> | ||
| 398 | Your passcode is your 4 - 8 digit pin plus<br/> | ||
| 399 | 6 digit SecurID code. | ||
| 400 | </p> | ||
| 401 | }; | ||
| 402 | } | ||
| 403 | return ( $message, $extra_input ); | ||
| 404 | } | ||
| 405 | |||
| 406 | 1; | ||
| 407 | |||
| 408 | __END__ |