#!/usr/local/bin/perl
#
#----------------------------------------------------------------------------
#
# This program was written by Clif Harden.
# It is uses the PERL LDAP, Tk, and Tk::JPEG modules.
# These modules are available from the PERL CPAN
# system.
#
# $Id: jpegDisplay.pl,v 1.2 2003/06/18 18:23:31 gbarr Exp $
#
# Purpose: This program is designed to retrieve jpeg photo data
# from a LDAP directory.
#
#
# Revisions:
# $Log: jpegDisplay.pl,v $
# Revision 1.2 2003/06/18 18:23:31 gbarr
# Remove all references to :all as it is not recomended
#
# Revision 1.1 2001/03/12 14:01:46 gbarr
# New contrib scripts from Clif Harden
#
#
#
#
use Getopt::Std;
use Net::LDAP;
use Net::LDAP::Filter;
use Net::LDAP;
use Net::LDAP::Util qw(ldap_error_name ldap_error_text);
use Tk;
use Tk::JPEG;
#
# Initialize opt hash.
# You can change the defaults to match your setup.
# This can eliminate the need for many of the input
# options on the command line.
#
my %opt = (
'b' => 'dc=harden,dc=org',
'h' => 'localhost',
'd' => 0,
'D' => 'cn=manager',
'w' => 'password',
'V' => '3',
'a' => 'cn',
'v' => 'commonName'
);
if ( @ARGV == 0 )
{
#
# print usage message.
#
Usage();
}
#
# Get command line options.
#
getopts('b:h:d:D:w:V:a:v:',\%opt);
#
# build filter string
#
my $match = "( $opt{'a'}=$opt{'v'} )";
$jpegFile = "./$opt{'a'}=$opt{'v'}.jpg";
#
# create filter object
#
my $f = Net::LDAP::Filter->new($match) or die "Bad filter '$match'";
#
# make ldap connection to directory.
#
my $ldap = new Net::LDAP($opt{'h'},
timeout => 10,
debug => $opt{'d'},
) or die $@;
#
# Bind to directory.
#
$ldap->bind($opt{'D'}, password => "$opt{'w'}", version => $opt{'V'}) or die $@;
#
# Search directory for record that matches filter
#
$mesg = $ldap->search(
base => $opt{b},
filter => $f,
attrs => ["jpegPhoto"],
) or die $@;
die $mesg->error,$mesg->code
if $mesg->code;
#
# get record entry object
#
$entry = $mesg->entry();
if ( !defined($entry) )
{
print "\n";
print "No data for filter $match.\n" if ($mesg->count == 0) ;
print "\n";
}
else
{
my @attrs = sort $entry->attributes;
my $max = 0;
$dn = $entry->dn();
my $attr = $entry->get_value("jpegPhoto");
if(ref($attr))
{
$picture = @$attr[0];
}
else
{
print "\n";
print "No jpegPhoto attribute for DN\n";
print "$dn\n";
print "\n";
$ldap->unbind;
exit;
}
}
#
# Store the jpeg data to a temp file.
#
open(TMP, "+>$jpegFile");
binmode(TMP);
$| = 1;
print TMP $picture;
close(TMP);
if ( !-e "$jpegFile" )
{
print "\n";
print "Could not create temporary jpeg file $jpegFile\n";
print "\n";
$ldap->unbind;
exit(1);
}
$ldap->unbind;
#
# Create a TK window to display the jpeg picture.
#
my $mw = MainWindow->new();
my $list = $mw ->Listbox( -height => 1, width => length($dn) );
$list->pack( -side => "top" );
$list->insert("end", $dn);
my $image = $mw->Photo(-file => $jpegFile, -format => "jpeg" );
print "\n";
print "Displaying jpegPhoto for\n";
print "$dn\n";
print "\n";
$mw->Label(-image => $image)->pack(-expand => 1, -fill => 'both');
$mw->Button(-text => 'Quit', -command => [destroy => $mw])->pack;
MainLoop;
unlink $jpegFile;
#----------------------------------------#
# Usage() - display simple usage message #
#----------------------------------------#
sub Usage
{
print( "Usage: [-b] | [-h] | [-d] | [-D] | [-w] | [-a] | [-v] \n" );
print( "\t-b Search base.\n" );
print( "\t-d Debug mode. Display debug messages to stdout.\n" );
print( "\t-D Authenication Distingushed Name.\n" );
print( "\t-h LDAP directory host computer.\n" );
print( "\t-w Authenication password.\n" );
print( "\t-a Attribute that will be incorporated into the search filter.\n" );
print( "\t-v Data that will be incorporated into the search filter.\n" );
print( "\t-V LDAP version of the LDAP directory.\n" );
print( "\n" );
print( "\t Perldoc pod documentation is included in this script.\n" );
print( "\t To read the pod documentation do the following;\n" );
print( "\t perldoc