Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 301 lines (242 sloc) 7.462 kb
8bcc653 @gbarr New contrib scripts from Clif Harden
gbarr authored
1 #!/usr/local/bin/perl
2 #
3 #----------------------------------------------------------------------------
4 #
5 # This program was written by Clif Harden.
6 # It uses the PERL LDAP module.
7 # This LDAP module is available from the PERL CPAN
8 # system.
9 #
10 # Purpose: This program is designed to load jpeg file data into a LDAP
11 # directory entry.
12 #
13 #
096bf69 @gbarr Remove all references to :all as it is not recomended
gbarr authored
14 # $Id: jpegLoad.pl,v 1.2 2003/06/18 18:23:31 gbarr Exp $
8bcc653 @gbarr New contrib scripts from Clif Harden
gbarr authored
15 #
16 # Revisions:
17 # $Log: jpegLoad.pl,v $
096bf69 @gbarr Remove all references to :all as it is not recomended
gbarr authored
18 # Revision 1.2 2003/06/18 18:23:31 gbarr
19 # Remove all references to :all as it is not recomended
20 #
8bcc653 @gbarr New contrib scripts from Clif Harden
gbarr authored
21 # Revision 1.1 2001/03/12 14:01:46 gbarr
22 # New contrib scripts from Clif Harden
23 #
24 #
25 #
26 #
27
28 use strict;
29 use Getopt::Std;
30 use Net::LDAP;
31 use Net::LDAP::Filter;
096bf69 @gbarr Remove all references to :all as it is not recomended
gbarr authored
32 use Net::LDAP;
8bcc653 @gbarr New contrib scripts from Clif Harden
gbarr authored
33 use Net::LDAP::Util qw( ldap_error_name ldap_error_text );
34 use Net::LDAP::Constant;
35
36
37 my $errstr = 0;
38 my $errmsg = "";
39
40 $errmsg = ldap_error_text($errstr);
41
42 #
43 # Initialize opt hash.
44 # You can change the defaults to match your setup.
45 # This can eliminate the need for many of the input
46 # options on the command line.
47 #
48 my %opt = (
49 'b' => 'dc=harden,dc=org',
50 'h' => 'localhost',
51 'd' => 0,
52 'D' => 'cn=manager',
53 'w' => 'password',
54 'V' => '3',
55 'a' => 'cn',
56 'v' => 'commonName'
57 );
58
59 if ( @ARGV == 0 )
60 {
61 #
62 # print usage message.
63 #
64 Usage();
65 }
66
67 #
68 # Get command line options.
69 #
70
71 getopts('b:f:h:d:D:w:V:a:v:',\%opt);
72
73
74 if ( !defined( $opt{'f'}) || !-e $opt{'f'} )
75 {
76 #
77 # No jpeg file specified or the file does not exist.
78 #
79 print "$opt{'f'}\n";
80 Usage();
81 }
82
83 $/ = undef;
84 $\ = undef;
85 $, = undef;
86
87 #
88 # Slurp all of the jpeg file in at once.
89 #
90 open(IN, "<$opt{'f'}");
91 $_ = <IN>;
92 close(IN);
93
94 #
95 # build filter string
96 #
97 my $match = "( $opt{'a'}=$opt{'v'} )";
98
99 #
100 # create filter object
101 #
102 my $f = Net::LDAP::Filter->new($match) or die "Bad filter '$match'";
103
104 #
105 # make ldap connection to directory.
106 #
107 my $ldap = new Net::LDAP($opt{'h'},
108 timeout => 10,
109 debug => $opt{'d'},
110 ) or die $@;
111
112 #
113 # Bind to directory.
114 #
115 $ldap->ldapbind(password => "$opt{'w'}", dn => "$opt{'D'}", version => $opt{'V'}) or die $@;
116
117 #
118 # Search directory for record that matches filter
119 #
120 my $mesg = $ldap->search(
121 base => $opt{'b'},
122 filter => $f,
123 attrs => [ "cn","jpegphoto" ],
124 ) or die $@;
125
126 die $mesg->error,$mesg->code
127 if $mesg->code;
128
129 #
130 # get record entry object
131 #
132 my $entry = $mesg->entry();
133
134 #
135 # get record DN
136 #
137 if ( !defined($entry) )
138 {
139 print "No record for filter $match\n";
140 $ldap->unbind;
141 exit;
142 }
143
144 my $dn = $entry->dn();
145
146 print "\n";
147 print "dn: $dn\n";
148 print "\n";
149
150 #
151 # initialize arrays
152 #
153 my @addMember = ();
154 my @memberChange = ();
155
156 push( @addMember, "jpegphoto" ); # attribute name
157 push( @addMember, $_ ); # attribute value
158
159 my $attr = $entry->get_attribute("jpegPhoto");
160 if(ref($attr))
161 {
162 #
163 # Entry already has a jpegPhoto, replace it.
164 #
165 push( @memberChange, "replace" ); # ldap replace operation
166 push( @memberChange, \@addMember ); # ldap data to add
167 }
168 else
169 {
170 #
171 # Entry does not have a jpegPhoto, add it.
172 #
173 push( @memberChange, "add" ); # ldap add operation
174 push( @memberChange, \@addMember ); # ldap data to add
175 }
176
177 $mesg = $ldap->modify( $dn, changes => [ @memberChange ] ) or die $@;
178
179 if ( $mesg->code )
180 {
181 $errstr = $mesg->code;
182 print "Error code: $errstr\n";
183 $errmsg = ldap_error_text($errstr);
184 print "$errmsg\n";
185
186 }
187
188 $ldap->unbind;
189
190 #----------------------------------------#
191 # Usage() - display simple usage message #
192 #----------------------------------------#
193 sub Usage
194 {
195 print( "Usage: [-b] <base> | [-h] <host> | [-d] <number> | [-D] <DN> | [-w] <password> | [-a] <attribute> | [-v] <data> | [-f] <jpeg file> \n" );
196 print( "\t-b Search base.\n" );
197 print( "\t-d Debug mode. Display debug messages to stdout.\n" );
198 print( "\t-D Authenication Distingushed Name.\n" );
199 print( "\t-f JPEG file to load in to attribute jpegPhoto.\n" );
200 print( "\t Required input option.\n" );
201 print( "\t-h LDAP directory host computer.\n" );
202 print( "\t-w Authenication password.\n" );
203 print( "\t-a Attribute that will be incorporated into the search filter.\n" );
204 print( "\t-v Data that will be incorporated into the search filter.\n" );
205 print( "\t-V LDAP version of the LDAP directory.\n" );
206 print( "\n" );
207 print( "\t Perldoc pod documentation is included in this script.\n" );
208 print( "\t To read the pod documentation do the following;\n" );
209 print( "\t perldoc <script name>\n" );
210 print( "\n" );
211 print( "\n" );
212 exit( 1 );
213 }
214
215 __END__
216
217 =head1 NAME
218
219 jpegLoad.pl - A script to load a jpeg picture into the jpegPhoto attribute of a directory entry.
220
221 =head1 SYNOPSIS
222
223 The intent of this script is to show the user how to load a
224 picture that is in jpeg format into the jpegPhoto attribute of
225 a directory entry.
226 The entry in question must have the schema defined to
227 allow the loading of the jpegPhoto attribute.
228
229 This script has been tested on a OpenLDAP 2.0.7 directory server
230 and a Netscape 4.x directory server.
231
232 You may need to change the first line of the PERL jpegLoad.pl script
233 to point to your file pathname of perl.
234
235 =head1 Input options.
236
237 -b Search base.
238 -d Debug mode. Display debug messages to stdout.
239 -D Distingushed Name for authenication purposes.
240 -f JPEG file to load in to attribute jpegPhoto.
241 Required input option and file must exist.
242 -h LDAP directory host computer.
243 -w Authenication password.
244 -a Attribute that will be incorporated into the search filter.
245 -v Data that will be incorporated into the search filter.
246 -V LDAP version of the LDAP directory.
247
248
249 Usage: jpegLoad.pl -b <base> -h <host> -d <number> -D <DN> \
250 -w <password> -a <attribute> -v <data> \
251 -f <jpeg file>
252
253 Inside the script is a opt hash that can be initialized to
254 default values that can eliminate the need for many of the
255 input options on the command line.
256
257 -------------------------------------------------------------------
258
259 =head1 REQUIREMENTS
260
261 To use this program you will need the following.
262
263 At least PERL version 5.004. You can get a stable version of PERL
264 from the following URL;
265 http://cpan.org/src/index.html
266
267 Perl LDAP module. You can get this from the following URL;
268 ftp://ftp.duke.edu/pub/CPAN/modules/by-module/Net/
269
270 Bundled inside each PERL module is instructions on how to install the
271 module into your PERL system.
272
273 -------------------------------------------------------------------
274
275 =head1 INSTALLING THE SCRIPT
276
277 Install the jpegLoad.pl script anywhere you wish, I suggest
278 /usr/local/bin/jpegLoad.pl.
279
280 -------------------------------------------------------------------
281
282 Since the script is in PERL, feel free to modify it if it does not
283 meet your needs. This is one of the main reasons I did it in PERL.
284 If you make an addition to the code that you feel other individuals
285 could use let me know about it. I may incorporate your code
286 into my code.
287
288 =head1 AUTHOR
289
290 Clif Harden <charden@pobox.com>
291 If you find any errors in the code please let me know at
292 charden@pobox.com.
293
294 =head1 COPYRIGHT
295
296 Copyright (c) 2001 Clif Harden. All rights reserved. This program is
297 free software; you can redistribute it and/or modify it under the same
298 terms as Perl itself.
299
300 =cut
Something went wrong with that request. Please try again.