| 1 | package Net::LDAP::SimpleServer::ProtocolHandler; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: LDAP protocol handler used with Net::LDAP::SimpleServer | ||
| 7 | |||
| 8 | our $VERSION = '0.0.20'; # VERSION | ||
| 9 | |||
| 10 | use constant MD5_PREFIX => '{md5}'; | ||
| 11 | |||
| 12 | use Net::LDAP::Server; | ||
| 13 | use base 'Net::LDAP::Server'; | ||
| 14 | use fields | ||
| 15 | qw(store root_dn root_pw allow_anon user_passwords user_id_attr user_pw_attr user_filter); | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | use Net::LDAP::LDIF; | ||
| 19 | use Net::LDAP::Util qw{canonical_dn}; | ||
| 20 | use Net::LDAP::Filter; | ||
| 21 | use Net::LDAP::FilterMatch; | ||
| 22 | |||
| 23 | use Net::LDAP::Constant qw/ | ||
| 24 | LDAP_SUCCESS LDAP_INVALID_CREDENTIALS LDAP_AUTH_METHOD_NOT_SUPPORTED | ||
| 25 | LDAP_INVALID_SYNTAX LDAP_NO_SUCH_OBJECT LDAP_INVALID_DN_SYNTAX/; | ||
| 26 | |||
| 27 | use Net::LDAP::SimpleServer::LDIFStore; | ||
| 28 | use Net::LDAP::SimpleServer::Constant; | ||
| 29 | |||
| 30 | use Scalar::Util qw{reftype}; | ||
| 31 | use UNIVERSAL::isa; | ||
| 32 | |||
| 33 | use Digest::MD5 qw/md5/; | ||
| 34 | use MIME::Base64; | ||
| 35 | |||
| 36 | sub _make_result { | ||
| 37 | my $code = shift; | ||
| 38 | my $dn = shift // ''; | ||
| 39 | my $msg = shift // ''; | ||
| 40 | |||
| 41 | return { | ||
| 42 | matchedDN => $dn, | ||
| 43 | errorMessage => $msg, | ||
| 44 | resultCode => $code, | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my $class = shift; | ||
| 50 | my $params = shift || croak 'Must pass parameters!'; | ||
| 51 | |||
| 52 | croak 'Parameter must be a HASHREF' unless reftype($params) eq 'HASH'; | ||
| 53 | for my $p (qw/store root_dn sock/) { | ||
| 54 | croak 'Must pass option {' . $p . '}' unless exists $params->{$p}; | ||
| 55 | } | ||
| 56 | croak 'Not a LDIFStore' | ||
| 57 | unless $params->{store}->isa('Net::LDAP::SimpleServer::LDIFStore'); | ||
| 58 | |||
| 59 | croak 'Option {root_dn} can not be empty' unless $params->{root_dn}; | ||
| 60 | croak 'Invalid root DN' | ||
| 61 | unless my $canon_dn = canonical_dn( $params->{root_dn} ); | ||
| 62 | |||
| 63 | my $self = $class->SUPER::new( $params->{sock} ); | ||
| 64 | $self->{store} = $params->{store}; | ||
| 65 | $self->{root_dn} = $canon_dn; | ||
| 66 | $self->{root_pw} = $params->{root_pw}; | ||
| 67 | $self->{allow_anon} = $params->{allow_anon}; | ||
| 68 | $self->{user_passwords} = $params->{user_passwords}; | ||
| 69 | $self->{user_id_attr} = $params->{user_id_attr}; | ||
| 70 | $self->{user_pw_attr} = $params->{user_pw_attr}; | ||
| 71 | $self->{user_filter} = $params->{user_filter}; | ||
| 72 | chomp( $self->{root_pw} ); | ||
| 73 | chomp( $self->{user_passwords} ); | ||
| 74 | |||
| 75 | return $self; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub unbind { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | $self->{store} = undef; | ||
| 82 | $self->{root_dn} = undef; | ||
| 83 | $self->{root_pw} = undef; | ||
| 84 | |||
| 85 | return _make_result(LDAP_SUCCESS); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _find_user_dn { | ||
| 89 | my ( $self, $username ) = @_; | ||
| 90 | |||
| 91 | my $filter = | ||
| 92 | Net::LDAP::Filter->new( '(&' | ||
| 93 | . $self->{user_filter} . '(' | ||
| 94 | . $self->{user_id_attr} . '=' | ||
| 95 | . $username | ||
| 96 | . '))' ); | ||
| 97 | return _match( $filter, $self->{store}->list() ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _encode_password { | ||
| 101 | my $plain = shift; | ||
| 102 | |||
| 103 | my $hashpw = encode_base64( md5($plain), '' ); | ||
| 104 | return MD5_PREFIX . $hashpw; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub bind { | ||
| 108 | ## no critic (ProhibitBuiltinHomonyms) | ||
| 109 | my ( $self, $request ) = @_; | ||
| 110 | |||
| 111 | # anonymous bind | ||
| 112 | return _make_result(LDAP_SUCCESS) | ||
| 113 | if ( $self->{allow_anon} | ||
| 114 | and not $request->{name} | ||
| 115 | and exists $request->{authentication}->{simple} ); | ||
| 116 | |||
| 117 | # As of now, accepts only simple authentication | ||
| 118 | return _make_result(LDAP_AUTH_METHOD_NOT_SUPPORTED) | ||
| 119 | unless exists $request->{authentication}->{simple}; | ||
| 120 | |||
| 121 | my $bind_pw = $request->{authentication}->{simple}; | ||
| 122 | chomp($bind_pw); | ||
| 123 | |||
| 124 | my $bind_dn = canonical_dn( $request->{name} ); | ||
| 125 | unless ($bind_dn) { | ||
| 126 | my $search_user_result = $self->_find_user_dn( $request->{name} ); | ||
| 127 | my $size = scalar( @{$search_user_result} ); | ||
| 128 | |||
| 129 | return _make_result( LDAP_INVALID_DN_SYNTAX, '', | ||
| 130 | 'Cannot find user: ' . $request->{name} ) | ||
| 131 | if $size == 0; | ||
| 132 | return _make_result( LDAP_INVALID_DN_SYNTAX, '', | ||
| 133 | 'Cannot retrieve an unique user entry for id: ' . $request->{name} ) | ||
| 134 | if $size > 1; | ||
| 135 | |||
| 136 | $bind_dn = $search_user_result->[0]; | ||
| 137 | } | ||
| 138 | elsif ( uc($bind_dn) ne uc( $self->{root_dn} ) ) { | ||
| 139 | my $search_dn_result = | ||
| 140 | $self->{store}->list_with_dn_scope( $bind_dn, SCOPE_BASEOBJ ); | ||
| 141 | return _make_result( LDAP_INVALID_DN_SYNTAX, '', | ||
| 142 | 'Cannot find user: ' . $request->{name} ) | ||
| 143 | unless $search_dn_result; | ||
| 144 | |||
| 145 | $bind_dn = $search_dn_result->[0]; | ||
| 146 | } | ||
| 147 | |||
| 148 | if ( $bind_dn->isa('Net::LDAP::Entry') ) { | ||
| 149 | |||
| 150 | # user was not a dn, but it was found | ||
| 151 | my $entry_pw = $bind_dn->get_value( $self->{user_pw_attr} ); | ||
| 152 | |||
| 153 | my $regexp = '^' . MD5_PREFIX; | ||
| 154 | $entry_pw = _encode_password($entry_pw) if $entry_pw =~ /$regexp/; | ||
| 155 | |||
| 156 | return _make_result( LDAP_INVALID_CREDENTIALS, undef, | ||
| 157 | 'entry dn: ' . $bind_dn->dn() ) | ||
| 158 | unless $entry_pw eq $bind_pw; | ||
| 159 | } | ||
| 160 | elsif ( uc($bind_dn) eq uc( $self->{root_dn} ) ) { | ||
| 161 | return _make_result( LDAP_INVALID_CREDENTIALS, undef, | ||
| 162 | 'bind dn: ' . $bind_dn ) | ||
| 163 | unless $bind_pw eq $self->{root_pw}; | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | return _make_result( LDAP_INVALID_DN_SYNTAX, '', | ||
| 167 | 'Cannot find user: ' . $request->{name} ); | ||
| 168 | } | ||
| 169 | |||
| 170 | return _make_result(LDAP_SUCCESS); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub _match { | ||
| 174 | my ( $filter_spec, $elems ) = @_; | ||
| 175 | |||
| 176 | my $f = bless $filter_spec, 'Net::LDAP::Filter'; | ||
| 177 | return [ grep { $f->match($_) } @{$elems} ]; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _encode_pw_attr { | ||
| 181 | my ( $pw_attr, $entry ) = @_; | ||
| 182 | |||
| 183 | return $entry unless grep { /person/ } $entry->get_value('objectclass'); | ||
| 184 | return $entry unless $entry->exists($pw_attr); | ||
| 185 | |||
| 186 | my $clone = $entry->clone(); | ||
| 187 | my @pwlist = (); | ||
| 188 | my $regexp = '^' . MD5_PREFIX; | ||
| 189 | foreach ( $clone->get_value($pw_attr) ) { | ||
| 190 | next if /$regexp/; | ||
| 191 | push @pwlist, _encode_password($_); | ||
| 192 | } | ||
| 193 | $clone->delete($pw_attr); | ||
| 194 | $clone->add( $pw_attr => [@pwlist] ); | ||
| 195 | return $clone; | ||
| 196 | |||
| 197 | } | ||
| 198 | |||
| 199 | sub _remove_pw_attr { | ||
| 200 | my ( $pw_attr, $entry ) = @_; | ||
| 201 | |||
| 202 | my $clone = $entry->clone(); | ||
| 203 | $clone->delete($pw_attr) if $clone->exists($pw_attr); | ||
| 204 | return $clone; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub _filter_attrs { | ||
| 208 | my ( $self, $list ) = @_; | ||
| 209 | |||
| 210 | # TODO find a better way to keep the store read-only but not costly to return searches with filtered attributes | ||
| 211 | return $list if $self->{user_passwords} eq USER_PW_ALL; | ||
| 212 | |||
| 213 | return [ map { _remove_pw_attr( $self->{user_pw_attr}, $_ ) } @{$list} ] | ||
| 214 | if $self->{user_passwords} eq USER_PW_NONE; | ||
| 215 | |||
| 216 | return [ map { _encode_pw_attr( $self->{user_pw_attr}, $_ ) } @{$list} ] | ||
| 217 | if $self->{user_passwords} eq USER_PW_MD5; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub search { | ||
| 221 | my ( $self, $request ) = @_; | ||
| 222 | |||
| 223 | my $list; | ||
| 224 | if ( defined( $request->{baseObject} ) ) { | ||
| 225 | my $basedn = canonical_dn( $request->{baseObject} ); | ||
| 226 | my $scope = $request->{scope} || SCOPE_SUBTREE; | ||
| 227 | |||
| 228 | $list = $self->{store}->list_with_dn_scope( $basedn, $scope ); | ||
| 229 | return _make_result( LDAP_NO_SUCH_OBJECT, '', | ||
| 230 | 'Cannot find BaseDN "' . $basedn . '"' ) | ||
| 231 | unless defined($list); | ||
| 232 | } | ||
| 233 | else { | ||
| 234 | $list = $self->{store}->list(); | ||
| 235 | } | ||
| 236 | |||
| 237 | my $match = $self->_filter_attrs( _match( $request->{filter}, $list ) ); | ||
| 238 | |||
| 239 | return ( _make_result(LDAP_SUCCESS), @{$match} ); | ||
| 240 | } | ||
| 241 | |||
| 242 | 1; # Magic true value required at end of module | ||
| 243 | |||
| 244 | __END__ |