3 |
# This program is free software; you can redistribute it and/or |
# This program is free software; you can redistribute it and/or |
4 |
# modify it under the same terms as Perl itself. |
# modify it under the same terms as Perl itself. |
5 |
|
|
6 |
|
# It's modified by Dobrica Pavlinusic <dpavlin@rot13.org> to include following: |
7 |
|
# |
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) |
12 |
|
|
13 |
use strict; |
use strict; |
14 |
use warnings; |
use warnings; |
20 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
21 |
use Convert::ASN1 qw(asn_read); |
use Convert::ASN1 qw(asn_read); |
22 |
use Net::LDAP::ASN qw(LDAPRequest LDAPResponse); |
use Net::LDAP::ASN qw(LDAPRequest LDAPResponse); |
23 |
our $VERSION = '0.2'; |
our $VERSION = '0.3'; |
24 |
use fields qw(socket target); |
use fields qw(socket target); |
25 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
26 |
|
|
27 |
|
my $debug = 0; |
28 |
|
|
29 |
my $config = { |
my $config = { |
30 |
yaml_dir => './yaml/', |
yaml_dir => './yaml/', |
31 |
listen => 'localhost:1389', |
listen => shift @ARGV || 'localhost:1389', |
32 |
upstream_ldap => 'ldap.ffzg.hr', |
upstream_ldap => 'ldap.ffzg.hr', |
33 |
upstream_ssl => 1, |
upstream_ssl => 1, |
34 |
overlay_prefix => 'ffzg-', |
overlay_prefix => 'ffzg-', |
35 |
|
# log_file => 'log/ldap-rewrite.log', |
36 |
|
|
37 |
}; |
}; |
38 |
|
|
39 |
|
my $log_fh; |
40 |
|
|
41 |
|
sub log { |
42 |
|
return unless $config->{log_file}; |
43 |
|
|
44 |
|
if ( ! $log_fh ) { |
45 |
|
open($log_fh, '>>', $config->{log_file}) || die "can't open ", $config->{log_file},": $!"; |
46 |
|
print $log_fh "# " . time; |
47 |
|
} |
48 |
|
$log_fh->autoflush(1); |
49 |
|
print $log_fh join("\n", @_),"\n"; |
50 |
|
} |
51 |
|
|
52 |
|
BEGIN { |
53 |
|
$SIG{'__WARN__'} = sub { warn @_; main::log(@_); } |
54 |
|
} |
55 |
|
|
56 |
|
|
57 |
if ( ! -d $config->{yaml_dir} ) { |
if ( ! -d $config->{yaml_dir} ) { |
58 |
warn "DISABLE ", $config->{yaml_dir}," data overlay"; |
warn "DISABLE ", $config->{yaml_dir}," data overlay"; |
59 |
} |
} |
66 |
|
|
67 |
# read from client |
# read from client |
68 |
asn_read($clientsocket, my $reqpdu); |
asn_read($clientsocket, my $reqpdu); |
69 |
log_request($reqpdu); |
if ( ! $reqpdu ) { |
70 |
|
warn "client closed connection\n"; |
71 |
return 1 unless $reqpdu; |
return 0; |
72 |
|
} |
73 |
|
$reqpdu = log_request($reqpdu); |
74 |
|
|
75 |
# send to server |
# send to server |
76 |
print $serversocket $reqpdu or die "Could not send PDU to server\n "; |
print $serversocket $reqpdu or die "Could not send PDU to server\n "; |
79 |
my $ready; |
my $ready; |
80 |
my $sel = IO::Select->new($serversocket); |
my $sel = IO::Select->new($serversocket); |
81 |
for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) { |
for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) { |
82 |
asn_read($serversocket, my $respdu) or return 1; |
asn_read($serversocket, my $respdu); |
83 |
|
if ( ! $respdu ) { |
84 |
|
warn "server closed connection\n"; |
85 |
|
return 0; |
86 |
|
} |
87 |
$respdu = log_response($respdu); |
$respdu = log_response($respdu); |
88 |
# and send the result to the client |
# and send the result to the client |
89 |
print $clientsocket $respdu; |
print $clientsocket $respdu || return 0; |
90 |
} |
} |
91 |
|
|
92 |
return 0; |
return 1; |
93 |
} |
} |
94 |
|
|
95 |
|
|
96 |
sub log_request { |
sub log_request { |
97 |
my $pdu=shift; |
my $pdu=shift; |
98 |
|
|
99 |
print '-' x 80,"\n"; |
die "empty pdu" unless $pdu; |
100 |
print "Request ASN 1:\n"; |
|
101 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# print '-' x 80,"\n"; |
102 |
print "Request Perl:\n"; |
# print "Request ASN 1:\n"; |
103 |
|
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
104 |
|
# print "Request Perl:\n"; |
105 |
my $request = $LDAPRequest->decode($pdu); |
my $request = $LDAPRequest->decode($pdu); |
106 |
print dump($request); |
warn "## request = ",dump($request); |
107 |
|
|
108 |
|
if ( defined $request->{bindRequest} ) { |
109 |
|
if ( $request->{bindRequest}->{name} =~ m{@} ) { |
110 |
|
my $old = $request->{bindRequest}->{name}; |
111 |
|
$request->{bindRequest}->{name} =~ s/[@\.]/,dc=/g; |
112 |
|
$request->{bindRequest}->{name} =~ s/^/uid=/; |
113 |
|
warn "rewrite bind cn $old -> ", $request->{bindRequest}->{name}; |
114 |
|
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug; |
115 |
|
$pdu = $LDAPRequest->encode($request); |
116 |
|
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug; |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
|
return $pdu; |
121 |
} |
} |
122 |
|
|
123 |
sub log_response { |
sub log_response { |
124 |
my $pdu=shift; |
my $pdu=shift; |
125 |
|
die "empty pdu" unless $pdu; |
126 |
|
|
127 |
print '-' x 80,"\n"; |
# print '-' x 80,"\n"; |
128 |
print "Response ASN 1:\n"; |
# print "Response ASN 1:\n"; |
129 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
130 |
print "Response Perl:\n"; |
# print "Response Perl:\n"; |
131 |
my $response = $LDAPResponse->decode($pdu); |
my $response = $LDAPResponse->decode($pdu); |
132 |
|
|
133 |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
134 |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
135 |
warn "## SEARCH $uid"; |
warn "## objectName $uid"; |
136 |
|
|
137 |
my @attrs; |
my @attrs; |
138 |
|
|
139 |
map { |
foreach my $attr ( @{ $response->{protocolOp}->{searchResEntry}->{attributes} } ) { |
140 |
if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) { |
if ( $attr->{type} =~ m/date/i ) { |
141 |
foreach my $val ( @{ $_->{vals} } ) { |
foreach my $i ( 0 .. $#{ $attr->{vals} } ) { |
142 |
|
$attr->{vals}->[$i] = "$1-$2-$3" if $attr->{vals}->[$i] =~ m/^([12]\d\d\d)([01]\d+)([0123]\d+)$/; |
143 |
|
} |
144 |
|
} elsif ( $attr->{type} eq 'hrEduPersonUniqueNumber' ) { |
145 |
|
foreach my $val ( @{ $attr->{vals} } ) { |
146 |
next if $val !~ m{.+:.+}; |
next if $val !~ m{.+:.+}; |
147 |
my ( $n, $v ) = split(/\s*:\s*/, $val ); |
my ( $n, $v ) = split(/\s*:\s*/, $val ); |
148 |
push @attrs, { type => $_->{type} . '_' . $n, vals => [ $v ] }; |
push @attrs, { type => $attr->{type} . '_' . $n, vals => [ $v ] }; |
149 |
} |
} |
150 |
} |
} |
151 |
} @{ $response->{protocolOp}->{searchResEntry}->{attributes} }; |
} |
152 |
|
|
153 |
warn "# ++ attrs ",dump( @attrs ); |
warn "# ++ attrs ",dump( @attrs ); |
154 |
|
|
155 |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs; |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs; |
156 |
|
|
157 |
my $path = $config->{yaml_dir} . "$uid.yaml"; |
my @additional_yamls = ( $uid ); |
158 |
if ( -e $path ) { |
foreach my $attr ( @{ $response->{protocolOp}->{searchResEntry}->{attributes} } ) { |
159 |
my $data = LoadFile($path); |
foreach my $v ( @{ $attr->{vals} } ) { |
160 |
warn "# yaml = ",dump($data); |
push @additional_yamls, $attr->{type} . '/' . $v; |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
#warn "# additional_yamls ",dump( @additional_yamls ); |
165 |
|
|
166 |
|
foreach my $path ( @additional_yamls ) { |
167 |
|
my $full_path = $config->{yaml_dir} . '/' . $path . '.yaml'; |
168 |
|
next unless -e $full_path; |
169 |
|
|
170 |
|
my $data = LoadFile( $full_path ); |
171 |
|
warn "# $full_path yaml = ",dump($data); |
172 |
|
|
173 |
foreach my $type ( keys %$data ) { |
foreach my $type ( keys %$data ) { |
174 |
|
|
184 |
$pdu = $LDAPResponse->encode($response); |
$pdu = $LDAPResponse->encode($response); |
185 |
} |
} |
186 |
|
|
187 |
print dump($response); |
warn "## response = ", dump($response); |
188 |
|
|
189 |
return $pdu; |
return $pdu; |
190 |
} |
} |
191 |
|
|
|
sub run_proxy { |
|
|
my $listenersock = shift; |
|
|
my $targetsock=shift; |
|
|
|
|
|
die "Could not create listener socket: $!\n" unless $listenersock; |
|
|
die "Could not create connection to server: $!\n" unless $targetsock; |
|
|
|
|
|
my $sel = IO::Select->new($listenersock); |
|
|
my %Handlers; |
|
|
while (my @ready = $sel->can_read) { |
|
|
foreach my $fh (@ready) { |
|
|
if ($fh == $listenersock) { |
|
|
# let's create a new socket |
|
|
my $psock = $listenersock->accept; |
|
|
$sel->add($psock); |
|
|
} else { |
|
|
my $result = handle($fh,$targetsock); |
|
|
if ($result) { |
|
|
# we have finished with the socket |
|
|
$sel->remove($fh); |
|
|
$fh->close; |
|
|
delete $Handlers{*$fh}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$ENV{LANG} = 'C'; # so we don't double-encode utf-8 if LANG is utf-8 |
|
192 |
|
|
193 |
my $listenersock = IO::Socket::INET->new( |
my $listenersock = IO::Socket::INET->new( |
194 |
Listen => 5, |
Listen => 5, |
195 |
Proto => 'tcp', |
Proto => 'tcp', |
196 |
Reuse => 1, |
Reuse => 1, |
197 |
LocalAddr => $config->{listen}, |
LocalAddr => $config->{listen}, |
198 |
); |
) || die "can't open listen socket: $!"; |
199 |
|
|
200 |
|
our $server_sock; |
201 |
|
|
202 |
my $targetsock = $config->{upstream_ssl} |
sub connect_to_server { |
203 |
? IO::Socket::INET->new( |
my $sock; |
204 |
Proto => 'tcp', |
if ( $config->{upstream_ssl} ) { |
205 |
PeerAddr => $config->{upstream_ldap}, |
$sock = IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps' ); |
206 |
PeerPort => 389, |
} else { |
207 |
) |
$sock = IO::Socket::INET->new( |
208 |
: IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps') |
Proto => 'tcp', |
209 |
; |
PeerAddr => $config->{upstream_ldap}, |
210 |
|
PeerPort => 389, |
211 |
|
); |
212 |
|
} |
213 |
|
die "can't open ", $config->{upstream_ldap}, " $!\n" unless $sock; |
214 |
|
warn "## connected to ", $sock->peerhost, ":", $sock->peerport, "\n"; |
215 |
|
return $sock; |
216 |
|
} |
217 |
|
|
218 |
run_proxy($listenersock,$targetsock); |
my $sel = IO::Select->new($listenersock); |
219 |
|
while (my @ready = $sel->can_read) { |
220 |
|
foreach my $fh (@ready) { |
221 |
|
if ($fh == $listenersock) { |
222 |
|
# let's create a new socket |
223 |
|
my $psock = $listenersock->accept; |
224 |
|
$sel->add($psock); |
225 |
|
warn "## add $psock " . time; |
226 |
|
} else { |
227 |
|
$server_sock->{$fh} ||= connect_to_server; |
228 |
|
if ( ! handle($fh,$server_sock->{$fh}) ) { |
229 |
|
warn "## remove $fh " . time; |
230 |
|
$sel->remove($server_sock->{$fh}); |
231 |
|
$server_sock->{$fh}->close; |
232 |
|
delete $server_sock->{$fh}; |
233 |
|
# we have finished with the socket |
234 |
|
$sel->remove($fh); |
235 |
|
$fh->close; |
236 |
|
} |
237 |
|
} |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
1; |
1; |