2 |
|
|
3 |
use strict; |
use strict; |
4 |
use warnings; |
use warnings; |
|
use Data::Dump qw/dump/; |
|
5 |
|
|
6 |
use lib '../lib'; |
use lib '../lib'; |
7 |
|
|
8 |
use Net::LDAP::Constant qw(LDAP_SUCCESS); |
use Net::LDAP::Constant qw(LDAP_SUCCESS); |
9 |
use Net::LDAP::Server; |
use Net::LDAP::Server; |
10 |
use base 'Net::LDAP::Server'; |
use base 'Net::LDAP::Server'; |
11 |
use fields qw(); |
use fields qw(); |
12 |
|
|
13 |
use DBI; |
use DBI; |
14 |
|
use File::Slurp; |
15 |
|
|
16 |
|
use Data::Dump qw/dump/; |
17 |
|
|
18 |
|
my $debug = 0; # XXX very slow |
19 |
|
|
20 |
# XXX test with: |
# XXX test with: |
21 |
# |
# |
27 |
our $user = 'unconfigured-user'; |
our $user = 'unconfigured-user'; |
28 |
our $passwd = 'unconfigured-password'; |
our $passwd = 'unconfigured-password'; |
29 |
|
|
30 |
our $max_results = 3; # 100; # FIXME |
our $max_results = $ENV{MAX_RESULTS} || 3000; # FIXME must be enough for all users |
31 |
|
our $objectclass_default = 'hrEduPerson'; |
32 |
|
|
33 |
our $objectclass = 'HrEduPerson'; |
our $objectclass; |
34 |
|
|
35 |
$SIG{__DIE__} = sub { |
$SIG{__DIE__} = sub { |
36 |
warn "!!! DIE ", @_; |
warn "!!! DIE ", @_; |
41 |
|
|
42 |
my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
43 |
|
|
|
# Net::LDAP::Entry will lc all our attribute names anyway, so |
|
|
# we don't really care about correctCapitalization for LDAP |
|
|
# attributes which won't pass through DBI |
|
|
my $objectclass_sql = { |
|
|
|
|
|
HrEduPerson => q{ |
|
|
|
|
|
select |
|
|
concat('uid=',trim(userid),',dc=ffzg,dc=hr') as dn, |
|
|
'person |
|
|
organizationalPerson |
|
|
inetOrgPerson |
|
|
hrEduPerson' as objectClass, |
|
|
|
|
|
trim(userid) as uid, |
|
|
firstname as givenName, |
|
|
surname as sn, |
|
|
concat(firstname,' ',surname) as cn, |
|
|
|
|
|
-- SAFEQ specific mappings from UMgr-LDAP.conf |
|
|
cardnumber as objectGUID, |
|
|
surname as displayName, |
|
|
rfid_sid as pager, |
|
|
email as mail, |
|
|
categorycode as ou, |
|
|
categorycode as organizationalUnit, |
|
|
categorycode as memberOf, |
|
|
categorycode as department, |
|
|
concat('/home/',borrowernumber) as homeDirectory |
|
|
from borrowers |
|
|
|
|
|
}, |
|
|
|
|
|
organizationalUnit => q{ |
|
|
|
|
|
select |
|
|
concat('ou=',categorycode) as dn, |
|
|
'organizationalUnit |
|
|
top' as objectClass, |
|
|
|
|
|
hex(md5(categorycode)) % 10000 as objectGUID, |
|
|
|
|
|
categorycode as ou, |
|
|
description as displayName |
|
|
from categories |
|
|
|
|
|
}, |
|
|
}; |
|
|
|
|
44 |
# we need reverse LDAP -> SQL mapping for where clause |
# we need reverse LDAP -> SQL mapping for where clause |
45 |
|
|
46 |
my $ldap_sql_mapping = { |
my $ldap_sql_mapping = { |
47 |
'uid' => 'userid', |
'uid' => 'userid', |
48 |
'objectGUID' => 'borrowernumber', |
'objectGUID' => 'b.borrowernumber', |
49 |
'displayName' => 'surname', |
'displayName' => 'surname', |
50 |
'sn' => 'surname', |
'sn' => 'surname', |
51 |
'pager' => 'rfid_sid', |
'pager' => 'a.attribute', # was: rfid_sid |
52 |
}; |
}; |
53 |
|
|
54 |
sub __sql_column { |
sub __sql_column { |
116 |
} |
} |
117 |
} |
} |
118 |
|
|
119 |
|
|
120 |
|
# my ( $dn,$attributes ) = _dn_attributes( $row, $base ); |
121 |
|
|
122 |
|
sub _dn_attributes { |
123 |
|
my ($row,$base) = @_; |
124 |
|
|
125 |
|
warn "## row = ",dump( $row ) if $debug; |
126 |
|
|
127 |
|
die "no objectClass column in ",dump( $row ) unless defined $row->{objectClass}; |
128 |
|
|
129 |
|
$row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n}; |
130 |
|
|
131 |
|
warn "## row = ",dump( $row ) if $debug; |
132 |
|
|
133 |
|
my $dn = delete( $row->{dn} ) || die "no dn in ",dump( $row ); |
134 |
|
|
135 |
|
# this does some sanity cleanup for our data |
136 |
|
# my $base_as_domain = $base; |
137 |
|
# $base_as_domain =~ s{dn=}{.}; |
138 |
|
# $base_as_domain =~ s{^\.}{@}; |
139 |
|
# $dn =~ s{$base_as_domain$}{}; |
140 |
|
# |
141 |
|
# $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present |
142 |
|
|
143 |
|
return ($dn, $row); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
# the search operation |
# the search operation |
148 |
sub search { |
sub search { |
149 |
my $self = shift; |
my $self = shift; |
159 |
|
|
160 |
my $sql_where = ''; |
my $sql_where = ''; |
161 |
@values = (); |
@values = (); |
162 |
|
$objectclass = ''; |
163 |
|
|
164 |
foreach my $filter ( keys %{ $reqData->{'filter'} } ) { |
foreach my $filter ( keys %{ $reqData->{'filter'} } ) { |
165 |
|
|
181 |
} |
} |
182 |
} |
} |
183 |
|
|
|
$sql_where .= ' ' . join( " $filter ", @limits ); |
|
|
|
|
184 |
} else { |
} else { |
185 |
__ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} ); |
__ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} ); |
186 |
} |
} |
187 |
|
|
188 |
|
$sql_where .= ' ' . join( " $filter ", @limits ) if @limits; |
189 |
|
|
190 |
} |
} |
191 |
|
|
192 |
|
$objectclass ||= $objectclass_default; |
193 |
|
|
194 |
|
my $sql_select = read_file( lc "sql/$objectclass.sql" ); |
195 |
if ( $sql_where ) { |
if ( $sql_where ) { |
196 |
$sql_where = " where $sql_where"; |
if ( $sql_select !~ m{where}i ) { |
197 |
|
$sql_where = " where $sql_where"; |
198 |
|
} else { |
199 |
|
$sql_where = " and $sql_where"; |
200 |
|
} |
201 |
} |
} |
202 |
|
|
|
my $sql_select = $objectclass_sql->{ $objectclass } || die "can't find SQL query for $objectclass"; |
|
203 |
|
|
204 |
warn "# SQL:\n$sql_select\n$sql_where\n# DATA: ",dump( @values ); |
my $sql |
205 |
my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit? |
= $sql_select |
206 |
|
. $sql_where |
207 |
|
# . ( $objectclass =~ m{person}i ? " LIMIT $max_results" : '' ) # add limit just for persons |
208 |
|
; |
209 |
|
|
210 |
|
warn "# SQL:\n$sql\n# DATA: ",dump( @values ); |
211 |
|
my $sth = $dbh->prepare( $sql ); |
212 |
$sth->execute( @values ); |
$sth->execute( @values ); |
213 |
|
|
214 |
warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} ); |
warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} ); |
215 |
|
|
216 |
|
my $last_dn = '?'; |
217 |
|
my $entry; |
218 |
|
|
219 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
220 |
|
|
221 |
die "no objectClass column in $sql_select" unless defined $row->{objectClass}; |
my ( $dn, $attributes ) = _dn_attributes( $row, $base ); |
222 |
|
|
223 |
$row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n}; |
warn "# dn $last_dn ... $dn\n"; |
224 |
|
|
225 |
warn "## row = ",dump( $row ); |
if ( $dn ne $last_dn ) { |
226 |
|
|
227 |
my $dn = delete( $row->{dn} ) || die "no dn in $sql_select"; |
if ( $entry ) { |
228 |
|
#$entry->changetype( 'modify' ); |
229 |
|
warn "### entry ",$entry->dump( \*STDERR ); |
230 |
|
push @entries, $entry; |
231 |
|
undef $entry; |
232 |
|
} |
233 |
|
|
234 |
|
$dn =~ s{@[^,]+}{}; |
235 |
|
|
236 |
my $entry = Net::LDAP::Entry->new; |
$entry = Net::LDAP::Entry->new; |
237 |
$entry->dn( $dn ); |
$entry->dn( $dn ); |
|
$entry->add( %$row ); |
|
238 |
|
|
239 |
#$entry->changetype( 'modify' ); |
$entry->add( %$attributes ); |
240 |
|
|
241 |
warn "### entry ",$entry->dump( \*STDERR ); |
} else { |
242 |
|
foreach my $n ( keys %$attributes ) { |
243 |
|
my $v = $attributes->{$n}; |
244 |
|
warn "# attr $n = $v\n"; |
245 |
|
$entry->add( $n, $v ) if $entry->get_value( $n ) ne $v; |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
$last_dn = $dn; |
251 |
|
|
252 |
|
} |
253 |
|
|
254 |
|
if ( $entry ) { |
255 |
|
warn "### last entry ",$entry->dump( \*STDERR ); |
256 |
push @entries, $entry; |
push @entries, $entry; |
257 |
} |
} |
258 |
|
|