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; |
my $debug = 0; |
28 |
|
|
29 |
my $config = { |
my $config = { |
30 |
yaml_dir => './yaml/', |
yaml_dir => './yaml/', |
67 |
# read from client |
# read from client |
68 |
asn_read($clientsocket, my $reqpdu); |
asn_read($clientsocket, my $reqpdu); |
69 |
if ( ! $reqpdu ) { |
if ( ! $reqpdu ) { |
70 |
warn "WARNING no reqpdu\n"; |
warn "client closed connection\n"; |
71 |
return 1; |
return 0; |
72 |
} |
} |
73 |
$reqpdu = log_request($reqpdu); |
$reqpdu = log_request($reqpdu); |
74 |
|
|
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 |
|
die "empty pdu" unless $pdu; |
100 |
|
|
101 |
# print '-' x 80,"\n"; |
# print '-' x 80,"\n"; |
102 |
# print "Request ASN 1:\n"; |
# print "Request ASN 1:\n"; |
103 |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
109 |
if ( $request->{bindRequest}->{name} =~ m{@} ) { |
if ( $request->{bindRequest}->{name} =~ m{@} ) { |
110 |
my $old = $request->{bindRequest}->{name}; |
my $old = $request->{bindRequest}->{name}; |
111 |
$request->{bindRequest}->{name} =~ s/[@\.]/,dc=/g; |
$request->{bindRequest}->{name} =~ s/[@\.]/,dc=/g; |
112 |
$request->{bindRequest}->{name} =~ s/^/uid=/ unless $request->{bindRequest}->{name} =~ m/^uid=/; |
$request->{bindRequest}->{name} =~ s/^/uid=/; |
113 |
warn "rewrite bind cn $old -> ", $request->{bindRequest}->{name}; |
warn "rewrite bind cn $old -> ", $request->{bindRequest}->{name}; |
114 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug; |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug; |
115 |
$pdu = $LDAPRequest->encode($request); |
$pdu = $LDAPRequest->encode($request); |
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"; |
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+)([123]\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 |
|
|
178 |
return $pdu; |
return $pdu; |
179 |
} |
} |
180 |
|
|
|
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}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
181 |
|
|
182 |
my $listenersock = IO::Socket::INET->new( |
my $listenersock = IO::Socket::INET->new( |
183 |
Listen => 5, |
Listen => 5, |
186 |
LocalAddr => $config->{listen}, |
LocalAddr => $config->{listen}, |
187 |
) || die "can't open listen socket: $!"; |
) || die "can't open listen socket: $!"; |
188 |
|
|
189 |
|
our $server_sock; |
190 |
|
|
191 |
my $targetsock = $config->{upstream_ssl} |
sub connect_to_server { |
192 |
? IO::Socket::INET->new( |
my $sock; |
193 |
Proto => 'tcp', |
if ( $config->{upstream_ssl} ) { |
194 |
PeerAddr => $config->{upstream_ldap}, |
$sock = IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps' ); |
195 |
PeerPort => 389, |
} else { |
196 |
) |
$sock = IO::Socket::INET->new( |
197 |
: IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps') |
Proto => 'tcp', |
198 |
|| die "can't open upstream socket: $!"; |
PeerAddr => $config->{upstream_ldap}, |
199 |
|
PeerPort => 389, |
200 |
run_proxy($listenersock,$targetsock); |
); |
201 |
|
} |
202 |
|
die "can't open ", $config->{upstream_ldap}, " $!\n" unless $sock; |
203 |
|
warn "## connected to ", $sock->peerhost, ":", $sock->peerport, "\n"; |
204 |
|
return $sock; |
205 |
|
} |
206 |
|
|
207 |
|
my $sel = IO::Select->new($listenersock); |
208 |
|
while (my @ready = $sel->can_read) { |
209 |
|
foreach my $fh (@ready) { |
210 |
|
if ($fh == $listenersock) { |
211 |
|
# let's create a new socket |
212 |
|
my $psock = $listenersock->accept; |
213 |
|
$sel->add($psock); |
214 |
|
warn "## add $psock " . time; |
215 |
|
} else { |
216 |
|
$server_sock->{$fh} ||= connect_to_server; |
217 |
|
if ( ! handle($fh,$server_sock->{$fh}) ) { |
218 |
|
warn "## remove $fh " . time; |
219 |
|
$sel->remove($server_sock->{$fh}); |
220 |
|
$server_sock->{$fh}->close; |
221 |
|
delete $server_sock->{$fh}; |
222 |
|
# we have finished with the socket |
223 |
|
$sel->remove($fh); |
224 |
|
$fh->close; |
225 |
|
} |
226 |
|
} |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
1; |
1; |