Fix to place cleanup_miniserv to the lib file

This commit is contained in:
iliajie
2022-11-13 23:40:58 +02:00
parent 0a6c67e55f
commit e6795f28f9
2 changed files with 29 additions and 25 deletions

View File

@@ -14,7 +14,7 @@ my @uinfo = getpwnam($user);
my ($uid, $gid);
if ($user ne "root" && !$<) {
if (!@uinfo) {
&cleanup_miniserv();
&cleanup_miniserv($port);
die "User $user does not exist!";
}
$uid = $uinfo[2];
@@ -68,7 +68,7 @@ my ($shellfh, $pid) = &proc::pty_process_exec($shellexec, $uid, $gid, $shelllogi
&reset_environment();
my $shcmd = "'$shellexec".($shelllogin ? " $shelllogin" : "")."'";
if (!$pid) {
&cleanup_miniserv();
&cleanup_miniserv($port);
die "Failed to run shell with $shcmd\n";
}
else {
@@ -83,7 +83,7 @@ untie(*STDIN);
close(STDIN);
$SIG{'ALRM'} = sub {
&cleanup_miniserv();
&cleanup_miniserv($port);
die "timeout waiting for connection";
};
alarm(60);
@@ -133,13 +133,13 @@ Net::WebSocket::Server->new(
}
if (!syswrite($shellfh, $msg, length($msg))) {
print STDERR "write to shell failed : $!\n";
&cleanup_miniserv();
&cleanup_miniserv($port);
exit(1);
}
},
disconnect => sub {
print STDERR "websocket connection closed\n";
&cleanup_miniserv();
&cleanup_miniserv($port);
kill('KILL', $pid) if ($pid);
exit(0);
}
@@ -152,7 +152,7 @@ Net::WebSocket::Server->new(
my $ok = sysread($shellfh, $buf, 1024);
if ($ok <= 0) {
print STDERR "end of output from shell\n";
&cleanup_miniserv();
&cleanup_miniserv($port);
exit(0);
}
if ($wsconn) {
@@ -165,20 +165,4 @@ Net::WebSocket::Server->new(
],
)->start;
print STDERR "exited websockets server\n";
&cleanup_miniserv();
sub cleanup_miniserv
{
my %miniserv;
if ($port) {
&lock_file(&get_miniserv_config_file());
&get_miniserv_config(\%miniserv);
my $wspath = "/$module_name/ws-".$port;
if ($miniserv{'websockets_'.$wspath}) {
delete($miniserv{'websockets_'.$wspath});
&put_miniserv_config(\%miniserv);
&reload_miniserv();
}
&unlock_file(&get_miniserv_config_file());
}
}
&cleanup_miniserv($port);

View File

@@ -5,9 +5,29 @@ use WebminCore;
&init_config();
our %access = &get_module_acl();
# cleanup_miniserv(port)
# Remove websocket in miniserv.conf
# that is no longer being used
sub cleanup_miniserv
{
my ($port) = @_;
my %miniserv;
if ($port) {
&lock_file(&get_miniserv_config_file());
&get_miniserv_config(\%miniserv);
my $wspath = "/$module_name/ws-".$port;
if ($miniserv{'websockets_'.$wspath}) {
delete($miniserv{'websockets_'.$wspath});
&put_miniserv_config(\%miniserv);
&reload_miniserv();
}
&unlock_file(&get_miniserv_config_file());
}
}
# cleanup_old_websockets([&skip-ports])
# Called by scheduled status collection to remove any websockets in
# miniserv.conf that are no longer used
# Called by scheduled status collection to remove any
# websockets in miniserv.conf that are no longer used
sub cleanup_old_websockets
{
my ($skip) = @_;