1 |
dpavlin |
222 |
package A3C::LDAP::Server; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
dpavlin |
223 |
use Net::LDAP::Constant qw( |
7 |
|
|
LDAP_SUCCESS |
8 |
|
|
LDAP_STRONG_AUTH_NOT_SUPPORTED |
9 |
|
|
LDAP_UNAVAILABLE |
10 |
|
|
LDAP_OPERATIONS_ERROR |
11 |
|
|
); |
12 |
dpavlin |
222 |
use Net::LDAP::Server; |
13 |
dpavlin |
223 |
use Net::LDAP::Filter; |
14 |
dpavlin |
222 |
use base 'Net::LDAP::Server'; |
15 |
dpavlin |
223 |
use fields qw(upstream); |
16 |
dpavlin |
222 |
|
17 |
dpavlin |
223 |
use A3C::LDAP; |
18 |
|
|
use Data::Dump qw/dump/; |
19 |
|
|
|
20 |
dpavlin |
225 |
=head1 NAME |
21 |
|
|
|
22 |
|
|
A3C::LDAP::Server |
23 |
|
|
|
24 |
|
|
=cut |
25 |
|
|
|
26 |
|
|
=head1 DESCRIPTION |
27 |
|
|
|
28 |
|
|
Provide LDAP server functionality for L<A3C> somewhat similar to C<slapo-rwm> |
29 |
|
|
|
30 |
|
|
=cut |
31 |
|
|
|
32 |
dpavlin |
222 |
use constant RESULT_OK => { |
33 |
|
|
'matchedDN' => '', |
34 |
|
|
'errorMessage' => '', |
35 |
|
|
'resultCode' => LDAP_SUCCESS |
36 |
|
|
}; |
37 |
|
|
|
38 |
|
|
# constructor |
39 |
|
|
sub new { |
40 |
|
|
my ($class, $sock) = @_; |
41 |
|
|
my $self = $class->SUPER::new($sock); |
42 |
|
|
printf "Accepted connection from: %s\n", $sock->peerhost(); |
43 |
|
|
return $self; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
# the bind operation |
47 |
|
|
sub bind { |
48 |
dpavlin |
223 |
my ($self,$req) = @_; |
49 |
|
|
|
50 |
|
|
warn "## bind req = ",dump($req); |
51 |
|
|
|
52 |
|
|
defined($req->{authentication}->{simple}) or return { |
53 |
|
|
matchedDN => '', |
54 |
|
|
errorMessage => '', |
55 |
|
|
resultCode => LDAP_STRONG_AUTH_NOT_SUPPORTED, |
56 |
|
|
}; |
57 |
|
|
|
58 |
|
|
$self->{upstream} ||= A3C::LDAP->new->ldap or return { |
59 |
|
|
matchedDN => '', |
60 |
|
|
errorMessage => $@, |
61 |
|
|
resultCode => LDAP_UNAVAILABLE, |
62 |
|
|
}; |
63 |
|
|
|
64 |
dpavlin |
224 |
# warn "## upstream = ",dump( $self->{upstream} ); |
65 |
dpavlin |
223 |
# warn "upstream not Net::LDAP but ",ref($self->{upstream}) unless ref($self->{upstream}) eq 'Net::LDAP'; |
66 |
|
|
|
67 |
dpavlin |
222 |
return RESULT_OK; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
# the search operation |
71 |
|
|
sub search { |
72 |
dpavlin |
223 |
my ($self,$req) = @_; |
73 |
dpavlin |
222 |
|
74 |
dpavlin |
223 |
warn "## search req = ",dump( $req ); |
75 |
|
|
|
76 |
|
|
if ( ! $self->{upstream} ) { |
77 |
|
|
warn "search without bind"; |
78 |
|
|
return { |
79 |
|
|
matchedDN => '', |
80 |
|
|
errorMessage => 'dude, bind first', |
81 |
|
|
resultCode => LDAP_OPERATIONS_ERROR, |
82 |
|
|
}; |
83 |
dpavlin |
222 |
} |
84 |
dpavlin |
223 |
|
85 |
|
|
my $filter; |
86 |
|
|
if (defined $req->{filter}) { |
87 |
|
|
# $req->{filter} is a ASN1-decoded tree; luckily, this is exactly the |
88 |
|
|
# internal representation Net::LDAP::Filter uses. [FIXME] Eventually |
89 |
|
|
# Net::LDAP::Filter should provide a corresponding constructor. |
90 |
|
|
bless($req->{filter}, 'Net::LDAP::Filter'); |
91 |
|
|
$filter = $req->{filter}->as_string; |
92 |
|
|
# $filter = '(&' . $req->{filter}->as_string |
93 |
|
|
# . '(objectClass=hrEduPerson)(host=aai.irb.hr))'; |
94 |
|
|
} |
95 |
|
|
|
96 |
dpavlin |
224 |
warn "search upstream for $filter\n"; |
97 |
dpavlin |
223 |
|
98 |
|
|
my $search = $self->{upstream}->search( |
99 |
|
|
base => $req->{baseObject}, |
100 |
|
|
scope => $req->{scope}, |
101 |
|
|
deref => $req->{derefAliases}, |
102 |
|
|
sizelimit => $req->{sizeLimit}, |
103 |
|
|
timelimit => $req->{timeLimit}, |
104 |
|
|
typesonly => $req->{typesOnly}, |
105 |
|
|
filter => $filter, |
106 |
|
|
attrs => $req->{attributes}, |
107 |
|
|
raw => qr/.*/, |
108 |
|
|
); |
109 |
|
|
|
110 |
dpavlin |
224 |
# warn "# search = ",dump( $search ); |
111 |
dpavlin |
223 |
|
112 |
|
|
if ( $search->code != LDAP_SUCCESS ) { |
113 |
|
|
warn "ERROR: ",$search->code,": ",$search->server_error; |
114 |
|
|
return { |
115 |
|
|
matchedDN => '', |
116 |
|
|
errorMessage => $search->server_error, |
117 |
|
|
resultCode => $search->code, |
118 |
|
|
}; |
119 |
|
|
}; |
120 |
|
|
|
121 |
|
|
my @entries = $search->entries; |
122 |
dpavlin |
224 |
warn "## got ", $search->count, " entries for $filter\n"; |
123 |
dpavlin |
223 |
foreach my $entry (@entries) { |
124 |
dpavlin |
225 |
# $entry->changetype('add'); # Don't record changes. |
125 |
dpavlin |
223 |
# foreach my $attr ($entry->attributes) { |
126 |
|
|
# if ($attr =~ /;lang-en$/) { |
127 |
|
|
# $entry->delete($attr); |
128 |
|
|
# } |
129 |
|
|
# } |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
warn "## entries = ",dump( @entries ); |
133 |
dpavlin |
222 |
return RESULT_OK, @entries; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
# the rest of the operations will return an "unwilling to perform" |
137 |
|
|
|
138 |
|
|
1; |