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-',
34 open($log_fh, '>', $config->{log_file}) || die "can't open ", $config->{log_file},": $!";
35 $log_fh->autoflush(1);
36 print $log_fh join("\n", @_),"\n";
40 $SIG{'__WARN__'} = sub { warn @_; main::log(@_); }
44 if ( ! -d $config->{yaml_dir} ) {
45 warn "DISABLE ", $config->{yaml_dir}," data overlay";
48 warn "# config = ",dump( $config );
51 my $clientsocket=shift;
52 my $serversocket=shift;
55 asn_read($clientsocket, my $reqpdu);
58 return 1 unless $reqpdu;
61 print $serversocket $reqpdu or die "Could not send PDU to server\n ";
65 my $sel = IO::Select->new($serversocket);
66 for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) {
67 asn_read($serversocket, my $respdu) or return 1;
68 $respdu = log_response($respdu);
69 # and send the result to the client
70 print $clientsocket $respdu;
81 print "Request ASN 1:\n";
82 Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
83 print "Request Perl:\n";
84 my $request = $LDAPRequest->decode($pdu);
92 print "Response ASN 1:\n";
93 Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
94 print "Response Perl:\n";
95 my $response = $LDAPResponse->decode($pdu);
97 if ( defined $response->{protocolOp}->{searchResEntry} ) {
98 my $uid = $response->{protocolOp}->{searchResEntry}->{objectName};
99 warn "## SEARCH $uid";
104 if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) {
105 foreach my $val ( @{ $_->{vals} } ) {
106 next if $val !~ m{.+:.+};
107 my ( $n, $v ) = split(/\s*:\s*/, $val );
108 push @attrs, { type => $_->{type} . '_' . $n, vals => [ $v ] };
111 } @{ $response->{protocolOp}->{searchResEntry}->{attributes} };
113 warn "# ++ attrs ",dump( @attrs );
115 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs;
117 my $path = $config->{yaml_dir} . "$uid.yaml";
119 my $data = LoadFile($path);
120 warn "# yaml = ",dump($data);
122 foreach my $type ( keys %$data ) {
124 my $vals = $data->{$type};
126 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, {
127 type => $config->{overlay_prefix} . $type,
128 vals => ref($vals) eq 'ARRAY' ? $vals : [ $vals ],
133 $pdu = $LDAPResponse->encode($response);
136 print dump($response);
142 my $listenersock = shift;
143 my $targetsock=shift;
145 die "Could not create listener socket: $!\n" unless $listenersock;
146 die "Could not create connection to server: $!\n" unless $targetsock;
148 my $sel = IO::Select->new($listenersock);
150 while (my @ready = $sel->can_read) {
151 foreach my $fh (@ready) {
152 if ($fh == $listenersock) {
153 # let's create a new socket
154 my $psock = $listenersock->accept;
157 my $result = handle($fh,$targetsock);
159 # we have finished with the socket
162 delete $Handlers{*$fh};
170 $ENV{LANG} = 'C'; # so we don't double-encode utf-8 if LANG is utf-8
172 my $listenersock = IO::Socket::INET->new(
176 LocalAddr => $config->{listen},
180 my $targetsock = $config->{upstream_ssl}
181 ? IO::Socket::INET->new(
183 PeerAddr => $config->{upstream_ldap},
186 : IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps')
189 run_proxy($listenersock,$targetsock);