Skip to content

Commit

Permalink
Complete implementation of killing of disallowed servers
Browse files Browse the repository at this point in the history
  • Loading branch information
jcameron committed Jun 13, 2017
1 parent 789cd74 commit c00e2c4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions collectinfo.pl
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ package virtual_server;
# Resync all jails
&copy_all_domain_jailkit_files();

# Kill disallowed server processes
if ($config{'check_ports'} == 2) {
foreach my $d (grep { $_->{'unix'} && !$_->{'parent'} }
&list_domains()) {
&kill_disallowed_domain_server_ports($d);
}
}

&run_post_actions_silently();
10 changes: 10 additions & 0 deletions lang/en
Original file line number Diff line number Diff line change
Expand Up @@ -6889,4 +6889,14 @@ mysqlpass_change=Changing the password for user $1 ..
mysqlpass_echange=.. password change failed : $1
mysqlpass_kill=Stopping the MySQL server with authentication disabled ..

kill_failed=Kill failed : $1
kill_still=Still running!
kill_done=Killed
kill_header=The following unexpected server processes have been detected :
kill_user=Username
kill_port=Port
kill_cmd=Server command
kill_result=Result
kill_subject=Virtualmin server process monitoring

__norefs=1
67 changes: 67 additions & 0 deletions ports-lib.pl
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,71 @@ sub disallowed_domain_server_ports
return grep { !$canports{$_->{'lport'}} } @usedports;
}

# kill_disallowed_domain_server_ports(&domain)
# Terminate server processes that shouldn't be running
sub kill_disallowed_domain_server_ports
{
my ($d) = @_;
my @ports = &disallowed_domain_server_ports($d);
return 0 if (!@ports);

# Kill the processes
foreach my $p (@ports) {
next if ($p->{'proc'}->{'pid'} <= 0);
next if (!$p->{'proc'}->{'user'} ||
$p->{'proc'}->{'user'} eq 'root');
$p->{'msg'} = "Killing $p->{'proc'}->{'pid'}";
my $ok = &kill_logged('TERM', $p->{'proc'}->{'pid'});
my $msg;
if (!$ok || kill(0, $p->{'proc'}->{'pid'})) {
# Maybe a KILL is needed?
sleep(2);
if (kill(0, $p->{'proc'}->{'pid'})) {
$ok = &kill_logged('KILL', $p->{'proc'}->{'pid'});
}
else {
# It shut down in the 2 seconds
$ok = 1;
}
}
if (!$ok) {
# Kill failed?!
$msg = &text('kill_failed', "$!");
}
elsif (kill(0, $p->{'proc'}->{'pid'})) {
# Somehow it is still running
$msg = $text{'kill_still'};
}
else {
# Worked!
$msg = $text{'kill_done'};
}
$p->{'msg'} = $msg;
}

# Email the master admin, if configured
if ($config{'bw_email'}) {
$fmt = "%-20.20s %-6.6s %-30.30s %-20.20s\n";
my $body = $text{'kill_header'}."\n\n";
$body .= sprintf($fmt, $text{'kill_user'},
$text{'kill_port'},
$text{'kill_cmd'},
$text{'kill_result'});
$body .= sprintf($fmt, "-" x 20, "-" x 6, "-" x 30, "-" x 20);
foreach my $p (@ports) {
$body .= sprintf($fmt, $p->{'user'}->{'user'},
$p->{'lport'},
$p->{'proc'}->{'args'},
$p->{'msg'});
}
&foreign_require("mailboxes");
&mailboxes::send_text_mail(
&get_global_from_address(),
$config{'bw_email'},
undef,
$text{'kill_subject'},
$body);
}
}

1;

0 comments on commit c00e2c4

Please sign in to comment.