| 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.19'; # VERSION | ||
| 9 | |||
| 10 | use Net::LDAP::Server; | ||
| 11 | use base 'Net::LDAP::Server'; | ||
| 12 | use fields qw(store root_dn root_pw allow_anon); | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use Net::LDAP::LDIF; | ||
| 16 | use Net::LDAP::Util qw{canonical_dn}; | ||
| 17 | use Net::LDAP::Filter; | ||
| 18 | use Net::LDAP::FilterMatch; | ||
| 19 | |||
| 20 | use Net::LDAP::Constant qw/ | ||
| 21 | LDAP_SUCCESS LDAP_INVALID_CREDENTIALS LDAP_AUTH_METHOD_NOT_SUPPORTED | ||
| 22 | LDAP_INVALID_SYNTAX LDAP_NO_SUCH_OBJECT/; | ||
| 23 | |||
| 24 | use Net::LDAP::SimpleServer::LDIFStore; | ||
| 25 | use Net::LDAP::SimpleServer::Constant; | ||
| 26 | |||
| 27 | use Scalar::Util qw{reftype}; | ||
| 28 | use UNIVERSAL::isa; | ||
| 29 | |||
| 30 | sub _make_result { | ||
| 31 | my $code = shift; | ||
| 32 | my $dn = shift // ''; | ||
| 33 | my $msg = shift // ''; | ||
| 34 | |||
| 35 | return { | ||
| 36 | matchedDN => $dn, | ||
| 37 | errorMessage => $msg, | ||
| 38 | resultCode => $code, | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my $class = shift; | ||
| 44 | my $params = shift || croak 'Must pass parameters!'; | ||
| 45 | |||
| 46 | croak 'Parameter must be a HASHREF' unless reftype($params) eq 'HASH'; | ||
| 47 | for my $p (qw/store root_dn sock/) { | ||
| 48 | croak 'Must pass option {' . $p . '}' unless exists $params->{$p}; | ||
| 49 | } | ||
| 50 | croak 'Not a LDIFStore' | ||
| 51 | unless $params->{store}->isa('Net::LDAP::SimpleServer::LDIFStore'); | ||
| 52 | |||
| 53 | croak 'Option {root_dn} can not be empty' unless $params->{root_dn}; | ||
| 54 | croak 'Invalid root DN' | ||
| 55 | unless my $canon_dn = canonical_dn( $params->{root_dn} ); | ||
| 56 | |||
| 57 | my $self = $class->SUPER::new( $params->{sock} ); | ||
| 58 | $self->{store} = $params->{store}; | ||
| 59 | $self->{root_dn} = $canon_dn; | ||
| 60 | $self->{root_pw} = $params->{root_pw}; | ||
| 61 | $self->{allow_anon} = $params->{allow_anon}; | ||
| 62 | chomp( $self->{root_pw} ); | ||
| 63 | |||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub unbind { | ||
| 68 | my $self = shift; | ||
| 69 | |||
| 70 | $self->{store} = undef; | ||
| 71 | $self->{root_dn} = undef; | ||
| 72 | $self->{root_pw} = undef; | ||
| 73 | |||
| 74 | return _make_result(LDAP_SUCCESS); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub bind { | ||
| 78 | ## no critic (ProhibitBuiltinHomonyms) | ||
| 79 | my ( $self, $request ) = @_; | ||
| 80 | |||
| 81 | my $OK = _make_result(LDAP_SUCCESS); | ||
| 82 | |||
| 83 | # anonymous bind | ||
| 84 | if ( not $request->{name} | ||
| 85 | and exists $request->{authentication}->{simple} | ||
| 86 | and $self->{allow_anon} ) | ||
| 87 | { | ||
| 88 | return $OK; | ||
| 89 | } | ||
| 90 | |||
| 91 | # As of now, accepts only simple authentication | ||
| 92 | return _make_result(LDAP_AUTH_METHOD_NOT_SUPPORTED) | ||
| 93 | unless exists $request->{authentication}->{simple}; | ||
| 94 | |||
| 95 | return _make_result(LDAP_INVALID_CREDENTIALS) | ||
| 96 | unless my $binddn = canonical_dn( $request->{name} ); | ||
| 97 | |||
| 98 | return _make_result(LDAP_INVALID_CREDENTIALS) | ||
| 99 | unless uc($binddn) eq uc( $self->{root_dn} ); | ||
| 100 | |||
| 101 | my $bindpw = $request->{authentication}->{simple}; | ||
| 102 | chomp($bindpw); | ||
| 103 | |||
| 104 | return _make_result(LDAP_INVALID_CREDENTIALS) | ||
| 105 | unless $bindpw eq $self->{root_pw}; | ||
| 106 | |||
| 107 | return $OK; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _match { | ||
| 111 | my ( $filter_spec, $elems ) = @_; | ||
| 112 | |||
| 113 | my $f = bless $filter_spec, 'Net::LDAP::Filter'; | ||
| 114 | return [ grep { $f->match($_) } @{$elems} ]; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub search { | ||
| 118 | my ( $self, $request ) = @_; | ||
| 119 | |||
| 120 | my $list; | ||
| 121 | if ( defined( $request->{baseObject} ) ) { | ||
| 122 | my $basedn = canonical_dn( $request->{baseObject} ); | ||
| 123 | my $scope = $request->{scope} || SCOPE_SUBTREE; | ||
| 124 | |||
| 125 | $list = $self->{store}->list_with_dn_scope( $basedn, $scope ); | ||
| 126 | return _make_result( LDAP_NO_SUCH_OBJECT, '', | ||
| 127 | 'Cannot find BaseDN "' . $basedn . '"' ) | ||
| 128 | unless defined($list); | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | $list = $self->{store}->list(); | ||
| 132 | } | ||
| 133 | |||
| 134 | my $match = _match( $request->{filter}, $list ); | ||
| 135 | return ( _make_result(LDAP_SUCCESS), @{$match} ); | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; # Magic true value required at end of module | ||
| 139 | |||
| 140 | __END__ |