a6eff6e7791e02ba877f671830ef4ea1e7df8837
[virtual-ldap] / bin / ldap-rewrite.pl
1 #!/usr/bin/perl
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.
5
6
7 use strict;
8 use warnings;
9
10 use IO::Select;
11 use IO::Socket;
12 use IO::Socket::SSL;
13 use warnings;
14 use Data::Dump qw/dump/;
15 use Convert::ASN1 qw(asn_read);
16 use Net::LDAP::ASN qw(LDAPRequest LDAPResponse);
17 our $VERSION = '0.2';
18 use fields qw(socket target);
19 use YAML qw/LoadFile/;
20
21 my $config = {
22         yaml_dir => './yaml/',
23         listen => 'localhost:1389',
24         upstream_ldap => 'ldap.ffzg.hr',
25         upstream_ssl => 1,
26         overlay_prefix => 'ffzg-',
27         log_file => 'log',
28
29 };
30
31 my $log_fh;
32
33 sub log {
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";
37 }
38
39 BEGIN {
40         $SIG{'__WARN__'} = sub { warn @_; main::log(@_); }
41 }
42
43
44 if ( ! -d $config->{yaml_dir} ) {
45         warn "DISABLE ", $config->{yaml_dir}," data overlay";
46 }
47
48 warn "# config = ",dump( $config );
49
50 sub handle {
51         my $clientsocket=shift;
52         my $serversocket=shift;
53
54         # read from client
55         asn_read($clientsocket, my $reqpdu);
56         log_request($reqpdu);
57
58         return 1 unless $reqpdu;
59
60         # send to server
61         print $serversocket $reqpdu or die "Could not send PDU to server\n ";
62         
63         # read from server
64         my $ready;
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;
71         }
72
73         return 0;
74 }
75
76
77 sub log_request {
78         my $pdu=shift;
79
80         print '-' x 80,"\n";
81         print "Request ASN 1:\n";
82         Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
83         print "Request Perl:\n";
84         my $request = $LDAPRequest->decode($pdu);
85         print dump($request);
86 }
87
88 sub log_response {
89         my $pdu=shift;
90
91         print '-' x 80,"\n";
92         print "Response ASN 1:\n";
93         Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
94         print "Response Perl:\n";
95         my $response = $LDAPResponse->decode($pdu);
96
97         if ( defined $response->{protocolOp}->{searchResEntry} ) {
98                 my $uid = $response->{protocolOp}->{searchResEntry}->{objectName};
99                 warn "## SEARCH $uid";
100
101                 my @attrs;
102
103                 map {
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 ] };
109                                 }
110                         }
111                 } @{ $response->{protocolOp}->{searchResEntry}->{attributes} };
112
113                 warn "# ++ attrs ",dump( @attrs );
114
115                 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs;
116
117                 my $path = $config->{yaml_dir} . "$uid.yaml";
118                 if ( -e $path ) {
119                         my $data = LoadFile($path);
120                         warn "# yaml = ",dump($data);
121
122                         foreach my $type ( keys %$data ) {
123
124                                 my $vals = $data->{$type};
125
126                                 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, {
127                                         type => $config->{overlay_prefix} . $type,
128                                         vals => ref($vals) eq 'ARRAY' ? $vals : [ $vals ],
129                                 };
130                         }
131                 }
132
133                 $pdu = $LDAPResponse->encode($response);
134         }
135
136         print dump($response);
137
138         return $pdu;
139 }
140
141 sub run_proxy {
142         my $listenersock = shift;
143         my $targetsock=shift;
144
145         die "Could not create listener socket: $!\n" unless $listenersock;
146         die "Could not create connection to server: $!\n" unless $targetsock;
147
148         my $sel = IO::Select->new($listenersock);
149         my %Handlers;
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;
155                                 $sel->add($psock);
156                         } else {
157                                 my $result = handle($fh,$targetsock);
158                                 if ($result) {
159                                         # we have finished with the socket
160                                         $sel->remove($fh);
161                                         $fh->close;
162                                         delete $Handlers{*$fh};
163                                 }
164                         }
165                 }
166         }
167 }
168
169
170 $ENV{LANG} = 'C'; # so we don't double-encode utf-8 if LANG is utf-8
171
172 my $listenersock = IO::Socket::INET->new(
173         Listen => 5,
174         Proto => 'tcp',
175         Reuse => 1,
176         LocalAddr => $config->{listen},
177 );
178
179
180 my $targetsock = $config->{upstream_ssl}
181         ? IO::Socket::INET->new(
182                 Proto => 'tcp',
183                 PeerAddr => $config->{upstream_ldap},
184                 PeerPort => 389,
185         )
186         : IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps')
187         ;
188
189 run_proxy($listenersock,$targetsock);
190
191 1;