fccc38f99894ae987601cce436fab4b245125df1
[virtual-ldap] / lib / LDAP / Koha.pm
1 package LDAP::Koha;
2
3 use strict;
4 use warnings;
5
6 use lib '../lib';
7
8 use Net::LDAP::Constant qw(LDAP_SUCCESS);
9 use Net::LDAP::Server;
10 use base 'Net::LDAP::Server';
11 use fields qw();
12
13 use DBI;
14 use File::Slurp;
15
16 use Data::Dump qw/dump/;
17
18 # XXX test with:
19 #
20 # ldapsearch -h localhost -p 2389 -b dc=ffzg,dc=hr -x 'otherPager=200903160021'
21 #
22
23 our $dsn      = 'DBI:mysql:dbname=';
24 our $database = 'koha';
25 our $user     = 'unconfigured-user';
26 our $passwd   = 'unconfigured-password';
27
28 our $max_results = 1500; # 100; # FIXME
29
30 our $objectclass = 'HrEduPerson';
31
32 $SIG{__DIE__} = sub {
33         warn "!!! DIE ", @_;
34         die @_;
35 };
36
37 require 'config.pl' if -e 'config.pl';
38
39 my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
40
41 # we need reverse LDAP -> SQL mapping for where clause
42
43 my $ldap_sql_mapping = {
44         'uid'           => 'userid',
45         'objectGUID'    => 'borrowernumber',
46         'displayName'   => 'surname',
47         'sn'            => 'surname',
48         'pager'         => 'rfid_sid',
49 };
50
51 sub __sql_column {
52         my $name = shift;
53         $ldap_sql_mapping->{$name} || $name;
54 }
55
56 use constant RESULT_OK => {
57         'matchedDN' => '',
58         'errorMessage' => '',
59         'resultCode' => LDAP_SUCCESS
60 };
61
62 # constructor
63 sub new {
64         my ($class, $sock) = @_;
65         my $self = $class->SUPER::new($sock);
66         print "connection from: ", $sock->peerhost(), "\n";
67         return $self;
68 }
69
70 # the bind operation
71 sub bind {
72         my $self = shift;
73         my $reqData = shift;
74         warn "# bind ",dump($reqData);
75         return RESULT_OK;
76 }
77
78 our @values;
79 our @limits;
80
81 sub __ldap_search_to_sql {
82         my ( $how, $what ) = @_;
83         warn "### __ldap_search_to_sql $how ",dump( $what ),"\n";
84         if ( $how eq 'equalityMatch' && defined $what ) {
85                 my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?";
86                 my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?";
87
88                 if ( lc $name eq 'objectclass' ) {
89                         $objectclass = $value;
90                 } else {
91                         push @limits, __sql_column($name) . ' = ?';
92                         push @values, $value;
93                 }
94         } elsif ( $how eq 'substrings' ) {
95                 foreach my $substring ( @{ $what->{substrings} } ) {
96                         my $name = $what->{type} || warn "ERROR: no type?";
97                         while ( my($op,$value) = each %$substring ) {
98                                 push @limits, __sql_column($name) . ' LIKE ?';
99                                 if ( $op eq 'any' ) {
100                                         $value = '%' . $value . '%';
101                                 } else {
102                                         warn "UNSUPPORTED: op $op - using plain $value";
103                                 }
104                                 push @values, $value;
105                         }
106                 }
107         } elsif ( $how eq 'present' ) {
108                 my $name = __sql_column( $what );
109                 push @limits, "$name IS NOT NULL and length($name) > 1";
110                 ## XXX length(foo) > 1 to avoid empty " " strings
111         } else {
112                 warn "UNSUPPORTED: $how ",dump( $what );
113         }
114 }
115
116
117 # my ( $dn,$attributes ) = _dn_attributes( $row, $base );
118
119 sub _dn_attributes {
120         my ($row,$base) = @_;
121
122         warn "## row = ",dump( $row );
123
124         die "no objectClass column in ",dump( $row ) unless defined $row->{objectClass};
125
126         $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n};
127
128         warn "## row = ",dump( $row );
129
130         my $dn = delete( $row->{dn} ) || die "no dn in ",dump( $row );
131
132         # this does some sanity cleanup for our data
133         my $base_as_domain = $base;
134         $base_as_domain =~ s{dn=}{.};
135         $base_as_domain =~ s{^\.}{@};
136         $dn =~ s{$base_as_domain$}{};
137
138         $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present
139
140         return ($dn, $row);
141 }
142
143
144 # the search operation
145 sub search {
146         my $self = shift;
147         my $reqData = shift;
148         print "searching...\n";
149
150         warn "# " . localtime() . " request = ", dump($reqData);
151
152         my $base = $reqData->{'baseObject'}; # FIXME use it?
153
154         my @entries;
155         if ( $reqData->{'filter'} ) {
156
157                 my $sql_where = '';
158                 @values = ();
159
160                 foreach my $filter ( keys %{ $reqData->{'filter'} } ) {
161
162                         warn "## filter $filter ", dump( $reqData->{'filter'}->{ $filter } ), "\n";
163
164                         @limits = ();
165
166                         if ( ref $reqData->{'filter'}->{ $filter } eq 'ARRAY' ) {
167
168                                 foreach my $filter ( @{ $reqData->{'filter'}->{ $filter } } ) {
169                                         warn "### filter ",dump($filter),$/;
170                                         foreach my $how ( keys %$filter ) {
171                                                 if ( $how eq 'or' ) {
172                                                         __ldap_search_to_sql( %$_ ) foreach ( @{ $filter->{$how} } );
173                                                 } else {
174                                                         __ldap_search_to_sql( $how, $filter->{$how} );
175                                                 }
176                                                 warn "## limits ",dump(@limits), " values ",dump(@values);
177                                         }
178                                 }
179
180                                 $sql_where .= ' ' . join( " $filter ", @limits );
181
182                         } else {
183                                 __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );
184                         }
185
186                 }
187
188                 if ( $sql_where ) {
189                         $sql_where = " where $sql_where";
190                 }
191
192                 my $sql_select = read_file( lc "sql/$objectclass.sql" );
193
194                 warn "# SQL:\n$sql_select\n", $sql_where ? $sql_where : '-- no where', "\n# DATA: ",dump( @values );
195                 my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?
196                 $sth->execute( @values );
197
198                 warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );
199
200                 my $last_dn = '?';
201                 my $entry;
202
203                 while (my $row = $sth->fetchrow_hashref) {
204
205                         my ( $dn, $attributes ) = _dn_attributes( $row, $base );
206
207                         warn "# dn $last_dn ... $dn\n";
208
209                         if ( $dn ne $last_dn ) {
210
211                                 if ( $entry ) {
212                                         #$entry->changetype( 'modify' );
213                                         warn "### entry ",$entry->dump( \*STDERR );
214                                         push @entries, $entry;
215                                         undef $entry;
216                                 }
217
218                                 $entry = Net::LDAP::Entry->new;
219                                 $entry->dn( $dn );
220
221                                 $entry->add( %$attributes );
222
223                         } else {
224                                 foreach my $n ( keys %$attributes ) {
225                                         my $v = $attributes->{$n};
226                                         warn "# attr $n = $v\n";
227                                         $entry->add( $n, $v ) if $entry->get_value( $n ) ne $v;
228                                 }
229                         }
230
231
232                         $last_dn = $dn;
233
234                 }
235
236                 warn "### last entry ",$entry->dump( \*STDERR );
237                 push @entries, $entry;
238
239         } else {
240                 warn "UNKNOWN request: ",dump( $reqData );
241         }
242
243         return RESULT_OK, @entries;
244 }
245
246 # the rest of the operations will return an "unwilling to perform"
247
248 1;