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 });
52 my $port = $args->{port} ||= 1389;
54 if ( $args->{fork} ) {
55 defined(my $pid = fork()) or die "Can't fork: $!";
57 $pids->{ $port } = $pid;
58 warn "# pids = ",dump( $pids );
64 my $sock = IO::Socket::INET->new(
69 ) or die "can't listen on port $port: $!\n";
71 warn "LDAP server listening on port $port\n";
73 my $sel = IO::Select->new($sock) or die "can't select socket: $!\n";
75 while (my @ready = $sel->can_read) {
76 foreach my $fh (@ready) {
78 # let's create a new socket
79 my $psock = $sock->accept;
81 $Handlers{*$psock} = LDAP::Virtual->new($psock);
83 my $result = $Handlers{*$fh}->handle;
85 # we have finished with the socket
88 delete $Handlers{*$fh};
97 my $stopped_pids = LDAP::Virtual->stop;
102 warn "## stop pids = ",dump( $pids );
105 foreach my $port ( keys %$pids ) {
106 my $pid = delete($pids->{$port}) or die "no pid?";
107 warn "# Shutdown LDAP server at port $port pid $pid\n";
108 kill(9,$pid) or die "can't kill $pid: $!";
109 waitpid($pid,0) or die "waitpid $pid: $!";
112 warn "## stopped $stopped processes\n";
116 use constant RESULT_OK => {
118 'errorMessage' => '',
119 'resultCode' => LDAP_SUCCESS
124 my ($class, $sock) = @_;
125 my $self = $class->SUPER::new($sock);
126 printf "Accepted connection from: %s\n", $sock->peerhost();
132 my ($self,$req) = @_;
134 warn "## bind req = ",dump($req);
136 defined($req->{authentication}->{simple}) or return {
139 resultCode => LDAP_STRONG_AUTH_NOT_SUPPORTED,
142 $self->{upstream} ||= Net::LDAP->new( 'ldaps://ldap.ffzg.hr/' ) or return {
145 resultCode => LDAP_UNAVAILABLE,
148 warn "## upstream = ",dump( $self->{upstream} );
149 warn "upstream not Net::LDAP but ",ref($self->{upstream}) unless ref($self->{upstream}) eq 'Net::LDAP';
153 # FIXME we would need to unbind because VLDAP binds us automatically, but that doesn't really work
154 #$msg = $self->{upstream}->unbind;
155 #warn "# unbind msg = ",dump( $msg );
158 $bind->{dn} = $req->{name} if $req->{name};
159 $bind->{password} = $req->{authentication}->{simple} if $req->{authentication}->{simple};
160 warn "# bind ",dump( $bind );
161 $msg = $self->{upstream}->bind( %$bind );
163 #warn "# bind msg = ",dump( $msg );
164 if ( $msg->code != LDAP_SUCCESS ) {
165 warn "ERROR: ", $msg->code, ": ", $msg->server_error, "\n";
168 errorMessage => $msg->server_error,
169 resultCode => $msg->code,
176 # the search operation
178 my ($self,$req) = @_;
180 warn "## search req = ",dump( $req );
182 if ( ! $self->{upstream} ) {
183 warn "search without bind";
186 errorMessage => 'dude, bind first',
187 resultCode => LDAP_OPERATIONS_ERROR,
192 if (defined $req->{filter}) {
193 # $req->{filter} is a ASN1-decoded tree; luckily, this is exactly the
194 # internal representation Net::LDAP::Filter uses. [FIXME] Eventually
195 # Net::LDAP::Filter should provide a corresponding constructor.
196 bless($req->{filter}, 'Net::LDAP::Filter');
197 $filter = $req->{filter}->as_string;
198 # $filter = '(&' . $req->{filter}->as_string
199 # . '(objectClass=hrEduPerson)(host=aai.irb.hr))';
202 warn "search upstream for $filter\n";
204 my $search = $self->{upstream}->search(
205 base => $req->{baseObject},
206 scope => $req->{scope},
207 deref => $req->{derefAliases},
208 sizelimit => $req->{sizeLimit},
209 timelimit => $req->{timeLimit},
210 typesonly => $req->{typesOnly},
212 attrs => $req->{attributes},
216 # warn "# search = ",dump( $search );
218 if ( $search->code != LDAP_SUCCESS ) {
219 warn "ERROR: ",$search->code,": ",$search->server_error;
222 errorMessage => $search->server_error,
223 resultCode => $search->code,
227 my @entries = $search->entries;
228 warn "## got ", $search->count, " entries for $filter\n";
229 foreach my $entry (@entries) {
230 # $entry->changetype('add'); # Don't record changes.
231 # foreach my $attr ($entry->attributes) {
232 # if ($attr =~ /;lang-en$/) {
233 # $entry->delete($attr);
238 warn "## entries = ",dump( @entries );
240 my $path = 'var/' . uri_escape( $filter ) . '.yml';
241 DumpFile( $path, \@entries );
242 warn "# created $path ", -s $path, " bytes";
244 return RESULT_OK, @entries;
247 # the rest of the operations will return an "unwilling to perform"