hiddenlinux has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I am writing a script to automate some system tasks. One of the tasks is to add information to mt LDAP db.

Postfix on my system uses LDAP for virtual domain mappings (user@foo.com ==> user (local system)). I have installed the Perl-LDAP module, but so far am having absolutely no luck in using it.

My script currently accepts username/domain name input from the command line, and puts them into $username and $domain, but now i realy need help from you peeps, in how to put that data into my ldap db.

Here are the ldif requirments:
dn: cn=%DOMAIN%,ou=v_domains,ou=postfix,ou=system_services,%DC% objectClass: inetLocalMailRecipient mailLocalAddress: %USER_NAME%@%DOMAIN%, %DOMAIN% mailRoutingAddress: %ALIAS_ADDRESS%
I will be eternally greatfull, if someone could show me how.

Replies are listed 'Best First'.
Re: Using Perl LDAP
by rob_au (Abbot) on Jul 18, 2003 at 08:38 UTC
    Here's some untested code employing Net::LDAP which may be of some use to you ...

    sub DN () { 'cn=admin,dc=domain,dc=com' } sub PASSWORD () { 'password' } sub SERVER () { 'localhost' } use Carp; use Net::LDAP; # Create connection to the LDAP server and bind authoratively using # administrator credentials my $ldap = Net::LDAP->new( SERVER ); unless ( defined $ldap ) { croak( 'Cannot create connection to LDAP server -- ', $! ); } $ldap->bind ( 'dn' => DN, 'password' => PASSWORD ); my $result = $ldap->add ( 'cn=domain,ou=v_domains,ou=postfix,ou=system_services,dc=domain,dc +=com', 'attr' => [ 'cn' => 'domain', 'objectClass' => 'inetLocalMailRecipient', 'mailLocalAddress' => 'username@domain, domain', 'mailRoutingAddress' => 'alias' ] ); if ( $result->code ) { carp( 'Error in adding LDAP entry -- ', $result->error ); } $ldap->unbind;

    Some links which may be of help to you include:

     

    perl -le 'print+unpack"N",pack"B32","00000000000000000000001001110100"'

      thanks for that, but i'm still having trouble using it:
      #! /usr/bin/perl sub DN () { 'cn=admin,dc=foo,dc=com' } sub PASSWORD () { 'mypass' } sub SERVER () { 'localhost' } use Carp; use Net::LDAP; # Create connection to the LDAP server and bind authoratively using # administrator credentials my $ldap = Net::LDAP->new( SERVER ); unless ( defined $ldap ) { croak( 'Cannot create connection to LDAP server -- ', $! ); } $ldap->bind ( 'dn' => DN, 'password' => PASSWORD ); my $result = $ldap->add ( 'cn=foo,ou=v_domains,ou=postfix,ou=system_services,dc=foo,dc=com', 'attr' => [ 'cn' => 'foo', 'objectClass' => 'inetLocalMailRecipient', 'mailLocalAddress' => 'test001@foo.com, xmbox.com', 'mailRoutingAddress' => 'foo' ] ); $ldap->unbind;
        Hey, I've started to work with LDAP in the last few months, and have been banging my head on problems like this for a while now. One tool I found amazingly useful (which helped me write my Net::LDAP code) was the Java LDAP browser. (http://www.iit.edu/~gawojar/ldap/) That may help you debug your queries.

        The book _LDAP_System_Administration_ is also quite useful (as much as perldoc Net::LDAP) and has become a part of my Safari bookshelf.

        Also, please add some errorchecking (you can do this on the bind too):
        die $result->error if $result->code;
        Maybe that will help debugging it? I also like the following shell command:
        ldapadd -x -D cn=mycn -w mypassword < myldiffile.ldif
        Good luck!

        Rohit

        Hi there,

        I've been using Net::LDAP intensively and extensively for the last year in a project that makes heavy use of the LDAP Directory Server.

        The code above looks good to me.

        Sometimes it's easier to make a search of the user you want to work on, this way you get an Entry object pointing to hir, and there are many things that are more easily done with an entry than with the whole LDAP object (even a kind of transactional nature on your addings/updatings...)

        Good luck,

        --
        our $Perl6 is Fantastic;

        thanks for that, but i'm still having trouble using it:

        Can you be more specific? What sorts of errors are you seeing? What kind of results or lack thereof are you seeing?

        I use Net::LDAP and its friend Net::LDAPS all the time with great success. I even do evil things like populate Active Directory <shudder!> from an Oracle database


        Peter L. BergholdBrewer of Belgian Ales
        Peter@Berghold.Netwww.berghold.net
        Unix Professional
        Its saying that the entry already exists. Even tho, when i use LDAP Browser, i cannot see the entry in there.