1 |
#!/usr/bin/perl |
2 |
# Copyright (c) 2006 Hans Klunder <hans.klunder@bigfoot.com>. All rights reserved. |
3 |
# This program is free software; you can redistribute it and/or |
4 |
# modify it under the same terms as Perl itself. |
5 |
|
6 |
|
7 |
use strict; |
8 |
use warnings; |
9 |
|
10 |
use IO::Select; |
11 |
use IO::Socket; |
12 |
use IO::Socket::SSL; |
13 |
use warnings; |
14 |
use Data::Dump qw/dump/; |
15 |
use Convert::ASN1 qw(asn_read); |
16 |
use Net::LDAP::ASN qw(LDAPRequest LDAPResponse); |
17 |
our $VERSION = '0.2'; |
18 |
use fields qw(socket target); |
19 |
use YAML qw/LoadFile/; |
20 |
|
21 |
sub handle { |
22 |
my $clientsocket=shift; |
23 |
my $serversocket=shift; |
24 |
|
25 |
# read from client |
26 |
asn_read($clientsocket, my $reqpdu); |
27 |
log_request($reqpdu); |
28 |
|
29 |
return 1 unless $reqpdu; |
30 |
|
31 |
# send to server |
32 |
print $serversocket $reqpdu or die "Could not send PDU to server\n "; |
33 |
|
34 |
# read from server |
35 |
my $ready; |
36 |
my $sel = IO::Select->new($serversocket); |
37 |
for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) { |
38 |
asn_read($serversocket, my $respdu) or return 1; |
39 |
$respdu = log_response($respdu); |
40 |
# and send the result to the client |
41 |
print $clientsocket $respdu; |
42 |
} |
43 |
|
44 |
return 0; |
45 |
} |
46 |
|
47 |
|
48 |
sub log_request { |
49 |
my $pdu=shift; |
50 |
|
51 |
print '-' x 80,"\n"; |
52 |
print "Request ASN 1:\n"; |
53 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
54 |
print "Request Perl:\n"; |
55 |
my $request = $LDAPRequest->decode($pdu); |
56 |
print dump($request); |
57 |
} |
58 |
|
59 |
sub log_response { |
60 |
my $pdu=shift; |
61 |
|
62 |
print '-' x 80,"\n"; |
63 |
print "Response ASN 1:\n"; |
64 |
Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
65 |
print "Response Perl:\n"; |
66 |
my $response = $LDAPResponse->decode($pdu); |
67 |
|
68 |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
69 |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
70 |
warn "## SEARCH $uid"; |
71 |
|
72 |
if(0) { |
73 |
map { |
74 |
if ( $_->{type} eq 'postalAddress' ) { |
75 |
$_->{vals} = [ 'foobar' ]; |
76 |
} |
77 |
} @{ $response->{protocolOp}->{searchResEntry}->{attributes} }; |
78 |
} |
79 |
|
80 |
my $path = "yaml/$uid.yaml"; |
81 |
if ( -e $path ) { |
82 |
my $data = LoadFile($path); |
83 |
warn "# yaml = ",dump($data); |
84 |
|
85 |
foreach my $type ( keys %$data ) { |
86 |
|
87 |
my $vals = $data->{$type}; |
88 |
$vals =~ s{#\s*$}{}; |
89 |
|
90 |
my @vals = split(/\s*#\s*/, $vals); |
91 |
|
92 |
push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, |
93 |
{ type => "ffzg-$type", vals => [ @vals ] }; |
94 |
} |
95 |
} |
96 |
|
97 |
$pdu = $LDAPResponse->encode($response); |
98 |
} |
99 |
|
100 |
print dump($response); |
101 |
|
102 |
return $pdu; |
103 |
} |
104 |
|
105 |
sub run_proxy { |
106 |
my $listenersock = shift; |
107 |
my $targetsock=shift; |
108 |
|
109 |
die "Could not create listener socket: $!\n" unless $listenersock; |
110 |
die "Could not create connection to server: $!\n" unless $targetsock; |
111 |
|
112 |
my $sel = IO::Select->new($listenersock); |
113 |
my %Handlers; |
114 |
while (my @ready = $sel->can_read) { |
115 |
foreach my $fh (@ready) { |
116 |
if ($fh == $listenersock) { |
117 |
# let's create a new socket |
118 |
my $psock = $listenersock->accept; |
119 |
$sel->add($psock); |
120 |
} else { |
121 |
my $result = handle($fh,$targetsock); |
122 |
if ($result) { |
123 |
# we have finished with the socket |
124 |
$sel->remove($fh); |
125 |
$fh->close; |
126 |
delete $Handlers{*$fh}; |
127 |
} |
128 |
} |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
|
134 |
my $listenersock = IO::Socket::INET->new( |
135 |
Listen => 5, |
136 |
Proto => 'tcp', |
137 |
Reuse => 1, |
138 |
LocalPort => 1389 |
139 |
); |
140 |
|
141 |
|
142 |
my $targetsock = new IO::Socket::INET ( |
143 |
Proto => 'tcp', |
144 |
PeerAddr => 'ldap.ffzg.hr', |
145 |
PeerPort => 389, |
146 |
); |
147 |
|
148 |
$targetsock = IO::Socket::SSL->new("ldap.ffzg.hr:ldaps"); |
149 |
|
150 |
run_proxy($listenersock,$targetsock); |
151 |
|
152 |
1; |