Fix CLI tool to consider default system hashing format

This commit is contained in:
Ilia
2022-05-29 14:04:06 +03:00
parent 3b850af7c0
commit db0c5d1d76

View File

@@ -5,9 +5,12 @@ use strict;
use warnings;
use 5.010;
use File::Basename;
use Getopt::Long;
use Pod::Usage;
use Term::ANSIColor qw(:constants);
use lib (dirname(dirname($0)));
use WebminCore;
sub main
{
@@ -47,28 +50,11 @@ sub change_password
{
my ($optref) = @_;
my ($minserv_uconf_file, %lusers, @users, %uinfos, %ulines);
my $user = $optref->{'user'};
my $pass = $optref->{'password'};
my $confdif = $optref->{'config'};
my $conf = "$confdif/config";
my $mconf = "$confdif/miniserv.conf";
my $encrypt_password = sub {
my ($pass, $gconfig) = @_;
if ($gconfig->{'md5pass'} == 1) {
# Use MD5 encryption
return &encrypt_md5($pass);
} elsif ($gconfig->{'md5pass'} == 2) {
# Use SHA512 encryption
return &encrypt_sha512($pass);
} else {
# Use Unix DES
srand(time() ^ $$);
return crypt($pass, chr(int(rand(26)) + 65) . chr(int(rand(26)) + 65));
}
};
my $user = $optref->{'user'};
my $pass = $optref->{'password'};
my $confdif = $optref->{'config'};
my $conf = "$confdif/config";
my $mconf = "$confdif/miniserv.conf";
my $conf_check = sub {
my ($configs) = @_;
foreach my $config (@{$configs}) {
@@ -79,11 +65,46 @@ sub change_password
}
}
};
my $root = root($confdif, \&$conf_check);
my $root = root($confdif, \&$conf_check);
my $encrypt_password = sub {
my ($pass, $gconfig, $config) = @_;
my $root = root($confdif, \&$conf_check);
# Load libs
do "$root/acl/md5-lib.pl";
do "$root/web-lib-funcs.pl";
# Use pre-defined encryption (forced by Webmin config)
if ($gconfig->{'md5pass'} == 1 ||
$gconfig->{'md5pass'} == 2)
{
do "$root/acl/md5-lib.pl";
# Use MD5 encryption
return &encrypt_md5($pass) if ($gconfig->{'md5pass'}) == 1;
# Use SHA512 encryption
return &encrypt_sha512($pass) if ($gconfig->{'md5pass'}) == 2;
} else {
# Try detecting system default first
my $module = 'useradmin';
if (-d "$root/$module") {
$ENV{'PERLLIB'} = "$root";
$ENV{'WEBMIN_CONFIG'} = "$confdif";
$ENV{'FOREIGN_ROOT_DIRECTORY'} = "$root/$module";
$ENV{'FOREIGN_MODULE_NAME'} = "$module";
chdir("$root/$module");
require "$root/useradmin/user-lib.pl";
# We need to set third parameter to make sure useradmin's config
# won't be used for hashing format, as we need to auto detect it
return &encrypt_password($pass, undef, 'force_system_detection');
} else {
# Use old Unix DES
srand(time() ^ $$);
return crypt($pass, chr(int(rand(26)) + 65) . chr(int(rand(26)) + 65));
}
}
};
# Check for main config and miniserv config files
&$conf_check([$conf, $mconf]);
@@ -138,7 +159,7 @@ sub change_password
}
# Update with new password and store timestamp
$uinfos{$user}->[0] = &$encrypt_password($pass, \%gconfig);
$uinfos{$user}->[0] = &$encrypt_password($pass, \%gconfig, \%config);
$uinfos{$user}->[5] = time() if ($uinfos{$user}->[5]);
map {$ulines{$_} = join(":", @{ $uinfos{$_} })} keys %uinfos;