Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 112 lines (73 sloc) 2.74 kb
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
1 #!/usr/bin/perl -w
2
5f234b0 @marschap contrib/LWPsearch.pl: set OUTPUT encoding to UTF-8
marschap authored
3 use open OUT => ':utf8';
4
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
5 use LWP::UserAgent;
6 use MIME::Base64;
7 use Getopt::Long;
8
9 # option variables
10 my $user = '';
11 my $password = '';
12 my $format = '';
13
14 # get options
15 my $result = GetOptions('u|user=s' => \$user,
16 'p|password=s' => \$password,
17 'f|format=s' => \$format);
18
19 # die on errors on the command line
20 die "Usage: LWPsearch [<options>] <LDAP-URL>\n" .
21 " where <options> are:\n" .
9a0ffe5 @marschap contrib/LWPsearch.pl: support DSML output format of LWP::Protocol::ldap
marschap authored
22 " -f --format {dsml|html|json|ldif} output format\n" .
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
23 " -u --user <username> user name (DN) to logon to LDAP server\n" .
24 " -p --password <password> password to logon to LDAP server\n"
9a0ffe5 @marschap contrib/LWPsearch.pl: support DSML output format of LWP::Protocol::ldap
marschap authored
25 if (!$result || scalar(@ARGV) != 1 || ($format && $format !~ /^(dsml|json|ldif|html)$/));
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
26
27 # create a user agent object
28 my $ua = LWP::UserAgent->new;
29 $ua->agent("LWPsearch");
30
368bd76 @marschap contrib/LWPsearch.pl: simplify a bit
marschap authored
31 # add headers as requested
32 my %headers = ();
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
33
368bd76 @marschap contrib/LWPsearch.pl: simplify a bit
marschap authored
34 $headers{Accept} = "text/$format"
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
35 if ($format);
36
368bd76 @marschap contrib/LWPsearch.pl: simplify a bit
marschap authored
37 $headers{Authorization} = 'Basic '.encode_base64("$user:$password")
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
38 if ($user);
39
368bd76 @marschap contrib/LWPsearch.pl: simplify a bit
marschap authored
40 # pass GET request to the user agent and get a response back
41 my $res = $ua->get($ARGV[0], %headers);
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
42
43 # check the outcome of the response
44 if ($res->is_success) {
45 print $res->content;
46 }
47 else {
48 print $res->status_line, ($res->content) ? ' ('.$res->content.')' : '', "\n";
49 }
50
51 =head1 NAME
52
53 LWPsearch.pl -- perform LDAP search using LWP mechanisms
54
55 =head1 SYNOPSIS
56
57 B<LWPsearch.pl>
9a0ffe5 @marschap contrib/LWPsearch.pl: support DSML output format of LWP::Protocol::ldap
marschap authored
58 [B<-f|--format> {C<dsml>|C<html>|C<json>|C<ldif>}]
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
59 [B<-u|--user> I<user>]
60 [B<-p|--password> I<password>]
61 B<LDAP-URL>
62
63 =head1 DESCRIPTION
64
65 LWPsearch.pl parses the LDAP URL given on the command line using the
66 methods provided by C<LWP::Protocol::ldap> message, connects to the
67 LDAP server given in the URL, and performs the search described in the URL.
68
69 If user and password are given, they are used to do a simple bind before the search.
70
71 The output depends on the option B<-f|--format>.
72
73 =head1 OPTIONS
74
75 =over 4
76
9a0ffe5 @marschap contrib/LWPsearch.pl: support DSML output format of LWP::Protocol::ldap
marschap authored
77 =item B<-f|--format> {C<dsml>|C<html>|C<json>|C<ldif>}
e16847b @marschap contrib/LWPsearch.pl: new
marschap authored
78
79 Specifies the the output format to use.
80
81 For the format C<json> to work, the Perl module C<JSON> needs to be installed.
82
83 =item B<-u|--user> I<user>
84
85 Specifies the user to log on to the LDAP server.
86 The I<user> must be a DN.
87
88 =item B<-p|--password> I<password>
89
90 Specifies the password with which the I<user> logs on to the LDAP server.
91
92 =back
93
94 =head1 ARGUMENTS
95
96 LWPsearch.pl only takes one argument.
97
98 B<LDAP-URL>, an LDAP URL as described in L<RFC 4516|http://tools.ietf.org/html/rfc4516>.
99
100 =head1 AUTHOR
101
102 Peter Marschall <peter@adpm.de>
103
104 =head1 COPYRIGHT & LICENSE
105
106 Copyright (c) 2012 Peter Marschall All rights reserved.
107 This program is free software; you can redistribute it and/or modify it
108 under the same terms as Perl itself.
109
110 =cut
111
Something went wrong with that request. Please try again.