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 = 3; # 100; # FIXME |
our $max_results = 15; # 100; # FIXME |
29 |
|
|
30 |
our $objectclass = 'HrEduPerson'; |
our $objectclass = 'HrEduPerson'; |
31 |
|
|
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 $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 |
|
|
|
|
|
}, |
|
|
}; |
|
|
|
|
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', |
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; |
189 |
$sql_where = " where $sql_where"; |
$sql_where = " where $sql_where"; |
190 |
} |
} |
191 |
|
|
192 |
my $sql_select = $objectclass_sql->{ $objectclass } || die "can't find SQL query for $objectclass"; |
my $sql_select = read_file( lc "sql/$objectclass.sql" ); |
193 |
|
|
194 |
warn "# SQL:\n$sql_select\n$sql_where\n# DATA: ",dump( @values ); |
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 |
|
|
199 |
|
|
200 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
201 |
|
|
202 |
die "no objectClass column in $sql_select" unless defined $row->{objectClass}; |
my ( $dn, $attributes ) = _dn_attributes( $row, $base ); |
|
|
|
|
$row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n}; |
|
|
|
|
|
warn "## row = ",dump( $row ); |
|
|
|
|
|
my $dn = delete( $row->{dn} ) || die "no dn in $sql_select"; |
|
203 |
|
|
204 |
my $entry = Net::LDAP::Entry->new; |
my $entry = Net::LDAP::Entry->new; |
205 |
$entry->dn( $dn ); |
$entry->dn( $dn ); |
206 |
$entry->add( %$row ); |
$entry->add( %$attributes ); |
207 |
|
|
208 |
#$entry->changetype( 'modify' ); |
#$entry->changetype( 'modify' ); |
209 |
|
|