Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

perl-LDAP process hangs

by Notromda (Pilgrim)
on Nov 03, 2002 at 04:11 UTC ( [id://210031]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to move a password file into an LDAP tree (to use with ispman) but the insert hangs when I try to update the ldap server. No activity appears to be happening - the script simply stops running. Ctrl-C stops the script, but there is no sign of error messages. Does anyone see something wrong here?

I included the code that populates a large HoH, which appears to be sane when I look at it from Data::Dumper. I can successfully add smaller records with Net:LDAP, so I know my ldap server works, and I know the module is installed right.

open P, "/mnt/p/etc/passwd"; while (<P>) { chop; my ($username,$x,$uid,$gid,$name,$home,$shell) = split ":"; next if exists ($admaccounts{$username}); $petra{$username} = [ userPassword => undef, uidNumber => $uidcounter++, gidNumber => "501", cn => $name eq "" ? "Osprey User" : $name +, homeDirectory => "/ispman/domains/osprey. +net/users/${username}_osprey_net/", loginShell => "/bin/bash", objectClass => [qw(top inetOrgPerson person +posixAccount ispmanDomainUser PureFTPdUser)], ispmanStatus => "active", ispmanCreateTimestamp => "1", #fixme uid => "${username}_osprey_net", ispmanUserId => $username, givenName => $name eq "" ? "Osprey User" : $ +name, sn => $name eq "" ? "Osprey User" : $name, mailHost => "freedom", FTPStatus => "enabled", FTPQuotaMBytes => "10", mailLocalAddress => "${username}\@osprey.net +", mailQuota => "10000", mailRoutingAddress => "${username}_osprey_ne +t\@osprey.net", ]; } close P; open P, "/mnt/p/etc/shadow"; while (<P>) { chop; my ($username, $pass, @rest) = split ':'; next if exists ($admaccounts{$username}); $petra{$username}[1] = "{crypt}$pass"; if ($pass eq "*") { $petra{$username}[15] = "inactive"; } } close P; use Net::LDAP; use Net::LDAP::Entry; my $ldap = Net::LDAP->new('localhost'); $ldap->bind ( # bind to a directory with dn and password dn => 'cn=Manager, dc=domain,dc=com', password => 'secret' ); my $entry=Net::LDAP::Entry->new(); $entry->changetype("add"); my $dn = "uid=user_domain_com, ou=users, ispmanDomain=domain.com, dc=d +omain,dc=com"; $entry->dn($dn); $entry->add( @{$petra{user}} ); my $result = $entry->update($ldap); $result->code && warn "$dn" ."failed to add entry: ", $result->error ;
When I try to debug it, everything works fine until the $entry->update() call. I haven't been able to step into the update call yet, though it seems that might provide some more information. In the meantime, does anyone see any problems?

Replies are listed 'Best First'.
Re: perl-LDAP process hangs
by lachoy (Parson) on Nov 03, 2002 at 16:46 UTC

    Are you sure the bind worked properly? I typically use code like this:

    my $bind_msg = $ldap->bind( $bind_dn, %bind_params ); if ( my $bind_code = $bind_msg->code ) { die "Cannot bind:", $bind_msg->error, " (Code: $bind_code)"; }

    Otherwise you might try setting a value for debug in the LDAP connection object:

    my $ldap = Net::LDAP->new( 'localhost', debug => 8 );

    Chris
    M-x auto-bs-mode

      As I said before, I can successfully add small amounts of data, but this big one fails - thus the bind works. I'll put the error check in for good measure though. :)

      I restarted the whole server, and now the whole thing seems to be working. Very odd. Thanks for the tip on the debug parameter, though.

Re: perl-LDAP process hangs
by bronto (Priest) on Nov 03, 2002 at 18:08 UTC

    You should verify that your bind call goes right. For example with

    use Net::LDAP::Util qw(ldap_error_text ldap_error_name) ; my $msg ; # you will need it several times $msg = $ldap->bind(%your_bind_params) ; if ($msg->is_error) { my $code = $msg->code ; die join "\n",ldap_error_name($code), ldap_error_text($code) ; }

    you will get an idea of what goes wrong.

    Next, it seems you don't cycle over the %petra hash, your $dn doesn't depend on the username... how could you get it to put all your users into the directory server?

    To put the users in, I'd use something like this untested code, based on yours:

    while (my ($username,$userdata) = each %petra) { my $entry = Net::LDAP::Entry->new(); my $dn = calculate_it_using($username) ; $entry->dn($dn) ; $entry->changetype("add"); $entry->add(@$userdata) ; $msg = $entry->update($ldap) ; if ($msg->is_error) { # die() the same way as before. I'd suggest # to create a subroutine to do the job, e.g. # die_ldap_error($msg->code) } }

    I seldom use that syntax anyway. I prefer using $ldap->add($entry) instead. But that's a matter of taste :-)

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->make($love) ;
    }

      I ripped out the looping code while debugging - I couldn't even get one record to work. And I used the $entry object to verify that my array of attributes are correct, which they were. So I'll go back to the $ldap->add() style. Maybe I'll write a tutorial when I'm done with this. :)

        Ok. Anyway, let us know when you solve it and how. That will be useful.

        Ciao!
        --bronto

        # Another Perl edition of a song:
        # The End, by The Beatles
        END {
          $you->take($love) eq $you->make($love) ;
        }

Re: perl-LDAP process hangs
by mandog (Curate) on Nov 04, 2002 at 05:24 UTC

    I may not understand your problem but there exist already tools to migrate to LDAP

    On debian,
    apt-get install migrationtools
    ...will do it for you. Otherwise see here here

    email: mandog
Update: Re: perl-LDAP process hangs
by Notromda (Pilgrim) on Nov 04, 2002 at 15:22 UTC
    Ok, I guess I'm not good enough XP-wise to edit my main post? Well, here's a new twist to the whole story. I logged in via ssh from my parent's home, ran the program, and it worked fine. I added the loop back in, changed back to the $ldap->add() style of code, and it successfully added about 50 entries.

    When I got back to my office this morning, it doesn't work. Is there something in my RH8 environment that might be knocking it out of shape? UTF-8 ?

    here's what the debug=>8 out yielded...

    Net::LDAP=HASH(0x8d9c378) sending: Net::LDAP=HASH(0x8d9c378) received: 0000 12: SEQUENCE { 0002 1: INTEGER = 1 0005 7: [APPLICATION 1] { 0007 1: ENUM = 0 000A 0: STRING = '' 000C 0: STRING = '' 000E : } 000E : } Net::LDAP=HASH(0x8d9c378) sending:
    And it just hangs right there. My current code base (just the ldap part):
    use Net::LDAP; use Net::LDAP::Entry; my $ldap = Net::LDAP->new('localhost',debug=>8); my $bind_msg = $ldap->bind( dn => 'cn=Manager, dc=osprey,dc=net', password => 'secret' ); if ( my $bind_code = $bind_msg->code ) { die "Cannot bind:", $bind_msg->error, " (Code: $bind_code)"; } foreach my $username (sort keys %petra) { my $dn = "uid=${username}_osprey_net, ou=users, ispmanDomain=osprey.n +et, dc=osprey,dc=net"; my $ref = $petra{$username}; $result = $ldap->add( dn=> $dn,attr => [@$ref] ); $result->code && warn "$dn" ."failed to add entry: ", $result->error ; }
      I was closer to the truth than I thought with the environment question. Redhat set the environment variable LANG to en_US.UTF-8. When I set it back to the more common en_US, the program starts to work. Now my question is why?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://210031]
Approved by graff
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found