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 YAML qw/DumpFile/;
25 use Data::Dump qw/dump/;
35 Provide LDAP server functionality somewhat similar to C<slapo-rwm>
41 my $pid = LDAP::Virtual->run({ port => 1389, fork => 0 });
57 my $port = $args->{port} ||= 1389;
59 if ( $args->{fork} ) {
60 defined(my $pid = fork()) or die "Can't fork: $!";
62 $pids->{ $port } = $pid;
63 warn "# pids = ",dump( $pids );
69 my $sock = IO::Socket::INET->new(
74 ) or die "can't listen on port $port: $!\n";
76 warn "LDAP server listening on port $port\n";
78 my $sel = IO::Select->new($sock) or die "can't select socket: $!\n";
80 while (my @ready = $sel->can_read) {
81 foreach my $fh (@ready) {
83 # let's create a new socket
84 my $psock = $sock->accept;
86 $Handlers{*$psock} = LDAP::Virtual->new($psock);
88 my $result = $Handlers{*$fh}->handle;
90 # we have finished with the socket
93 delete $Handlers{*$fh};
102 my $stopped_pids = LDAP::Virtual->stop;
107 warn "## stop pids = ",dump( $pids );
110 foreach my $port ( keys %$pids ) {
111 my $pid = delete($pids->{$port}) or die "no pid?";
112 warn "# Shutdown LDAP server at port $port pid $pid\n";
113 kill(9,$pid) or die "can't kill $pid: $!";
114 waitpid($pid,0) or die "waitpid $pid: $!";
117 warn "## stopped $stopped processes\n";
121 use constant RESULT_OK => {
123 'errorMessage' => '',
124 'resultCode' => LDAP_SUCCESS
129 my ($class, $sock) = @_;
130 my $self = $class->SUPER::new($sock);
131 printf "Accepted connection from: %s\n", $sock->peerhost();
137 my ($self,$req) = @_;
139 warn "## bind req = ",dump($req);
141 defined($req->{authentication}->{simple}) or return {
144 resultCode => LDAP_STRONG_AUTH_NOT_SUPPORTED,
147 $self->{upstream} ||= Net::LDAP->new( 'ldaps://ldap.ffzg.hr/' ) or return {
150 resultCode => LDAP_UNAVAILABLE,
153 warn "## upstream = ",dump( $self->{upstream} );
154 warn "upstream not Net::LDAP but ",ref($self->{upstream}) unless ref($self->{upstream}) eq 'Net::LDAP';
158 # FIXME we would need to unbind because VLDAP binds us automatically, but that doesn't really work
159 #$msg = $self->{upstream}->unbind;
160 #warn "# unbind msg = ",dump( $msg );
163 $bind->{dn} = $req->{name} if $req->{name};
164 $bind->{password} = $req->{authentication}->{simple} if $req->{authentication}->{simple};
165 warn "# bind ",dump( $bind );
166 $msg = $self->{upstream}->bind( %$bind );
168 #warn "# bind msg = ",dump( $msg );
169 if ( $msg->code != LDAP_SUCCESS ) {
170 warn "ERROR: ", $msg->code, ": ", $msg->server_error, "\n";
173 errorMessage => $msg->server_error,
174 resultCode => $msg->code,
181 # the search operation
183 my ($self,$req) = @_;
185 warn "## search req = ",dump( $req );
187 if ( ! $self->{upstream} ) {
188 warn "search without bind";
191 errorMessage => 'dude, bind first',
192 resultCode => LDAP_OPERATIONS_ERROR,
197 if (defined $req->{filter}) {
198 # $req->{filter} is a ASN1-decoded tree; luckily, this is exactly the
199 # internal representation Net::LDAP::Filter uses. [FIXME] Eventually
200 # Net::LDAP::Filter should provide a corresponding constructor.
201 bless($req->{filter}, 'Net::LDAP::Filter');
202 $filter = $req->{filter}->as_string;
203 # $filter = '(&' . $req->{filter}->as_string
204 # . '(objectClass=hrEduPerson)(host=aai.irb.hr))';
207 warn "search upstream for $filter\n";
209 my $search = $self->{upstream}->search(
210 base => $req->{baseObject},
211 scope => $req->{scope},
212 deref => $req->{derefAliases},
213 sizelimit => $req->{sizeLimit},
214 timelimit => $req->{timeLimit},
215 typesonly => $req->{typesOnly},
217 attrs => $req->{attributes},
221 # warn "# search = ",dump( $search );
223 if ( $search->code != LDAP_SUCCESS ) {
224 warn "ERROR: ",$search->code,": ",$search->server_error;
227 errorMessage => $search->server_error,
228 resultCode => $search->code,
232 my @entries = $search->entries;
233 warn "## got ", $search->count, " entries for $filter\n";
234 foreach my $entry (@entries) {
235 # $entry->changetype('add'); # Don't record changes.
236 # foreach my $attr ($entry->attributes) {
237 # if ($attr =~ /;lang-en$/) {
238 # $entry->delete($attr);
243 warn "## entries = ",dump( @entries );
245 # $self->cache->write_cache( \@entries, uri_escape( $filter ));
247 my $path = uri_escape( $filter );
248 DumpFile( "var/${path}.yml", \@entries );
249 warn "# created $path ", -s $path, " bytes";
251 return RESULT_OK, @entries;
254 # the rest of the operations will return an "unwilling to perform"