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 = 3; # 100; # FIXME
27 our $objectclass = 'HrEduPerson';
34 require 'config.pl' if -e 'config.pl';
36 my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
38 # Net::LDAP::Entry will lc all our attribute names anyway, so
39 # we don't really care about correctCapitalization for LDAP
40 # attributes which won't pass through DBI
41 my $objectclass_sql = {
46 concat('uid=',trim(userid),',dc=ffzg,dc=hr') as dn,
50 hrEduPerson' as objectClass,
53 firstname as givenName,
55 concat(firstname,' ',surname) as cn,
57 -- SAFEQ specific mappings from UMgr-LDAP.conf
58 cardnumber as objectGUID,
59 surname as displayName,
63 categorycode as organizationalUnit,
64 categorycode as memberOf,
65 categorycode as department,
66 concat('/home/',borrowernumber) as homeDirectory
71 organizationalUnit => q{
74 concat('ou=',categorycode) as dn,
78 hex(md5(categorycode)) % 10000 as objectGUID,
81 description as displayName
87 # we need reverse LDAP -> SQL mapping for where clause
88 my $ldap_sql_mapping = {
90 'objectGUID' => 'borrowernumber',
91 'displayName' => 'surname',
93 'pager' => 'rfid_sid',
98 $ldap_sql_mapping->{$name} || $name;
101 use constant RESULT_OK => {
103 'errorMessage' => '',
104 'resultCode' => LDAP_SUCCESS
109 my ($class, $sock) = @_;
110 my $self = $class->SUPER::new($sock);
111 print "connection from: ", $sock->peerhost(), "\n";
119 warn "# bind ",dump($reqData);
126 sub __ldap_search_to_sql {
127 my ( $how, $what ) = @_;
128 warn "### __ldap_search_to_sql $how ",dump( $what ),"\n";
129 if ( $how eq 'equalityMatch' && defined $what ) {
130 my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?";
131 my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?";
133 if ( lc $name eq 'objectclass' ) {
134 $objectclass = $value;
136 push @limits, __sql_column($name) . ' = ?';
137 push @values, $value;
139 } elsif ( $how eq 'substrings' ) {
140 foreach my $substring ( @{ $what->{substrings} } ) {
141 my $name = $what->{type} || warn "ERROR: no type?";
142 while ( my($op,$value) = each %$substring ) {
143 push @limits, __sql_column($name) . ' LIKE ?';
144 if ( $op eq 'any' ) {
145 $value = '%' . $value . '%';
147 warn "UNSUPPORTED: op $op - using plain $value";
149 push @values, $value;
152 } elsif ( $how eq 'present' ) {
153 my $name = __sql_column( $what );
154 push @limits, "$name IS NOT NULL and length($name) > 1";
155 ## XXX length(foo) > 1 to avoid empty " " strings
157 warn "UNSUPPORTED: $how ",dump( $what );
161 # the search operation
165 print "searching...\n";
167 warn "# " . localtime() . " request = ", dump($reqData);
169 my $base = $reqData->{'baseObject'}; # FIXME use it?
172 if ( $reqData->{'filter'} ) {
177 foreach my $filter ( keys %{ $reqData->{'filter'} } ) {
179 warn "## filter $filter ", dump( $reqData->{'filter'}->{ $filter } ), "\n";
183 if ( ref $reqData->{'filter'}->{ $filter } eq 'ARRAY' ) {
185 foreach my $filter ( @{ $reqData->{'filter'}->{ $filter } } ) {
186 warn "### filter ",dump($filter),$/;
187 foreach my $how ( keys %$filter ) {
188 if ( $how eq 'or' ) {
189 __ldap_search_to_sql( %$_ ) foreach ( @{ $filter->{$how} } );
191 __ldap_search_to_sql( $how, $filter->{$how} );
193 warn "## limits ",dump(@limits), " values ",dump(@values);
197 $sql_where .= ' ' . join( " $filter ", @limits );
200 __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );
206 $sql_where = " where $sql_where";
209 my $sql_select = $objectclass_sql->{ $objectclass } || die "can't find SQL query for $objectclass";
211 warn "# SQL:\n$sql_select\n$sql_where\n# DATA: ",dump( @values );
212 my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?
213 $sth->execute( @values );
215 warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );
217 while (my $row = $sth->fetchrow_hashref) {
219 die "no objectClass column in $sql_select" unless defined $row->{objectClass};
221 $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n};
223 warn "## row = ",dump( $row );
225 my $dn = delete( $row->{dn} ) || die "no dn in $sql_select";
227 my $entry = Net::LDAP::Entry->new;
229 $entry->add( %$row );
231 #$entry->changetype( 'modify' );
233 warn "### entry ",$entry->dump( \*STDERR );
235 push @entries, $entry;
239 warn "UNKNOWN request: ",dump( $reqData );
242 return RESULT_OK, @entries;
245 # the rest of the operations will return an "unwilling to perform"