Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 301 lines (243 sloc) 7.453 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
#!/usr/local/bin/perl
#
#----------------------------------------------------------------------------
#
# This program was written by Clif Harden.
# It uses the PERL LDAP module.
# This LDAP module is available from the PERL CPAN
# system.
#
# Purpose: This program is designed to load jpeg file data into a LDAP
# directory entry.
#
#
# $Id: jpegLoad.pl,v 1.2 2003/06/18 18:23:31 gbarr Exp $
#
# Revisions:
# $Log: jpegLoad.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 strict;
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 Net::LDAP::Constant;


my $errstr = 0;
my $errmsg = "";

$errmsg = ldap_error_text($errstr);
   
#
# 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:f:h:d:D:w:V:a:v:',\%opt);


if ( !defined( $opt{'f'}) || !-e $opt{'f'} )
{
#
# No jpeg file specified or the file does not exist.
#
print "$opt{'f'}\n";
Usage();
}

$/ = undef;
$\ = undef;
$, = undef;

#
# Slurp all of the jpeg file in at once.
#
open(IN, "<$opt{'f'}");
binmode(IN);
$_ = <IN>;
close(IN);

#
# build filter string
#
my $match = "( $opt{'a'}=$opt{'v'} )";

#
# 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
#
my $mesg = $ldap->search(
  base => $opt{'b'},
  filter => $f,
  attrs => [ "cn","jpegphoto" ],
) or die $@;

die $mesg->error,$mesg->code
if $mesg->code;

#
# get record entry object
#
my $entry = $mesg->entry();

#
# get record DN
#
if ( !defined($entry) )
{
  print "No record for filter $match\n";
  $ldap->unbind;
  exit;
}

my $dn = $entry->dn();

print "\n";
print "dn: $dn\n";
print "\n";

#
# initialize arrays
#
my @addMember = ();
my @memberChange = ();

push( @addMember, "jpegphoto" ); # attribute name
push( @addMember, $_ ); # attribute value

my $attr = $entry->get_value("jpegPhoto");
if(ref($attr))
{
  #
  # Entry already has a jpegPhoto, replace it.
  #
  push( @memberChange, "replace" ); # ldap replace operation
  push( @memberChange, \@addMember ); # ldap data to add
}
else
{
  #
  # Entry does not have a jpegPhoto, add it.
  #
  push( @memberChange, "add" ); # ldap add operation
  push( @memberChange, \@addMember ); # ldap data to add
}

$mesg = $ldap->modify( $dn, changes => [ @memberChange ] ) or die $@;

if ( $mesg->code )
{
   $errstr = $mesg->code;
   print "Error code: $errstr\n";
   $errmsg = ldap_error_text($errstr);
   print "$errmsg\n";
   
}

$ldap->unbind;

#----------------------------------------#
# Usage() - display simple usage message #
#----------------------------------------#
sub Usage
{
   print( "Usage: [-b] <base> | [-h] <host> | [-d] <number> | [-D] <DN> | [-w] <password> | [-a] <attribute> | [-v] <data> | [-f] <jpeg file> \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-f JPEG file to load in to attribute jpegPhoto.\n" );
   print( "\t Required input option.\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 <script name>\n" );
   print( "\n" );
   print( "\n" );
   exit( 1 );
}

__END__

=head1 NAME

jpegLoad.pl - A script to load a jpeg picture into the jpegPhoto attribute of a directory entry.

=head1 SYNOPSIS

The intent of this script is to show the user how to load a
picture that is in jpeg format into the jpegPhoto attribute of
a directory entry.
The entry in question must have the schema defined to
allow the loading of the jpegPhoto attribute.

This script has been tested on a OpenLDAP 2.0.7 directory server
and a Netscape 4.x directory server.

You may need to change the first line of the PERL jpegLoad.pl script
to point to your file pathname of perl.

=head1 Input options.

-b Search base.
-d Debug mode. Display debug messages to stdout.
-D Distingushed Name for authenication purposes.
-f JPEG file to load in to attribute jpegPhoto.
Required input option and file must exist.
-h LDAP directory host computer.
-w Authenication password.
-a Attribute that will be incorporated into the search filter.
-v Data that will be incorporated into the search filter.
-V LDAP version of the LDAP directory.


Usage: jpegLoad.pl -b <base> -h <host> -d <number> -D <DN> \
-w <password> -a <attribute> -v <data> \
-f <jpeg file>

Inside the script is a opt hash that can be initialized to
default values that can eliminate the need for many of the
input options on the command line.

-------------------------------------------------------------------

=head1 REQUIREMENTS

To use this program you will need the following.

At least PERL version 5.004. You can get a stable version of PERL
from the following URL;
http://cpan.org/src/index.html

Perl LDAP module. You can get this from the following URL;
ftp://ftp.duke.edu/pub/CPAN/modules/by-module/Net/

Bundled inside each PERL module is instructions on how to install the
module into your PERL system.

-------------------------------------------------------------------

=head1 INSTALLING THE SCRIPT

Install the jpegLoad.pl script anywhere you wish, I suggest
/usr/local/bin/jpegLoad.pl.

-------------------------------------------------------------------

Since the script is in PERL, feel free to modify it if it does not
meet your needs. This is one of the main reasons I did it in PERL.
If you make an addition to the code that you feel other individuals
could use let me know about it. I may incorporate your code
into my code.

=head1 AUTHOR

Clif Harden <charden@pobox.com>
If you find any errors in the code please let me know at
charden@pobox.com.

=head1 COPYRIGHT

Copyright (c) 2001 Clif Harden. All rights reserved. This program is
free software; you can redistribute it and/or modify it under the same
terms as Perl itself.

=cut
Something went wrong with that request. Please try again.