Apache2-AuthenNTLM

 view release on metacpan or  search on metacpan

AuthenNTLM.pm  view on Meta::CPAN

    {
	$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) ;

AuthenNTLM.pm  view on Meta::CPAN


    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

AuthenNTLM.pm  view on Meta::CPAN

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, re...


=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/

AuthenNTLM.pm  view on Meta::CPAN


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...

=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/



( run in 1.007 second using v1.01-cache-2.11-cpan-d8267643d1d )