http://www.perlmonks.org?node_id=312730

bionicle32 has asked for the wisdom of the Perl Monks concerning the following question:

Perl Guru's who have wrote LDAP scripts to search the LDAP database, I am in need of your help. This is my first stab at writing a searchable script and have had success, but this runs very slow compared to a PHP script that another developer in my company wrote.

I was hoping that someone or many people could offer up some suggestions, modifications, etc...

Here is the code and I do have further comments that follow:

#!/usr/local/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw ( fatalsToBrowser); use Net::LDAP; use Net::LDAP::Constant qw(LDAP_SUCCESS); use vars qw( $ldap @attributes @entries $search $msg $result ); my $cgi = new CGI; my $flag = param('looking'); print $cgi->header(); if (!$flag) { lookUp(); # Subroutine to display search form } else { my $fn = param('first'); my $ln = param('last'); my $dept = param('dept'); ## Connect and bind to the server. $ldap = Net::LDAP->new("server name",port => 389,version => 3 ) or die + $!; my $result; $result = $ldap->bind("ou=people,o=intra,dc=xxxxx,dc=com") or die $res +ult->error(); if ($result->code != LDAP_SUCCESS) { die $result->error(); } else { my @count; my $sent; my $searchstory; if ($fn ne "") { $searchstory .= "(givenname=*" . $fn . "*)"; push(@count, $fn); } if ($ln ne "") { $searchstory .= "(sn=*" . $ln . "*)"; push(@count, $ln); } if ($dept ne "") { $searchstory .= "(departmentnumber=*" . $dept . "*)"; push(@count, $dept); } $sent = @count; # Number of unempty fields returned. if ($sent > 1) { $search = "(&" . $searchstory . ")"; } else { $search = $searchstory; } @attributes = ("cn","uid","l","departmentnumber","title","mail"); } my $returned = ldapSearch($search, @attributes); # Send HTML format +ted results back to browser print <<RESULTS <html> <head><title>LDAP Results</title> <link href="/_vti_templates/templates/visual/visual.css" rel="styleshe +et" type="text/css"></head> <body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bg +color="ffffff"> <p align="center"><center><table cellpadding="5" cellspacing="1" borde +r="0" width="90%"> <tr> <td valign="center" align="center"><p class="breadcrumb">Found Entries + of LDAP Search:</td> </tr> <tr> <td valign="center" align="center"> $returned </td> </tr> </table></center> </body> </html> RESULTS ; } sub lookUp { print <<LOOKUP <html> <head><title>LDAP Lookup</title> <link href="/_vti_templates/templates/visual/visual.css" rel="styleshe +et" type="text/css"></head> <body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bg +color="ffffff"> <form method="post" action="ldap.pl"> <input type="hidden" name="looking" value="yes"> <p align="center"><center><table cellpadding="5" cellspacing="1" borde +r="0" width="60%"> <tr> <td valign="center" colspan="2"><p class="text10pt">LDAP Lookup:</td> </tr> <tr> <td valign="center" colspan="2"><p class="text10pt">Use this form to f +ind a employee. Enter as much information as you know.</td> </tr> <tr> <td valign="center" width="20%"><p class="text9pt">First Name</td> <td valign="center" width="40%"><input type="text" name="first" size=" +20" class="text9pt"></td> </tr> <tr> <td valign="center" width="20%"><p class="text9pt">Last Name</td> <td valign="center" width="40%"><input type="text" name="last" size="2 +0" class="text9pt"></td> </tr> <tr> <td valign="center" width="20%"><p class="text9pt">Department Number</ +td> <td valign="center" width="40%"><input type="text" name="dept" size="1 +0" class="text9pt"></td> </tr> <tr> <td align="center" colspan="2"><input type="submit" value="Lookup"></a +></td> </tr> </table></center> </form> </body> </html> LOOKUP ; } sub ldapSearch { my ($filter, $attrs) = @_; ## Query for the cn and mail attributes. $msg = $ldap->search( base => "ou=people,o=intra,dc=xxxxx,dc=com", scope => "sub", filter => $filter, attrs => "[" . $attrs . "]" ); if ( $msg->count == 0 ) { return "No matches found for $filter"; } ## Print resulting entries to standard output. #if ( $msg->count() > 0 ) { # print $msg->count(), " entries returned.\n"; #} @entries = $msg->entries; my $totalEntries = $msg->count(); ## Unbind and exit. $ldap->unbind(); parseResults($totalEntries); } sub parseResults { my $total = $_[0]; my $info = qq(<table cellpadding="3" cellspacing="1" border="1" width= +"75%"><tr><td valign="center" colspan="2"><p class="breadcrumb">Found +: $total</tr>); my $entry; foreach $entry (@entries) { my $fln = $entry->get('cn')->[0]; my $uid = $entry->get('uid')->[0]; my $loc = $entry->get('l')->[0]; my $department = $entry->get('departmentnumber')->[0]; my $title; if ($entry->get('title')) { $title = $entry->get('title')->[0]; } else { $title = "You don't have a job!"; } my $email; if ($entry->get('mail')) { $email = $entry->get('mail')->[0]; } else { $email = "sgrif12"; } $info .= qq(<tr><td valign="center" width="15%"><a href="mailto:$ +email" class="link9pt">$fln</a></td>); $info .= qq(<td valign="center" width="10%"><p class="text9pt">$u +id</td></tr>); $info .= qq(<tr><td valign="center" colspan="2"><p class="text9pt +">$department, $title</td></tr>); $info .= qq(<tr><td valign="center" colspan="2"><p class="text9pt +">$loc</td>); } $info .= qq(</table>); }

I used if statements within the for loop for the individuals job title and e-mail information. I received a software error due to the fact that it returned an empty string and I did not do any error checking. Some employees do not have a job title or e-mail information for their perspective role.

I took snippets of code from a script that a perl programmer had sitting on this very sit. The script was very useful in guiding me with writing my own that could be customized to my needs.

Now I am in need of suggestions and help. Be as critical as you need to be because as a programmer I feel those who have the most experience and teach you the best methods of programming logic and theory. Looking forward to your advice.

Thank you all,
Bionicle32

20031206 Edit by Corion: Added READMORE tags plus some paragraph formatting