blib/lib/Apache2/AuthenNTLM.pm untested

filter: Statementstmt 0.0% 0 / 355 Branchbran 0.0% 0 / 220 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 712 CC 111 SCAR 63.8
CC
111
Cov
0%
handler
1806.0
get_config
182.0
get_msg3
156.0
CRAP 587.9
1 ###################################################################################
2 #
3 # Apache2::AuthenNTLM - Copyright (c) 2002 Gerald Richter / ECOS
4 #
5 # You may distribute under the terms of either the GNU General Public
6 # License or the Artistic License, as specified in the Perl README file.
7 #
8 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #
12 # $Id: AuthenNTLM.pm,v 1.28 2002/11/12 05:31:50 richter Exp $
13 #
14 ###################################################################################
15  
16  
17 package Apache2::AuthenNTLM ;
18  
19 use strict ;
20 use vars qw{$cache $VERSION %msgflags1 %msgflags2 %msgflags3 %invflags1 %invflags2 %invflags3 $addr $port $debug} ;
21  
22 $VERSION = 0.02 ;
23  
24 $debug = 0 ;
25  
26 $cache = undef ;
27  
28 use MIME::Base64 () ;
29 use Authen::Smb 0.95 ;
30 use Socket ;
31  
32 %msgflags1 = ( 0x01 => "NEGOTIATE_UNICODE",
33 0x02 => "NEGOTIATE_OEM",
34 0x04 => "REQUEST_TARGET",
35 0x10 => "NEGOTIATE_SIGN",
36 0x20 => "NEGOTIATE_SEAL",
37 0x80 => "NEGOTAITE_LM_KEY",
38 );
39  
40 %msgflags2 = ( 0x02 => "NEGOTIATE_NTLM",
41 0x40 => "NEGOTIATE_LOCAL_CALL",
42 0x80 => "NEGOTIATE_ALWAYS_SIGN",
43 );
44  
45 %msgflags3 = ( 0x01 => "TARGET_TYPE_DOMAIN",
46 0x02 => "TARGET_TYPE_SERVER",
47 );
48  
49 %invflags1 = ( "NEGOTIATE_UNICODE" => 0x01,
50 "NEGOTIATE_OEM" => 0x02,
51 "REQUEST_TARGET" => 0x04,
52 "NEGOTIATE_SIGN" => 0x10,
53 "NEGOTIATE_SEAL" => 0x20,
54 "NEGOTIATE_LM_KEY" => 0x80,
55 );
56  
57 %invflags2 = ( "NEGOTIATE_NTLM" => 0x02,
58 "NEGOTIATE_LOCAL_CALL" => 0x40,
59 "NEGOTIATE_ALWAYS_SIGN" => 0x80,
60 );
61  
62 %invflags3 = ( "TARGET_TYPE_DOMAIN" => 0x01,
63 "TARGET_TYPE_SERVER" => 0x02,
64 );
65
66 ############################################
67 # here is where we start the new code....
68 ############################################
69 use mod_perl2 ;
70  
71 use Apache2::Access ;
72 use Apache2::Connection ;
73 use Apache2::Log ;
74 use Apache2::RequestRec ;
75 use Apache2::RequestUtil ;
76 use Apache2::RequestIO ;
77 use APR::Table ;
78 use APR::SockAddr ;
79 use Apache2::Const -compile => qw(HTTP_UNAUTHORIZED HTTP_INTERNAL_SERVER_ERROR DECLINED HTTP_FORBIDDEN OK) ;
80  
81 ##################### end modperl code ######################
82  
83 sub get_config
84 {
85 my ($self, $r) = @_ ;
86  
87 return if ($self -> {smbpdc}) ; # config already setup
88  
89 $debug = $r -> dir_config ('ntlmdebug') || 0 ;
90 $debug = $self -> {debug} = lc($debug) eq 'on'?1:($debug+0) ;
91  
92 my @config = $r -> dir_config -> get ('ntdomain') ;
93  
94 foreach (@config)
95 {
96 my ($domain, $pdc, $bdc) = split /\s+/ ;
97 $domain = lc ($domain) ;
98 $self -> {smbpdc}{$domain} = $pdc ;
99 $self -> {smbbdc}{$domain} = $bdc ;
100 print STDERR "[$$] AuthenNTLM: Config Domain = $domain pdc = $pdc bdc = $bdc\n" if ($debug) ;
101 }
102  
103 $self -> {defaultdomain} = $r -> dir_config ('defaultdomain') || '' ;
104 $self -> {fallbackdomain} = $r -> dir_config ('fallbackdomain') || '' ;
105 $self -> {cacheuser} = $r -> dir_config ('cacheuser') || '0' ;
106 $self -> {authtype} = $r -> auth_type || 'ntlm,basic' ;
107 $self -> {authname} = $r -> auth_name || '' ;
108 my $autho = $r -> dir_config ('ntlmauthoritative') || 'on' ;
109 $self -> {ntlmauthoritative} = lc($autho) eq 'on' || $autho == 1?1:0 ;
110 $autho = $r -> dir_config ('basicauthoritative') || 'on' ;
111 $self -> {basicauthoritative} = lc($autho) eq 'on' || $autho == 1?1:0 ;
112
113 $self -> {authntlm} = 0 ;
114 $self -> {authbasic} = 0 ;
115  
116 $self -> {authntlm} = 1 if ($self -> {authtype} =~ /(^|,)ntlm($|,)/i) ;
117 $self -> {authbasic} = 1 if ($self -> {authtype} =~ /(^|,)basic($|,)/i) ;
118  
119 $self -> {semkey} = $r -> dir_config ('ntlmsemkey') ;
120 $self -> {semkey} = 23754 if (!defined ($self -> {semkey})) ;
121 $self -> {semtimeout} = $r -> dir_config ('ntlmsemtimeout') ;
122 $self -> {semtimeout} = 2 if (!defined ($self -> {semtimeout})) ;
123 $self -> {splitdomainprefix} = $r -> dir_config ('splitdomainprefix') || '' ;
124  
125 if ($debug)
126 {
127 print STDERR "[$$] AuthenNTLM: Config Default Domain = $self->{defaultdomain}\n" ;
128 print STDERR "[$$] AuthenNTLM: Config Fallback Domain = $self->{fallbackdomain}\n" ;
129 print STDERR "[$$] AuthenNTLM: Config AuthType = $self->{authtype} AuthName = $self->{authname}\n" ;
130 print STDERR "[$$] AuthenNTLM: Config Auth NTLM = $self->{authntlm} Auth Basic = $self->{authbasic}\n" ;
131 print STDERR "[$$] AuthenNTLM: Config NTLMAuthoritative = " . ($self -> {ntlmauthoritative}?'on':'off') . " BasicAuthoritative = " . ($self -> {basicauthoritative}?'on':'off') . "\n" ;
132 print STDERR "[$$] AuthenNTLM: Config Semaphore key = $self->{semkey} timeout = $self->{semtimeout}\n" ;
133 print STDERR "[$$] AuthenNTLM: Config SplitDomainPrefix = $self->{splitdomainprefix}\n" ;
134 }
135 }
136  
137  
138 sub get_nonce
139 {
140 my ($self, $r) = @_ ;
141  
142 # reuse connection if possible
143 if ($self -> {nonce} && $self -> {ok})
144 {
145 print STDERR "[$$] AuthenNTLM: Returning cached nonce\n" if ($debug) ;
146 return $self -> {nonce}
147 }
148  
149 # this is not the real nonce!
150 # we just need to preallocate some space (8 bytes) where Authen::Smb::Valid_User_Connect
151 # puts the real nonce
152 my $nonce = '12345678' ;
153 $self -> {domain} = $self -> {fallbackdomain}
154 if ($self -> {fallbackdomain} && !($self -> {smbpdc}{lc ($self -> {domain})}));
155 my $domain = lc ($self -> {domain}) ;
156 my $pdc = $self -> {smbpdc}{$domain} ;
157 my $bdc = $self -> {smbbdc}{$domain} ;
158  
159 if (!$pdc)
160 {
161 $r->log_error("No PDC and no fallbackdomain given for domain $domain") ;
162 return '' ;
163 }
164  
165 $self -> {nonce} = undef ;
166  
167 print STDERR "[$$] AuthenNTLM: Connect to pdc = $pdc bdc = $bdc domain = $domain\n" if ($debug) ;
168  
169 # smb aborts any connection that where no user is logged on as soon as somebody
170 # tries to open another one. So we have to make sure two request, do not start
171 # two auth cycles at the same time. To avoid a hang of the whole server we wrap it with
172 # a small timeout
173 if ($self->{semkey})
174 {
175 eval
176 {
177 local $SIG{ALRM} = sub { print STDERR "[$$] AuthenNTLM: timed out"
178 . "while waiting for lock (key = $self->{semkey})\n"; die ; };
179  
180 alarm $self -> {semtimeout} ;
181 $self -> {lock} = Apache2::AuthenNTLM::Lock -> lock ($self->{semkey}, $debug) ;
182 alarm 0;
183 };
184 }
185  
186 $self -> {smbhandle} = Authen::Smb::Valid_User_Connect ($pdc, $bdc, $domain, $nonce) ;
187
188 print STDERR "[$$] AuthenNTLM: verify handle $self->{username} smbhandle == $self->{smbhandle} \n" if ($debug) ;
189
190 if (!$self -> {smbhandle})
191 {
192 $r->log_error("Connect to SMB Server failed (pdc = $pdc bdc = $bdc domain = $domain error = "
193 . Authen::Smb::SMBlib_errno . '/' . Authen::Smb::SMBlib_SMB_Error . ") for " .
194 $r -> uri) ;
195 return undef ;
196 }
197  
198 return $self -> {nonce} = $nonce ;
199 }
200  
201  
202  
203 sub verify_user
204 {
205 my ($self, $r) = @_ ;
206  
207 if (!$self -> {smbhandle})
208 {
209 $self -> {lock} = undef ; # reset lock in case anything has gone wrong
210 $r->log_error("SMB Server connection not open in state 3 for " . $r -> uri) ;
211 return ;
212 }
213  
214 my $rc ;
215  
216 print STDERR "[$$] AuthenNTLM: Verify user $self->{username} via smb server\n" if ($debug) ;
217  
218 if ($self -> {basic})
219 {
220 $rc = Authen::Smb::Valid_User_Auth ($self -> {smbhandle}, $self->{username}, $self -> {password}) ;
221 }
222 else
223 {
224 $rc = Authen::Smb::Valid_User_Auth ($self -> {smbhandle}, $self->{username}, $self -> {usernthash}, 1, $self->{userdomain}) ;
225 }
226 my $errno = Authen::Smb::SMBlib_errno ;
227 my $smberr = Authen::Smb::SMBlib_SMB_Error ;
228 Authen::Smb::Valid_User_Disconnect ($self -> {smbhandle}) if ($self -> {smbhandle}) ;
229 $self -> {smbhandle} = undef ;
230 $self -> {lock} = undef ;
231  
232 if ($rc == &Authen::Smb::NTV_LOGON_ERROR)
233 {
234 $r->log_error("Wrong password/user (rc=$rc/$errno/$smberr): $self->{userdomain}\\$self->{username} for " . $r -> uri) ;
235 print STDERR "[$$] AuthenNTLM: rc = $rc ntlmhash = $self->{usernthash}\n" if ($debug) ;
236 return ;
237 }
238  
239 if ($rc)
240 {
241 $r->log_reason("SMB Server error $rc/$errno/$smberr for " . $r -> uri) ;
242 return ;
243 }
244  
245 return 1 ;
246 }
247  
248  
249 sub map_user
250 {
251 my ($self, $r) = @_ ;
252  
253 if ($self -> {splitdomainprefix} == 1)
254 {
255 return lc("$self->{username}") ;
256 }
257 else
258 {
259 return lc("$self->{userdomain}\\$self->{username}") ;
260 }
261 }
262  
263  
264  
265 sub substr_unicode
266 {
267 my ($data, $off, $len) = @_ ;
268
269 my $i = 0 ;
270 my $end = $off + $len ;
271 my $result = '' ;
272 for ($i = $off ; $i < $end ; $i += 2)
273 {# for now we simply ignore high order byte
274 $result .= substr ($data, $i, 1) ;
275 }
276  
277 return $result ;
278 }
279  
280  
281 sub get_msg_data
282 {
283 my ($self, $r) = @_ ;
284  
285 my $auth_line = $r->headers_in->{$r->proxyreq ? 'Proxy-Authorization' : 'Authorization'} ;
286
287 $self -> {ntlm} = 0 ;
288 $self -> {basic} = 0 ;
289 if ($debug)
290 {
291 $auth_line =~ /^(.*?)\s+/ ;
292 my $type = $1 ;
293 print STDERR "[$$] AuthenNTLM: Authorization Header "
294 . (defined($auth_line)?($debug > 1?$auth_line:$type):'<not given>') . "\n" if ($debug) ;
295 }
296 if ($self -> {authntlm} && ($auth_line =~ /^NTLM\s+(.*?)$/i))
297 {
298 $self -> {ntlm} = 1 ;
299 }
300 elsif ($self -> {authbasic} && ($auth_line =~ /^Basic\s+(.*?)$/i))
301 {
302 $self -> {basic} = 1 ;
303 }
304 else
305 {
306 return undef ;
307 }
308  
309 my $data = MIME::Base64::decode($1) ;
310  
311 if ($debug > 1)
312 {
313 my @out ;
314 for (my $i = 0; $i < length($data); $i++)
315 {
316 push @out, unpack('C', substr($data, $i, 1)) ;
317 }
318 print STDERR "[$$] AuthenNTLM: Got: " . join (' ', @out). "\n" ;
319 }
320  
321 return $data ;
322 }
323  
324  
325  
326 sub get_msg
327 {
328 my ($self, $r) = @_ ;
329  
330 my $data = $self -> get_msg_data ($r) ;
331 return undef if (!$data) ;
332  
333 if ($self -> {ntlm})
334 {
335 my ($protocol, $type) = unpack ('Z8C', $data) ;
336 return $self -> get_msg1 ($r, $data) if ($type == 1) ;
337 return $self -> get_msg3 ($r, $data) if ($type == 3) ;
338 return $type ;
339 }
340 elsif ($self -> {basic})
341 {
342 return $self -> get_basic ($r, $data) ;
343 }
344 return undef ;
345 }
346  
347  
348  
349 sub get_msg1
350 {
351 my ($self, $r, $data) = @_ ;
352
353 my ($protocol, $type, $zero, $flags1, $flags2, $zero2, $dom_len, $x1, $dom_off, $x2, $host_len, $x3, $host_off, $x4) = unpack ('Z8Ca3CCa2vvvvvvvv', $data) ;
354 my $host = $host_off?substr ($data, $host_off, $host_len):'' ;
355 my $domain = $dom_off?substr ($data, $dom_off, $dom_len):'' ;
356  
357 $self -> {domain} = $dom_len?$domain:$self -> {defaultdomain} ;
358 $self -> {host} = $host_len?$host:'' ;
359  
360 $self -> {accept_unicode} = $flags1 & 0x01;
361  
362 if ($debug)
363 {
364 my @flag1str;
365 foreach my $i ( sort keys %msgflags1 )
366 {
367 push @flag1str, $msgflags1{ $i } if $flags1 & $i;
368 }
369 my $flag1str = join( ",", @flag1str );
370  
371 my @flag2str;
372 foreach my $i ( sort keys %msgflags2 )
373 {
374 push @flag2str, $msgflags2{ $i } if $flags2 & $i;
375 }
376 my $flag2str = join( ",", @flag2str );
377
378 print STDERR "[$$] AuthenNTLM: protocol=$protocol, type=$type, flags1=$flags1($flag1str), "
379 . "flags2=$flags2($flag2str), domain length=$dom_len, domain offset=$dom_off, "
380 . "host length=$host_len, host offset=$host_off, host=$host, domain=$domain\n" ;
381 }
382  
383  
384 return $type ;
385 }
386  
387  
388 sub set_msg2
389 {
390 my ($self, $r, $nonce) = @_ ;
391  
392 my $charencoding = $self->{ accept_unicode } ? $invflags1{ NEGOTIATE_UNICODE } : $invflags1{ NEGOTIATE_OEM };
393  
394 my $flags2 = $invflags2{ NEGOTIATE_ALWAYS_SIGN } | $invflags2{ NEGOTIATE_NTLM };
395  
396 my $data = pack ('Z8Ca7vvCCa2a8a8', 'NTLMSSP', 2, '', 40, 0, $charencoding, $flags2, '', $nonce, '') ;
397  
398 my $header = 'NTLM '. MIME::Base64::encode($data, '') ;
399  
400 if ($debug)
401 {
402 if ($debug > 1)
403 {
404 my @out ;
405 for (my $i = 0; $i < length($data); $i++)
406 {
407 push @out, unpack('C', substr($data, $i, 1)) ;
408 }
409 print STDERR "[$$] AuthenNTLM: Send: " . join (' ', @out). "\n" ;
410 }
411 print STDERR "[$$] AuthenNTLM: charencoding = $charencoding\n";
412 print STDERR "[$$] AuthenNTLM: flags2 = $flags2\n";
413 print STDERR "[$$] AuthenNTLM: nonce=$nonce\n" if ($debug > 1);
414 print STDERR "[$$] AuthenNTLM: Send header: " . ($debug > 1?$header:'NTLM ...') . "\n" ;
415 }
416  
417 return $header;
418 }
419  
420  
421 sub get_msg3
422 {
423 my ($self, $r, $data) = @_ ;
424  
425 my ($protocol, $type, $zero,
426 $lm_len, $l1, $lm_off,
427 $nt_len, $l3, $nt_off,
428 $dom_len, $x1, $dom_off,
429 $user_len, $x3, $user_off,
430 $host_len, $x5, $host_off,
431 $msg_len
432 ) = unpack ('Z8Ca3vvVvvVvvVvvVvvVv', $data) ;
433
434 my $lm = $lm_off ? substr ($data, $lm_off, $lm_len):'' ;
435 my $nt = $nt_off ? substr ($data, $nt_off, $nt_len):'' ;
436 my $user = $user_off ? ($self->{accept_unicode} ? substr_unicode ($data, $user_off, $user_len) : substr( $data, $user_off, $user_len ) ) :'' ;
437 my $host = $host_off ? ($self->{accept_unicode} ? substr_unicode ($data, $host_off, $host_len) : substr( $data, $host_off, $host_len ) ) :'' ;
438 my $domain = $dom_off ? ($self->{accept_unicode} ? substr_unicode ($data, $dom_off, $dom_len) : substr( $data, $dom_off, $dom_len ) ) :'' ;
439  
440 $self -> {userdomain} = $dom_len?$domain:$self -> {defaultdomain} ;
441 $self -> {username} = $user ;
442 $self -> {usernthash} = $nt_len ? $nt : $lm;
443  
444 if ($debug)
445 {
446 print STDERR "[$$] AuthenNTLM: protocol=$protocol, type=$type, user=$user, "
447 . "host=$host, domain=$domain, msg_len=$msg_len\n" ;
448 }
449  
450  
451 return $type ;
452 }
453  
454  
455 sub get_basic
456 {
457 my ($self, $r, $data) = @_ ;
458  
459 ($self -> {username}, $self -> {password}) = split (/:/, $data) ;
460  
461 my ($domain, $username) = split (/\\|\//, $self -> {username}) ;
462 if ($username)
463 {
464 $self -> {domain} = $domain ;
465 $self -> {username} = $username ;
466 }
467 else
468 {
469 $self -> {domain} = $self -> {defaultdomain} ;
470 }
471  
472 $self -> {userdomain} = $self -> {domain} ;
473  
474 if ($debug)
475 {
476 print STDERR "[$$] AuthenNTLM: basic auth username = $self->{domain}\\$self->{username}\n" ;
477 }
478  
479 return -1 ;
480 }
481  
482  
483 sub DESTROY
484 {
485 my ($self) = @_ ;
486  
487 Authen::Smb::Valid_User_Disconnect ($self -> {smbhandle}) if ($self -> {smbhandle}) ;
488 }
489  
490  
491 sub handler : method
492 {
493 my ($class, $r) = @_ ;
494 my $type ;
495 my $nonce = '' ;
496 my $self ;
497 my $conn = $r -> connection ;
498 my $connhdr = $r -> headers_in -> {'Connection'} ;
499  
500 my $fh = select (STDERR) ;
501 $| = 1 ;
502 select ($fh) ;
503  
504 my $addr = $conn -> remote_addr -> ip_get ;
505 my $port = $conn -> remote_addr -> port ;
506
507 print STDERR "[$$] AuthenNTLM: Start NTLM Authen handler pid = $$, connection = "
508 . "$$conn conn_http_hdr = $connhdr main = " . ($r -> main)
509 . " cuser = " . $r -> user . ' remote_ip = ' . $conn -> remote_ip
510 . " remote_port = " . unpack('n', $port) . ' remote_host = <'
511 . $conn -> remote_host . "> version = $VERSION "
512 . "smbhandle = " . $self -> {smbhandle} . "\n" if ($debug) ;
513  
514 # we cannot attach our object to the connection record. Since in
515 # Apache 1.3 there is only one connection at a time per process
516 # we can cache our object and check if the connection has changed.
517 # The check is done by slightly changing the remote_host member, which
518 # persists as long as the connection does
519 # This has to be reworked to work with Apache 2.0
520 my $table;
521 $table = $conn->notes();
522 if (ref ($cache) ne $class || $$conn != $cache->{connectionid} || ($table->get('status') ne "AUTHSTARTED"))
523 {
524 $table->add('status','AUTHSTARTED');
525 $conn->notes($table);
526 $self = {connectionid => $$conn } ;
527 bless $self, $class ;
528 $cache = $self ;
529 print STDERR "[$$] AuthenNTLM: Setup new object\n" if ($debug) ;
530 }
531 else
532 {
533 $self = $cache ;
534 print STDERR "[$$] AuthenNTLM: Object exists user = $self->{userdomain}\\$self->{username}\n" if ($debug) ;
535
536 if ($self -> {ok})
537 {
538 $r -> user($self->{mappedusername}) ;
539
540 # we accept the user because we are on the same connection
541 $type = $self -> get_msg ($r);
542 my $content_len = $r->headers_in->{'content-length'} ;
543 my $method = $r -> method ;
544 print STDERR "[$$] AuthenNTLM: Same connection pid = $$, connection = $$conn cuser = " .
545 $r -> user . ' ip = ' . $conn -> remote_ip . ' method = ' .
546 $method . ' Content-Length = ' .
547 $content_len . ' type = ' . $type . "\n" if ($debug) ;
548
549 # IE (5.5, 6.0, probably others) can send a type 1 message
550 # after authenticating on the same connection. This is a
551 # problem for POST messages, because IE also sends a
552 # "Content-length: 0" with no POST data.
553 if ($method eq 'GET' || $method eq 'HEAD' || $method eq 'OPTION' || $method eq 'DELETE' ||
554 $content_len > 0 || $type == 3)
555 {
556 print STDERR "[$$] AuthenNTLM: OK because same connection\n" if ($debug) ;
557 return Apache2::Const::OK ;
558 }
559 }
560 }
561 # end of if statement
562  
563 $self -> get_config ($r) ;
564 $type = $self -> get_msg ($r) if (!$type) ;
565
566 if (!$type)
567 {
568 $self -> {lock} = undef ; # reset lock in case anything has gone wrong
569 if (!$self->{ntlmauthoritative})
570 { # see if we have any header
571 my $auth_line = $r -> headers_in->{$r->proxyreq ? 'Proxy-Authorization' : 'Authorization'} ;
572 if ($auth_line)
573 {
574 $r->log_error('Bad/Missing NTLM Authorization Header for ' . $r->uri
575 . '; DECLINEing because we are not authoritative' ) ;
576 return Apache2::Const::DECLINED ;
577 }
578 }
579  
580 $r->log_error('Bad/Missing NTLM/Basic Authorization Header for ' . $r->uri) ;
581  
582 my $hdr = $r -> err_headers_out ;
583 $hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', 'NTLM') if ($self -> {authntlm}) ;
584 $hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', 'Basic realm="'
585 . $self -> {authname} . '"') if ($self -> {authbasic}) ;
586 # $r->discard_request_body ;
587 return Apache2::Const::HTTP_UNAUTHORIZED ;
588 }
589  
590 if ($type == 1)
591 {
592 print STDERR "[$$] handler type == 1 \n" if ($debug) ;
593 my $nonce = $self -> get_nonce ($r) ;
594 if (!$nonce)
595 {
596 $self -> {lock} = undef ; # reset lock in case anything has gone wrong
597 $r->log_error("Cannot get nonce") ;
598 # $r->discard_request_body ;
599 return $self->{ntlmauthoritative} ? (defined($nonce))
600 ? Apache2::Const::HTTP_FORBIDDEN
601 : Apache2::Const::HTTP_INTERNAL_SERVER_ERROR
602 : Apache2::Const::DECLINED ;
603 }
604  
605 my $header1 = $self -> set_msg2 ($r, $nonce) ;
606 my $hdr = $r -> err_headers_out ;
607 $hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', $header1) if ($self -> {authntlm}) ;
608 # $r->discard_request_body ;
609 print STDERR "[$$] AuthenNTLM: verify handle = 1 smbhandle == $self->{smbhandle} \n" if ($debug) ;
610 return Apache2::Const::HTTP_UNAUTHORIZED ;
611 }
612 elsif ($type == 3)
613 {
614 print STDERR "[$$] handler type == 3 \n" if ($debug) ;
615 print STDERR "[$$] AuthenNTLM: verify handle = 3 smbhandle == $self->{smbhandle} \n" if ($debug) ;
616 if ( !$self->verify_user( $r ) )
617 {
618 if ( $self->{ntlmauthoritative} )
619 {
620 my $hdr = $r -> err_headers_out ;
621 $hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', 'NTLM') if ($self -> {authntlm}) ;
622 $hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', 'Basic realm="'
623 . $self -> {authname} . '"') if ($self -> {authbasic}) ;
624 # $r->discard_request_body ;
625 return Apache2::Const::HTTP_UNAUTHORIZED ;
626 }
627 else
628 {
629 return Apache2::Const::DECLINED ;
630 }
631 }
632 }
633 elsif ($type == -1)
634 {
635 my $nonce = $self -> get_nonce ($r) ;
636 if (!$nonce)
637 {
638 $self -> {lock} = undef ; # reset lock in case anything has gone wrong
639 $r->log_error("Cannot get nonce for " . $r->uri) ;
640 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR ;
641 }
642  
643 if (!$self -> verify_user ($r))
644 {
645 if ($self -> {basicauthoritative})
646 {
647 my $hdr = $r -> err_headers_out ;
648 $hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' :'WWW-Authenticate', 'Basic realm="'
649 . $self -> {authname} . '"') if ($self -> {authbasic}) ;
650 # $r->discard_request_body ;
651 return Apache2::Const::HTTP_UNAUTHORIZED ;
652 }
653 else
654 {
655 return Apache2::Const::DECLINED ;
656 }
657 }
658 }
659 else
660 {
661 $self -> {lock} = undef ; # reset lock in case anything has gone wrong
662 $r->log_error("Bad NTLM Authorization Header type $type for " . $r->uri) ;
663 # $r->discard_request_body ;
664 return Apache2::Const::HTTP_UNAUTHORIZED ;
665 }
666  
667 $self -> {lock} = undef ; # reset lock in case anything has gone wrong
668 $r -> user($self -> {mappedusername} = $self -> map_user ($r)) ;
669  
670 $self->{ok} = 1 ;
671  
672 print STDERR "[$$] AuthenNTLM: OK pid = $$, connection = $$conn cuser = " . $r -> user
673 . ' ip = ' . $conn -> remote_ip . "\n" if ($debug) ;
674  
675 return Apache2::Const::OK ;
676 }
677  
678  
679 package Apache2::AuthenNTLM::Lock ;
680  
681 use IPC::SysV qw(IPC_CREAT S_IRWXU SEM_UNDO);
682 use IPC::Semaphore;
683  
684  
685 sub lock
686 {
687 my $class = shift ;
688 my $key = shift ;
689 my $debug = shift ;
690  
691 my $self = bless {debug => $debug}, $class ;
692 $self->{sem} = new IPC::Semaphore($key, 1,
693 IPC_CREAT | S_IRWXU) or die "Cannot create semaphore with key $key ($!)" ;
694  
695 $self->{sem}->op(0, 0, SEM_UNDO,
696 0, 1, SEM_UNDO);
697 print STDERR "[$$] AuthenNTLM: enter lock\n" if ($self -> {debug}) ;
698
699 return $self ;
700 }
701  
702 sub DESTROY
703 {
704 my $self = shift;
705  
706 $self->{sem}->op(0, -1, SEM_UNDO);
707 print STDERR "[$$] AuthenNTLM: leave lock\n" if ($self -> {debug}) ;
708 }
709  
710 1 ;
711  
712 __END__