blib/lib/Apache/AuthenSecurID/Auth.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 215 CC 18 SCAR 37.3
CC
18
Cov
0%
Ace_Result
90.0
check_pin
42.0
handler
20.0
CRAP 41.7
1 # $Id: Auth.pm,v 1.9 2002/07/31 16:44:14 Administrator Exp $
2  
3 package Apache::AuthenSecurID::Auth;
4  
5 use strict;
6 use Apache;
7 use Apache::Registry;
8 use Apache::Constants qw(:common);
9 use IO::Socket::INET;
10 use Crypt::CBC;
11 use vars qw($VERSION);
12  
13 $VERSION = '0.4';
14  
15  
16 sub handler {
17  
18 my $r = shift;
19  
20 # seed the random number generator
21 srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip -f`);
22  
23 #get params
24 my %params = ( $r->args, $r->content );
25 my $username = $params {'username'};
26 my $passcode = $params{'passcode'};
27 my $type = $params{'type'};
28 my $uri = $params{'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 ) = ( ($r->header_in("Cookie") || "") =~
36 /Apache=([^;]+)/);
37  
38 my $client = IO::Socket::INET->new ( PeerAddr => $ace_initd_server,
39 PeerPort => $ace_initd_port,
40 Proto => 'udp' );
41  
42 my %ACE;
43 my $request;
44 my $message;
45 my $extra_input;
46  
47 if ( ( ! $username && ! $passcode ) || ( $type ne "pin" && ! $passcode ) ||
48 ( $passcode =~ /\:/ ) ) {
49 $message = qq{
50 Please enter your username and passcode<br>
51 Your passcode is your 4 - 8 digit pin plus<br>
52 6 digit SecurID code. If you do not have<br>
53 a PIN yet just enter the 6 digit SecurID code.
54 };
55 $extra_input = qq{
56 <tr>
57 <td>
58 <font color=000000 face="Arial, Helvetica, sans-serif">
59 <b>
60 Passcode :
61 </b>
62 </font>
63 </td>
64 <td>
65 <input type=password name=passcode>
66 <input type=hidden name=type value=check>
67 <input type=hidden name=a value=$uri>
68 </td>
69 </tr>
70 };
71 } else {
72 if ( $type eq "pin" ) {
73 ( $passcode, $message,$extra_input) = check_pin ( \%params ) ;
74 if ( $passcode ) {
75 ($message,$extra_input) = Do_ACE($username,$passcode,$type,$session_id,$client,$r,\%params);
76 }
77 } else {
78 ($message,$extra_input) = Do_ACE($username,$passcode,$type,$session_id,$client,$r,\%params);
79 }
80  
81 }
82  
83  
84 my $head = qq{
85 <body bgcolor=F0F8FF link=FFFF00 alink=FFFF00 vlink=FFFF00>
86 <html>
87 <!-- $session_id -->
88 <center>
89 <form method=post>
90 <table>
91 <tr>
92 <td colspan=2>
93 <table bgcolor=cccccc >
94 <tr>
95 <td>
96 <table cellspacing=2 bgcolor=dddddd width=100%>
97 <tr>
98 <td align=center>
99 <font size=+2 color=000000 face="Arial, Helvetica, sans-serif">
100 <b>
101 SecurID Authorization
102 </b>
103 </font>
104 </td>
105 </tr>
106 <tr>
107 <td>
108 <font color=000000 face="Arial, Helvetica, sans-serif">
109 $message
110 </font>
111 </td>
112 </tr>
113 </table>
114 </td>
115 </tr>
116 <tr>
117 <td align=right>
118 <table cellspacing=0 cellpadding=0 bgcolor=dddddd width=100%>
119 <tr>
120 <td>
121 <font color=000000 face="Arial, Helvetica, sans-serif">
122 <b>
123 Username :
124 </b>
125 </font>
126 </td>
127 <td>
128 <input type=text name=username value=$username>
129 </td>
130 </tr>
131 $extra_input
132 </table>
133 </td>
134 </tr>
135 </table>
136 </td>
137 </tr>
138 <tr>
139 <td>
140 <input type=submit name=Submit value="Enter">
141 </td>
142 <td>
143 <input type=reset name=reset>
144 </td>
145 </tr>
146 </table>
147 </form>
148 </center>
149 </html>
150 };
151  
152 $r->content_type ('text/html');
153 $r->send_http_header;
154 $r->print ($head);
155  
156 }
157  
158 sub check_pin {
159  
160 my ( $params ) = @_;
161  
162 my $pin1 = $$params{'pin1'};
163 my $pin2 = $$params{'pin2'};
164 my $alphanumeric = $$params{'alphanumeric'};
165 my $min_pin_len = $$params{'min_pin_len'};
166 my $max_pin_len = $$params{'max_pin_len'};
167 my $uri = $$params{'a'};
168 my $message;
169  
170 my $extra_info = qq{
171 <tr>
172 <td>
173 <font color=000000 face="Arial, Helvetica, sans-serif">
174 <b>
175 PIN :
176 </b>
177 </font>
178 </td>
179 <td>
180 <input type=password name=pin1>
181 <input type=hidden name=type value=pin>
182 <input type=hidden name=alphanumeric value=$alphanumeric>
183 <input type=hidden name=min_pin_len value=$min_pin_len>
184 <input type=hidden name=max_pin_len value=$max_pin_len>
185 <input type=hidden name=a value=$uri>
186 </td>
187 </tr>
188 <tr>
189 <td>
190 <font color=000000 face="Arial, Helvetica, sans-serif">
191 <b>
192 PIN ( Again ) :
193 </b>
194 </font>
195 </td>
196 <td>
197 <input type=password name=pin2>
198 </td>
199 </tr>
200 };
201  
202 if ( $pin1 != $pin2 ) {
203 $message = qq{
204 <b>New Pin Required</b><p>
205 Pins do not match!!<p>
206 Please enter a $min_pin_len to $max_pin_len digit pin.
207 };
208 return ( 0, $message, $extra_info );
209 }
210  
211 if ( $alphanumeric ) {
212 if ( $pin1 =~ /[^0-9a-zA-Z]/ ) {
213 $message = qq{
214 <b>New Pin Required</b><p>
215 Pin must be alphanumeric!!<p>
216 Please enter a $min_pin_len to $max_pin_len digit pin.
217 };
218 return ( 0, $message, $extra_info );
219 }
220 } else {
221 if ( $pin1 =~ /[^0-9]/ ) {
222 $message = qq{
223 <b>New Pin Required</b><p>
224 Pin must be numeric!!<p>
225 Please enter a $min_pin_len to $max_pin_len digit pin.
226 };
227 return ( 0, $message, $extra_info );
228 }
229 }
230  
231 my $pin_length = length ( $pin1 );
232  
233 if ( $pin_length < $min_pin_len || $pin_length > $max_pin_len ) {
234 $message = qq{
235 <b>New Pin Required</b><p>
236 Pin must be the correct length!!<p>
237 Please enter a $min_pin_len to $max_pin_len digit pin.
238 };
239 return ( 0, $message, $extra_info );
240 }
241  
242 return ( $pin1, 0, 0 );
243
244 }
245  
246 sub Do_ACE {
247  
248 my ( $username,$passcode,$type,$session_id,$client,$r,$params ) = @_;
249  
250 $ENV{'VAR_ACE'} = "/opt/ace/data";
251 my $message;
252 my $extra_input;
253 my $result;
254 my %info;
255 my $ace;
256 my $mesg;
257 my $my_rand = rand();
258 my $return_rand;
259  
260 my $crypt_key = $r->dir_config("AuthCryptKey");
261 my $crypt = new Crypt::CBC ( $crypt_key, "Blowfish" );
262
263 $mesg = $crypt->encrypt_hex ("$my_rand:$session_id:$type:$username:$passcode");
264 $client->send($mesg);
265  
266 $client->recv($mesg, 1024);
267 $mesg = $crypt->decrypt_hex ( $mesg );
268  
269  
270 ( $return_rand, $result, $info{system_pin}, $info{min_pin_len}, $info{max_pin_len},
271 $info{alphanumeric}, $info{user_selectable} )
272 = split /\:/, $mesg;
273  
274 if ( $my_rand ne $return_rand ) {
275 $result = 100;
276 }
277 ($message,$extra_input)=Ace_Result($result,\%info,$r,$crypt,$params,$username);
278  
279 return ( $message, $extra_input );
280  
281 }
282  
283 sub Ace_Result {
284  
285 my ( $result, $info, $r, $crypt, $params,$username ) = @_;
286 my $message;
287 my $extra_input;
288 my $uri = $$params{'a'};
289 my $time = time ();
290  
291 if ( $result == 0 ) {
292  
293 my $auth_cookie = $r->dir_config("AuthCookie") || "SecurID";
294 my $auth_user_cookie = $r->dir_config("AuthUserCookie") || "SecurID_User";
295 my $crypt_cookie = $crypt->encrypt_hex ( "$time:$username" );
296 $r->headers_out->add("Set-Cookie" => $auth_user_cookie . "=" .
297 $username . "; path=" . "/");
298 $r->headers_out->add("Set-Cookie" => $auth_cookie . "=" .
299 $crypt_cookie . "; path=" . "/");
300  
301 $uri = $crypt->decrypt_hex ( $uri );
302  
303 # success
304 $message = qq{
305 <b>User Authenticated</b><p>
306 <SCRIPT LANGUAGE="JavaScript">
307 <!-- Begin
308 window.location="$uri";
309 // End -->
310 </script>
311 If you do not have Java Script enabled<br>
312 please click <a href="$uri">here</a> to go to<br>
313 the protected page.<p>
314 Plase enter your username and passcode<br>
315 Your passcode is your 4 - 8 digit pin plus<br>
316 6 digit SecurID code. If you do not have<br>
317 a PIN yet just enter the 6 digit SecurID code.
318 };
319 $extra_input = qq{
320 <tr>
321 <td>
322 <font color=000000 face="Arial, Helvetica, sans-serif">
323 <b>
324 Passcode :
325 </b>
326 </font>
327 </td>
328 <td>
329 <input type=password name=passcode>
330 <input type=hidden name=type value=check>
331 <input type=hidden name=a value=$uri>
332 </td>
333 </tr>
334 };
335 } elsif ( $result == 1 ) {
336 # failure
337 $message = qq{
338 <b>User Authentication Failed</b><p>
339 Plase enter your username and passcode<br>
340 Your passcode is your 4 - 8 digit pin plus<br>
341 6 digit SecurID code. If you do not have<br>
342 a PIN yet just enter the 6 digit SecurID code.
343 };
344 $extra_input = qq{
345 <tr>
346 <td>
347 <font color=000000 face="Arial, Helvetica, sans-serif">
348 <b>
349 Passcode :
350 </b>
351 </font>
352 </td>
353 <td>
354 <input type=password name=passcode>
355 <input type=hidden name=type value=check>
356 <input type=hidden name=a value=$uri>
357 </td>
358 </tr>
359 };
360 } elsif ( $result == 100 ) {
361 # failure
362 $message = qq{
363 <b>User Authentication Failed</b><p>
364 The ACE server is either down or behaving<br>
365 incorrectly. Please conact your system<br>
366 administrator.
367 };
368 $extra_input = qq{
369 <tr>
370 <td>
371 <font color=000000 face="Arial, Helvetica, sans-serif">
372 <b>
373 Passcode :
374 </b>
375 </font>
376 </td>
377 <td>
378 <input type=password name=passcode>
379 <input type=hidden name=type value=check>
380 <input type=hidden name=a value=$uri>
381 </td>
382 </tr>
383 };
384 } elsif ( $result == 2 ) {
385 # next token code
386 $message = qq{
387 <b>Next Token Required</b><p>
388 Plase wait for you token to change and enter<br>
389 the 6 digit SecurID token code.
390 };
391 $extra_input = qq{
392 <tr>
393 <td>
394 <font color=000000 face="Arial, Helvetica, sans-serif">
395 <b>
396 Next Token Code :
397 </b>
398 </font>
399 </td>
400 <td>
401 <input type=password name=passcode>
402 <input type=hidden name=type value=next>
403 <input type=hidden name=a value=$uri>
404 </td>
405 </tr>
406 };
407 } elsif ( $result == 5 ) {
408 # New PIN required.
409 if ( $$info{user_selectable} >= 1 ) {
410 $message = qq{
411 <b>New Pin Required</b><p>
412 Please enter a $$info{min_pin_len} to $$info{max_pin_len} digit pin.
413 };
414 $extra_input = qq{
415 <tr>
416 <td>
417 <font color=000000 face="Arial, Helvetica, sans-serif">
418 <b>
419 PIN :
420 </b>
421 </font>
422 </td>
423 <td>
424 <input type=password name=pin1>
425 <input type=hidden name=type value=pin>
426 <input type=hidden name=a value=$uri>
427 <input type=hidden name=alphanumeric value=$$info{alphanumeric}>
428 <input type=hidden name=min_pin_len value=$$info{min_pin_len}>
429 <input type=hidden name=max_pin_len value=$$info{max_pin_len}>
430 </td>
431 </tr>
432 <tr>
433 <td>
434 <font color=000000 face="Arial, Helvetica, sans-serif">
435 <b>
436 PIN ( Again ) :
437 </b>
438 </font>
439 </td>
440 <td>
441 <input type=password name=pin2>
442 </td>
443 </tr>
444 };
445
446 } else {
447 $message = qq{
448 <b>You have been assignes a new PIN.</b><p>
449 Your PIN is: <b>$$info{system_pin}</b><p>
450 Please remember your PIN. Do not share it<br>
451 with anyone else.<p>
452 Please enter your username and passcode<br>
453 Your passcode is your 4 - 8 digit pin plus<br>
454 6 digit SecurID code.
455 };
456 $extra_input = qq{
457 <tr>
458 <td>
459 <font color=000000 face="Arial, Helvetica, sans-serif">
460 <b>
461 Passcode :
462 </b>
463 </font>
464 </td>
465 <td>
466 <input type=password name=passcode>
467 <input type=hidden name=type value=check>
468 <input type=hidden name=a value=$uri>
469 </td>
470 </tr>
471 };
472 }
473 } elsif ( $result == 6 ) {
474 $message = qq{
475 <b>PIN Accepted</b><p>
476 Please remember you PIN. Do not share it<br>
477 with anyone else.<p>
478 Please enter your username and passcode<br>
479 Your passcode is your 4 - 8 digit pin plus<br>
480 6 digit SecurID code.
481 };
482 $extra_input = qq{
483 <tr>
484 <td>
485 <font color=000000 face="Arial, Helvetica, sans-serif">
486 <b>
487 Passcode :
488 </b>
489 </font>
490 </td>
491 <td>
492 <input type=password name=passcode>
493 <input type=hidden name=type value=check>
494 <input type=hidden name=a value=$uri>
495 </td>
496 </tr>
497 };
498  
499 } elsif ( $result == 7 ) {
500 $message = qq{
501 <b>PIN Rejected</b><p>
502 Please contact the system administrator.<p>
503 Please enter your username and passcode<br>
504 Your passcode is your 4 - 8 digit pin plus<br>
505 6 digit SecurID code.
506 };
507 $extra_input = qq{
508 <tr>
509 <td>
510 <font color=000000 face="Arial, Helvetica, sans-serif">
511 <b>
512 Passcode :
513 </b>
514 </font>
515 </td>
516 <td>
517 <input type=password name=passcode>
518 <input type=hidden name=type value=check>
519 <input type=hidden name=a value=$uri>
520 </td>
521 </tr>
522 };
523  
524 }
525 return ( $message, $extra_input );
526 }
527  
528  
529 1;
530  
531 __END__