9 |
|
|
10 |
use IO::Select; |
use IO::Select; |
11 |
use IO::Socket; |
use IO::Socket; |
12 |
|
use IO::Socket::SSL; |
13 |
use warnings; |
use warnings; |
14 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
15 |
use Convert::ASN1 qw(asn_read); |
use Convert::ASN1 qw(asn_read); |
16 |
use Net::LDAP::ASN qw(LDAPRequest LDAPResponse); |
use Net::LDAP::ASN qw(LDAPRequest LDAPResponse); |
17 |
our $VERSION = '0.2'; |
our $VERSION = '0.2'; |
18 |
use fields qw(socket target); |
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 |
|
|
28 |
|
}; |
29 |
|
|
30 |
|
if ( ! -d $config->{yaml_dir} ) { |
31 |
|
warn "DISABLE ", $config->{yaml_dir}," data overlay"; |
32 |
|
} |
33 |
|
|
34 |
|
warn "# config = ",dump( $config ); |
35 |
|
|
36 |
sub handle { |
sub handle { |
37 |
my $clientsocket=shift; |
my $clientsocket=shift; |
79 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
80 |
print "Response Perl:\n"; |
print "Response Perl:\n"; |
81 |
my $response = $LDAPResponse->decode($pdu); |
my $response = $LDAPResponse->decode($pdu); |
|
print dump($response); |
|
82 |
|
|
83 |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
84 |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
85 |
warn "## SEARCH $uid"; |
warn "## SEARCH $uid"; |
86 |
|
|
87 |
|
my @attrs; |
88 |
|
|
89 |
map { |
map { |
90 |
if ( $_->{type} eq 'postalAddress' ) { |
if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) { |
91 |
$_->{vals} = [ 'foobar' ]; |
foreach my $val ( @{ $_->{vals} } ) { |
92 |
|
next if $val !~ m{.+:.+}; |
93 |
|
my ( $n, $v ) = split(/\s*:\s*/, $val ); |
94 |
|
push @attrs, { type => $_->{type} . '_' . $n, vals => [ $v ] }; |
95 |
|
} |
96 |
} |
} |
97 |
} @{ $response->{protocolOp}->{searchResEntry}->{attributes} }; |
} @{ $response->{protocolOp}->{searchResEntry}->{attributes} }; |
98 |
|
|
99 |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, |
warn "# ++ attrs ",dump( @attrs ); |
100 |
{ type => 'ffzg-datum_rodjenja', vals => [ '2009-01-01' ], } |
|
101 |
; |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs; |
102 |
|
|
103 |
|
my $path = $config->{yaml_dir} . "$uid.yaml"; |
104 |
|
if ( -e $path ) { |
105 |
|
my $data = LoadFile($path); |
106 |
|
warn "# yaml = ",dump($data); |
107 |
|
|
108 |
|
foreach my $type ( keys %$data ) { |
109 |
|
|
110 |
|
my $vals = $data->{$type}; |
111 |
|
$vals =~ s{#\s*$}{}; |
112 |
|
|
113 |
|
my @vals = split(/\s*#\s*/, $vals); |
114 |
|
|
115 |
|
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, |
116 |
|
{ type => $config->{overlay_prefix} . $type, vals => [ @vals ] }; |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
$pdu = $LDAPResponse->encode($response); |
$pdu = $LDAPResponse->encode($response); |
121 |
} |
} |
122 |
|
|
123 |
|
print dump($response); |
124 |
|
|
125 |
return $pdu; |
return $pdu; |
126 |
} |
} |
127 |
|
|
158 |
Listen => 5, |
Listen => 5, |
159 |
Proto => 'tcp', |
Proto => 'tcp', |
160 |
Reuse => 1, |
Reuse => 1, |
161 |
LocalPort => 1389 |
LocalAddr => $config->{listen}, |
162 |
); |
); |
163 |
|
|
164 |
|
|
165 |
my $targetsock = new IO::Socket::INET ( |
my $targetsock = $config->{upstream_ssl} |
166 |
Proto => 'tcp', |
? IO::Socket::INET->new( |
167 |
PeerAddr => 'ldap.ffzg.hr', |
Proto => 'tcp', |
168 |
PeerPort => 389, |
PeerAddr => $config->{upstream_ldap}, |
169 |
); |
PeerPort => 389, |
170 |
|
) |
171 |
|
: IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps') |
172 |
|
; |
173 |
|
|
174 |
run_proxy($listenersock,$targetsock); |
run_proxy($listenersock,$targetsock); |
175 |
|
|