6 use Net::LDAP::Constant qw(
8 LDAP_STRONG_AUTH_NOT_SUPPORTED
12 use Net::LDAP::Server;
13 use Net::LDAP::Filter;
14 use base qw(Net::LDAP::Server);
15 use fields qw(upstream);
19 use URI::Escape; # uri_escape
23 use Data::Dump qw/dump/;
33 Provide LDAP server functionality somewhat similar to C<slapo-rwm>
39 my $pid = VLDAP::Server->run({ port => 1389, fork => 0 });
47 return $cache if $cache;
48 $cache = new A3C::Cache->new({ instance => '', dir => 'ldap' });
56 my $port = $args->{port} ||= 1389;
58 if ( $args->{fork} ) {
59 defined(my $pid = fork()) or die "Can't fork: $!";
61 $pids->{ $port } = $pid;
62 warn "# pids = ",dump( $pids );
68 my $sock = IO::Socket::INET->new(
73 ) or die "can't listen on port $port: $!\n";
75 warn "LDAP server listening on port $port\n";
77 my $sel = IO::Select->new($sock) or die "can't select socket: $!\n";
79 while (my @ready = $sel->can_read) {
80 foreach my $fh (@ready) {
82 # let's create a new socket
83 my $psock = $sock->accept;
85 $Handlers{*$psock} = VLDAP::Server->new($psock);
87 my $result = $Handlers{*$fh}->handle;
89 # we have finished with the socket
92 delete $Handlers{*$fh};
101 my $stopped_pids = VLDAP::Server->stop;
106 warn "## stop pids = ",dump( $pids );
109 foreach my $port ( keys %$pids ) {
110 my $pid = delete($pids->{$port}) or die "no pid?";
111 warn "# Shutdown LDAP server at port $port pid $pid\n";
112 kill(9,$pid) or die "can't kill $pid: $!";
113 waitpid($pid,0) or die "waitpid $pid: $!";
116 warn "## stopped $stopped processes\n";
120 use constant RESULT_OK => {
122 'errorMessage' => '',
123 'resultCode' => LDAP_SUCCESS
128 my ($class, $sock) = @_;
129 my $self = $class->SUPER::new($sock);
130 printf "Accepted connection from: %s\n", $sock->peerhost();
136 my ($self,$req) = @_;
138 warn "## bind req = ",dump($req);
140 defined($req->{authentication}->{simple}) or return {
143 resultCode => LDAP_STRONG_AUTH_NOT_SUPPORTED,
146 $self->{upstream} ||= Net::LDAP->new( 'ldaps://ldap.ffzg.hr/' ) or return {
149 resultCode => LDAP_UNAVAILABLE,
152 warn "## upstream = ",dump( $self->{upstream} );
153 warn "upstream not Net::LDAP but ",ref($self->{upstream}) unless ref($self->{upstream}) eq 'Net::LDAP';
157 # FIXME we would need to unbind because VLDAP binds us automatically, but that doesn't really work
158 #$msg = $self->{upstream}->unbind;
159 #warn "# unbind msg = ",dump( $msg );
162 $bind->{dn} = $req->{name} if $req->{name};
163 $bind->{password} = $req->{authentication}->{simple} if $req->{authentication}->{simple};
164 warn "# bind ",dump( $bind );
165 $msg = $self->{upstream}->bind( %$bind );
167 #warn "# bind msg = ",dump( $msg );
168 if ( $msg->code != LDAP_SUCCESS ) {
169 warn "ERROR: ", $msg->code, ": ", $msg->server_error, "\n";
172 errorMessage => $msg->server_error,
173 resultCode => $msg->code,
180 # the search operation
182 my ($self,$req) = @_;
184 warn "## search req = ",dump( $req );
186 if ( ! $self->{upstream} ) {
187 warn "search without bind";
190 errorMessage => 'dude, bind first',
191 resultCode => LDAP_OPERATIONS_ERROR,
196 if (defined $req->{filter}) {
197 # $req->{filter} is a ASN1-decoded tree; luckily, this is exactly the
198 # internal representation Net::LDAP::Filter uses. [FIXME] Eventually
199 # Net::LDAP::Filter should provide a corresponding constructor.
200 bless($req->{filter}, 'Net::LDAP::Filter');
201 $filter = $req->{filter}->as_string;
202 # $filter = '(&' . $req->{filter}->as_string
203 # . '(objectClass=hrEduPerson)(host=aai.irb.hr))';
206 warn "search upstream for $filter\n";
208 my $search = $self->{upstream}->search(
209 base => $req->{baseObject},
210 scope => $req->{scope},
211 deref => $req->{derefAliases},
212 sizelimit => $req->{sizeLimit},
213 timelimit => $req->{timeLimit},
214 typesonly => $req->{typesOnly},
216 attrs => $req->{attributes},
220 # warn "# search = ",dump( $search );
222 if ( $search->code != LDAP_SUCCESS ) {
223 warn "ERROR: ",$search->code,": ",$search->server_error;
226 errorMessage => $search->server_error,
227 resultCode => $search->code,
231 my @entries = $search->entries;
232 warn "## got ", $search->count, " entries for $filter\n";
233 foreach my $entry (@entries) {
234 # $entry->changetype('add'); # Don't record changes.
235 # foreach my $attr ($entry->attributes) {
236 # if ($attr =~ /;lang-en$/) {
237 # $entry->delete($attr);
242 warn "## entries = ",dump( @entries );
244 $self->cache->write_cache( \@entries, uri_escape( $filter ));
246 return RESULT_OK, @entries;
249 # the rest of the operations will return an "unwilling to perform"