#!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= Service Accounts,OU=CityCORP,DC=MyCompanyName,DC=com"; my $ldappassword ="LDAPPaassw00rd"; my $LDAP_SERVER = "DCCity5.MyCompanyName.com"; my $LDAP_ROOT_DN = "DC=MyCompanyName,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('MyCompanyName Active Directory' ), h3(" 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 rendering 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 all Stanleys; st* will find all first & Last names starting with ST)
"; 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 search, or type in the form above."); exit(); } my $filter = "(&(objectClass=user)(objectCategory=person)" . "(|(givenname=$NameWildCard)(sn=$NameWildCard)(SAMAccountName=$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 . "]
\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 "
\n"; print start_table({-border=>undef, -width=>'80%', -align=>'CENTER',-cellspacing=>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 ) ,"
\n"; } print end_td; $toggle or print end_Tr; } print "\n",end_Tr, end_table,end_html; $ldap->unbind;