/usr/lib/perl5/Apache2/AuthenNTLM.pm is in libapache2-authenntlm-perl 0.02-5build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | ###################################################################################
#
# Apache2::AuthenNTLM - Copyright (c) 2002 Gerald Richter / ECOS
#
# You may distribute under the terms of either the GNU General Public
# License or the Artistic License, as specified in the Perl README file.
#
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# $Id: AuthenNTLM.pm,v 1.28 2002/11/12 05:31:50 richter Exp $
#
###################################################################################
package Apache2::AuthenNTLM ;
use strict ;
use vars qw{$cache $VERSION %msgflags1 %msgflags2 %msgflags3 %invflags1 %invflags2 %invflags3 $addr $port $debug} ;
$VERSION = 0.02 ;
$debug = 0 ;
$cache = undef ;
use MIME::Base64 () ;
use Authen::Smb 0.95 ;
use Socket ;
%msgflags1 = ( 0x01 => "NEGOTIATE_UNICODE",
0x02 => "NEGOTIATE_OEM",
0x04 => "REQUEST_TARGET",
0x10 => "NEGOTIATE_SIGN",
0x20 => "NEGOTIATE_SEAL",
0x80 => "NEGOTAITE_LM_KEY",
);
%msgflags2 = ( 0x02 => "NEGOTIATE_NTLM",
0x40 => "NEGOTIATE_LOCAL_CALL",
0x80 => "NEGOTIATE_ALWAYS_SIGN",
);
%msgflags3 = ( 0x01 => "TARGET_TYPE_DOMAIN",
0x02 => "TARGET_TYPE_SERVER",
);
%invflags1 = ( "NEGOTIATE_UNICODE" => 0x01,
"NEGOTIATE_OEM" => 0x02,
"REQUEST_TARGET" => 0x04,
"NEGOTIATE_SIGN" => 0x10,
"NEGOTIATE_SEAL" => 0x20,
"NEGOTIATE_LM_KEY" => 0x80,
);
%invflags2 = ( "NEGOTIATE_NTLM" => 0x02,
"NEGOTIATE_LOCAL_CALL" => 0x40,
"NEGOTIATE_ALWAYS_SIGN" => 0x80,
);
%invflags3 = ( "TARGET_TYPE_DOMAIN" => 0x01,
"TARGET_TYPE_SERVER" => 0x02,
);
############################################
# here is where we start the new code....
############################################
use mod_perl2 ;
use Apache2::Access ;
use Apache2::Connection ;
use Apache2::Log ;
use Apache2::RequestRec ;
use Apache2::RequestUtil ;
use Apache2::RequestIO ;
use APR::Table ;
use APR::SockAddr ;
use Apache2::Const -compile => qw(HTTP_UNAUTHORIZED HTTP_INTERNAL_SERVER_ERROR DECLINED HTTP_FORBIDDEN OK) ;
##################### end modperl code ######################
sub get_config
{
my ($self, $r) = @_ ;
return if ($self -> {smbpdc}) ; # config already setup
$debug = $r -> dir_config ('ntlmdebug') || 0 ;
$debug = $self -> {debug} = lc($debug) eq 'on'?1:($debug+0) ;
my @config = $r -> dir_config -> get ('ntdomain') ;
foreach (@config)
{
my ($domain, $pdc, $bdc) = split /\s+/ ;
$domain = lc ($domain) ;
$self -> {smbpdc}{$domain} = $pdc ;
$self -> {smbbdc}{$domain} = $bdc ;
print STDERR "[$$] AuthenNTLM: Config Domain = $domain pdc = $pdc bdc = $bdc\n" if ($debug) ;
}
$self -> {defaultdomain} = $r -> dir_config ('defaultdomain') || '' ;
$self -> {fallbackdomain} = $r -> dir_config ('fallbackdomain') || '' ;
$self -> {cacheuser} = $r -> dir_config ('cacheuser') || '0' ;
$self -> {authtype} = $r -> auth_type || 'ntlm,basic' ;
$self -> {authname} = $r -> auth_name || '' ;
my $autho = $r -> dir_config ('ntlmauthoritative') || 'on' ;
$self -> {ntlmauthoritative} = lc($autho) eq 'on' || $autho == 1?1:0 ;
$autho = $r -> dir_config ('basicauthoritative') || 'on' ;
$self -> {basicauthoritative} = lc($autho) eq 'on' || $autho == 1?1:0 ;
$self -> {authntlm} = 0 ;
$self -> {authbasic} = 0 ;
$self -> {authntlm} = 1 if ($self -> {authtype} =~ /(^|,)ntlm($|,)/i) ;
$self -> {authbasic} = 1 if ($self -> {authtype} =~ /(^|,)basic($|,)/i) ;
$self -> {semkey} = $r -> dir_config ('ntlmsemkey') ;
$self -> {semkey} = 23754 if (!defined ($self -> {semkey})) ;
$self -> {semtimeout} = $r -> dir_config ('ntlmsemtimeout') ;
$self -> {semtimeout} = 2 if (!defined ($self -> {semtimeout})) ;
$self -> {splitdomainprefix} = $r -> dir_config ('splitdomainprefix') || '' ;
if ($debug)
{
print STDERR "[$$] AuthenNTLM: Config Default Domain = $self->{defaultdomain}\n" ;
print STDERR "[$$] AuthenNTLM: Config Fallback Domain = $self->{fallbackdomain}\n" ;
print STDERR "[$$] AuthenNTLM: Config AuthType = $self->{authtype} AuthName = $self->{authname}\n" ;
print STDERR "[$$] AuthenNTLM: Config Auth NTLM = $self->{authntlm} Auth Basic = $self->{authbasic}\n" ;
print STDERR "[$$] AuthenNTLM: Config NTLMAuthoritative = " . ($self -> {ntlmauthoritative}?'on':'off') . " BasicAuthoritative = " . ($self -> {basicauthoritative}?'on':'off') . "\n" ;
print STDERR "[$$] AuthenNTLM: Config Semaphore key = $self->{semkey} timeout = $self->{semtimeout}\n" ;
print STDERR "[$$] AuthenNTLM: Config SplitDomainPrefix = $self->{splitdomainprefix}\n" ;
}
}
sub get_nonce
{
my ($self, $r) = @_ ;
# reuse connection if possible
if ($self -> {nonce} && $self -> {ok})
{
print STDERR "[$$] AuthenNTLM: Returning cached nonce\n" if ($debug) ;
return $self -> {nonce}
}
# this is not the real nonce!
# we just need to preallocate some space (8 bytes) where Authen::Smb::Valid_User_Connect
# puts the real nonce
my $nonce = '12345678' ;
$self -> {domain} = $self -> {fallbackdomain}
if ($self -> {fallbackdomain} && !($self -> {smbpdc}{lc ($self -> {domain})}));
my $domain = lc ($self -> {domain}) ;
my $pdc = $self -> {smbpdc}{$domain} ;
my $bdc = $self -> {smbbdc}{$domain} ;
if (!$pdc)
{
$r->log_error("No PDC and no fallbackdomain given for domain $domain") ;
return '' ;
}
$self -> {nonce} = undef ;
print STDERR "[$$] AuthenNTLM: Connect to pdc = $pdc bdc = $bdc domain = $domain\n" if ($debug) ;
# smb aborts any connection that where no user is logged on as soon as somebody
# tries to open another one. So we have to make sure two request, do not start
# two auth cycles at the same time. To avoid a hang of the whole server we wrap it with
# a small timeout
if ($self->{semkey})
{
eval
{
local $SIG{ALRM} = sub { print STDERR "[$$] AuthenNTLM: timed out"
. "while waiting for lock (key = $self->{semkey})\n"; die ; };
alarm $self -> {semtimeout} ;
$self -> {lock} = Apache2::AuthenNTLM::Lock -> lock ($self->{semkey}, $debug) ;
alarm 0;
};
}
$self -> {smbhandle} = Authen::Smb::Valid_User_Connect ($pdc, $bdc, $domain, $nonce) ;
print STDERR "[$$] AuthenNTLM: verify handle $self->{username} smbhandle == $self->{smbhandle} \n" if ($debug) ;
if (!$self -> {smbhandle})
{
$r->log_error("Connect to SMB Server failed (pdc = $pdc bdc = $bdc domain = $domain error = "
. Authen::Smb::SMBlib_errno . '/' . Authen::Smb::SMBlib_SMB_Error . ") for " .
$r -> uri) ;
return undef ;
}
return $self -> {nonce} = $nonce ;
}
sub verify_user
{
my ($self, $r) = @_ ;
if (!$self -> {smbhandle})
{
$self -> {lock} = undef ; # reset lock in case anything has gone wrong
$r->log_error("SMB Server connection not open in state 3 for " . $r -> uri) ;
return ;
}
my $rc ;
print STDERR "[$$] AuthenNTLM: Verify user $self->{username} via smb server\n" if ($debug) ;
if ($self -> {basic})
{
$rc = Authen::Smb::Valid_User_Auth ($self -> {smbhandle}, $self->{username}, $self -> {password}) ;
}
else
{
$rc = Authen::Smb::Valid_User_Auth ($self -> {smbhandle}, $self->{username}, $self -> {usernthash}, 1, $self->{userdomain}) ;
}
my $errno = Authen::Smb::SMBlib_errno ;
my $smberr = Authen::Smb::SMBlib_SMB_Error ;
Authen::Smb::Valid_User_Disconnect ($self -> {smbhandle}) if ($self -> {smbhandle}) ;
$self -> {smbhandle} = undef ;
$self -> {lock} = undef ;
if ($rc == &Authen::Smb::NTV_LOGON_ERROR)
{
$r->log_error("Wrong password/user (rc=$rc/$errno/$smberr): $self->{userdomain}\\$self->{username} for " . $r -> uri) ;
print STDERR "[$$] AuthenNTLM: rc = $rc ntlmhash = $self->{usernthash}\n" if ($debug) ;
return ;
}
if ($rc)
{
$r->log_reason("SMB Server error $rc/$errno/$smberr for " . $r -> uri) ;
return ;
}
return 1 ;
}
sub map_user
{
my ($self, $r) = @_ ;
if ($self -> {splitdomainprefix} == 1)
{
return lc("$self->{username}") ;
}
else
{
return lc("$self->{userdomain}\\$self->{username}") ;
}
}
sub substr_unicode
{
my ($data, $off, $len) = @_ ;
my $i = 0 ;
my $end = $off + $len ;
my $result = '' ;
for ($i = $off ; $i < $end ; $i += 2)
{# for now we simply ignore high order byte
$result .= substr ($data, $i, 1) ;
}
return $result ;
}
sub get_msg_data
{
my ($self, $r) = @_ ;
my $auth_line = $r->headers_in->{$r->proxyreq ? 'Proxy-Authorization' : 'Authorization'} ;
$self -> {ntlm} = 0 ;
$self -> {basic} = 0 ;
if ($debug)
{
$auth_line =~ /^(.*?)\s+/ ;
my $type = $1 ;
print STDERR "[$$] AuthenNTLM: Authorization Header "
. (defined($auth_line)?($debug > 1?$auth_line:$type):'<not given>') . "\n" if ($debug) ;
}
if ($self -> {authntlm} && ($auth_line =~ /^NTLM\s+(.*?)$/i))
{
$self -> {ntlm} = 1 ;
}
elsif ($self -> {authbasic} && ($auth_line =~ /^Basic\s+(.*?)$/i))
{
$self -> {basic} = 1 ;
}
else
{
return undef ;
}
my $data = MIME::Base64::decode($1) ;
if ($debug > 1)
{
my @out ;
for (my $i = 0; $i < length($data); $i++)
{
push @out, unpack('C', substr($data, $i, 1)) ;
}
print STDERR "[$$] AuthenNTLM: Got: " . join (' ', @out). "\n" ;
}
return $data ;
}
sub get_msg
{
my ($self, $r) = @_ ;
my $data = $self -> get_msg_data ($r) ;
return undef if (!$data) ;
if ($self -> {ntlm})
{
my ($protocol, $type) = unpack ('Z8C', $data) ;
return $self -> get_msg1 ($r, $data) if ($type == 1) ;
return $self -> get_msg3 ($r, $data) if ($type == 3) ;
return $type ;
}
elsif ($self -> {basic})
{
return $self -> get_basic ($r, $data) ;
}
return undef ;
}
sub get_msg1
{
my ($self, $r, $data) = @_ ;
my ($protocol, $type, $zero, $flags1, $flags2, $zero2, $dom_len, $x1, $dom_off, $x2, $host_len, $x3, $host_off, $x4) = unpack ('Z8Ca3CCa2vvvvvvvv', $data) ;
my $host = $host_off?substr ($data, $host_off, $host_len):'' ;
my $domain = $dom_off?substr ($data, $dom_off, $dom_len):'' ;
$self -> {domain} = $dom_len?$domain:$self -> {defaultdomain} ;
$self -> {host} = $host_len?$host:'' ;
$self -> {accept_unicode} = $flags1 & 0x01;
if ($debug)
{
my @flag1str;
foreach my $i ( sort keys %msgflags1 )
{
push @flag1str, $msgflags1{ $i } if $flags1 & $i;
}
my $flag1str = join( ",", @flag1str );
my @flag2str;
foreach my $i ( sort keys %msgflags2 )
{
push @flag2str, $msgflags2{ $i } if $flags2 & $i;
}
my $flag2str = join( ",", @flag2str );
print STDERR "[$$] AuthenNTLM: protocol=$protocol, type=$type, flags1=$flags1($flag1str), "
. "flags2=$flags2($flag2str), domain length=$dom_len, domain offset=$dom_off, "
. "host length=$host_len, host offset=$host_off, host=$host, domain=$domain\n" ;
}
return $type ;
}
sub set_msg2
{
my ($self, $r, $nonce) = @_ ;
my $charencoding = $self->{ accept_unicode } ? $invflags1{ NEGOTIATE_UNICODE } : $invflags1{ NEGOTIATE_OEM };
my $flags2 = $invflags2{ NEGOTIATE_ALWAYS_SIGN } | $invflags2{ NEGOTIATE_NTLM };
my $data = pack ('Z8Ca7vvCCa2a8a8', 'NTLMSSP', 2, '', 40, 0, $charencoding, $flags2, '', $nonce, '') ;
my $header = 'NTLM '. MIME::Base64::encode($data, '') ;
if ($debug)
{
if ($debug > 1)
{
my @out ;
for (my $i = 0; $i < length($data); $i++)
{
push @out, unpack('C', substr($data, $i, 1)) ;
}
print STDERR "[$$] AuthenNTLM: Send: " . join (' ', @out). "\n" ;
}
print STDERR "[$$] AuthenNTLM: charencoding = $charencoding\n";
print STDERR "[$$] AuthenNTLM: flags2 = $flags2\n";
print STDERR "[$$] AuthenNTLM: nonce=$nonce\n" if ($debug > 1);
print STDERR "[$$] AuthenNTLM: Send header: " . ($debug > 1?$header:'NTLM ...') . "\n" ;
}
return $header;
}
sub get_msg3
{
my ($self, $r, $data) = @_ ;
my ($protocol, $type, $zero,
$lm_len, $l1, $lm_off,
$nt_len, $l3, $nt_off,
$dom_len, $x1, $dom_off,
$user_len, $x3, $user_off,
$host_len, $x5, $host_off,
$msg_len
) = unpack ('Z8Ca3vvVvvVvvVvvVvvVv', $data) ;
my $lm = $lm_off ? substr ($data, $lm_off, $lm_len):'' ;
my $nt = $nt_off ? substr ($data, $nt_off, $nt_len):'' ;
my $user = $user_off ? ($self->{accept_unicode} ? substr_unicode ($data, $user_off, $user_len) : substr( $data, $user_off, $user_len ) ) :'' ;
my $host = $host_off ? ($self->{accept_unicode} ? substr_unicode ($data, $host_off, $host_len) : substr( $data, $host_off, $host_len ) ) :'' ;
my $domain = $dom_off ? ($self->{accept_unicode} ? substr_unicode ($data, $dom_off, $dom_len) : substr( $data, $dom_off, $dom_len ) ) :'' ;
$self -> {userdomain} = $dom_len?$domain:$self -> {defaultdomain} ;
$self -> {username} = $user ;
$self -> {usernthash} = $nt_len ? $nt : $lm;
if ($debug)
{
print STDERR "[$$] AuthenNTLM: protocol=$protocol, type=$type, user=$user, "
. "host=$host, domain=$domain, msg_len=$msg_len\n" ;
}
return $type ;
}
sub get_basic
{
my ($self, $r, $data) = @_ ;
($self -> {username}, $self -> {password}) = split (/:/, $data) ;
my ($domain, $username) = split (/\\|\//, $self -> {username}) ;
if ($username)
{
$self -> {domain} = $domain ;
$self -> {username} = $username ;
}
else
{
$self -> {domain} = $self -> {defaultdomain} ;
}
$self -> {userdomain} = $self -> {domain} ;
if ($debug)
{
print STDERR "[$$] AuthenNTLM: basic auth username = $self->{domain}\\$self->{username}\n" ;
}
return -1 ;
}
sub DESTROY
{
my ($self) = @_ ;
Authen::Smb::Valid_User_Disconnect ($self -> {smbhandle}) if ($self -> {smbhandle}) ;
}
sub handler : method
{
my ($class, $r) = @_ ;
my $type ;
my $nonce = '' ;
my $self ;
my $conn = $r -> connection ;
my $connhdr = $r -> headers_in -> {'Connection'} ;
my $fh = select (STDERR) ;
$| = 1 ;
select ($fh) ;
my $addr = $conn -> remote_addr -> ip_get ;
my $port = $conn -> remote_addr -> port ;
print STDERR "[$$] AuthenNTLM: Start NTLM Authen handler pid = $$, connection = "
. "$$conn conn_http_hdr = $connhdr main = " . ($r -> main)
. " cuser = " . $r -> user . ' remote_ip = ' . $conn -> remote_ip
. " remote_port = " . unpack('n', $port) . ' remote_host = <'
. $conn -> remote_host . "> version = $VERSION "
. "smbhandle = " . $self -> {smbhandle} . "\n" if ($debug) ;
# we cannot attach our object to the connection record. Since in
# Apache 1.3 there is only one connection at a time per process
# we can cache our object and check if the connection has changed.
# The check is done by slightly changing the remote_host member, which
# persists as long as the connection does
# This has to be reworked to work with Apache 2.0
my $table;
$table = $conn->notes();
if (ref ($cache) ne $class || $$conn != $cache->{connectionid} || ($table->get('status') ne "AUTHSTARTED"))
{
$table->add('status','AUTHSTARTED');
$conn->notes($table);
$self = {connectionid => $$conn } ;
bless $self, $class ;
$cache = $self ;
print STDERR "[$$] AuthenNTLM: Setup new object\n" if ($debug) ;
}
else
{
$self = $cache ;
print STDERR "[$$] AuthenNTLM: Object exists user = $self->{userdomain}\\$self->{username}\n" if ($debug) ;
if ($self -> {ok})
{
$r -> user($self->{mappedusername}) ;
# we accept the user because we are on the same connection
$type = $self -> get_msg ($r);
my $content_len = $r->headers_in->{'content-length'} ;
my $method = $r -> method ;
print STDERR "[$$] AuthenNTLM: Same connection pid = $$, connection = $$conn cuser = " .
$r -> user . ' ip = ' . $conn -> remote_ip . ' method = ' .
$method . ' Content-Length = ' .
$content_len . ' type = ' . $type . "\n" if ($debug) ;
# IE (5.5, 6.0, probably others) can send a type 1 message
# after authenticating on the same connection. This is a
# problem for POST messages, because IE also sends a
# "Content-length: 0" with no POST data.
if ($method eq 'GET' || $method eq 'HEAD' || $method eq 'OPTION' || $method eq 'DELETE' ||
$content_len > 0 || $type == 3)
{
print STDERR "[$$] AuthenNTLM: OK because same connection\n" if ($debug) ;
return Apache2::Const::OK ;
}
}
}
# end of if statement
$self -> get_config ($r) ;
$type = $self -> get_msg ($r) if (!$type) ;
if (!$type)
{
$self -> {lock} = undef ; # reset lock in case anything has gone wrong
if (!$self->{ntlmauthoritative})
{ # see if we have any header
my $auth_line = $r -> headers_in->{$r->proxyreq ? 'Proxy-Authorization' : 'Authorization'} ;
if ($auth_line)
{
$r->log_error('Bad/Missing NTLM Authorization Header for ' . $r->uri
. '; DECLINEing because we are not authoritative' ) ;
return Apache2::Const::DECLINED ;
}
}
$r->log_error('Bad/Missing NTLM/Basic Authorization Header for ' . $r->uri) ;
my $hdr = $r -> err_headers_out ;
$hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', 'NTLM') if ($self -> {authntlm}) ;
$hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', 'Basic realm="'
. $self -> {authname} . '"') if ($self -> {authbasic}) ;
# $r->discard_request_body ;
return Apache2::Const::HTTP_UNAUTHORIZED ;
}
if ($type == 1)
{
print STDERR "[$$] handler type == 1 \n" if ($debug) ;
my $nonce = $self -> get_nonce ($r) ;
if (!$nonce)
{
$self -> {lock} = undef ; # reset lock in case anything has gone wrong
$r->log_error("Cannot get nonce") ;
# $r->discard_request_body ;
return $self->{ntlmauthoritative} ? (defined($nonce))
? Apache2::Const::HTTP_FORBIDDEN
: Apache2::Const::HTTP_INTERNAL_SERVER_ERROR
: Apache2::Const::DECLINED ;
}
my $header1 = $self -> set_msg2 ($r, $nonce) ;
my $hdr = $r -> err_headers_out ;
$hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', $header1) if ($self -> {authntlm}) ;
# $r->discard_request_body ;
print STDERR "[$$] AuthenNTLM: verify handle = 1 smbhandle == $self->{smbhandle} \n" if ($debug) ;
return Apache2::Const::HTTP_UNAUTHORIZED ;
}
elsif ($type == 3)
{
print STDERR "[$$] handler type == 3 \n" if ($debug) ;
print STDERR "[$$] AuthenNTLM: verify handle = 3 smbhandle == $self->{smbhandle} \n" if ($debug) ;
if ( !$self->verify_user( $r ) )
{
if ( $self->{ntlmauthoritative} )
{
my $hdr = $r -> err_headers_out ;
$hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', 'NTLM') if ($self -> {authntlm}) ;
$hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' : 'WWW-Authenticate', 'Basic realm="'
. $self -> {authname} . '"') if ($self -> {authbasic}) ;
# $r->discard_request_body ;
return Apache2::Const::HTTP_UNAUTHORIZED ;
}
else
{
return Apache2::Const::DECLINED ;
}
}
}
elsif ($type == -1)
{
my $nonce = $self -> get_nonce ($r) ;
if (!$nonce)
{
$self -> {lock} = undef ; # reset lock in case anything has gone wrong
$r->log_error("Cannot get nonce for " . $r->uri) ;
return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR ;
}
if (!$self -> verify_user ($r))
{
if ($self -> {basicauthoritative})
{
my $hdr = $r -> err_headers_out ;
$hdr -> add ($r->proxyreq ? 'Proxy-Authenticate' :'WWW-Authenticate', 'Basic realm="'
. $self -> {authname} . '"') if ($self -> {authbasic}) ;
# $r->discard_request_body ;
return Apache2::Const::HTTP_UNAUTHORIZED ;
}
else
{
return Apache2::Const::DECLINED ;
}
}
}
else
{
$self -> {lock} = undef ; # reset lock in case anything has gone wrong
$r->log_error("Bad NTLM Authorization Header type $type for " . $r->uri) ;
# $r->discard_request_body ;
return Apache2::Const::HTTP_UNAUTHORIZED ;
}
$self -> {lock} = undef ; # reset lock in case anything has gone wrong
$r -> user($self -> {mappedusername} = $self -> map_user ($r)) ;
$self->{ok} = 1 ;
print STDERR "[$$] AuthenNTLM: OK pid = $$, connection = $$conn cuser = " . $r -> user
. ' ip = ' . $conn -> remote_ip . "\n" if ($debug) ;
return Apache2::Const::OK ;
}
package Apache2::AuthenNTLM::Lock ;
use IPC::SysV qw(IPC_CREAT S_IRWXU SEM_UNDO);
use IPC::Semaphore;
sub lock
{
my $class = shift ;
my $key = shift ;
my $debug = shift ;
my $self = bless {debug => $debug}, $class ;
$self->{sem} = new IPC::Semaphore($key, 1,
IPC_CREAT | S_IRWXU) or die "Cannot create semaphore with key $key ($!)" ;
$self->{sem}->op(0, 0, SEM_UNDO,
0, 1, SEM_UNDO);
print STDERR "[$$] AuthenNTLM: enter lock\n" if ($self -> {debug}) ;
return $self ;
}
sub DESTROY
{
my $self = shift;
$self->{sem}->op(0, -1, SEM_UNDO);
print STDERR "[$$] AuthenNTLM: leave lock\n" if ($self -> {debug}) ;
}
1 ;
__END__
=head1 NAME
Apache2::AuthenNTLM - Perform Microsoft NTLM and Basic User Authentication
=head1 SYNOPSIS
<Location />
PerlAuthenHandler Apache2::AuthenNTLM
AuthType ntlm,basic
AuthName test
require valid-user
# domain pdc bdc
PerlAddVar ntdomain "name_domain1 name_of_pdc1"
PerlAddVar ntdomain "other_domain pdc_for_domain bdc_for_domain"
PerlSetVar defaultdomain wingr1
PerlSetVar splitdomainprefix 1
PerlSetVar ntlmdebug 1
</Location>
=head1 DESCRIPTION
The purpose of this module is to perform a user authentication via Microsoft's
NTLM protocol. This protocol is supported by all versions of the Internet
Explorer and is mainly useful for intranets. Depending on your preferences
setting IE will supply your windows logon credentials to the web server
when the server asks for NTLM authentication. This saves the user to type in
his/her password again.
The NTLM protocol performs a challenge/response to exchange a random number
(nonce) and get back a md4 hash, which is built from the user's password
and the nonce. This makes sure that no password goes over the wire in plain text.
The main advantage of the Perl implementation is, that it can be easily extended
to verify the user/password against other sources than a windows domain controller.
The defaultf implementation is to go to the domain controller for the given domain
and verify the user. If you want to verify the user against another source, you
can inherit from Apache2::AuthenNTLM and override it's methods.
To support users that aren't using Internet Explorer, Apache2::AuthenNTLM can
also perform basic authentication depending on its configuration.
B<IMPORTANT:> NTLM authentification works only when KeepAlive is on. (If you have set ntlmdebug 2, and see that there is no return message (type 3), check your httpd.conf file for "KeepAlive Off". If KeepAlive Off, then change it to KeepAlive On, restart Apache, and test again).
=head1 CONFIGURATION
=head2 AuthType
Set the type of authentication. Can be either "basic", "ntlm"
or "ntlm,basic" for doing both.
=head2 AuthName
Set the realm for basic authentication
=head2 require valid-user
Necessary to tell Apache to require user authentication at all. Can also
used to allow only some users, e.g.
require user foo bar
Note that Apache2::AuthenNTLM does not perform any authorization, if
the require xxx is executed by Apache itself. Alternatively you can
use another (Perl-)module to perform authorization.
=head2 PerlAddVar ntdomain "domain pdc bdc"
This is used to create a mapping between a domain and both a pdc and bdc for
that domain. Domain, pdc and bdc must be separated by a space. You can
specify mappings for more than one domain.
NOTE FOR WINDOWS ACTIVE DIRECTORY USERS: You must specify the DOMAIN for
the pdc and/or bdc. Windows smb servers will not accept ip address in dotted
quad form. For example, the SPEEVES domain pdc has an ip address of 192.168.0.2.
If you enter the ntdomain as:
PerlAddVar ntdomain 192.168.0.2
Then you will never be able be able to authenticate to the remote server correctly,
and you will receive a "Can not get NONCE" error in the error_log. You must
specify it as:
PerlAddVar ntdomain SPEEVES
This means that you will need to resolve the DOMAIN locally on the web server
machine. I put it into the /etc/hosts file.
For the complete run-down on this issue, check out:
http://www.gossamer-threads.com/archive/mod_perl_C1/modperl_F7/%5BFwd:_Re:_Apache::AuthenNTLM-2.04_Problems..%5D_P104237/
=head2 PerlSetVar defaultdomain
Set the default domain. This is used when the client does not provide
any information about the domain.
=head2 PerlSetVar fallbackdomain
fallbackdomain is used in cases where the domain that the user supplied
isn't configured. This is useful in environments where you have a lot of
domains, which trust each other, allowing you to always authenticate against
a single domain, (removing the need to configure all domains available in
your network).
=head2 PerlSetVar ntlmauthoritative
Setting the ntlmauthoritative directive explicitly to 'off' allows authentication
to be passed on to lower level modules if AuthenNTLM cannot authenticate the user
and the NTLM authentication scheme is used.
If set to 'on', which is the default, AuthenNTLM will try to verify the user and,
if it fails, will give an Authorization Required reply.
=head2 PerlSetVar basicauthoritative
Setting the ntlmauthoritative directive explicitly to 'off' allows authentication
to be passed on to lower level modules if AuthenNTLM cannot authenticate the user
and the Basic authentication scheme is used.
If set to 'on', which is the default, AuthenNTLM will try to verify the user and
if it fails will give an Authorization Required reply.
=head2 PerlSetVar ntlmsemkey
There are troubles when two authentication requests take place at the same
time. If the second request starts, before the first request has successfully
verified the user to the smb (windows) server, the smb server will terminate the first
request. To avoid this Apache2::AuthenNTLM serializes all requests. It uses a semaphore
for this purpose. The semkey directive set the key which is used (default: 23754).
Set it to zero to turn serialization off.
=head2 PerlSetVar ntlmsemtimeout
This set the timeout value used to wait for the semaphore. The default is two seconds.
It is very small because during the time Apache waits for the semaphore, no other
authentication request can be sent to the windows server. Also Apache2::AuthenNTLM
only asks the windows server once per keep-alive connection, this timeout value
should be as small as possible.
=head2 PerlSetVar splitdomainprefix
If set to 1, $self -> map_user ($r) will return "username"
else $self -> map_user ($r) will return "domain\username"
Default is "domain\username"
=head2 PerlSetVar ntlmdebug
Set this to 1 if you want extra debugging information in the error log.
Set it to 2 to also see the binary data of the NTLM headers.
=head1 OVERRIDEABLE METHODS
Each of the following methods takes the Apache object as argument. Information
about the current authentication can be found inside the object Apache2::AuthenNTLM
itself. To override the methods, create our own class which inherits from
Apache2::AuthenNTLM and use it in httpd.conf e.g.
PerlAuthenHandler Apache2::MyAuthenNTLM
=head2 $self -> get_config ($r)
Will be called after the object is setup to read in configuration informations.
The $r -> dir_config can be used for that purpose.
=head2 $self -> get_nonce ($r)
Will be called to setup the connection to the windows domain controller
for $self -> {domain} and retrieve the nonce.
In case you do not authenticate against a windows machine, you simply need
to set $self -> {nonce} to a 8 byte random string. Returns undef on error.
=head2 $self -> verify_user ($r)
Should verify that the given user supplied the right credentials. Input:
=over
=item $self -> {basic}
Set when we are doing basic authentication
=item $self -> {ntlm}
Set when we are doing ntlm authentication
=item $self -> {username}
The username
=item $self -> {password}
The password when doing basic authentication
=item $self -> {usernthash}
The md4 hash when doing ntlm authentication
=item $self -> {userdomain}
The domain
=back
returns true if this is a valid user.
=head2 $self -> map_user ($r)
Is called before to get the user name which should be available as REMOTE_USER
to the request. Default is to return DOMAIN\USERNAME.
=head2 Example for overriding
The following code shows the a basic example for creating a module which
overrides the map_user method and calls AuthenNTLM's handler only if a
precondition is met. Note: The functions preconditon_met and lookup_user
do the real work and are not shown here.
package Apache2::MyAuthenNTLM ;
use Apache2::AuthenNTLM ;
@ISA = ('Apache2::AuthenNTLM') ;
sub handler ($$)
{
my ($self, $r) = @_ ;
return Apache2::AuthenNTLM::handler ($self, $r) if (precondition_met()) ;
return DECLINED ;
}
sub map_user
{
my ($self, $r) = @_ ;
return lookup_user ($self->{userdomain}, $self->{username}) ;
}
=head1 SUPPORT
Speeves: Thanks to everyone that is helping to find bugs, etc. in this module. Please, feel free to contact me and let me know of any strange things are going on with this module. Also, please copy the modperl@perl.apache.org mailing list, as there are probably many others that are experiencing the same problems as you, and they may be able to return an answer faster than I can by myself. Thanks :)
=head1 SEE ALSO
An implementation of this module which uses cookies to cache the session.
Apache-AuthCookieNTLM - Leo Lapworth
http://search.cpan.org/~llap/Apache-AuthCookieNTLM/
=head1 AUTHOR
G. Richter (richter@dev.ecos.de)
Ported by Shannon Eric Peevey (speeves@unt.edu)
Development of this package, versions 0.01-0.13 was sponsored by:
Siemens: http://www.siemens.com
|