blib/lib/Apache/AuthenNTLM.pm untested

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