#!/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 $result->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 formatted results back to browser print < LDAP Results

$returned
RESULTS ; } sub lookUp { print < LDAP Lookup

LDAP Lookup:

Use this form to find a employee. Enter as much information as you know.

First Name

Last Name

Department Number

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(); 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(); $info .= qq(); $info .= qq(); $info .= qq(); } $info .= qq(
$fln

$uid

$department, $title

$loc

); }