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 = LDAP::Virtual->run({ port => 1389, fork => 0 });
55 my $port = $args->{port} ||= 1389;
57 if ( $args->{fork} ) {
58 defined(my $pid = fork()) or die "Can't fork: $!";
60 $pids->{ $port } = $pid;
61 warn "# pids = ",dump( $pids );
67 my $sock = IO::Socket::INET->new(
72 ) or die "can't listen on port $port: $!\n";
74 warn "LDAP server listening on port $port\n";
76 my $sel = IO::Select->new($sock) or die "can't select socket: $!\n";
78 while (my @ready = $sel->can_read) {
79 foreach my $fh (@ready) {
81 # let's create a new socket
82 my $psock = $sock->accept;
84 $Handlers{*$psock} = LDAP::Virtual->new($psock);
86 my $result = $Handlers{*$fh}->handle;
88 # we have finished with the socket
91 delete $Handlers{*$fh};
100 my $stopped_pids = LDAP::Virtual->stop;
105 warn "## stop pids = ",dump( $pids );
108 foreach my $port ( keys %$pids ) {
109 my $pid = delete($pids->{$port}) or die "no pid?";
110 warn "# Shutdown LDAP server at port $port pid $pid\n";
111 kill(9,$pid) or die "can't kill $pid: $!";
112 waitpid($pid,0) or die "waitpid $pid: $!";
115 warn "## stopped $stopped processes\n";
119 use constant RESULT_OK => {
121 'errorMessage' => '',
122 'resultCode' => LDAP_SUCCESS
127 my ($class, $sock) = @_;
128 my $self = $class->SUPER::new($sock);
129 printf "Accepted connection from: %s\n", $sock->peerhost();
135 my ($self,$req) = @_;
137 warn "## bind req = ",dump($req);
139 defined($req->{authentication}->{simple}) or return {
142 resultCode => LDAP_STRONG_AUTH_NOT_SUPPORTED,
145 $self->{upstream} ||= Net::LDAP->new( 'ldaps://ldap.ffzg.hr/' ) or return {
148 resultCode => LDAP_UNAVAILABLE,
151 warn "## upstream = ",dump( $self->{upstream} );
152 warn "upstream not Net::LDAP but ",ref($self->{upstream}) unless ref($self->{upstream}) eq 'Net::LDAP';
156 # FIXME we would need to unbind because VLDAP binds us automatically, but that doesn't really work
157 #$msg = $self->{upstream}->unbind;
158 #warn "# unbind msg = ",dump( $msg );
161 $bind->{dn} = $req->{name} if $req->{name};
162 $bind->{password} = $req->{authentication}->{simple} if $req->{authentication}->{simple};
163 warn "# bind ",dump( $bind );
164 $msg = $self->{upstream}->bind( %$bind );
166 #warn "# bind msg = ",dump( $msg );
167 if ( $msg->code != LDAP_SUCCESS ) {
168 warn "ERROR: ", $msg->code, ": ", $msg->server_error, "\n";
171 errorMessage => $msg->server_error,
172 resultCode => $msg->code,
179 # the search operation
181 my ($self,$req) = @_;
183 warn "## search req = ",dump( $req );
185 if ( ! $self->{upstream} ) {
186 warn "search without bind";
189 errorMessage => 'dude, bind first',
190 resultCode => LDAP_OPERATIONS_ERROR,
195 if (defined $req->{filter}) {
196 # $req->{filter} is a ASN1-decoded tree; luckily, this is exactly the
197 # internal representation Net::LDAP::Filter uses. [FIXME] Eventually
198 # Net::LDAP::Filter should provide a corresponding constructor.
199 bless($req->{filter}, 'Net::LDAP::Filter');
200 $filter = $req->{filter}->as_string;
201 # $filter = '(&' . $req->{filter}->as_string
202 # . '(objectClass=hrEduPerson)(host=aai.irb.hr))';
205 warn "search upstream for $filter\n";
207 my $search = $self->{upstream}->search(
208 base => $req->{baseObject},
209 scope => $req->{scope},
210 deref => $req->{derefAliases},
211 sizelimit => $req->{sizeLimit},
212 timelimit => $req->{timeLimit},
213 typesonly => $req->{typesOnly},
215 attrs => $req->{attributes},
219 # warn "# search = ",dump( $search );
221 if ( $search->code != LDAP_SUCCESS ) {
222 warn "ERROR: ",$search->code,": ",$search->server_error;
225 errorMessage => $search->server_error,
226 resultCode => $search->code,
230 my @entries = $search->entries;
231 warn "## got ", $search->count, " entries for $filter\n";
232 foreach my $entry (@entries) {
233 # $entry->changetype('add'); # Don't record changes.
234 # foreach my $attr ($entry->attributes) {
235 # if ($attr =~ /;lang-en$/) {
236 # $entry->delete($attr);
241 warn "## entries = ",dump( @entries );
243 # $self->cache->write_cache( \@entries, uri_escape( $filter ));
245 return RESULT_OK, @entries;
248 # the rest of the operations will return an "unwilling to perform"