. . . # implementatie functie bind_object: zie reeks 6, oefening 4
. . . # implementatie functie valueattribuut: zie oefening 1
use Win32::OLE qw(in);
#$Win32::OLE::Warn = 3;
sub bind_object{
my $parameter=shift;
my $moniker;
if ( uc($ENV{USERDOMAIN}) eq "III") { #ingelogd op het III domein
$moniker = (uc( substr( $parameter, 0, 7) ) eq "LDAP://" ? "" : "LDAP://").$parameter;
return (Win32::OLE->GetObject($moniker));
}
else { #niet ingelogd
my $ip="193.190.126.71"; #voor de controle thuis
$moniker = (uc( substr( $parameter, 0, 7) ) eq "LDAP://" ? "" : "LDAP://$ip/").$parameter;
my $dso = Win32::OLE->GetObject("LDAP:");
my $loginnaam = "..."; #vul in
my $paswoord = "..."; #vul in
return ($dso->OpenDSObject( $moniker, $loginnaam, $paswoord, 1 ));
}
}
sub valueattribuut {
my ($object,$attribuut)=@_;
my $attr_schema = bind_object( "schema/$attribuut" );
my $tabel = $object->GetEx($attribuut);
if (Win32::OLE->LastError() == Win32::OLE::HRESULT(0x8000500D)){
$object->GetInfoEx([$attribuut], 0);
$tabel = $object->GetEx($attribuut);
}
return [""] if Win32::OLE->LastError() == Win32::OLE::HRESULT(0x8000500D);
my $v=[];
foreach ( in $tabel ) {
if ( $attr_schema->{Syntax} eq "OctetString" ) {
$waarde = sprintf ("%*v02X ","", $_) ;
}
elsif ( $attr_schema->{Syntax} eq "ObjectSecurityDescriptor" ) {
$waarde = "eigenaar is ... " . $_->{owner};
}
elsif ( $attr_schema->{Syntax} eq "INTEGER8" ) {
$waarde = convert_BigInt_string($_->{HighPart},$_->{LowPart});
}
else {
$waarde = $_;
}
push @{$v},$waarde;
}
return $v;
}
use Math::BigInt;
sub convert_BigInt_string{
my ($high,$low)=@_;
my $HighPart = Math::BigInt->new($high);
my $LowPart = Math::BigInt->new($low);
my $Radix = Math::BigInt->new('0x100000000'); #dit is 2^32
$LowPart+=$Radix if ($LowPart<0); #als unsigned int interperteren
return ($HighPart * $Radix + $LowPart);
}
if ($WScript->Arguments->{count} !=1){
print "geef als enige parameter de SamAccountName van de user\n";
return;
}
my $user = $WScript->Arguments->Item(0);
my $ADOconnection = Win32::OLE->CreateObject("ADODB.Connection");
$ADOconnection->{Provider} = "ADsDSOObject";
$ADOconnection->{Properties}->{"User ID"} = "mdn"; # vul in of zet in commentaar
$ADOconnection->{Properties}->{"Password"} = "wo62lf59"; # vul in of zet in commentaar
$ADOconnection->{Properties}->{"Encrypt Password"} = True;
$ADOconnection->Open(); # mag je niet vergeten
my $ADOcommand = Win32::OLE->CreateObject("ADODB.Command");
$ADOcommand->{ActiveConnection} = $ADOconnection; # verwijst naar het voorgaand object
$ADOcommand->{Properties}->{"Page Size"} = 20;
my $sBase = "LDAP://";
$sBase .= "193.190.126.71/" unless (uc($ENV{USERDOMAIN}) eq "III"); # als je niet in het domein zelf zit
$sBase .= "DC=iii,DC=hogent,DC=be";
my $sFilter = "(&(objectcategory=person)(objectclass=user))";
my $sAttributes = "adspath,samAccountName";
my $sScope = "subtree";
$ADOcommand->{CommandText} = "<$sBase>;$sFilter;$sAttributes;$sScope";
my $ADOrecordset = $ADOcommand->Execute();
my %lijst;
until ( $ADOrecordset->{EOF} ) {
$lijst{lc($ADOrecordset->Fields("samaccountname")->{Value})}
=$ADOrecordset->Fields("adspath")->{Value};
$ADOrecordset->MoveNext();
}
$ADOrecordset->Close();
my %samlijst,%userlijst,%groeplijst;
sub groepenlijst {
$sFilter = "(objectcategory=group)";
$sAttributes = "samaccountname,adspath,objectclass";
$ADOcommand->{CommandText} = "<$sBase>;$sFilter;$sAttributes;$sScope";
my $ADOrecordset = $ADOcommand->Execute();
until ( $ADOrecordset->{EOF} ) {
my $adsPath=$ADOrecordset->Fields("adsPath")->{Value};
my $samAccountName=lc($ADOrecordset->Fields("samAccountName")->{Value});
$groeplijst{$samAccountName}=$adsPath;
$ADOrecordset->MoveNext();
}
$ADOrecordset->Close();
}
sub lijsten_maken{
$sFilter = "(samAccountName=*)";
$sAttributes = "samAccountName,adsPath,objectclass";
$ADOcommand->{CommandText} = "<$sBase>;$sFilter;$sAttributes;$sScope";
my $ADOrecordset = $ADOcommand->Execute();
until ( $ADOrecordset->{EOF} ) {
my $adsPath=$ADOrecordset->Fields("adsPath")->{Value};
my $samAccountName=lc($ADOrecordset->Fields("samAccountName")->{Value});
my $objectclass=$ADOrecordset->Fields("objectclass")->{Value};
$samlijst{$samAccountName}=$adsPath;
$userlijst{$samAccountName}=$adsPath if $objectclass->[$#{$objectclass}] eq "user";
$ADOrecordset->MoveNext();
}
$ADOrecordset->Close();
groepenlijst();
}
lijsten_maken();
while ((my $key,$value)=each(%groeplijst)){
printf "\n%s\t:%s",$key,$value;
}
$ADOconnection->Close();