5 use Data::Dump qw/dump/;
8 use Net::LDAP::Constant qw(LDAP_SUCCESS);
10 use base 'Net::LDAP::Server';
17 # ldapsearch -h localhost -p 2389 -b dc=ffzg,dc=hr -x 'otherPager=200903160021'
20 our $dsn = 'DBI:mysql:dbname=';
21 our $database = 'koha';
22 our $user = 'unconfigured-user';
23 our $passwd = 'unconfigured-password';
25 our $max_results = 10; # 100; # FIXME
27 require 'config.pl' if -e 'config.pl';
29 my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
31 # Net::LDAP::Entry will lc all our attribute names anyway, so
32 # we don't really care about correctCapitalization for LDAP
33 # attributes which won't pass through DBI
37 firstname as givenName,
39 concat(firstname,' ',surname) as cn,
41 -- SAFEQ specific mappings from UMgr-LDAP.conf
42 surname as displayName,
45 categorycode as organizationalUnit,
46 borrowernumber as objectGUID,
47 concat('/home/',borrowernumber) as homeDirectory
51 # we need reverse LDAP -> SQL mapping for where clause
52 my $ldap_sql_mapping = {
54 'objectGUID' => 'borrowernumber',
55 'displayName' => 'surname',
57 'pager' => 'cardnumber',
60 # attributes which are same for whole set, but somehow
61 # LDAP clients are sending they anyway and we don't
62 # have them in database
69 $ldap_sql_mapping->{$name} || $name;
72 use constant RESULT_OK => {
75 'resultCode' => LDAP_SUCCESS
80 my ($class, $sock) = @_;
81 my $self = $class->SUPER::new($sock);
82 print "connection from: ", $sock->peerhost(), "\n";
90 warn "# bind ",dump($reqData);
97 sub __ldap_search_to_sql {
98 my ( $how, $what ) = @_;
99 warn "### how $how\n";
100 if ( $how eq 'equalityMatch' && defined $what ) {
101 my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?";
102 my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?";
103 if ( ! $ldap_ignore->{ $name } ) {
104 push @limits, __sql_column($name) . ' = ?';
105 push @values, $value;
107 } elsif ( $how eq 'substrings' ) {
108 foreach my $substring ( @{ $what->{substrings} } ) {
109 my $name = $what->{type} || warn "ERROR: no type?";
110 while ( my($op,$value) = each %$substring ) {
111 push @limits, __sql_column($name) . ' LIKE ?';
112 if ( $op eq 'any' ) {
113 $value = '%' . $value . '%';
115 warn "UNSUPPORTED: op $op - using plain $value";
117 push @values, $value;
120 } elsif ( $how eq 'present' ) {
121 my $name = __sql_column( $what );
122 push @limits, "$name IS NOT NULL and length($name) > 1";
123 ## XXX length(foo) > 1 to avoid empty " " strings
125 warn "UNSUPPORTED: how $how what ",dump( $what );
129 # the search operation
133 print "searching...\n";
135 warn "# " . localtime() . " request = ", dump($reqData);
137 my $base = $reqData->{'baseObject'}; # FIXME use it?
140 if ( $reqData->{'filter'} ) {
145 foreach my $join_with ( keys %{ $reqData->{'filter'} } ) {
147 warn "## join_with $join_with\n";
151 foreach my $filter ( @{ $reqData->{'filter'}->{ $join_with } } ) {
152 warn "### filter ",dump($filter),$/;
153 foreach my $how ( keys %$filter ) {
154 if ( $how eq 'or' ) {
155 __ldap_search_to_sql( %$_ ) foreach ( @{ $filter->{$how} } );
157 __ldap_search_to_sql( $how, $filter->{$how} );
159 warn "## limits ",dump(@limits), " values ",dump(@values);
163 $sql_where .= ' ' . join( " $join_with ", @limits );
168 $sql_where = " where $sql_where";
171 warn "# SQL:\n$sql_select $sql_where\n# DATA: ",dump( @values );
172 my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?
173 $sth->execute( @values );
175 warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );
177 while (my $row = $sth->fetchrow_hashref) {
179 warn "## row = ",dump( $row );
181 my $dn = 'uid=' . $row->{uid} || die "no uid";
182 $dn =~ s{[@\.]}{,dc=}g;
184 my $entry = Net::LDAP::Entry->new;
185 $entry->dn( $dn . $base );
186 $entry->add( %$row );
188 #warn "### entry ",dump( $entry );
190 push @entries, $entry;
194 warn "UNKNOWN request: ",dump( $reqData );
197 return RESULT_OK, @entries;
200 # the rest of the operations will return an "unwilling to perform"