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 |
# XXX test with: |
# XXX test with: |
19 |
# |
# |
25 |
our $user = 'unconfigured-user'; |
our $user = 'unconfigured-user'; |
26 |
our $passwd = 'unconfigured-password'; |
our $passwd = 'unconfigured-password'; |
27 |
|
|
28 |
our $max_results = 10; # 100; # FIXME |
our $max_results = 3000; # FIXME must be enough for all users |
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'; |
require 'config.pl' if -e 'config.pl'; |
38 |
|
|
39 |
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; |
40 |
|
|
|
# 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 $sql_select = q{ |
|
|
select |
|
|
trim(userid) as uid, |
|
|
firstname as givenName, |
|
|
surname as sn, |
|
|
concat(firstname,' ',surname) as cn, |
|
|
|
|
|
-- SAFEQ specific mappings from UMgr-LDAP.conf |
|
|
surname as displayName, |
|
|
rfid_sid as pager, |
|
|
email as mail, |
|
|
categorycode as ou, |
|
|
categorycode as organizationalUnit, |
|
|
categorycode as memberOf, |
|
|
categorycode as department, |
|
|
borrowernumber as objectGUID, |
|
|
concat('/home/',borrowernumber) as homeDirectory |
|
|
from borrowers |
|
|
}; |
|
|
|
|
41 |
# we need reverse LDAP -> SQL mapping for where clause |
# we need reverse LDAP -> SQL mapping for where clause |
42 |
|
|
43 |
my $ldap_sql_mapping = { |
my $ldap_sql_mapping = { |
44 |
'uid' => 'userid', |
'uid' => 'userid', |
45 |
'objectGUID' => 'borrowernumber', |
'objectGUID' => 'borrowernumber', |
48 |
'pager' => 'rfid_sid', |
'pager' => 'rfid_sid', |
49 |
}; |
}; |
50 |
|
|
|
# attributes which are same for whole set, but somehow |
|
|
# LDAP clients are sending they anyway and we don't |
|
|
# have them in database |
|
|
my $ldap_ignore = { |
|
|
'objectclass' => 1, |
|
|
}; |
|
|
|
|
51 |
sub __sql_column { |
sub __sql_column { |
52 |
my $name = shift; |
my $name = shift; |
53 |
$ldap_sql_mapping->{$name} || $name; |
$ldap_sql_mapping->{$name} || $name; |
84 |
if ( $how eq 'equalityMatch' && defined $what ) { |
if ( $how eq 'equalityMatch' && defined $what ) { |
85 |
my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?"; |
my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?"; |
86 |
my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?"; |
my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?"; |
87 |
if ( ! $ldap_ignore->{ $name } ) { |
|
88 |
|
if ( lc $name eq 'objectclass' ) { |
89 |
|
$objectclass = $value; |
90 |
|
} else { |
91 |
push @limits, __sql_column($name) . ' = ?'; |
push @limits, __sql_column($name) . ' = ?'; |
92 |
push @values, $value; |
push @values, $value; |
|
} else { |
|
|
warn "IGNORED: $name = $value"; |
|
93 |
} |
} |
94 |
} elsif ( $how eq 'substrings' ) { |
} elsif ( $how eq 'substrings' ) { |
95 |
foreach my $substring ( @{ $what->{substrings} } ) { |
foreach my $substring ( @{ $what->{substrings} } ) { |
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 |
# the search operation |
145 |
sub search { |
sub search { |
146 |
my $self = shift; |
my $self = shift; |
177 |
} |
} |
178 |
} |
} |
179 |
|
|
|
$sql_where .= ' ' . join( " $filter ", @limits ); |
|
|
|
|
180 |
} else { |
} else { |
181 |
__ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} ); |
__ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} ); |
182 |
} |
} |
183 |
|
|
184 |
|
$sql_where .= ' ' . join( " $filter ", @limits ); |
185 |
|
|
186 |
} |
} |
187 |
|
|
188 |
if ( $sql_where ) { |
if ( $sql_where ) { |
189 |
$sql_where = " where $sql_where"; |
$sql_where = " where $sql_where"; |
190 |
} |
} |
191 |
|
|
192 |
warn "# SQL:\n$sql_select\n$sql_where\n# DATA: ",dump( @values ); |
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? |
my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit? |
196 |
$sth->execute( @values ); |
$sth->execute( @values ); |
197 |
|
|
198 |
warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} ); |
warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} ); |
199 |
|
|
200 |
|
my $last_dn = '?'; |
201 |
|
my $entry; |
202 |
|
|
203 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
204 |
|
|
205 |
warn "## row = ",dump( $row ); |
my ( $dn, $attributes ) = _dn_attributes( $row, $base ); |
206 |
|
|
207 |
my $dn = 'uid=' . $row->{uid} || die "no uid"; |
warn "# dn $last_dn ... $dn\n"; |
|
$dn =~ s{[@\.]}{,dc=}g; |
|
|
$dn .= ',' . $base unless $dn =~ m{dc}i; |
|
|
|
|
|
my $entry = Net::LDAP::Entry->new; |
|
|
$entry->dn( $dn ); |
|
|
$entry->add( objectClass => [ |
|
|
"person", |
|
|
"organizationalPerson", |
|
|
"inetOrgPerson", |
|
|
"hrEduPerson", |
|
|
] ); |
|
|
$entry->add( %$row ); |
|
208 |
|
|
209 |
#$entry->changetype( 'modify' ); |
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 |
|
|
|
warn "### entry ",$entry->dump( \*STDERR ); |
|
231 |
|
|
232 |
push @entries, $entry; |
$last_dn = $dn; |
233 |
|
|
234 |
} |
} |
235 |
|
|
236 |
|
warn "### last entry ",$entry->dump( \*STDERR ); |
237 |
|
push @entries, $entry; |
238 |
|
|
239 |
} else { |
} else { |
240 |
warn "UNKNOWN request: ",dump( $reqData ); |
warn "UNKNOWN request: ",dump( $reqData ); |
241 |
} |
} |