| 1 | package DJabberd::Authen::LDAP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use base 'DJabberd::Authen'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use DJabberd::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | our $logger = DJabberd::Log->get_logger; | |||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Net::LDAP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub log { | ||||||||||||||||||||||||||
|
Subroutine
log: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 0 | $logger; | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | DJabberd::Authen::LDAP - An LDAP authentication module for DJabberd | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 VERSION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Version 0.04 | ||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | <VHost mydomain.com> | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | [...] | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | <Plugin DJabberd::Authen::LDAP> | ||||||||||||||||||||||||||||
| 33 | LDAPURI ldap://localhost/ | ||||||||||||||||||||||||||||
| 34 | LDAPBindDN cn=reader | ||||||||||||||||||||||||||||
| 35 | LDAPBindPW pass | ||||||||||||||||||||||||||||
| 36 | LDAPBaseDN ou=people | ||||||||||||||||||||||||||||
| 37 | LDAPFilter (&(inetAuthorizedServices=jabber)(uid=%u)) | ||||||||||||||||||||||||||||
| 38 | LDAPVersion 2 | ||||||||||||||||||||||||||||
| 39 | LDAPMethod rebind | ||||||||||||||||||||||||||||
| 40 | </Plugin> | ||||||||||||||||||||||||||||
| 41 | </VHost> | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | LDAPURI , LDAPBaseDN, and LDAPFilter are required | ||||||||||||||||||||||||||||
| 44 | Everything else is optional. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | The Only LDAPMethod supported at the moment is rebind which performs a bind as LDAPBindDN | ||||||||||||||||||||||||||||
| 47 | or does anonymous bind, then searches for the user using LDAPFilter and then will rebind | ||||||||||||||||||||||||||||
| 48 | as the found DN to verify the password. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | LDAPFilter is an LDAP filter substutions | ||||||||||||||||||||||||||||
| 51 | - %u will be substituted with the incoming userid (w/o the domain) (ie. myuser) | ||||||||||||||||||||||||||||
| 52 | - %d will be substituted with the incoming userid's domain (ie. mydoman.com) | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | LDAPVersion is either 2 or 3, if nothing is specified then default to Net::LDAP default. | ||||||||||||||||||||||||||||
| 55 | This value is passed straight to Net::LDAP | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Edward Rudd, C<< <urkle at outoforder.cc> >> | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub set_config_ldapuri { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapuri: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 0 | my ($self, $ldapuri) = @_; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $ldapuri =~ /((?:ldap[si]?\:\/\/)?[\w\.%\d]+\/?)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $self->{'ldap_uri'} = $ldapuri; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub set_config_ldapbinddn { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapbinddn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 0 | my ($self, $ldapbinddn) = @_; | |||||||||||||||||||||||||||
| 72 | 0 | $self->{'ldap_binddn'} = $ldapbinddn; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub set_config_ldapbindpw { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapbindpw: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 0 | my ($self, $ldapbindpw) = @_; | |||||||||||||||||||||||||||
| 77 | 0 | $self->{'ldap_bindpw'} = $ldapbindpw; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub set_config_ldapbasedn { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapbasedn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my ($self, $ldapbasedn) = @_; | |||||||||||||||||||||||||||
| 82 | 0 | $self->{'ldap_basedn'} = $ldapbasedn; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub set_config_ldapfilter { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapfilter: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 0 | my ($self, $ldapfilter) = @_; | |||||||||||||||||||||||||||
| 87 | 0 | $self->{'ldap_filter'} = $ldapfilter; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub set_config_ldapversion { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapversion: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 0 | my ($self, $ldapversion) = @_; | |||||||||||||||||||||||||||
| 92 | 0 | $self->{'ldap_version'} = $ldapversion; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub set_config_ldapmethod { | ||||||||||||||||||||||||||
|
Subroutine
set_config_ldapmethod: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 0 | my ($self, $ldapmethod) = @_; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( $ldapmethod =~ /^(?:rebind)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | $self->{'ldap_method'} = $ldapmethod; | |||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 0 | $self->{'ldap_method'} = 'unknown'; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub finalize { | ||||||||||||||||||||||||||
|
Subroutine
finalize: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | $logger->error_die("Invalid LDAP URI") unless $self->{ldap_uri}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $logger->error_die("No LDAP BaseDN Specified") unless $self->{ldap_basedn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (not defined $self->{'ldap_method'}) { $self->{'ldap_method'} = 'rebind'; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | for ($self->{ldap_method}) { | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (/^rebind$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | # check additional required params | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $logger->error_die("Must specify filter with userid as %u") unless $self->{ldap_filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 0 | $logger->error_die("Invalid LDAP Authentication Method"); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my %options; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | $options{version} = $self->{ldap_version} if $self->{ldap_version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Initialize ldap connection | ||||||||||||||||||||||||||||
| 122 | $self->{'ldap_conn'} = Net::LDAP->new($self->{ldap_uri}, %options) | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | or $logger->error_die("Could not connect to LDAP Server ".$self->{ldap_uri}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub can_retrieve_cleartext { 0 } | ||||||||||||||||||||||||||
|
Subroutine
can_retrieve_cleartext: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub check_cleartext { | ||||||||||||||||||||||||||
|
Subroutine
check_cleartext: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | 0 | my ($self, $cb, %args) = @_; | |||||||||||||||||||||||||||
| 130 | 0 | my $username = $args{username}; | |||||||||||||||||||||||||||
| 131 | 0 | my $password = $args{password}; | |||||||||||||||||||||||||||
| 132 | 0 | my $conn = $args{conn}; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | unless ($username =~ /^\w+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $cb->reject; | |||||||||||||||||||||||||||
| 135 | 0 | return; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | my $ldap = $self->{'ldap_conn'}; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if (defined $self->{'ldap_binddn'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (not $ldap->bind($self->{'ldap_binddn'}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | password=>$self->{'ldap_bindpw'})) { | ||||||||||||||||||||||||||||
| 143 | 0 | $logger->info("Could not bind to ldap server"); | |||||||||||||||||||||||||||
| 144 | 0 | $cb->decline; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 0 | $ldap->bind; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | my $filter = $self->{'ldap_filter'}; | |||||||||||||||||||||||||||
| 151 | 0 | my $vhost = $conn->vhost->server_name; | |||||||||||||||||||||||||||
| 152 | 0 | $filter =~ s/%u/$username/; | |||||||||||||||||||||||||||
| 153 | 0 | $filter =~ s/%d/$vhost/; | |||||||||||||||||||||||||||
| 154 | 0 | $logger->info("Searching $filter on ".$self->{'ldap_basedn'}); | |||||||||||||||||||||||||||
| 155 | my $srch = $ldap->search( | ||||||||||||||||||||||||||||
| 156 | 0 | base=>$self->{'ldap_basedn'}, | |||||||||||||||||||||||||||
| 157 | filter=>$filter, | ||||||||||||||||||||||||||||
| 158 | attrs=>['dn']); | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ($srch->code || $srch->count != 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$srch->code or $srch->count != 1
MC/DC0%0/2
$srch->code or $srch->count != 1
$srch->code
$srch->count != 1
Truth table0%0/3
A or B
A= $srch->codeB= $srch->count != 1
| |||||||||||||||||||||||||||||
| 160 | 0 | $logger->info("Account $username not found."); | |||||||||||||||||||||||||||
| 161 | 0 | $cb->decline; | |||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||
| 163 | 0 | my $entry = $srch->entry(0); | |||||||||||||||||||||||||||
| 164 | 0 | my $DN = $entry->dn(); | |||||||||||||||||||||||||||
| 165 | 0 | undef($entry); | |||||||||||||||||||||||||||
| 166 | 0 | undef($srch); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | my $res = $ldap->bind($DN,password=>$password); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if ($res->code == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | $cb->accept; | |||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||
| 173 | 0 | $cb->reject; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Original work Copyright 2006 Alexander Karelas, Martin Atkins, Brad Fitzpatrick and Aleksandar Milanov. All rights reserved. | ||||||||||||||||||||||||||||
| 181 | Copyright 2007-2010 Edward Rudd. All rights reserved. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 184 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 2 | 1; | |||||||||||||||||||||||||||