Skip to content

Instantly share code, notes, and snippets.

@XavierGeerinck
Created January 5, 2016 09:54
Show Gist options
  • Save XavierGeerinck/75b09851b2854a038a21 to your computer and use it in GitHub Desktop.
Save XavierGeerinck/75b09851b2854a038a21 to your computer and use it in GitHub Desktop.
Property Cache Exercise (Active Directory)
# Laad CN=Administrator,CN=Users,DC=iii,DC=hogent,DC=be in en vraag volgende attributen op met GetEx:
# 'cn', 'allowedAttributes', 'mail', 'canonicalName', 'objectClass'
# Indien dit niet lukt, vang dit op en probeer met een andere methode
use Win32::OLE;
use Win32::OLE::Const 'Active DS Type Library';
use Win32::OLE::Variant;
# Error codes, zie AD Service Interfaces Reference ADSI Error Codes / Generic ADSI Error Codes
my %E_ADS = (
BAD_PATHNAME => Win32::OLE::HRESULT(0x80005000),
UNKNOWN_OBJECT => Win32::OLE::HRESULT(0x80005004),
PROPERTY_NOT_SET => Win32::OLE::HRESULT(0x80005005),
PROPERTY_INVALID => Win32::OLE::HRESULT(0x80005007),
BAD_PARAMETER => Win32::OLE::HRESULT(0x80005008),
OBJECT_UNBOUND => Win32::OLE::HRESULT(0x80005009),
PROPERTY_MODIFIED => Win32::OLE::HRESULT(0x8000500B),
OBJECT_EXISTS => Win32::OLE::HRESULT(0x8000500E),
SCHEMA_VIOLATION => Win32::OLE::HRESULT(0x8000500F),
COLUMN_NOT_SET => Win32::OLE::HRESULT(0x80005010),
ERRORSOCCURRED => Win32::OLE::HRESULT(0x00005011),
NOMORE_ROWS => Win32::OLE::HRESULT(0x00005012),
NOMORE_COLUMNS => Win32::OLE::HRESULT(0x00005013),
INVALID_FILTER => Win32::OLE::HRESULT(0x80005014),
INVALID_DOMAIN_OBJECT => Win32::OLE::HRESULT(0x80005001),
INVALID_USER_OBJECT => Win32::OLE::HRESULT(0x80005002),
INVALID_COMPUTER_OBJECT => Win32::OLE::HRESULT(0x80005003),
PROPERTY_NOT_SUPPORTED => Win32::OLE::HRESULT(0x80005006),
PROPERTY_NOT_MODIFIED => Win32::OLE::HRESULT(0x8000500A),
CANT_CONVERT_DATATYPE => Win32::OLE::HRESULT(0x8000500C),
PROPERTY_NOT_FOUND => Win32::OLE::HRESULT(0x8000500D)
);
@ARGV = ('cn', 'allowedAttributes', 'mail', 'canonicalName', 'objectClass');
# Bind aan object
my $rootObj = bind_object('rootDSE');
my $obj = bind_object('CN=Administrator,CN=Users,'.$rootObj->Get('defaultNamingContext'));
# Ga door elk attribuut opgegeven en probeer te printen
foreach my $arg (@ARGV) {
# Eerst proberen met GetEx, deze haalt op van property cache en kan dus niet bestaan
my $attr = $obj->GetEx($arg);
# Vang op! Indien niet bestaat (zie GetEx method in IADs interface)
if (Win32::OLE->LastError() == $E_ADS{PROPERTY_NOT_FOUND}) {
print "------------ TWEEDE MANIER -------------\n";
# Probeer met GetInfoEx, deze vult cache eerst in
# Dit roept GetInfo expliciet op
$obj->GetInfoEx([ $arg ], 0);
$attr = $obj->GetEx($arg);
} else {
print "------------ EERSTE MANIER -------------\n";
}
# Als nog niet ingevuld, toon <niet ingevuld>
if (Win32::OLE->LastError() == $E_ADS{PROPERTY_NOT_FOUND}) {
print "$arg: <niet ingevuld>\n\n";
} else {
print "$arg: ", join(",", @{$attr}), "\n\n";
}
}
sub bind_object {
my $rdn = shift;
my $dso = Win32::OLE->GetObject('LDAP:');
my $dn = "LDAP://satan.hogent.be/$rdn";
my $con = $dso->OpenDSObject($dn, "Xavier Geerinck", "Xavier Geerinck", ADS_SECURE_AUTHENTICATION);
return $con;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment