-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutt-ldap.pl
56 lines (45 loc) · 1.65 KB
/
mutt-ldap.pl
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
#! /usr/bin/perl -Tw
# 2005-02-24: Fixed for AD/Exchange 2003 & Unicode characters,
# [email protected] If you find this script useful, let me know. :-)
#
# 2000/2001: Original version obtained from Andreas Plesner Jacobsen at
# World Online Denmark. Worked for me with Exchange versions prior to Exchange
# 2000.
#
# Use it with mutt by putting in your .muttrc:
# set query_command = "/home/user/bin/mutt-ldap.pl '%s'"
#
# Then you can search for your users by name directly from mutt. Press ^t
# after having typed parts of the name. Remember to edit configuration
# variables below.
use strict;
use Encode qw/encode decode/;
use vars qw { $ldapserver $domain $username $password $basedn };
# --- configuration ---
$ldapserver = "domaincontroller.yourdomain.com";
$domain = "YOURDOMAIN";
$username = "myuser";
$password = "mypassword";
$basedn = "ou=companyxy,dc=companyxy,dc=tld";
# --- end configuration ---
#my $search=shift;
my $search=encode("UTF-8", join(" ", @ARGV));
if (!$search=~/[\.\*\w\s]+/) {
print("Invalid search parameters\n");
exit 1;
}
use Net::LDAP;
my $ldap = Net::LDAP->new($ldapserver) or die "$@";
$ldap->bind("$domain\\$username", password=>$password);
my $mesg = $ldap->search (base => $basedn,
filter => "(|(cn=*$search*) (rdn=*$search*) (uid=*$search*) (mail=*$search*))",
attrs => ['mail','cn']);
$mesg->code && die $mesg->error;
print(scalar($mesg->all_entries), " entries found\n");
foreach my $entry ($mesg->all_entries) {
if ($entry->get_value('mail')) {
print($entry->get_value('mail'),"\t",
decode("UTF-8", $entry->get_value('cn')),"\tFrom Exchange LDAP database\n");
}
}
$ldap->unbind;