mirror of
https://github.com/webmin/webmin.git
synced 2026-03-20 16:50:24 +00:00
Merge branch 'master' of github.com:webmin/webmin
This commit is contained in:
23
bin/passwd
23
bin/passwd
@@ -16,10 +16,18 @@ sub main
|
||||
'config|c=s' => \$opt{'config'},
|
||||
'user|u=s' => \$opt{'user'},
|
||||
'password|p=s' => \$opt{'password'});
|
||||
pod2usage(0) if ($opt{'help'} || !$opt{'user'});
|
||||
|
||||
$opt{'config'} ||= "/etc/webmin";
|
||||
# If username passed as regular param
|
||||
my $user = scalar(@ARGV) == 1 && $ARGV[0];
|
||||
|
||||
# Show usage
|
||||
pod2usage(0) if ($opt{'help'} || (!$opt{'user'} && !$user));
|
||||
|
||||
# Assign defaults
|
||||
$opt{'config'} ||= "/etc/webmin";
|
||||
$opt{'user'} = $user if ($user && !$opt{'user'});
|
||||
|
||||
# Catch kill signal
|
||||
my $sigkill = sub {
|
||||
system("stty echo");
|
||||
print "\n^C";
|
||||
@@ -28,6 +36,7 @@ sub main
|
||||
};
|
||||
$SIG{INT} = \&$sigkill;
|
||||
|
||||
# Run change password command
|
||||
change_password(\%opt);
|
||||
|
||||
return 0;
|
||||
@@ -87,7 +96,7 @@ sub change_password
|
||||
|
||||
# Check for main user file
|
||||
&$conf_check([$minserv_uconf_file]);
|
||||
|
||||
|
||||
# Read and parse `miniserv.users` config file
|
||||
read_file($minserv_uconf_file, \%lusers, undef, undef, ":");
|
||||
@users = keys %lusers;
|
||||
@@ -104,7 +113,7 @@ sub change_password
|
||||
}
|
||||
|
||||
# Ask for password on stdin
|
||||
my $suc_pre_msg = GREEN . 'Success:' . RESET;
|
||||
my $suc_pre_msg = "";
|
||||
my $suc_msg = 'updated successfully';
|
||||
if (!$pass) {
|
||||
print "Enter password for user ", BRIGHT_YELLOW, $user, RESET, ":";
|
||||
@@ -123,7 +132,7 @@ sub change_password
|
||||
}
|
||||
chomp $pass;
|
||||
if (!$pass) {
|
||||
$suc_pre_msg = BOLD BRIGHT_RED ON_WHITE . 'Warning:' . RESET;
|
||||
$suc_pre_msg = BOLD BRIGHT_RED ON_WHITE . 'Warning:' . RESET . " ";
|
||||
$suc_msg = "has been removed, enabling anyone to login without authentication";
|
||||
}
|
||||
}
|
||||
@@ -142,7 +151,7 @@ sub change_password
|
||||
system("$confdif/start >/dev/null 2>&1");
|
||||
|
||||
# Print user message
|
||||
say "$suc_pre_msg Password for Webmin user ", BRIGHT_YELLOW, $user, RESET, " $suc_msg";
|
||||
say "${suc_pre_msg}Password for Webmin user ", BRIGHT_YELLOW, $user, RESET, " $suc_msg";
|
||||
|
||||
exit 0;
|
||||
}
|
||||
@@ -199,6 +208,8 @@ Print this usage summary and exit.
|
||||
|
||||
Examples of usage:
|
||||
|
||||
- passwd root
|
||||
|
||||
- passwd --user root
|
||||
|
||||
- passwd --user root --password ycwyMQRVAZY
|
||||
|
||||
115
changepass.pl
115
changepass.pl
@@ -2,110 +2,24 @@
|
||||
# changepass.pl
|
||||
# Script for the user to change their webmin password
|
||||
|
||||
# Get Webmin directory
|
||||
my $cwd = $0;
|
||||
$cwd =~ s/(.*)\/.*/$1/;
|
||||
|
||||
# Check command line arguments
|
||||
require "./acl/md5-lib.pl";
|
||||
usage() if (@ARGV != 3);
|
||||
($config, $user, $pass) = @ARGV;
|
||||
if (!-d $config) {
|
||||
print STDERR "The config directory $config does not exist\n";
|
||||
exit 2;
|
||||
}
|
||||
if (!open(CONF, "<$config/miniserv.conf")) {
|
||||
print STDERR "Failed to open $config/miniserv.conf : $!\n";
|
||||
print STDERR "Maybe $config is not the Webmin config directory.\n";
|
||||
exit 3;
|
||||
}
|
||||
while(<CONF>) {
|
||||
if (/^([^=]+)=(\S+)/) { $config{$1} = $2; }
|
||||
}
|
||||
close(CONF);
|
||||
if (!open(CONF, "<$config/config")) {
|
||||
print STDERR "Failed to open $config/config : $!\n";
|
||||
print STDERR "Maybe $config is not the Webmin config directory.\n";
|
||||
exit 3;
|
||||
}
|
||||
while(<CONF>) {
|
||||
if (/^([^=]+)=(\S+)/) { $gconfig{$1} = $2; }
|
||||
}
|
||||
close(CONF);
|
||||
|
||||
# Update the users file
|
||||
if (!open(USERS, "<".$config{'userfile'})) {
|
||||
print STDERR "Failed to open Webmin users file $config{'userfile'} : $!\n";
|
||||
exit 4;
|
||||
}
|
||||
while(<USERS>) {
|
||||
s/\r|\n//g;
|
||||
local @user = split(/:/, $_);
|
||||
if (@user) {
|
||||
$users{$user[0]} = \@user;
|
||||
push(@users, $user[0]);
|
||||
my ($config, $user, $pass) = @ARGV;
|
||||
my $status = system("$cwd/bin/webmin passwd --config $config --user $user --pass $pass");
|
||||
if ($status != 0) {
|
||||
if ($! =~ /no such file/i) {
|
||||
print "Error: Webmin CLI command cannot be found\n";
|
||||
}
|
||||
else {
|
||||
print "Error: $!\n";
|
||||
}
|
||||
}
|
||||
close(USERS);
|
||||
$uinfo = $users{$user};
|
||||
if (!defined($uinfo)) {
|
||||
print STDERR "The Webmin user $user does not exist\n";
|
||||
print STDERR "The users on your system are: ",join(" ", @users),"\n";
|
||||
exit 5;
|
||||
}
|
||||
$uinfo->[1] = encrypt_password($pass);
|
||||
$uinfo->[6] = time();
|
||||
if (!open(USERS, ">$config{'userfile'}")) {
|
||||
print STDERR "Failed to open Webmin users file $config{'userfile'} : $!\n";
|
||||
exit 6;
|
||||
}
|
||||
foreach $v (values %users) {
|
||||
print USERS join(":", @$v),"\n";
|
||||
}
|
||||
close(USERS);
|
||||
print "Updated password of Webmin user $user\n";
|
||||
|
||||
# Send a signal to have miniserv reload it's config
|
||||
if (open(PID, "<".$config{'pidfile'})) {
|
||||
$pid = <PID>;
|
||||
$pid =~ s/\r|\n//;
|
||||
close(PID);
|
||||
if (!$pid) {
|
||||
print STDERR "Webmin is not running - cannot refresh configuration\n";
|
||||
}
|
||||
elsif (!kill('USR1', $pid)) {
|
||||
print STDERR "Failed to signal process $pid - cannot refresh configuration\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
print STDERR "Webmin is not running - cannot refresh configuration\n";
|
||||
}
|
||||
|
||||
sub encrypt_password
|
||||
{
|
||||
my ($pass) = @_;
|
||||
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() ^ $$);
|
||||
$salt ||= chr(int(rand(26))+65).chr(int(rand(26))+65);
|
||||
return &unix_crypt($pass, $salt);
|
||||
}
|
||||
}
|
||||
|
||||
sub unix_crypt
|
||||
{
|
||||
local ($pass, $salt) = @_;
|
||||
if ($use_perl_crypt) {
|
||||
return Crypt::UnixCrypt::crypt($pass, $salt);
|
||||
}
|
||||
else {
|
||||
return crypt($pass, $salt);
|
||||
}
|
||||
}
|
||||
exit $status;
|
||||
|
||||
sub usage
|
||||
{
|
||||
@@ -115,9 +29,8 @@ usage: changepass.pl <config-dir> <login> <password>
|
||||
This program allows you to change the password of a user in the Webmin
|
||||
password file. For example, to change the password of the admin user
|
||||
to foo, you would run:
|
||||
changepass.pl /etc/webmin admin foo
|
||||
- changepass.pl /etc/webmin admin foo
|
||||
This assumes that /etc/webmin is the Webmin configuration directory.
|
||||
EOF
|
||||
exit 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user