2 # Copyright (c) 2006 Hans Klunder <hans.klunder@bigfoot.com>. All rights reserved.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the same terms as Perl itself.
14 use Data::Dump qw/dump/;
15 use Convert::ASN1 qw(asn_read);
16 use Net::LDAP::ASN qw(LDAPRequest LDAPResponse);
18 use fields qw(socket target);
19 use YAML qw/LoadFile/;
22 yaml_dir => './yaml/',
23 listen => 'localhost:1389',
24 upstream_ldap => 'ldap.ffzg.hr',
26 overlay_prefix => 'ffzg-',
30 if ( ! -d $config->{yaml_dir} ) {
31 warn "DISABLE ", $config->{yaml_dir}," data overlay";
34 warn "# config = ",dump( $config );
37 my $clientsocket=shift;
38 my $serversocket=shift;
41 asn_read($clientsocket, my $reqpdu);
44 return 1 unless $reqpdu;
47 print $serversocket $reqpdu or die "Could not send PDU to server\n ";
51 my $sel = IO::Select->new($serversocket);
52 for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) {
53 asn_read($serversocket, my $respdu) or return 1;
54 $respdu = log_response($respdu);
55 # and send the result to the client
56 print $clientsocket $respdu;
67 print "Request ASN 1:\n";
68 Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
69 print "Request Perl:\n";
70 my $request = $LDAPRequest->decode($pdu);
78 print "Response ASN 1:\n";
79 Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
80 print "Response Perl:\n";
81 my $response = $LDAPResponse->decode($pdu);
83 if ( defined $response->{protocolOp}->{searchResEntry} ) {
84 my $uid = $response->{protocolOp}->{searchResEntry}->{objectName};
85 warn "## SEARCH $uid";
90 if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) {
91 foreach my $val ( @{ $_->{vals} } ) {
92 next if $val !~ m{.+:.+};
93 my ( $n, $v ) = split(/\s*:\s*/, $val );
94 push @attrs, { type => $_->{type} . '_' . $n, vals => [ $v ] };
97 } @{ $response->{protocolOp}->{searchResEntry}->{attributes} };
99 warn "# ++ attrs ",dump( @attrs );
101 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs;
103 my $path = $config->{yaml_dir} . "$uid.yaml";
105 my $data = LoadFile($path);
106 warn "# yaml = ",dump($data);
108 foreach my $type ( keys %$data ) {
110 my $vals = $data->{$type};
113 my @vals = split(/\s*#\s*/, $vals);
115 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} },
116 { type => $config->{overlay_prefix} . $type, vals => [ @vals ] };
120 $pdu = $LDAPResponse->encode($response);
123 print dump($response);
129 my $listenersock = shift;
130 my $targetsock=shift;
132 die "Could not create listener socket: $!\n" unless $listenersock;
133 die "Could not create connection to server: $!\n" unless $targetsock;
135 my $sel = IO::Select->new($listenersock);
137 while (my @ready = $sel->can_read) {
138 foreach my $fh (@ready) {
139 if ($fh == $listenersock) {
140 # let's create a new socket
141 my $psock = $listenersock->accept;
144 my $result = handle($fh,$targetsock);
146 # we have finished with the socket
149 delete $Handlers{*$fh};
157 my $listenersock = IO::Socket::INET->new(
161 LocalAddr => $config->{listen},
165 my $targetsock = $config->{upstream_ssl}
166 ? IO::Socket::INET->new(
168 PeerAddr => $config->{upstream_ldap},
171 : IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps')
174 run_proxy($listenersock,$targetsock);