$ENV{'PATH'}= '/bin:/usr/bin';
$ENV{'SHELL'}= '/bin/sh';
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
use strict;
use FindBin;
use FindBin qw($RealBin);
use lib "$RealBin/";
use smbldap_tools;
use Crypt::SmbHash;
use Digest::MD5 qw(md5);
use Digest::SHA1 qw(sha1);
use MIME::Base64 qw(encode_base64);
sub make_hash;
sub make_salt;
my $user= undef;
my $oldpass= undef;
my $arg;
my $update_samba_passwd= 1;
my $update_unix_passwd= 1;
foreach $arg (@ARGV) {
if ( substr( $arg, 0, 1 ) eq '-' ) {
if ( $arg eq '-h' || $arg eq '-?' || $arg eq '--help' ) {
print_banner;
print "Usage: $0 [options] [username]\n";
print " -h, -?, --help show this help message\n";
print " -s update only samba password\n";
print " -u update only UNIX password\n";
exit (6);
} elsif ($arg eq '-s') {
$update_samba_passwd= 1; $update_unix_passwd= 0;
} elsif ($arg eq '-u') {
$update_samba_passwd= 0; $update_unix_passwd= 1;
}
} else {
if ( $< != 0 ) {
die "Only root can specify username\n";
}
$user= $arg; last;
}
}
if (!defined($user)) {
$user = getpwuid($<); }
$user =~ /^([-\@\ \w.]+\$?)$/ and $user = $1 or
die "$0: username '$user' is tainted\n";
my ($dn,$ldap_master);
if ($< != 0) {
if (!defined($oldpass)) {
print "Identity validation...\nenter your UNIX password: ";
system "/bin/stty -echo" if (-t STDIN);
chomp($oldpass=<STDIN>);
system "/bin/stty echo" if (-t STDIN);
print "\n";
$config{masterDN}="uid=$user,$config{usersdn}";
$config{masterPw}="$oldpass";
$ldap_master=connect_ldap_master();
$dn=$config{masterDN};
if (!is_user_valid($user, $dn, $oldpass)) {
print "Authentication failure\n";
exit (10);
}
}
} else {
$ldap_master=connect_ldap_master();
my $dn_line;
if (!defined($dn_line = get_user_dn($user))) {
print "$0: user $user doesn't exist\n";
exit (10);
}
$dn = get_dn_from_line($dn_line);
}
my $samba = is_samba_user($user);
if ( $samba and $update_samba_passwd ) {
if ( $update_unix_passwd ) {
print "Changing UNIX and samba passwords for $user\n";
} else {
print "Changing samba password for $user\n";
}
} else {
if ( $update_unix_passwd ) {
print "Changing UNIX password for $user\n";
} else {
die "Internal error";
}
}
my $pass;
my $pass2;
print "New password: ";
system "/bin/stty -echo" if (-t STDIN);
chomp($pass=<STDIN>);
system "/bin/stty echo" if (-t STDIN);
print "\n";
print "Retype new password: ";
system "/bin/stty -echo" if (-t STDIN);
chomp($pass2=<STDIN>);
system "/bin/stty echo" if (-t STDIN);
print "\n";
if ($pass ne $pass2) {
print "New passwords don't match!\n";
exit (10);
}
my $hash_password;
if ($config{with_slappasswd}) {
($pass =~ /^(.*)$/ and $pass=$1) or
die "$0: user password is tainted\n";
if ( $config{hash_encrypt} eq "CRYPT" && defined($config{crypt_salt_format}) ) {
open BUF, "-|" or
exec "$config{slappasswd}",
"-h","{$config{hash_encrypt}}",
"-c","$config{crypt_salt_format}",
"-s","$pass";
$hash_password = <BUF>;
close BUF;
} else {
open(BUF, "-|") or
exec "$config{slappasswd}",
"-h","{$config{hash_encrypt}}",
"-s","$pass";
$hash_password = <BUF>;
close BUF;
}
} else {
$hash_password = make_hash($pass,$config{hash_encrypt},$config{crypt_salt_format});
}
defined($hash_password) or
die "I cannot generate the proper hash!\n";
chomp($hash_password);
if ($< != 0) {
$ldap_master->unbind;
$config{masterDN}="uid=$user,$config{usersdn}";
$config{masterPw}="$oldpass";
$ldap_master=connect_ldap_master();
}
if ( $samba and $update_samba_passwd ) {
if (!$config{with_smbpasswd}) {
my ($sambaLMPassword,$sambaNTPassword) = ntlmgen $pass;
my $date=time;
my @mods;
push(@mods, 'sambaLMPassword' => $sambaLMPassword);
push(@mods, 'sambaNTPassword' => $sambaNTPassword);
push(@mods, 'sambaPwdLastSet' => $date);
if (defined $config{defaultMaxPasswordAge}) {
my $new_sambaPwdMustChange=$date+$config{defaultMaxPasswordAge}*24*60*60;
push(@mods, 'sambaPwdMustChange' => $new_sambaPwdMustChange);
if ($< ==0) {
push(@mods, 'sambaAcctFlags' => '[U]');
}
}
my $modify = $ldap_master->modify ( "$dn",
'replace' => { @mods }
);
$modify->code && warn "failed to modify entry: ", $modify->error ;
} else {
if ($< != 0) {
my $FILE="|$config{smbpasswd} -s >/dev/null";
open (FILE, $FILE) || die "$!\n";
print FILE <<EOF;
$oldpass
$pass
$pass
EOF
;
close FILE;
} else {
open FILE,"|-" or
exec "$config{smbpasswd}","$user","-s";
local $SIG{PIPE} = sub {die "buffer pipe terminated" };
print FILE <<EOF;
$pass
$pass
EOF
;
close FILE;
}
}
}
if ( $update_unix_passwd ) {
my $modify = $ldap_master->modify ( "$dn",
changes => [
replace => [userPassword => "$hash_password"]
]
);
$modify->code && warn "Unable to change password: ", $modify->error ;
}
$ldap_master->unbind;
exit 0;
sub make_hash
{
my $hash_encrypt;
my $crypt_salt_format;
my $clear_pass=$_[0] or return undef;
$hash_encrypt='{' . $_[1] . '}' or $hash_encrypt = "{SSHA}";
$crypt_salt_format=$_[2] or $crypt_salt_format = '%s';
my $hash_pass;
if ($hash_encrypt eq "{CRYPT}" && defined($crypt_salt_format)) {
my $salt = sprintf($crypt_salt_format,make_salt());
$hash_pass = "{CRYPT}" . crypt($clear_pass,$salt);
} elsif ($hash_encrypt eq "{MD5}") {
$hash_pass = "{MD5}" . encode_base64( md5($clear_pass),'' );
} elsif ($hash_encrypt eq "{SMD5}") {
my $salt = make_salt(4);
$hash_pass = "{SMD5}" . encode_base64( md5($clear_pass . $salt) . $salt,'');
} elsif ($hash_encrypt eq "{SHA}") {
$hash_pass = "{SHA}" . encode_base64( sha1($clear_pass),'' );
} elsif ($hash_encrypt eq "{SSHA}") {
my $salt = make_salt(4);
$hash_pass = "{SSHA}" . encode_base64( sha1($clear_pass . $salt) . $salt,'' );
} elsif ($hash_encrypt eq "{CLEARTEXT}") {
$hash_pass=$clear_pass;
} else {
$hash_pass=undef;
}
return $hash_pass;
}
sub make_salt
{
my $length=32;
$length = $_[0] if exists($_[0]);
my @tab = ('.', '/', 0..9, 'A'..'Z', 'a'..'z');
return join "",@tab[map {rand 64} (1..$length)];
}
=head1 NAME
smbldap-passwd - change user password
=head1 SYNOPSIS
smbldap-passwd [-?|--help|-s|-u] [name]
=head1 DESCRIPTION
smbldap-passwd changes passwords for user accounts. A normal user may only change the password for their own account, the super user may change the password for any account.
If option -s specified then changed only samba password.
If options -u specified then changed only UNIX password.
With no options then changed both - UNIX and samba passwords.
Password Changes
The user is first prompted for their old password, if one is present. This password is then tested against the stored password by binding to the server. The user has only one chance to enter the correct passwword. The super user is permitted to bypass this step so that forgotten passwords may be changed.
The user is then prompted for a replacement password. As a general guideline, passwords should consist of 6 to 8 characters including one or more from each of following sets:
Lower case alphabetics
Upper case alphabetics
Digits 0 thru 9
Punctuation marks
Password will prompt again and compare the second entry against the first. Both entries are require to match in order for the password to be changed.
=head1 SEE ALSO
passwd(1)
=cut