forked from spicyjack/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutt_ldap_query
83 lines (69 loc) · 2.7 KB
/
mutt_ldap_query
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
#!/usr/bin/perl -w
# Copyright (C) 4/14/98 Marc de Courville <[email protected]>
# but feel free to redistribute it however you like.
#
# http://wwwhome.cs.utwente.nl/~meentr/mutt-ldap.html
#
# Modifications for University of Twente service by Remco van de Meent
#
# mutt_ldap_query.pl: perl script to parse the outputs of ldapsearch
# (ldap server query tool present in ldap-3.3 distribution
# http://www.umich.edu/~rsug/ldap) in order to pass the required
# formatted data to mutt (mail client http://www.mutt.org/)
# using Brandon Long's the "External Address Query" patch
# (http://www.fiction.net/blong/programs/mutt/#query).
#
# Warren Jones <[email protected]> 2-10-99
# o Instead of just matching "sn", I try to match these fields
# in the LDAP database: "cn", "mail", "sn" and "givenname".
# A wildcard is used to make a prefix match. (I borrowed
# this query from pine.)
#
# o Commas separating command line arguments are optional.
# (Does mutt really start up $query_command with comma
# separated args?)
#
# o Streamlined the perl here and there. In particular,
# I used paragraph mode to read in each match in a single
# chunk.
#
# o Added "use strict" and made the script "-w" safe.
#
# o Returned non-zero exit status for errors or when there
# is no match, as specified by the mutt docs.
#
# o Explicitly close the pipe from ldapsearch and check
# error status.
#
# enable this command in mutt with:
# set query_command = "/usr/local/bin/mutt_ldap_query '%s'"
use strict;
# Please change the following 2 lines to match your site configuration
#
my $ldap_server = "directory.qualcomm.com";
my $BASEDN = "o=qualcomm";
# Fields to search in the LDAP database:
#
my @fields = qw(cn mail sn givenname);
die "Usage: $0 <name_to_query>, [[<other_name_to_query>], ...]\n"
if ! @ARGV;
$/ = ''; # Paragraph mode for input.
my @results;
foreach my $askfor ( @ARGV ) {
$askfor =~ s/,$//; # Remove optional trailing comma.
my $query = join '', map { "($_=$askfor*)" } @fields;
my $command = "ldapsearch -h $ldap_server -b '$BASEDN' -x '(|$query)'" .
" sn cn givenName mail telephoneNumber";
print STDERR "about to run command: $command\n";
open( LDAPQUERY, "$command |" ) or die "LDAP query error: $!";
while ( <LDAPQUERY> ) {
next if ! /^mail:(.*)$/im;
my $email = $1;
my $phone = /^telephoneNumber:(.*)$/im ? $1 : '';
my ( @name ) = ( /^cn:(.*)$/im, /^sn=(.*)$/im );
push @results, "$email\t@name\t$phone\n";
} # while ( <LDAPQUERY> )
close( LDAPQUERY ) or die "ldapsearch failed: $!\n";
}
print "LDAP query: found ", scalar(@results), "\n", @results;
exit 1 if ! @results;