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 "WARNING no reqpdu\n"; |
71 |
return 1 unless $reqpdu; |
return 1; |
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 "; |
92 |
sub log_request { |
sub log_request { |
93 |
my $pdu=shift; |
my $pdu=shift; |
94 |
|
|
95 |
print '-' x 80,"\n"; |
# print '-' x 80,"\n"; |
96 |
print "Request ASN 1:\n"; |
# print "Request ASN 1:\n"; |
97 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
98 |
print "Request Perl:\n"; |
# print "Request Perl:\n"; |
99 |
my $request = $LDAPRequest->decode($pdu); |
my $request = $LDAPRequest->decode($pdu); |
100 |
print dump($request); |
warn "## request = ",dump($request); |
101 |
|
|
102 |
|
if ( defined $request->{bindRequest} ) { |
103 |
|
if ( $request->{bindRequest}->{name} =~ m{@} ) { |
104 |
|
my $old = $request->{bindRequest}->{name}; |
105 |
|
$request->{bindRequest}->{name} =~ s/[@\.]/,dc=/g; |
106 |
|
$request->{bindRequest}->{name} =~ s/^/uid=/; |
107 |
|
warn "rewrite bind cn $old -> ", $request->{bindRequest}->{name}; |
108 |
|
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug; |
109 |
|
$pdu = $LDAPRequest->encode($request); |
110 |
|
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug; |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
|
return $pdu; |
115 |
} |
} |
116 |
|
|
117 |
sub log_response { |
sub log_response { |
118 |
my $pdu=shift; |
my $pdu=shift; |
119 |
|
|
120 |
print '-' x 80,"\n"; |
# print '-' x 80,"\n"; |
121 |
print "Response ASN 1:\n"; |
# print "Response ASN 1:\n"; |
122 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
123 |
print "Response Perl:\n"; |
# print "Response Perl:\n"; |
124 |
my $response = $LDAPResponse->decode($pdu); |
my $response = $LDAPResponse->decode($pdu); |
125 |
|
|
126 |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
127 |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
128 |
warn "## SEARCH $uid"; |
warn "## objectName $uid"; |
129 |
|
|
130 |
my @attrs; |
my @attrs; |
131 |
|
|
162 |
$pdu = $LDAPResponse->encode($response); |
$pdu = $LDAPResponse->encode($response); |
163 |
} |
} |
164 |
|
|
165 |
print dump($response); |
warn "## response = ", dump($response); |
166 |
|
|
167 |
return $pdu; |
return $pdu; |
168 |
} |
} |
196 |
} |
} |
197 |
|
|
198 |
|
|
|
$ENV{LANG} = 'C'; # so we don't double-encode utf-8 if LANG is utf-8 |
|
|
|
|
199 |
my $listenersock = IO::Socket::INET->new( |
my $listenersock = IO::Socket::INET->new( |
200 |
Listen => 5, |
Listen => 5, |
201 |
Proto => 'tcp', |
Proto => 'tcp', |
202 |
Reuse => 1, |
Reuse => 1, |
203 |
LocalAddr => $config->{listen}, |
LocalAddr => $config->{listen}, |
204 |
); |
) || die "can't open listen socket: $!"; |
205 |
|
|
206 |
|
|
207 |
my $targetsock = $config->{upstream_ssl} |
my $targetsock = $config->{upstream_ssl} |
211 |
PeerPort => 389, |
PeerPort => 389, |
212 |
) |
) |
213 |
: IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps') |
: IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps') |
214 |
; |
|| die "can't open upstream socket: $!"; |
215 |
|
|
216 |
run_proxy($listenersock,$targetsock); |
run_proxy($listenersock,$targetsock); |
217 |
|
|