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