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 = 1; |
28 |
|
|
29 |
|
my $config = { |
30 |
|
yaml_dir => './yaml/', |
31 |
|
listen => shift @ARGV || 'localhost:1389', |
32 |
|
upstream_ldap => 'ldap.ffzg.hr', |
33 |
|
upstream_ssl => 1, |
34 |
|
overlay_prefix => 'ffzg-', |
35 |
|
log_file => 'log', |
36 |
|
|
37 |
|
}; |
38 |
|
|
39 |
|
my $log_fh; |
40 |
|
|
41 |
|
sub log { |
42 |
|
if ( ! $log_fh ) { |
43 |
|
open($log_fh, '>>', $config->{log_file}) || die "can't open ", $config->{log_file},": $!"; |
44 |
|
print $log_fh "# " . time; |
45 |
|
} |
46 |
|
$log_fh->autoflush(1); |
47 |
|
print $log_fh join("\n", @_),"\n"; |
48 |
|
} |
49 |
|
|
50 |
|
BEGIN { |
51 |
|
$SIG{'__WARN__'} = sub { warn @_; main::log(@_); } |
52 |
|
} |
53 |
|
|
54 |
|
|
55 |
|
if ( ! -d $config->{yaml_dir} ) { |
56 |
|
warn "DISABLE ", $config->{yaml_dir}," data overlay"; |
57 |
|
} |
58 |
|
|
59 |
|
warn "# config = ",dump( $config ); |
60 |
|
|
61 |
sub handle { |
sub handle { |
62 |
my $clientsocket=shift; |
my $clientsocket=shift; |
63 |
my $serversocket=shift; |
my $serversocket=shift; |
64 |
|
|
65 |
# read from client |
# read from client |
66 |
asn_read($clientsocket, my $reqpdu); |
asn_read($clientsocket, my $reqpdu); |
67 |
log_request($reqpdu); |
if ( ! $reqpdu ) { |
68 |
|
warn "WARNING no reqpdu\n"; |
69 |
return 1 unless $reqpdu; |
return 1; |
70 |
|
} |
71 |
|
$reqpdu = log_request($reqpdu); |
72 |
|
|
73 |
# send to server |
# send to server |
74 |
print $serversocket $reqpdu or die "Could not send PDU to server\n "; |
print $serversocket $reqpdu or die "Could not send PDU to server\n "; |
90 |
sub log_request { |
sub log_request { |
91 |
my $pdu=shift; |
my $pdu=shift; |
92 |
|
|
93 |
print '-' x 80,"\n"; |
# print '-' x 80,"\n"; |
94 |
print "Request ASN 1:\n"; |
# print "Request ASN 1:\n"; |
95 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
96 |
print "Request Perl:\n"; |
# print "Request Perl:\n"; |
97 |
my $request = $LDAPRequest->decode($pdu); |
my $request = $LDAPRequest->decode($pdu); |
98 |
print dump($request); |
warn "## request = ",dump($request); |
99 |
|
|
100 |
|
if ( defined $request->{bindRequest} ) { |
101 |
|
if ( $request->{bindRequest}->{name} =~ m{@} ) { |
102 |
|
my $old = $request->{bindRequest}->{name}; |
103 |
|
$request->{bindRequest}->{name} =~ s/[@\.]/,dc=/g; |
104 |
|
$request->{bindRequest}->{name} =~ s/^/uid=/; |
105 |
|
warn "rewrite bind cn $old -> ", $request->{bindRequest}->{name}; |
106 |
|
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug; |
107 |
|
$pdu = $LDAPRequest->encode($request); |
108 |
|
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug; |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
|
return $pdu; |
113 |
} |
} |
114 |
|
|
115 |
sub log_response { |
sub log_response { |
116 |
my $pdu=shift; |
my $pdu=shift; |
117 |
|
|
118 |
print '-' x 80,"\n"; |
# print '-' x 80,"\n"; |
119 |
print "Response ASN 1:\n"; |
# print "Response ASN 1:\n"; |
120 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
121 |
print "Response Perl:\n"; |
# print "Response Perl:\n"; |
122 |
my $response = $LDAPResponse->decode($pdu); |
my $response = $LDAPResponse->decode($pdu); |
123 |
|
|
124 |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
125 |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
126 |
warn "## SEARCH $uid"; |
warn "## objectName $uid"; |
127 |
|
|
128 |
|
my @attrs; |
129 |
|
|
|
if(0) { |
|
130 |
map { |
map { |
131 |
if ( $_->{type} eq 'postalAddress' ) { |
if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) { |
132 |
$_->{vals} = [ 'foobar' ]; |
foreach my $val ( @{ $_->{vals} } ) { |
133 |
|
next if $val !~ m{.+:.+}; |
134 |
|
my ( $n, $v ) = split(/\s*:\s*/, $val ); |
135 |
|
push @attrs, { type => $_->{type} . '_' . $n, vals => [ $v ] }; |
136 |
|
} |
137 |
} |
} |
138 |
} @{ $response->{protocolOp}->{searchResEntry}->{attributes} }; |
} @{ $response->{protocolOp}->{searchResEntry}->{attributes} }; |
|
} |
|
139 |
|
|
140 |
my $path = "yaml/$uid.yaml"; |
warn "# ++ attrs ",dump( @attrs ); |
141 |
|
|
142 |
|
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs; |
143 |
|
|
144 |
|
my $path = $config->{yaml_dir} . "$uid.yaml"; |
145 |
if ( -e $path ) { |
if ( -e $path ) { |
146 |
my $data = LoadFile($path); |
my $data = LoadFile($path); |
147 |
warn "# yaml = ",dump($data); |
warn "# yaml = ",dump($data); |
149 |
foreach my $type ( keys %$data ) { |
foreach my $type ( keys %$data ) { |
150 |
|
|
151 |
my $vals = $data->{$type}; |
my $vals = $data->{$type}; |
|
$vals =~ s{#\s*$}{}; |
|
|
|
|
|
my @vals = split(/\s*#\s*/, $vals); |
|
152 |
|
|
153 |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, { |
154 |
{ type => "ffzg-$type", vals => [ @vals ] }; |
type => $config->{overlay_prefix} . $type, |
155 |
|
vals => ref($vals) eq 'ARRAY' ? $vals : [ $vals ], |
156 |
|
}; |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
160 |
$pdu = $LDAPResponse->encode($response); |
$pdu = $LDAPResponse->encode($response); |
161 |
} |
} |
162 |
|
|
163 |
print dump($response); |
warn "## response = ", dump($response); |
164 |
|
|
165 |
return $pdu; |
return $pdu; |
166 |
} |
} |
198 |
Listen => 5, |
Listen => 5, |
199 |
Proto => 'tcp', |
Proto => 'tcp', |
200 |
Reuse => 1, |
Reuse => 1, |
201 |
LocalPort => 1389 |
LocalAddr => $config->{listen}, |
202 |
); |
) || die "can't open listen socket: $!"; |
|
|
|
203 |
|
|
|
my $targetsock = new IO::Socket::INET ( |
|
|
Proto => 'tcp', |
|
|
PeerAddr => 'ldap.ffzg.hr', |
|
|
PeerPort => 389, |
|
|
); |
|
204 |
|
|
205 |
$targetsock = IO::Socket::SSL->new("ldap.ffzg.hr:ldaps"); |
my $targetsock = $config->{upstream_ssl} |
206 |
|
? IO::Socket::INET->new( |
207 |
|
Proto => 'tcp', |
208 |
|
PeerAddr => $config->{upstream_ldap}, |
209 |
|
PeerPort => 389, |
210 |
|
) |
211 |
|
: IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps') |
212 |
|
|| die "can't open upstream socket: $!"; |
213 |
|
|
214 |
run_proxy($listenersock,$targetsock); |
run_proxy($listenersock,$targetsock); |
215 |
|
|