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.
6 # It's modified by Dobrica Pavlinusic <dpavlin@rot13.org> to include following:
8 # * rewrite LDAP bind request cn: username@domain.com -> uid=username,dc=domain,dc=com
9 # * rewrite search responses:
10 # ** expand key:value pairs from hrEduPersonUniqueNumber into hrEduPersonUniqueNumber_key
11 # ** augment response with yaml/dn.yaml data (for external data import)
20 use Data::Dump qw/dump/;
21 use Convert::ASN1 qw(asn_read);
22 use Net::LDAP::ASN qw(LDAPRequest LDAPResponse);
24 use fields qw(socket target);
25 use YAML qw/LoadFile/;
27 my $debug = $ENV{DEBUG} || 0;
31 yaml_dir => './yaml/',
32 listen => shift @ARGV || 'localhost:1389',
33 upstream_ldap => 'ldap.ffzg.hr',
35 overlay_prefix => 'ffzg-',
36 # log_file => 'log/ldap-rewrite.log',
43 my $level = $1 if $_[0] =~ m/^(#+)/;
44 return if defined($level) && length($level) > $debug;
48 return unless $config->{log_file};
51 open($log_fh, '>>', $config->{log_file}) || die "can't open ", $config->{log_file},": $!";
52 print $log_fh "# " . time;
54 $log_fh->autoflush(1);
55 print $log_fh join("\n", @_),"\n";
59 $SIG{'__WARN__'} = sub { main::log(@_); }
63 if ( ! -d $config->{yaml_dir} ) {
64 warn "DISABLE ", $config->{yaml_dir}," data overlay";
67 warn "# config = ",dump( $config );
70 my $clientsocket=shift;
71 my $serversocket=shift;
74 asn_read($clientsocket, my $reqpdu);
76 warn "# client closed connection\n";
79 $reqpdu = log_request($reqpdu);
82 print $serversocket $reqpdu or die "Could not send PDU to server\n ";
86 my $sel = IO::Select->new($serversocket);
87 for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) {
88 asn_read($serversocket, my $respdu);
90 warn "server closed connection\n";
93 $respdu = log_response($respdu);
94 # and send the result to the client
95 print $clientsocket $respdu || return 0;
105 die "empty pdu" unless $pdu;
107 # print '-' x 80,"\n";
108 # print "Request ASN 1:\n";
109 # Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
110 # print "Request Perl:\n";
111 my $request = $LDAPRequest->decode($pdu);
112 warn "## request = ",dump($request);
114 if ( defined $request->{bindRequest} ) {
115 if ( $request->{bindRequest}->{name} =~ m{@} ) {
116 my $old = $request->{bindRequest}->{name};
117 $request->{bindRequest}->{name} =~ s/[@\.]/,dc=/g;
118 $request->{bindRequest}->{name} =~ s/^/uid=/;
119 print "rewrite bind cn $old -> ", $request->{bindRequest}->{name}, "\n";
120 Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug;
121 $pdu = $LDAPRequest->encode($request);
122 Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug;
131 die "empty pdu" unless $pdu;
133 # print '-' x 80,"\n";
134 # print "Response ASN 1:\n";
135 # Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
136 # print "Response Perl:\n";
137 my $response = $LDAPResponse->decode($pdu);
139 if ( defined $response->{protocolOp}->{searchResEntry} ) {
140 my $uid = $response->{protocolOp}->{searchResEntry}->{objectName};
141 warn "# rewrite objectName $uid\n";
145 foreach my $attr ( @{ $response->{protocolOp}->{searchResEntry}->{attributes} } ) {
146 if ( $attr->{type} =~ m/date/i ) {
147 foreach my $i ( 0 .. $#{ $attr->{vals} } ) {
148 $attr->{vals}->[$i] = "$1-$2-$3" if $attr->{vals}->[$i] =~ m/^([12]\d\d\d)([01]\d+)([0123]\d+)$/;
150 } elsif ( $attr->{type} eq 'hrEduPersonUniqueNumber' ) {
151 foreach my $val ( @{ $attr->{vals} } ) {
152 next if $val !~ m{.+:.+};
153 my ( $n, $v ) = split(/\s*:\s*/, $val );
154 push @attrs, { type => $attr->{type} . '_' . $n, vals => [ $v ] };
156 } elsif ( $attr->{type} eq 'hrEduPersonGroupMember' ) {
157 foreach my $i ( 0 .. $#{ $attr->{vals} } ) {
158 $attr->{vals}->[$i] =~ s/^u2010/p2010/gs && warn "FIXME group";
163 warn "# ++ attrs ",dump( @attrs );
165 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs;
167 my @additional_yamls = ( $uid );
168 foreach my $attr ( @{ $response->{protocolOp}->{searchResEntry}->{attributes} } ) {
169 foreach my $v ( @{ $attr->{vals} } ) {
170 push @additional_yamls, $attr->{type} . '/' . $v;
174 #warn "# additional_yamls ",dump( @additional_yamls );
176 foreach my $path ( @additional_yamls ) {
177 my $full_path = $config->{yaml_dir} . '/' . $path . '.yaml';
178 next unless -e $full_path;
180 my $data = LoadFile( $full_path );
181 warn "# $full_path yaml = ",dump($data);
183 foreach my $type ( keys %$data ) {
185 my $vals = $data->{$type};
187 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, {
188 type => $config->{overlay_prefix} . $type,
189 vals => ref($vals) eq 'ARRAY' ? $vals : [ $vals ],
194 $pdu = $LDAPResponse->encode($response);
197 warn "## response = ", dump($response);
203 my $listenersock = IO::Socket::INET->new(
207 LocalAddr => $config->{listen},
208 ) || die "can't open listen socket: $!";
212 sub connect_to_server {
214 if ( $config->{upstream_ssl} ) {
215 $sock = IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps' );
217 $sock = IO::Socket::INET->new(
219 PeerAddr => $config->{upstream_ldap},
223 die "can't open ", $config->{upstream_ldap}, " $!\n" unless $sock;
224 warn "## connected to ", $sock->peerhost, ":", $sock->peerport, "\n";
228 my $sel = IO::Select->new($listenersock);
229 while (my @ready = $sel->can_read) {
230 foreach my $fh (@ready) {
231 if ($fh == $listenersock) {
232 # let's create a new socket
233 my $psock = $listenersock->accept;
235 warn "## add $psock " . time;
237 $server_sock->{$fh} ||= connect_to_server;
238 if ( ! handle($fh,$server_sock->{$fh}) ) {
239 warn "## remove $fh " . time;
240 $sel->remove($server_sock->{$fh});
241 $server_sock->{$fh}->close;
242 delete $server_sock->{$fh};
243 # we have finished with the socket