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 |
my $config = { |
22 |
yaml_dir => './yaml/', |
23 |
listen => 'localhost:2389', |
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 { |
54 |
my $clientsocket=shift; |
55 |
my $serversocket=shift; |
56 |
|
57 |
# read from client |
58 |
asn_read($clientsocket, my $reqpdu); |
59 |
log_request($reqpdu); |
60 |
|
61 |
return 1 unless $reqpdu; |
62 |
|
63 |
# send to server |
64 |
print $serversocket $reqpdu or die "Could not send PDU to server\n "; |
65 |
|
66 |
# read from server |
67 |
my $ready; |
68 |
my $sel = IO::Select->new($serversocket); |
69 |
for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) { |
70 |
asn_read($serversocket, my $respdu) or return 1; |
71 |
$respdu = log_response($respdu); |
72 |
# and send the result to the client |
73 |
print $clientsocket $respdu; |
74 |
} |
75 |
|
76 |
return 0; |
77 |
} |
78 |
|
79 |
|
80 |
sub log_request { |
81 |
my $pdu=shift; |
82 |
|
83 |
# print '-' x 80,"\n"; |
84 |
# print "Request ASN 1:\n"; |
85 |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
86 |
# print "Request Perl:\n"; |
87 |
my $request = $LDAPRequest->decode($pdu); |
88 |
warn "## request = ",dump($request); |
89 |
} |
90 |
|
91 |
sub log_response { |
92 |
my $pdu=shift; |
93 |
|
94 |
# print '-' x 80,"\n"; |
95 |
# print "Response ASN 1:\n"; |
96 |
# Convert::ASN1::asn_hexdump(\*STDOUT,$pdu); |
97 |
# print "Response Perl:\n"; |
98 |
my $response = $LDAPResponse->decode($pdu); |
99 |
|
100 |
if ( defined $response->{protocolOp}->{searchResEntry} ) { |
101 |
my $uid = $response->{protocolOp}->{searchResEntry}->{objectName}; |
102 |
warn "## SEARCH $uid"; |
103 |
|
104 |
my @attrs; |
105 |
|
106 |
map { |
107 |
if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) { |
108 |
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} }; |
115 |
|
116 |
warn "# ++ attrs ",dump( @attrs ); |
117 |
|
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); |
137 |
} |
138 |
|
139 |
warn "## response = ", dump($response); |
140 |
|
141 |
return $pdu; |
142 |
} |
143 |
|
144 |
sub run_proxy { |
145 |
my $listenersock = shift; |
146 |
my $targetsock=shift; |
147 |
|
148 |
die "Could not create listener socket: $!\n" unless $listenersock; |
149 |
die "Could not create connection to server: $!\n" unless $targetsock; |
150 |
|
151 |
my $sel = IO::Select->new($listenersock); |
152 |
my %Handlers; |
153 |
while (my @ready = $sel->can_read) { |
154 |
foreach my $fh (@ready) { |
155 |
if ($fh == $listenersock) { |
156 |
# let's create a new socket |
157 |
my $psock = $listenersock->accept; |
158 |
$sel->add($psock); |
159 |
} else { |
160 |
my $result = handle($fh,$targetsock); |
161 |
if ($result) { |
162 |
# we have finished with the socket |
163 |
$sel->remove($fh); |
164 |
$fh->close; |
165 |
delete $Handlers{*$fh}; |
166 |
} |
167 |
} |
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
|
173 |
$ENV{LANG} = 'C'; # so we don't double-encode utf-8 if LANG is utf-8 |
174 |
|
175 |
my $listenersock = IO::Socket::INET->new( |
176 |
Listen => 5, |
177 |
Proto => 'tcp', |
178 |
Reuse => 1, |
179 |
LocalAddr => $config->{listen}, |
180 |
); |
181 |
|
182 |
|
183 |
my $targetsock = $config->{upstream_ssl} |
184 |
? IO::Socket::INET->new( |
185 |
Proto => 'tcp', |
186 |
PeerAddr => $config->{upstream_ldap}, |
187 |
PeerPort => 389, |
188 |
) |
189 |
: IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps') |
190 |
; |
191 |
|
192 |
run_proxy($listenersock,$targetsock); |
193 |
|
194 |
1; |