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-',
35 open($log_fh, '>>', $config->{log_file}) || die "can't open ", $config->{log_file},": $!";
36 print $log_fh "# " . time;
38 $log_fh->autoflush(1);
39 print $log_fh join("\n", @_),"\n";
43 $SIG{'__WARN__'} = sub { warn @_; main::log(@_); }
47 if ( ! -d $config->{yaml_dir} ) {
48 warn "DISABLE ", $config->{yaml_dir}," data overlay";
51 warn "# config = ",dump( $config );
54 my $clientsocket=shift;
55 my $serversocket=shift;
58 asn_read($clientsocket, my $reqpdu);
61 return 1 unless $reqpdu;
64 print $serversocket $reqpdu or die "Could not send PDU to server\n ";
68 my $sel = IO::Select->new($serversocket);
69 for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) {
70 asn_read($serversocket, my $respdu) or return 1;
71 $respdu = log_response($respdu);
72 # and send the result to the client
73 print $clientsocket $respdu;
83 # print '-' x 80,"\n";
84 # print "Request ASN 1:\n";
85 # Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
86 # print "Request Perl:\n";
87 my $request = $LDAPRequest->decode($pdu);
88 warn "## request = ",dump($request);
94 # print '-' x 80,"\n";
95 # print "Response ASN 1:\n";
96 # Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
97 # print "Response Perl:\n";
98 my $response = $LDAPResponse->decode($pdu);
100 if ( defined $response->{protocolOp}->{searchResEntry} ) {
101 my $uid = $response->{protocolOp}->{searchResEntry}->{objectName};
102 warn "## SEARCH $uid";
107 if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) {
108 foreach my $val ( @{ $_->{vals} } ) {
109 next if $val !~ m{.+:.+};
110 my ( $n, $v ) = split(/\s*:\s*/, $val );
111 push @attrs, { type => $_->{type} . '_' . $n, vals => [ $v ] };
114 } @{ $response->{protocolOp}->{searchResEntry}->{attributes} };
116 warn "# ++ attrs ",dump( @attrs );
118 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs;
120 my $path = $config->{yaml_dir} . "$uid.yaml";
122 my $data = LoadFile($path);
123 warn "# yaml = ",dump($data);
125 foreach my $type ( keys %$data ) {
127 my $vals = $data->{$type};
129 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, {
130 type => $config->{overlay_prefix} . $type,
131 vals => ref($vals) eq 'ARRAY' ? $vals : [ $vals ],
136 $pdu = $LDAPResponse->encode($response);
139 warn "## response = ", dump($response);
145 my $listenersock = shift;
146 my $targetsock=shift;
148 die "Could not create listener socket: $!\n" unless $listenersock;
149 die "Could not create connection to server: $!\n" unless $targetsock;
151 my $sel = IO::Select->new($listenersock);
153 while (my @ready = $sel->can_read) {
154 foreach my $fh (@ready) {
155 if ($fh == $listenersock) {
156 # let's create a new socket
157 my $psock = $listenersock->accept;
160 my $result = handle($fh,$targetsock);
162 # we have finished with the socket
165 delete $Handlers{*$fh};
173 my $listenersock = IO::Socket::INET->new(
177 LocalAddr => $config->{listen},
178 ) || die "can't open listen socket: $!";
181 my $targetsock = $config->{upstream_ssl}
182 ? IO::Socket::INET->new(
184 PeerAddr => $config->{upstream_ldap},
187 : IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps')
188 || die "can't open upstream socket: $!";
190 run_proxy($listenersock,$targetsock);