#!/usr/bin/perl
use strict;
use warnings;
use Net::LDAP;
use Carp qw( croak );

# Connects to server
my $ldap = new Net::LDAP( 'localhost' ) or croak $@;
my $message = $ldap->bind( 'uid=user,ou=People,dc=company,dc=com', password => 'secret' );
croak $message->error if $message->is_error;

# Locate user
$message = $ldap->search( base   => 'ou=People, dc=company, dc=com',
                          filter => 'uid=user', scope  => 'one' );
croak $message->error if $message->is_error;
my $entry = $message->entry(0);
croak 'Nobody found!?!' unless $entry;

# Replace password.
$entry->replace( userPassword => '{CRYPT}'. crypt( 'secret', join( '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64] ) ) );
$message = $entry->update( $ldap );
croak $message->error if $message->is_error;

# Report results.
print q{User password changed to: "}.$entry->get_value( 'userPassword' ).qq{".\n};

