[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: CGI to change LDAP passwords



Here's a sub. Expects submission from a form with 4 fields:

user - username
cur - current password
new1 - new password
new2 - new password again

Watch out for line wraps.

sub change_pass {

    my $html = CGI->new();

    my($user, $cur, $new1, $new2) = (
                    $html->param('user'),
                    $html->param('cur'),
                    $html->param('new1'),
                    $html->param('new2'),
                    );
    $user =~ s@^\s+|\s+$@@g;
    
    my(@err);
    if ($user eq "") {
        push @err, "<li>You must enter your username (ie $ENV{LOGON_USER})";
        
    } elsif ($user =~ /\@/) {
        push @err, "<li>You must enter your username (ie $ENV{LOGON_USER}).
It looks like you entered your email address (ie $user).";
        
    } elsif ($cur eq "") {
        push @err, "<li>You must enter your current password.",
        
    } elsif ($new1 eq "" || $new2 eq "") {
        push @err, "<li>You must enter your new password twice.",
        
    } elsif ($new2 ne $new1) {
        push @err, "<li>The two new passwords you entered do not match.",
        
    }
    
    if (@err) {
        die("The following errors occurred:", @err);
        
    }

    my $ldap = Net::LDAP->new('ldap.nettonettech.com') or die "$@";
    $mesg = $ldap->bind("uid=$user,ou=people,dc=nettonettech,dc=com",
password=>$cur) or die "$@";
    
    die("Unable to bind. Your current password was probably incorrect",
$mesg->code, $mesg->error) if $mesg->code;
    
    my $ctoa = 
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    my @ctoa = split(//, $ctoa);
    my $t = srand( time() ^ ($$ + ($$ << 15)) );
    my $salt = $ctoa[$q % 64] . $ctoa[($t/64)%64];
    my $passwd = crypt($new1, $salt);
    
    $mesg = $ldap->modify("uid=$user,ou=people,dc=nettonettech,dc=com",
replace=>{'userpassword' => "{crypt}$passwd"});
    
    die("Unable to modify ",$mesg->code, $mesg->error) if $mesg->code;
    
   # print out your "password changed" message here...
    
}

> From: "Mike Coughlan" <mcoughlan@gothambroadband.com>
> Reply-To: <mcoughlan@gothambroadband.com>
> Date: Thu, 19 Apr 2001 16:43:53 -0400
> To: <openldap-software@OpenLDAP.org>
> Subject: CGI to change LDAP passwords
> 
> Before I reinvent this wheel, does anyone have CGI code, either PHP or perl
> to change an LDAP password?
> 
>