#!/usr/bin/perl -w # by Ben Collins , butchered by Marco d'Itri # Hacked by Brad Marshall for use at PI # to use, add to ~/.muttrc: # set query_command="/home/username/bin/ldap-query %s" use strict; # We're only interested in a set of attributes, no need to return all attributes. my @attrs = qw(sn cn uid); # From where do we want to perform the search? my $base = 'ou=people,dc=example,dc=com'; # Remote or local name of the host running slapd my $server = 'ldap.localdomain'; # ldap:// default runs on port 389 - change if you're running it on a non-default port. my $port = 389; # Echo howto for the user, if no arguments are sent die "Usage: $0 [...]\n" if not $ARGV[0]; # Check if we have the needed ldap-modules eval 'require Net::LDAP;'; die "Could not load Net::LDAP: $@\n" if $@; # Open a connection to the server my $ldap = Net::LDAP->new($server, port => $port) or die "Could not contact LDAP server $server:$port"; # Bind anonymously to the server $ldap->bind or die 'Could not bind'; # Variable to store results in my @results = (); foreach my $search (@ARGV) { my $query = join '', map { "($_=*$search*)" } @attrs; my $mesg = $ldap->search(base => $base, filter => "(|$query)") or die 'Failed search'; # Iterate over the search-result, and push result into our results-variable foreach my $entry ($mesg->entries) { my $uid = $entry->get('uid'); next unless (defined $uid); my $fname = $entry->get('cn'); my $mail = $entry->get('mail'); push @results, "$$fname[0]\t<$$mail[0]>\t\n"; } } # Close connection after fetching search-results $ldap->unbind; # Now we can print the result print 'LDAP query: found ', scalar @results, "\n", @results; exit 1 unless @results;