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 => shift @ARGV || 'localhost:1389', |
24 |
|
upstream_ldap => 'ldap.ffzg.hr', |
25 |
|
upstream_ssl => 1, |
26 |
|
overlay_prefix => 'ffzg-', |
27 |
|
log_file => 'log', |
28 |
|
|
29 |
|
}; |
30 |
|
|
31 |
|
my $log_fh; |
32 |
|
|
33 |
|
sub log { |
34 |
|
if ( ! $log_fh ) { |
35 |
|
open($log_fh, '>>', $config->{log_file}) || die "can't open ", $config->{log_file},": $!"; |
36 |
|
print $log_fh "# " . time; |
37 |
|
} |
38 |
|
$log_fh->autoflush(1); |
39 |
|
print $log_fh join("\n", @_),"\n"; |
40 |
|
} |
41 |
|
|
42 |
|
BEGIN { |
43 |
|
$SIG{'__WARN__'} = sub { warn @_; main::log(@_); } |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
|
if ( ! -d $config->{yaml_dir} ) { |
48 |
|
warn "DISABLE ", $config->{yaml_dir}," data overlay"; |
49 |
|
} |
50 |
|
|
51 |
|
warn "# config = ",dump( $config ); |
52 |
|
|
53 |
sub handle { |
sub handle { |
54 |
my $clientsocket=shift; |
my $clientsocket=shift; |
80 |
sub log_request { |
sub log_request { |
81 |
my $pdu=shift; |
my $pdu=shift; |
82 |
|
|
83 |
print '-' x 80,"\n"; |
# print '-' x 80,"\n"; |
84 |
print "Request ASN 1:\n"; |
# print "Request ASN 1:\n"; |
85 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
86 |
print "Request Perl:\n"; |
# print "Request Perl:\n"; |
87 |
my $request = $LDAPRequest->decode($pdu); |
my $request = $LDAPRequest->decode($pdu); |
88 |
print dump($request); |
warn "## request = ",dump($request); |
89 |
} |
} |
90 |
|
|
91 |
sub log_response { |
sub log_response { |
92 |
my $pdu=shift; |
my $pdu=shift; |
93 |
|
|
94 |
print '-' x 80,"\n"; |
# print '-' x 80,"\n"; |
95 |
print "Response ASN 1:\n"; |
# print "Response ASN 1:\n"; |
96 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
97 |
print "Response Perl:\n"; |
# print "Response Perl:\n"; |
98 |
my $response = $LDAPResponse->decode($pdu); |
my $response = $LDAPResponse->decode($pdu); |
|
print dump($response); |
|
99 |
|
|
100 |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
101 |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
102 |
warn "## SEARCH $uid"; |
warn "## objectName $uid"; |
103 |
|
|
104 |
|
my @attrs; |
105 |
|
|
106 |
map { |
map { |
107 |
if ( $_->{type} eq 'postalAddress' ) { |
if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) { |
108 |
$_->{vals} = [ 'foobar' ]; |
foreach my $val ( @{ $_->{vals} } ) { |
109 |
|
next if $val !~ m{.+:.+}; |
110 |
|
my ( $n, $v ) = split(/\s*:\s*/, $val ); |
111 |
|
push @attrs, { type => $_->{type} . '_' . $n, vals => [ $v ] }; |
112 |
|
} |
113 |
} |
} |
114 |
} @{ $response->{protocolOp}->{searchResEntry}->{attributes} }; |
} @{ $response->{protocolOp}->{searchResEntry}->{attributes} }; |
115 |
|
|
116 |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, |
warn "# ++ attrs ",dump( @attrs ); |
117 |
{ type => 'ffzg-datum_rodjenja', vals => [ '2009-01-01' ], } |
|
118 |
; |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs; |
119 |
|
|
120 |
|
my $path = $config->{yaml_dir} . "$uid.yaml"; |
121 |
|
if ( -e $path ) { |
122 |
|
my $data = LoadFile($path); |
123 |
|
warn "# yaml = ",dump($data); |
124 |
|
|
125 |
|
foreach my $type ( keys %$data ) { |
126 |
|
|
127 |
|
my $vals = $data->{$type}; |
128 |
|
|
129 |
|
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, { |
130 |
|
type => $config->{overlay_prefix} . $type, |
131 |
|
vals => ref($vals) eq 'ARRAY' ? $vals : [ $vals ], |
132 |
|
}; |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
$pdu = $LDAPResponse->encode($response); |
$pdu = $LDAPResponse->encode($response); |
137 |
} |
} |
138 |
|
|
139 |
|
warn "## response = ", dump($response); |
140 |
|
|
141 |
return $pdu; |
return $pdu; |
142 |
} |
} |
143 |
|
|
174 |
Listen => 5, |
Listen => 5, |
175 |
Proto => 'tcp', |
Proto => 'tcp', |
176 |
Reuse => 1, |
Reuse => 1, |
177 |
LocalPort => 1389 |
LocalAddr => $config->{listen}, |
178 |
); |
) || die "can't open listen socket: $!"; |
179 |
|
|
180 |
|
|
181 |
my $targetsock = new IO::Socket::INET ( |
my $targetsock = $config->{upstream_ssl} |
182 |
Proto => 'tcp', |
? IO::Socket::INET->new( |
183 |
PeerAddr => 'ldap.ffzg.hr', |
Proto => 'tcp', |
184 |
PeerPort => 389, |
PeerAddr => $config->{upstream_ldap}, |
185 |
); |
PeerPort => 389, |
186 |
|
) |
187 |
|
: IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps') |
188 |
|
|| die "can't open upstream socket: $!"; |
189 |
|
|
190 |
run_proxy($listenersock,$targetsock); |
run_proxy($listenersock,$targetsock); |
191 |
|
|