ui: improve settings navigation and safe form handling

This commit is contained in:
Mario Fetka
2026-05-21 16:45:30 +02:00
parent 0d6197bfb6
commit 7c69a35a25
3 changed files with 393 additions and 37 deletions

View File

@@ -109,6 +109,11 @@ sub handle_request()
{
delconfigline( '4 ' . $c[2] );
}
if( defined( $p{interface_manual} ) && $p{interface_manual} ne '' )
{
$p{interface} = $p{interface_manual};
}
$p{interface} =~ s/[^-_\.A-Za-z0-9:\*]//g;
if( $p{number} ne '' )
{
addconfigline( '4 ' . $p{number} . ' ' . $p{interface} . ' ' . $p{frametype} . ' ' . $p{delay} );

View File

@@ -9,7 +9,7 @@ use warnings;
our ($server_id, $mars_nwe_service, $smart_systemctl_path, $cc, %p);
sub html_escape($)
sub html_escape
{
my ($s) = @_;
$s = '' unless defined $s;
@@ -20,8 +20,137 @@ sub html_escape($)
return $s;
}
sub handle_request()
sub run_systemctl_command
{
my ($systemctl, @args) = @_;
my $output = '';
my ($reader, $writer);
if( ! pipe( $reader, $writer ) )
{
return (1, 'Could not create pipe: ' . $! . "\n");
}
my $pid = fork();
if( ! defined( $pid ) )
{
close( $reader );
close( $writer );
return (1, 'Could not fork: ' . $! . "\n");
}
if( $pid == 0 )
{
close( $reader );
# Capture stdout for the web UI. Leave stderr untouched so systemctl
# warnings/errors still go to smart.log, matching the old behaviour.
open( STDOUT, '>&', $writer ) or exit 127;
close( $writer );
exec( $systemctl, @args ) or do {
print 'Could not execute ' . $systemctl . ': ' . $! . "\n";
exit 127;
};
}
close( $writer );
while( my $line = <$reader> )
{
$output .= $line;
}
close( $reader );
waitpid( $pid, 0 );
my $rc = $? >> 8;
return ( $rc, $output );
}
sub wait_for_service_state
{
my ($systemctl, $service, $wanted, $timeout) = @_;
my $elapsed = 0;
my $last_state = '';
my $ok = 0;
$timeout = 45 unless defined($timeout) && $timeout > 0;
print '# waiting for ' . html_escape($service) . ' to become ' . html_escape($wanted) . "\n";
while( $elapsed <= $timeout )
{
my ($state_rc, $state_output) = run_systemctl_command( $systemctl, 'is-active', $service );
my $state = $state_output;
$state =~ s/[\r\n]+$//;
$state =~ s/^\s+//;
$state =~ s/\s+$//;
$state = 'unknown' if $state eq '';
if( $state ne $last_state )
{
print sprintf( "[%2ds] state: %s\n", $elapsed, html_escape($state) );
$last_state = $state;
}
if( $wanted eq 'inactive' )
{
if( $state eq 'inactive' || $state eq 'failed' )
{
$ok = 1;
last;
}
}
elsif( $wanted eq 'active' )
{
if( $state eq 'active' )
{
$ok = 1;
last;
}
if( $state eq 'failed' )
{
last;
}
}
sleep( 1 );
$elapsed++;
}
if( $ok )
{
print "wait result: reached $wanted\n";
return 0;
}
print "wait result: timeout after $timeout seconds\n";
return 1;
}
sub append_command_output
{
my ($title, $rc, $output) = @_;
print '# ' . html_escape($title) . "\n";
if( defined($output) && $output ne '' )
{
print html_escape($output);
}
else
{
print "(no output)\n";
}
print "exit code: $rc\n";
}
sub handle_request
{
local $| = 1;
my $service = $mars_nwe_service || '@MARS_NWE_SYSTEMD_SERVICE@';
my $systemctl = $smart_systemctl_path || '@SYSTEMCTL_EXECUTABLE@';
my $query = defined($cc) ? $cc : '';
@@ -42,7 +171,7 @@ sub handle_request()
$action = $1;
}
print <<EOF;
print <<HTML_HEAD;
HTTP/1.0 200 OK
Content-Type: text/html
$server_id
@@ -61,12 +190,16 @@ a{color:#9f1f1f;text-decoration:none;font-weight:bold}a:hover{text-decoration:un
h1{margin-top:0;color:#9f1f1f}.meta{padding:12px 14px;background:#fbf6ef;border:1px solid #ecdcc8;border-radius:12px;margin-bottom:14px}
pre{background:#2b241f;color:#f8eadc;padding:16px;border-radius:12px;overflow:auto;white-space:pre-wrap}.ok{color:#2f6f35;font-weight:bold}.bad{color:#9f1f1f;font-weight:bold}
.actions a{display:inline-block;margin:5px 8px 5px 0;padding:9px 12px;border-radius:10px;background:#9f1f1f;color:#fff}.actions a.secondary{background:#6f5b4f}
.waitbox{display:flex;align-items:center;gap:14px;margin:14px 0;padding:14px 16px;border:1px solid #ecdcc8;border-radius:14px;background:#fbf6ef;color:#6f5b4f}
.spinner{width:30px;height:30px;border:4px solid #e5d6c6;border-top-color:#9f1f1f;border-radius:50%;animation:spin .9s linear infinite;flex:0 0 auto}
\@keyframes spin{to{transform:rotate(360deg)}}
.waittitle{font-weight:bold;color:#9f1f1f}.waitsub{font-size:13px;margin-top:3px}
</style>
</head>
<body>
<div class="box">
<h1>MARS_NWE service control</h1>
EOF
HTML_HEAD
if( $action eq '' )
{
@@ -80,48 +213,85 @@ EOF
print '<div class="meta">Action: <b>' . html_escape($action) . '</b><br>' . "\n";
print 'Service: <b>' . html_escape($service) . '</b><br>' . "\n";
print 'systemctl: <b>' . html_escape($systemctl) . '</b></div>' . "\n";
if( $action ne 'status' )
{
my $wait_text = 'Waiting for service state change';
if( $action eq 'stop' )
{
$wait_text = 'Stopping service, waiting until it is inactive';
}
elsif( $action eq 'start' )
{
$wait_text = 'Starting service, waiting until it is active';
}
elsif( $action eq 'restart' )
{
$wait_text = 'Restarting service, waiting until it is active';
}
print '<div id="waitbox" class="waitbox">' . "\n";
print '<div class="spinner" aria-hidden="true"></div>' . "\n";
print '<div><div class="waittitle">' . html_escape($wait_text) . '</div>' . "\n";
print '<div class="waitsub">This can take up to 45 seconds. Please wait until the final status appears below.</div></div>' . "\n";
print '</div>' . "\n";
print " " x 4096; # help browsers render the waiting box before the command finishes
}
print "<pre>\n";
my @cmd = ( $systemctl, $action, $service );
my $fh;
if( ! open( $fh, '-|', @cmd ) )
{
print 'Could not execute systemctl: ' . html_escape($!) . "\n";
print "</pre><p class=\"bad\">Failed.</p>\n";
print "<p><a href=\"/static/start.html\">Back</a></p>\n";
print "</div></body></html>\n";
return;
}
my $rc = 0;
my $output = '';
while( my $line = <$fh> )
if( $action eq 'status' )
{
print html_escape($line);
}
close($fh);
my $rc = $? >> 8;
if( $rc == 0 )
{
print "</pre><p class=\"ok\">Command completed successfully.</p>\n";
( $rc, $output ) = run_systemctl_command( $systemctl, '--no-pager', '--full', 'status', $service );
append_command_output( "systemctl --no-pager --full status $service", $rc, $output );
}
else
{
print "</pre><p class=\"bad\">Command failed with exit code $rc.</p>\n";
# Avoid repeated "unit file changed on disk" warnings after install/update.
my ( $reload_rc, $reload_output ) = run_systemctl_command( $systemctl, 'daemon-reload' );
append_command_output( 'systemctl daemon-reload', $reload_rc, $reload_output );
print "\n";
( $rc, $output ) = run_systemctl_command( $systemctl, $action, $service );
append_command_output( "systemctl $action $service", $rc, $output );
print "\n";
if( $rc == 0 )
{
my $wanted_state = ( $action eq 'stop' ) ? 'inactive' : 'active';
my $wait_rc = wait_for_service_state( $systemctl, $service, $wanted_state, 45 );
print "\n";
$rc = $wait_rc if $wait_rc != 0;
}
my ( $status_rc, $status_output ) = run_systemctl_command( $systemctl, '--no-pager', '--full', 'status', $service );
append_command_output( "systemctl --no-pager --full status $service", $status_rc, $status_output );
}
print <<EOF;
if( $rc == 0 )
{
print "</pre><script>var w=document.getElementById('waitbox');if(w){w.style.display='none';}</script><p class=\"ok\">Command completed successfully.</p>\n";
}
else
{
print "</pre><script>var w=document.getElementById('waitbox');if(w){w.style.display='none';}</script><p class=\"bad\">Command failed with exit code $rc.</p>\n";
}
print <<HTML_FOOT;
<div class="actions">
<a href="/cgi-bin/control?status" class="secondary">Status</a>
<a href="/cgi-bin/control?start">Start</a>
<a href="/cgi-bin/control?stop" class="secondary">Stop</a>
<a href="/cgi-bin/control?restart">Restart</a>
<a href="/service/control?status" class="secondary">Status</a>
<a href="/service/control?start">Start</a>
<a href="/service/control?stop" class="secondary">Stop</a>
<a href="/service/control?restart">Restart</a>
</div>
<p><a href="/static/start.html">Back</a></p>
</div>
</body>
</html>
EOF
HTML_FOOT
}
1;

View File

@@ -22,6 +22,166 @@
#
#
sub html_escape( $ )
{
my $s = $_[0];
$s = '' unless defined $s;
$s =~ s/&/&amp;/g;
$s =~ s/</&lt;/g;
$s =~ s/>/&gt;/g;
$s =~ s/"/&quot;/g;
return $s;
}
sub settings_nav_bar()
{
return <<'EOF_NAV';
<div class="settings-nav" style="position:sticky;top:0;z-index:20;margin:0 0 14px 0;padding:10px;background:#fffdf9;border:1px solid #ddcfba;border-radius:12px;box-shadow:0 4px 12px rgba(80,55,30,0.06);">
<a class="settings-nav-back" style="display:inline-block;margin-right:10px;padding:7px 12px;border-radius:9px;background:#6d5d53;color:#fff;text-decoration:none;font-weight:bold;" href="javascript:history.back()">Back</a>
<a class="settings-nav-main" style="display:inline-block;padding:7px 12px;border-radius:9px;background:#a32020;color:#fff;text-decoration:none;font-weight:bold;" href="/" target="_top">Main menu</a>
</div>
EOF_NAV
}
sub delete_confirm_attr( $ )
{
my $what = html_escape( $_[0] );
return ' onclick="return confirm(\'Delete ' . $what . '?\')"';
}
$settings_nav_bar = settings_nav_bar();
sub kernel_network_interfaces()
{
my %interfaces = ();
if( opendir( my $dh, '/sys/class/net' ) )
{
foreach my $ifname ( readdir( $dh ) )
{
next if $ifname =~ /^\./;
next if $ifname =~ /[^-_\.A-Za-z0-9]/;
$interfaces{$ifname} = 1;
}
closedir( $dh );
}
return %interfaces;
}
sub ipx_enabled_interfaces()
{
my %ipx_interfaces = ();
# Modern Linux IPX procfs format:
# Network Node_Address Primary Device Frame_Type
# AC100B98 000000000001 Yes Internal None
# 00000022 508140F6AC45 No enp46s0u1u3u3 802.2
if( open( my $fh, '<', '/proc/net/ipx/interface' ) )
{
while( my $line = <$fh> )
{
chomp( $line );
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if $line eq '';
next if $line =~ /^Network\s+Node_Address\s+Primary\s+Device\s+Frame_Type/i;
my @fields = split( /\s+/, $line );
# Device is the fourth column. Do not scan all tokens, otherwise
# Network/Node/Frame_Type values can accidentally be treated as names.
next if scalar( @fields ) < 5;
my $dev = $fields[3];
next if ! defined( $dev );
next if $dev eq '';
next if lc( $dev ) eq 'internal';
next if $dev =~ /[^-_\.A-Za-z0-9]/;
# Only show real Linux network interfaces.
next if ! -e '/sys/class/net/' . $dev;
$ipx_interfaces{$dev} = 1;
}
close( $fh );
}
# Fallback for older distributions/tools, if present. This parser is kept
# deliberately conservative and still verifies /sys/class/net/<device>.
if( open( my $fh, '<', '/proc/net/ipx_interfaces' ) )
{
while( my $line = <$fh> )
{
chomp( $line );
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if $line eq '';
next if $line =~ /^(Network|Net|Address|Node|Interface|Device)\b/i;
foreach my $dev ( split( /\s+/, $line ) )
{
next if ! defined( $dev );
next if $dev eq '';
next if lc( $dev ) eq 'internal';
next if $dev =~ /[^-_\.A-Za-z0-9]/;
next if ! -e '/sys/class/net/' . $dev;
$ipx_interfaces{$dev} = 1;
}
}
close( $fh );
}
return sort keys %ipx_interfaces;
}
sub network_interface_options( $ )
{
my $current = $_[0];
my @interfaces = ();
my %seen = ();
my $html = '';
$current = '' unless defined $current;
# Only show interfaces where IPX is currently active. The current value is
# still preserved below, so an existing config entry is not lost if the
# interface is temporarily down or IPX is not loaded at display time.
foreach my $ifname ( ipx_enabled_interfaces() )
{
next if $seen{$ifname};
push( @interfaces, $ifname );
$seen{$ifname} = 1;
}
# Keep special/manual values usable even if they are not real kernel interfaces.
foreach my $ifname ( '*', 'auto', $current )
{
next if !defined( $ifname ) || $ifname eq '';
next if $seen{$ifname};
push( @interfaces, $ifname );
$seen{$ifname} = 1;
}
foreach my $ifname ( @interfaces )
{
my $selected = ( $ifname eq $current ) ? ' SELECTED' : '';
my $label = $ifname;
$label = '* (all IPX interfaces)' if $ifname eq '*';
$label = 'auto' if $ifname eq 'auto';
$html .= '\t\t\t\t<OPTION VALUE="' . html_escape( $ifname ) . '"' . $selected . '>' . html_escape( $label ) . '</OPTION>\n';
}
return $html;
}
sub handle_request()
{
if( $c[1] eq 'general' )
@@ -78,6 +238,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/general" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -235,6 +396,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/dirs" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -363,6 +525,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/configh" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -493,6 +656,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/security" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -612,6 +776,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/susers" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -775,6 +940,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/logging" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -1013,6 +1179,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
<TR BGCOLOR="#d7c0a0">
@@ -1034,7 +1201,7 @@ EOF
<A HREF="/settings/volumes/$c[1]"><TT>$c[1]</TT></A> (<TT>$c[2]</TT>) <!-- NETSCAPE RRRAAARRR -->
</TD>
<TD ALIGN=RIGHT>
<A HREF="/apply/volumes/$c[1]">Delete</A><BR>
<A HREF="/apply/volumes/$c[1]" onclick="return confirm('Delete volume $c[1]?')">Delete</A><BR>
</TD>
</TR>
EOF
@@ -1137,6 +1304,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/volumes/$c[0]" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -1306,6 +1474,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
<TR BGCOLOR="#d7c0a0">
@@ -1327,7 +1496,7 @@ EOF
<A HREF="/settings/devices/$c[1]"><TT>$c[1]</TT></A> ($c[2]/$c[3])
</TD>
<TD ALIGN=RIGHT>
<A HREF="/apply/devices/$c[1]">Delete</A><BR>
<A HREF="/apply/devices/$c[1]" onclick="return confirm('Delete device $c[1]?')">Delete</A><BR>
</TD>
</TR>
EOF
@@ -1349,6 +1518,7 @@ EOF
{
$c = getconfigline( '4 ' . $c[2] );
@c = split( ' ', $c );
$interface_options = network_interface_options( $c[1] );
$c[2] =~ s/\.//g;
eval( '$frametype_' . $c[2] . ' = " SELECTED";' );
@@ -1389,6 +1559,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/devices/$c[0]" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -1413,7 +1584,10 @@ TT { color:#5b4b38; }
<B>Network interface:</B>
</TD>
<TD ALIGN=RIGHT>
<INPUT NAME="interface" TYPE=TEXT SIZE=20 VALUE="$c[1]"><BR>
<SELECT NAME="interface">
$interface_options </SELECT><BR>
<SMALL>Manual override:</SMALL><BR>
<INPUT NAME="interface_manual" TYPE=TEXT SIZE=20 VALUE=""><BR>
</TD>
</TR>
<TR BGCOLOR="#fbf7f1">
@@ -1500,6 +1674,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/smart" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -1605,6 +1780,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
<TR BGCOLOR="#d7c0a0">
@@ -1626,7 +1802,7 @@ EOF
<A HREF="/settings/users/$c[0]">$c[0]</A>
</TD>
<TD ALIGN=RIGHT>
<A HREF="/apply/users/$c[0]">Delete</A><BR>
<A HREF="/apply/users/$c[0]" onclick="return confirm('Delete user $c[0]?')">Delete</A><BR>
</TD>
</TR>
EOF
@@ -1719,6 +1895,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/users/$c[2]" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -1837,6 +2014,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
<TR BGCOLOR="#d7c0a0">
@@ -1858,7 +2036,7 @@ EOF
<A HREF="/settings/groups/$c[0]">$c[0]</A>
</TD>
<TD ALIGN=RIGHT>
<A HREF="/apply/groups/$c[0]">Delete</A><BR>
<A HREF="/apply/groups/$c[0]" onclick="return confirm('Delete group $c[0]?')">Delete</A><BR>
</TD>
</TR>
EOF
@@ -1944,6 +2122,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/groups/$c[2]" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
@@ -2045,6 +2224,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>
<TR BGCOLOR="#d7c0a0">
@@ -2066,7 +2246,7 @@ EOF
<A HREF="/settings/queues/$c[0]">$c[0]</A>
</TD>
<TD ALIGN=RIGHT>
<A HREF="/apply/queues/$c[0]">Delete</A><BR>
<A HREF="/apply/queues/$c[0]" onclick="return confirm('Delete queue $c[0]?')">Delete</A><BR>
</TD>
</TR>
EOF
@@ -2130,6 +2310,7 @@ TT { color:#5b4b38; }
</style>
</HEAD>
<BODY BGCOLOR="#f6f2ea">
$settings_nav_bar
<FORM ACTION="/apply/queues/$c[2]" METHOD=GET>
<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>