| 1 | # Connector::Builtin::Authentication::LDAP | ||
| 2 | # | ||
| 3 | # Authenticate users against LDAP directory. | ||
| 4 | |||
| 5 | package Connector::Builtin::Authentication::LDAP; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use English; | ||
| 10 | use Template; | ||
| 11 | use Data::Dumper; | ||
| 12 | use Net::LDAP; | ||
| 13 | |||
| 14 | use Moose; | ||
| 15 | extends 'Connector::Proxy::Net::LDAP'; | ||
| 16 | |||
| 17 | # if we use direct bind we dont need base or filter | ||
| 18 | has '+base' => ( | ||
| 19 | required => 0 | ||
| 20 | ); | ||
| 21 | |||
| 22 | has '+filter' => ( | ||
| 23 | required => 0 | ||
| 24 | ); | ||
| 25 | |||
| 26 | # | ||
| 27 | # Authentication-specific options | ||
| 28 | # | ||
| 29 | has userattr => ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => 'Str', | ||
| 32 | default => 'uid', | ||
| 33 | ); | ||
| 34 | |||
| 35 | has groupattr => ( | ||
| 36 | is => 'rw', | ||
| 37 | isa => 'Str', | ||
| 38 | default => 'member', | ||
| 39 | ); | ||
| 40 | |||
| 41 | has groupdn => ( | ||
| 42 | is => 'rw', | ||
| 43 | isa => 'Str', | ||
| 44 | ); | ||
| 45 | |||
| 46 | has indirect => ( | ||
| 47 | is => 'rw', | ||
| 48 | isa => 'Bool', | ||
| 49 | default => 1, | ||
| 50 | ); | ||
| 51 | |||
| 52 | has ambiguous => ( | ||
| 53 | is => 'rw', | ||
| 54 | isa => 'Bool', | ||
| 55 | default => 0, | ||
| 56 | ); | ||
| 57 | |||
| 58 | # NOTE: it returns undef in case of error, or ref to an array of user DNs | ||
| 59 | # it MAY return an empty array if user is not found | ||
| 60 | sub _search_user { | ||
| 61 | my $self = shift; | ||
| 62 | my $user = shift; | ||
| 63 | |||
| 64 | my $ldap = $self->ldap(); | ||
| 65 | |||
| 66 | $self->log()->debug('Searching LDAP databse for user "'.$user. '"'); | ||
| 67 | my $result = $self->_run_search({ LOGIN => $user }, { noattrs => 1 }); | ||
| 68 | if($result->is_error()) { | ||
| 69 | $self->log()->error('LDAP search returned error code '.$result->code.' (error: '.$result->error_desc().')'); | ||
| 70 | return undef; | ||
| 71 | } else { | ||
| 72 | $self->log()->debug('LDAP search returned '.$result->count . (($result->count ==1) ? ' entry' : ' entries')); | ||
| 73 | } | ||
| 74 | if($self->groupdn()) { | ||
| 75 | $self->log()->debug('Group check requested, groupdn: "'.$self->groupdn().'", groupattr: "'.$self->groupattr().'"'); | ||
| 76 | } | ||
| 77 | |||
| 78 | my @entries; | ||
| 79 | for my $entry ($result->entries()) { | ||
| 80 | my $dn = $entry->dn(); | ||
| 81 | if(defined $self->groupdn()) { | ||
| 82 | if(!$self->_check_user_group($dn)) { | ||
| 83 | next; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | push @entries, $dn; | ||
| 87 | } | ||
| 88 | |||
| 89 | return unless(@entries); | ||
| 90 | |||
| 91 | $self->log()->debug('Found '.scalar @entries.' LDAP entries matching the user "'.$user.'"'); | ||
| 92 | |||
| 93 | if (@entries > 1 && !$self->ambiguous()) { | ||
| 94 | $self->log()->error('Ambiguous search result'); | ||
| 95 | return $self->_node_not_exists($user); | ||
| 96 | } | ||
| 97 | |||
| 98 | return \@entries; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _check_user_group { | ||
| 102 | my $self = shift; | ||
| 103 | my $dn = shift; | ||
| 104 | my $ldap = $self->ldap(); | ||
| 105 | |||
| 106 | $self->log()->debug('Checking if "'.$dn.'" belongs to group "'.$self->groupdn().'"'); | ||
| 107 | my $result = $ldap->compare($self->groupdn(), attr => $self->groupattr(), value => $dn); | ||
| 108 | if($result->is_error()) { | ||
| 109 | $self->log()->error('LDAP compare returned error code '.$result->code.' (error: '.$result->error_desc().')'); | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | if($result->code != 6) { # !compareTrue | ||
| 113 | $self->log()->debug('User "'.$dn.'" does not belong to group "'.$self->groupdn().'"'); | ||
| 114 | return 0; | ||
| 115 | } | ||
| 116 | $self->log()->debug('User "'.$dn.'" belongs to group "'.$self->groupdn().'"'); | ||
| 117 | return 1 | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _check_user_password { | ||
| 121 | my $self = shift; | ||
| 122 | my $userdns = shift; | ||
| 123 | my $password = shift; | ||
| 124 | my $ldap = $self->ldap; | ||
| 125 | |||
| 126 | my $userdn; | ||
| 127 | foreach my $dn (@$userdns) { | ||
| 128 | # Try to bind to $dn | ||
| 129 | $self->log()->debug('Trying to bind to dn: '.$dn); | ||
| 130 | my $mesg = $ldap->bind($dn, password => $password); | ||
| 131 | if($mesg->is_error()) { | ||
| 132 | $self->log()->debug('LDAP bind to '.$dn.' returned error code '.$mesg->code.' (error: '.$mesg->error_desc().')'); | ||
| 133 | } else { | ||
| 134 | $self->log()->debug('LDAP bind to '.$dn.' succeeded'); | ||
| 135 | $userdn = $dn; | ||
| 136 | last; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | # restore the connection using the orginal credentials | ||
| 141 | $self->rebind(); | ||
| 142 | |||
| 143 | if(!defined $userdn) { | ||
| 144 | $self->log()->warn('Authentication failed'); | ||
| 145 | return 0; | ||
| 146 | } else { | ||
| 147 | $self->log()->info('User successfuly authenticated: (dn: '.$userdn.')'); | ||
| 148 | return $userdn; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | sub get { | ||
| 153 | |||
| 154 | my $self = shift; | ||
| 155 | my $arg = shift; | ||
| 156 | my $params = shift; | ||
| 157 | |||
| 158 | my @args = $self->_build_path( $arg ); | ||
| 159 | my $user = shift @args; | ||
| 160 | |||
| 161 | my $password = $params->{password}; | ||
| 162 | |||
| 163 | if(!$user) { | ||
| 164 | $self->log()->warn('Missing user name'); | ||
| 165 | return undef; | ||
| 166 | } | ||
| 167 | # enforce valueencoding, see RFC4515, note that we allow non-ascii (utf-8) characters | ||
| 168 | # I assume that Net::LDAP->search() escapes them internally as needed | ||
| 169 | if (!($user =~ /^([\x01-\x27\x2B-\x5B\x5D-\x7F]|[^[:ascii:]]|\\[0-9a-fA-F][0-9a-fA-F])*$/)) { | ||
| 170 | $self->log()->warn('Invalid chars in username ("'.$user.'")'); | ||
| 171 | return undef; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | # let's check if we were instructed to search for the auth user | ||
| 176 | if($self->indirect()) { | ||
| 177 | my $result = $self->_search_user( $user ); | ||
| 178 | if(!defined $result) { | ||
| 179 | $self->log()->warn('User not found in LDAP database'); | ||
| 180 | return $self->_node_not_exists($user); | ||
| 181 | } | ||
| 182 | return $self->_check_user_password($result, $password); | ||
| 183 | } | ||
| 184 | |||
| 185 | # direct bind = username is the DN to bind | ||
| 186 | my $res = $self->_check_user_password([$user], $password); | ||
| 187 | # we can not check if the user or the password is wrong | ||
| 188 | return unless($res); | ||
| 189 | |||
| 190 | # if we are here we have a successful direct bind, we now check | ||
| 191 | # for the group using the bound connection, this requires that the | ||
| 192 | # user himself has access permissions on the group objects | ||
| 193 | if($self->groupdn()) { | ||
| 194 | if(!$self->_check_user_group($user)) { | ||
| 195 | $self->log()->warn('User was authenticated but is not member of this group'); | ||
| 196 | return $self->_node_not_exists($user); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | return $res; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub get_meta { | ||
| 203 | my $self = shift; | ||
| 204 | |||
| 205 | # If we have no path, we tell the caller that we are a connector | ||
| 206 | my @path = $self->_build_path( shift ); | ||
| 207 | if (scalar @path == 0) { | ||
| 208 | return { TYPE => "connector" }; | ||
| 209 | } | ||
| 210 | |||
| 211 | return {TYPE => "scalar" }; | ||
| 212 | } | ||
| 213 | |||
| 214 | no Moose; | ||
| 215 | __PACKAGE__->meta->make_immutable; | ||
| 216 | |||
| 217 | 1; | ||
| 218 | __END__ |