/[virtual-ldap]/bin/ldap-rewrite.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /bin/ldap-rewrite.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 66 - (hide annotations)
Sun Feb 21 01:02:47 2010 UTC (14 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 5340 byte(s)
don't write to log without log_file config

1 dpavlin 8 #!/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 dpavlin 61 # It's modified by Dobrica Pavlinusic <dpavlin@rot13.org> to include following:
7     #
8     # * rewrite LDAP bind request cn: username@domain.com -> uid=username,dc=domain,dc=com
9     # * rewrite search responses:
10     # ** expand key:value pairs from hrEduPersonUniqueNumber into hrEduPersonUniqueNumber_key
11     # ** augment response with yaml/dn.yaml data (for external data import)
12 dpavlin 8
13     use strict;
14     use warnings;
15    
16     use IO::Select;
17     use IO::Socket;
18 dpavlin 16 use IO::Socket::SSL;
19 dpavlin 8 use warnings;
20     use Data::Dump qw/dump/;
21     use Convert::ASN1 qw(asn_read);
22     use Net::LDAP::ASN qw(LDAPRequest LDAPResponse);
23 dpavlin 61 our $VERSION = '0.3';
24 dpavlin 8 use fields qw(socket target);
25 dpavlin 10 use YAML qw/LoadFile/;
26 dpavlin 8
27 dpavlin 61 my $debug = 1;
28    
29 dpavlin 18 my $config = {
30     yaml_dir => './yaml/',
31 dpavlin 37 listen => shift @ARGV || 'localhost:1389',
32 dpavlin 18 upstream_ldap => 'ldap.ffzg.hr',
33     upstream_ssl => 1,
34     overlay_prefix => 'ffzg-',
35 dpavlin 66 # log_file => 'log/ldap-rewrite.log',
36 dpavlin 18
37     };
38    
39 dpavlin 27 my $log_fh;
40    
41     sub log {
42 dpavlin 66 return unless $config->{log_file};
43    
44 dpavlin 28 if ( ! $log_fh ) {
45     open($log_fh, '>>', $config->{log_file}) || die "can't open ", $config->{log_file},": $!";
46     print $log_fh "# " . time;
47     }
48 dpavlin 27 $log_fh->autoflush(1);
49     print $log_fh join("\n", @_),"\n";
50     }
51    
52     BEGIN {
53     $SIG{'__WARN__'} = sub { warn @_; main::log(@_); }
54     }
55    
56    
57 dpavlin 18 if ( ! -d $config->{yaml_dir} ) {
58     warn "DISABLE ", $config->{yaml_dir}," data overlay";
59     }
60    
61     warn "# config = ",dump( $config );
62    
63 dpavlin 8 sub handle {
64     my $clientsocket=shift;
65     my $serversocket=shift;
66    
67     # read from client
68     asn_read($clientsocket, my $reqpdu);
69 dpavlin 61 if ( ! $reqpdu ) {
70     warn "WARNING no reqpdu\n";
71     return 1;
72     }
73     $reqpdu = log_request($reqpdu);
74 dpavlin 8
75     # send to server
76     print $serversocket $reqpdu or die "Could not send PDU to server\n ";
77    
78     # read from server
79     my $ready;
80     my $sel = IO::Select->new($serversocket);
81     for( $ready = 1 ; $ready ; $ready = $sel->can_read(0)) {
82     asn_read($serversocket, my $respdu) or return 1;
83     $respdu = log_response($respdu);
84     # and send the result to the client
85     print $clientsocket $respdu;
86     }
87    
88     return 0;
89     }
90    
91    
92     sub log_request {
93     my $pdu=shift;
94    
95 dpavlin 28 # print '-' x 80,"\n";
96     # print "Request ASN 1:\n";
97     # Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
98     # print "Request Perl:\n";
99 dpavlin 8 my $request = $LDAPRequest->decode($pdu);
100 dpavlin 28 warn "## request = ",dump($request);
101 dpavlin 61
102     if ( defined $request->{bindRequest} ) {
103     if ( $request->{bindRequest}->{name} =~ m{@} ) {
104     my $old = $request->{bindRequest}->{name};
105     $request->{bindRequest}->{name} =~ s/[@\.]/,dc=/g;
106     $request->{bindRequest}->{name} =~ s/^/uid=/;
107     warn "rewrite bind cn $old -> ", $request->{bindRequest}->{name};
108     Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug;
109     $pdu = $LDAPRequest->encode($request);
110     Convert::ASN1::asn_hexdump(\*STDOUT,$pdu) if $debug;
111     }
112     }
113    
114     return $pdu;
115 dpavlin 8 }
116    
117     sub log_response {
118     my $pdu=shift;
119    
120 dpavlin 28 # print '-' x 80,"\n";
121     # print "Response ASN 1:\n";
122     # Convert::ASN1::asn_hexdump(\*STDOUT,$pdu);
123     # print "Response Perl:\n";
124 dpavlin 8 my $response = $LDAPResponse->decode($pdu);
125    
126     if ( defined $response->{protocolOp}->{searchResEntry} ) {
127     my $uid = $response->{protocolOp}->{searchResEntry}->{objectName};
128 dpavlin 37 warn "## objectName $uid";
129 dpavlin 10
130 dpavlin 19 my @attrs;
131    
132 dpavlin 8 map {
133 dpavlin 19 if ( $_->{type} eq 'hrEduPersonUniqueNumber' ) {
134     foreach my $val ( @{ $_->{vals} } ) {
135     next if $val !~ m{.+:.+};
136     my ( $n, $v ) = split(/\s*:\s*/, $val );
137     push @attrs, { type => $_->{type} . '_' . $n, vals => [ $v ] };
138     }
139 dpavlin 8 }
140     } @{ $response->{protocolOp}->{searchResEntry}->{attributes} };
141    
142 dpavlin 19 warn "# ++ attrs ",dump( @attrs );
143    
144     push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, $_ foreach @attrs;
145    
146 dpavlin 18 my $path = $config->{yaml_dir} . "$uid.yaml";
147 dpavlin 10 if ( -e $path ) {
148     my $data = LoadFile($path);
149     warn "# yaml = ",dump($data);
150 dpavlin 8
151 dpavlin 10 foreach my $type ( keys %$data ) {
152    
153     my $vals = $data->{$type};
154    
155 dpavlin 22 push @{ $response->{protocolOp}->{searchResEntry}->{attributes} }, {
156     type => $config->{overlay_prefix} . $type,
157     vals => ref($vals) eq 'ARRAY' ? $vals : [ $vals ],
158     };
159 dpavlin 10 }
160     }
161    
162 dpavlin 8 $pdu = $LDAPResponse->encode($response);
163     }
164    
165 dpavlin 28 warn "## response = ", dump($response);
166 dpavlin 10
167 dpavlin 8 return $pdu;
168     }
169    
170     sub run_proxy {
171     my $listenersock = shift;
172     my $targetsock=shift;
173    
174     die "Could not create listener socket: $!\n" unless $listenersock;
175     die "Could not create connection to server: $!\n" unless $targetsock;
176    
177     my $sel = IO::Select->new($listenersock);
178     my %Handlers;
179     while (my @ready = $sel->can_read) {
180     foreach my $fh (@ready) {
181     if ($fh == $listenersock) {
182     # let's create a new socket
183     my $psock = $listenersock->accept;
184     $sel->add($psock);
185     } else {
186     my $result = handle($fh,$targetsock);
187     if ($result) {
188     # we have finished with the socket
189     $sel->remove($fh);
190     $fh->close;
191     delete $Handlers{*$fh};
192     }
193     }
194     }
195     }
196     }
197    
198    
199     my $listenersock = IO::Socket::INET->new(
200     Listen => 5,
201     Proto => 'tcp',
202     Reuse => 1,
203 dpavlin 18 LocalAddr => $config->{listen},
204 dpavlin 29 ) || die "can't open listen socket: $!";
205 dpavlin 8
206    
207 dpavlin 18 my $targetsock = $config->{upstream_ssl}
208     ? IO::Socket::INET->new(
209     Proto => 'tcp',
210     PeerAddr => $config->{upstream_ldap},
211     PeerPort => 389,
212     )
213     : IO::Socket::SSL->new( $config->{upstream_ldap} . ':ldaps')
214 dpavlin 29 || die "can't open upstream socket: $!";
215 dpavlin 8
216     run_proxy($listenersock,$targetsock);
217    
218     1;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26