#!/usr/bin/env perl
# server - control Webmin web-server
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
{
my %opt;
GetOptions('help|h' => \$opt{'help'},
'command|x=s' => \$opt{'command'},
'config|c=s' => \$opt{'config'});
# If username passed as regular param
my $cmd = scalar(@ARGV) == 1 && $ARGV[0];
$cmd = $opt{'command'} if ($opt{'command'});
if ($cmd !~ /^(status|start|stop|restart|reload|force-restart|kill)$/) {
$cmd = undef;
}
# Show usage
pod2usage(0) if ($opt{'help'} || !$cmd);
# Assign defaults
$opt{'config'} ||= "/etc/webmin";
$opt{'cmd'} = $cmd;
# Catch kill signal
my $sigkill = sub {
system("stty echo");
print "\n^C";
print "\n";
exit 1;
};
$SIG{INT} = \&$sigkill;
# Run change password command
run(\%opt);
return 0;
}
exit main(\@ARGV) if !caller(0);
sub run
{
my ($o) = @_;
my $conf_check = sub {
my ($configs) = @_;
foreach my $config (@{$configs}) {
if (!-r $config) {
say BRIGHT_RED, "Error: ", RESET, "Failed to read Webmin essential config file: ", BRIGHT_YELLOW, $config,
RESET, " doesn't exist";
exit 1;
}
}
};
root($o->{'config'}, \&$conf_check);
my $service = ($o->{'config'} =~ /usermin/ ? 'usermin' : 'webmin');
my $systemctlcmd = `which systemctl`;
$systemctlcmd =~ s/\s+$//;
if ($o->{'cmd'} =~ /^(start|stop|restart|reload)$/) {
my $rs = system("$o->{'config'}/$o->{'cmd'} $service");
exit $rs;
}
if ($o->{'cmd'} =~ /^(kill)$/) {
my $rs;
if (-x $systemctlcmd) {
$rs = system("$systemctlcmd stop $service");
$rs = system("$systemctlcmd kill -s SIGTERM $service");
}
$rs = system("$o->{'config'}/.stop-init --kill >/dev/null 2>&1 $service");
exit $rs;
}
if ($o->{'cmd'} =~ /^(force-restart)$/) {
my $rs = system("$o->{'config'}/restart-by-force-kill $service");
exit $rs;
}
if ($o->{'cmd'} =~ /^(status)$/) {
my $rs;
if (-x $systemctlcmd) {
$rs = system("$systemctlcmd status $service");
} else {
$rs = system("service $service status");
}
exit $rs;
}
exit 0;
}
sub root
{
my ($config, $conf_check) = @_;
my $mconf = "$config/miniserv.conf";
$conf_check->([$mconf]);
open(my $CONF, "<", $mconf);
my $root;
while (<$CONF>) {
if (/^root=(.*)/) {
$root = $1;
}
}
close($CONF);
# Does the Webmin root exist?
if ($root) {
die BRIGHT_RED, "Error: ", BRIGHT_YELLOW, $root, RESET, " is not a directory\n" unless (-d $root);
} else {
# Try to guess where Webmin lives, since config file didn't know.
die BRIGHT_RED, "Error: ", RESET, "Unable to determine Webmin installation directory\n";
}
return $root;
}
1;
=pod
=head1 NAME
server
=head1 DESCRIPTION
This program allows you to control Webmin web-server
=head1 SYNOPSIS
webmin server [command]
webmin [command]
=head1 OPTIONS
=over
=item --help, -h
Print this usage summary and exit.
Examples of usage:
- webmin server status
- webmin server restart
- webmin server --config /usr/local/etc/webmin --command start
- webmin status
- webmin restart
=item --config, -c
Specify the full path to the Webmin configuration directory. Defaults to C</etc/webmin>
=item --command, -x
Available commands:
- status
- start
- stop
- restart
- force-restart
- reload
- kill
=back
=head1 LICENSE AND COPYRIGHT
Copyright 2018 Jamie Cameron <jcameron@webmin.com>
Joe Cooper <joe@virtualmin.com>
Ilia Rostovtsev <ilia@virtualmin.com>