Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

LDAP address lookup from Mutt

by lachoy (Parson)
on Mar 17, 2001 at 20:45 UTC ( #65152=sourcecode: print w/ replies, xml ) Need Help??

Category: E-Mail Programs
Author/Contact Info Chris Winters <chris@cwinters.com>
Description:

Mutt allows you to add a hook for an external address lookup program. A common one is LDAP, and this simple script does the lookup for you. Once this is setup, you can enter part of a person's first or last name, hit ^T and the LDAP search will be performed.

Just add the following line to your .muttrc, changing the name of the script to whatever you save it as:

set query_command = "~/bin/mutt_ldap.pl '%s'"

You'll have to set the proper information for the constants HOST and BASE and, if your LDAP server doesn't allow anonymous binds, also pass the full DN mapping to your user record and password to the bind() method. Also, if you have a nonstandard schema you'll have to modify the LDAP property names.

#!/usr/bin/perl

use strict;
use Net::LDAP;

use constant HOST => 'ldap.mysite.com';
use constant BASE => 'cn=People, dc=MySite, dc=com';

{
  print "Searching database... ";
  my $name = shift || die "Usage: $0 filter\n";
  my $ldap = Net::LDAP->new( HOST, onerror => 'die' ) 
                || die "Cannot connect: $@";
  $ldap->bind() or die "Cannot bind: $@";
  my $msg = $ldap->search( base => BASE, 
                           filter => "(|(sn=*$name*)(givenName=*$name*
+))" );
  my @entries = ();
  foreach my $entry ( $msg->entries() ) {
    push @entries, 
        { email      => $entry->get_value( 'mail' ),
          first_name => $entry->get_value( 'givenName' ),
          last_name  => $entry->get_value( 'sn' ) };
  }
  $ldap->unbind();
  print scalar @entries, " entries found.\n";
  foreach my $entry ( @entries ) {
    print "$entry->{email}\t$entry->{first_name} $entry->{last_name}\n
+";
  }
}

Comment on LDAP address lookup from Mutt
Download Code
Re: LDAP address lookup from Mutt
by waxhead (Acolyte) on Dec 24, 2002 at 07:40 UTC

    This is my first comment...

    I grabbed this script to give me ldap access to my server, naturally. However my mail attribute is multivalued for a number of entries, so I have made some modifications to the script to handle the multivalued attribute.

    Make sure you change all entries as required.

    #!/usr/bin/perl use strict; use Net::LDAP; use constant HOST => 'ldap.your.domain'; use constant BASE => 'ou=People, dc=your, dc=domain'; use constant VERSION => 3; use constant SCOPE => 'sub'; my $name; my @attributes = qw( dn givenName sn mail ); my $filter = "(|(sn=$name*)(givenName=$name*))"; { print "Searching directory... "; $name = shift || die "Usage: $0 filter\n"; my $ldap = Net::LDAP->new( HOST, onerror => 'die' ) || die "Cannot connect: $@"; $ldap->bind(version => VERSION) or die "Cannot bind: $@"; my $result = $ldap->search( base => BASE, scope => SCOPE, attrs => \@attributes, filter => $filter ); my @entries = $result->entries; $ldap->unbind(); print scalar @entries, " entries found.\n"; foreach my $entry ( @entries ) { my @emailAddr = $entry->get_value('mail'); foreach my $addr (@emailAddr) { print $addr , "\t"; print $entry->get_value('givenName'), " "; print $entry->get_value('sn'), "\n"; } } }

    Make sure you have the Net::LDAP module installed.

    Hope someone finds it of use, and thanks to the original author... I just hacked on what was already provided.

      There's a slight error... sorry about that...

      Move the line creating the filter after the line that shift's the parameter into $name:

      $name = shift || die "Usage: $0 filter\n"; my $filter = "(|(sn=$name*)(givenName=$name*))";

      This works much better.. :-/

        After searching endlessly for a straight forward ldap search script sample that actually returned info I can use immediately and not have to through into an array to further massage(because I don't understand the Net::LDAP interworkings), This is is awesome! This should be the sample script used to teach folks who don't yet understand the Perl OO stuff and would like to use Net::LDAP. Some minor issues with "Odd number of elements in anonymous hash", but otherwise works great and was able to further modify to use. Frankly, the official examples(as someone new to using the Net::LDAP" are not very good as far as the search goes. This example is just fantastic, now I can start digging further for better understand this. But honestly, I don't know why some of these modules are built so complex to use. At any rate, Thanks Lachoy!!

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://65152]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (16)
As of 2014-10-30 18:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls