Probably a lot more than you want/need, but this is (old) working code - feel free to trim.
#!c:/Perl/bin/perl.exe
##
## LDAP Users and groups, by Netwallah
## Aug, 2007
##
use CGI qw/:standard *table *Tr *td start_ul/;
use Net::LDAP;
use strict;
# basic sequence with LDAP is connect, bind, search, interpret search
# result, close connection
my $ldapuser ="CN=LDAPREADONLY,OU=XX Service Accounts,OU=CompanyOU
+,DC=Company,DC=com";
my $ldappassword ="LDAPPassWord";
my $LDAP_SERVER = "DC1.Company.com";
my $LDAP_ROOT_DN = "DC=Company,DC=com";
my $NameWildCard = "A*"; # If unspecified, all "A's" will be listed.
my @attributes = ("cn",
"initials",
"mail",
"telephonenumber",
"title",
"employeetype",
"employeenumber",
"givenname",
"displayname",
"cn",
"sn",
"userAccountControl",
"SAMAccountName"
);
print header,
start_html('Company Active Directory' ),
h3("Company Active Directory query"),
start_table({-border=>undef, -width=>'50%', -align=>'LEFT'}),
Tr(
td({-bgcolor=>'LIGHTGREEN'}, a({-href=>url(-base=>1) }, b("
+HOME")) ),
td( "Please select or search for a USER or GROUP" ),
),
end_table, "\n",
br({-clear=>"left"}), # This piece is required, to fix renderi
+ng problems
;
print start_form;
print "Search for:", textfield('"FORM_NAME_WILDCARD'),
radio_group(-name=>'searchType',
-values=>['user','group','computer'],
-rows=>1,-columns=>3),
submit('Lookup','go'),end_form,br();
print "(You can use * for wildcard searches, ex. *Stanley will find al
+l Stanleys; st* will find all first & Last names starting with ST) <
+BR>";
foreach my $letter ('A' .. 'Z' , 0..9) {
print a({-href=>url(-relative=>1) . "?FORM_NAME_WILDCARD=$letter*"
+ }, b("$letter"));
}
if ( param('FORM_NAME_WILDCARD') ) {
$NameWildCard= param 'FORM_NAME_WILDCARD' ;
print h4("Searching for $NameWildCard");
}else{
print h4({align=>'center'},"Please select a start-letter for the se
+arch, or type in the form above.");
exit();
}
my $filter = "(&(objectClass=user)(objectCategory=person)" .
"(|(givenname=$NameWildCard)(sn=$NameWildCard)(SAMAcco
+untName=$NameWildCard)))";
print "Connecting to $LDAP_SERVER ... " ;
my $ldap=Net::LDAP->new($LDAP_SERVER) or die "$@"; # must be a valid
+LDAP server!
print "Binding ...\n";
my $ldap_bind_Result= $ldap->bind( $ldapuser,
password => $ldappassword,
version => 3
);
print "Bind result is [" . $ldap_bind_Result . "]<br />\n";
print "Searching for $filter ....\n";
# Search name entry -----
my $Search_Result= $ldap->search( base=> $LDAP_ROOT_DN,
filter=>$filter, # scope => "sub",
attrs =>\@attributes);
$Search_Result->code && die $Search_Result->error;
print $Search_Result->count() . " Entries .. Fetching them...\n";
#print "<CENTER><TABLE BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"10
+\" BGCOLOR=\"#FFFFEA\" >\n";
print start_table({-border=>undef, -width=>'80%', -align=>'CENTER',-ce
+llspacing=>0,
-cellpadding=>5, -bgcolor=>'LIGHTBLUE'}), "\n";
my $toggle = 0;
foreach my $entr ( $Search_Result->entries() ) {
if ($toggle = 1 - $toggle){
print start_Tr;
}
print start_td;
print p("DN: ". $entr->dn. "\n");
foreach my $attr ( sort $entr->attributes ) {
# skip binary we can't handle
next if ( $attr =~ /;binary$/ );
print " $attr : ", $entr->get_value ( $attr ) ,"<br/>\n";
}
print end_td;
$toggle or print end_Tr;
}
print "\n",end_Tr, end_table,end_html;
$ldap->unbind;
Syntactic sugar causes cancer of the semicolon. --Alan Perlis
|