#!/usr/local/bin/perl
# Show a form for editing or creating an jail

use strict;
use warnings;
no warnings 'redefine';
no warnings 'uninitialized';
require './fail2ban-lib.pl';
our (%in, %text);
&ReadParse();

# Get default jail
my @jails = &list_jails();
my ($def) = grep { $_->{'name'} eq 'DEFAULT' } @jails;

# Show header and get the jail object
my ($jail);
if ($in{'new'}) {
	&ui_print_header(undef, $text{'jail_title1'}, "");
	$jail = { };
	}
else {
	&ui_print_header(undef, $text{'jail_title2'}, "");
	($jail) = grep { $_->{'name'} eq $in{'name'} } @jails;
	$jail || &error($text{'jail_egone'});
	}

print &ui_form_start("save_jail.cgi", "post");
print &ui_hidden("new", $in{'new'});
print &ui_hidden("old", $in{'name'});
print &ui_table_start($text{'jail_header'}, undef, 2);

# Enabled or disabled?
my $enabled = &find_value("enabled", $jail);
print &ui_table_row($text{'jail_enabled'},
	&ui_yesno_radio("enabled", $enabled =~ /true|yes|1/i));

# Jail name
print &ui_table_row($text{'jail_name'},
	&ui_textbox("name", $jail->{'name'}, 30));

# Filter to match
my @filters = &list_filters();
my $filter = &find_value("filter", $jail);
print &ui_table_row($text{'jail_filter'},
	&ui_select("filter",
		   $filter,
		   [ [ undef, "&lt;$text{'default'}&gt;" ],
		     map { &filename_to_name($_->[0]->{'file'}) } @filters ],
		   1, 0, $filter ? 1 : 0));

# Backend
my $backend = &find_value("backend", $jail);
my @backend_options = (
	[ "", "" ],
	[ "auto", $text{'jail_auto'} ],
	[ "systemd", $text{'jail_systemd'} ],
	[ "polling", $text{'jail_polling'} ],
	[ "gamin", $text{'jail_gamin'} ],
	[ "pyinotify", $text{'jail_pyinotify'} ],
);
if ($backend && $backend =~ /^\%\(\w+\)s$/) { # placeholder for backend
	splice(@backend_options, 2, 0, [ $backend, $backend ]);
	}

print &ui_table_row($text{'jail_backend'},
	&ui_select("backend", $backend || "", \@backend_options));

# Ports to monitor
my $port = &find_value("port", $jail);
print &ui_table_row($text{'jail_ports'},
	&ui_textbox("port", $port, 35));

# Actions to run
my $actionlist = &find("action", $jail);
my @actions = &list_actions();
my $atable = &ui_columns_start([
		$text{'jail_action'},
		$text{'jail_aname'},
		$text{'jail_port'},
		$text{'jail_protocol'},
		$text{'jail_others'},
		]);
my $i = 0;
foreach my $a (@{$actionlist->{'words'}}, undef) {
	my ($action, $opts) = $a ? &parse_action_definition($a) : (undef, []);
	my %opts = map { ($_->[0], $_->[1]) } @$opts;
	my @oopts = grep { $_->[0] !~ /^(name|port|protocol)$/ } @$opts;
	my $others = join("", map { $_->[2] } @oopts);
	$others =~ s/^\s*,?\s*//;
	$others =~ s/\s*,?\s*$//;
	$atable .= &ui_columns_row([
		&ui_select("action_$i", $action,
		   [ [ "", "&nbsp;" ],
		     map { &filename_to_name($_->[0]->{'file'}) } @actions ],
		   1, 0, $action ? 1 : 0),
		&ui_textbox("name_$i", $opts{'name'}, 15),
		&ui_textbox("port_$i", $opts{'port'}, 6),
		&ui_select("protocol_$i", $opts{'protocol'},
			   [ [ '', '&nbsp;' ],
			     [ 'tcp', 'TCP' ],
			     [ 'udp', 'UDP' ],
			     [ 'icmp', 'ICMP' ] ]),
		&ui_textbox("others_$i", $others, 40),
		]);
	$i++;
	}
$atable .= &ui_columns_end();
print &ui_table_row($text{'jail_actions'}, $atable);

# Log file paths
my $logpath = &find_value("logpath", $jail);
print &ui_table_row($text{'jail_logpath'},
	&ui_textarea("logpath", $logpath, 5, 80, "hard"));

# Matches needed
my $def_maxretry = &find_value("maxretry", $def) || 3;
my $maxretry = &find_value("maxretry", $jail);
print &ui_table_row($text{'jail_maxretry'},
	&ui_opt_textbox("maxretry", $maxretry, 6,
			$text{'default'}." (".$def_maxretry.")"));

# Time to scan over
my $def_findtime = &find_value("findtime", $def) || 600;
my $findtime = &find_value("findtime", $jail);
print &ui_table_row($text{'jail_findtime'},
	&ui_opt_textbox("findtime", $findtime, 6,
			$text{'default'}." (".$def_findtime.")"));

# Time to ban for
my $def_bantime = &find_value("bantime", $def) || 600;
my $bantime = &find_value("bantime", $jail);
print &ui_table_row($text{'jail_bantime'},
	&ui_opt_textbox("bantime", $bantime, 6,
			$text{'default'}." (".$def_bantime.")"));

# Incremental banning options, supported since Fail2Ban 0.11.1
if (&supports_bantime_increment()) {
	print &ui_table_hr();
	print &ui_table_span("<b>".$text{'jail_increment_header'}."</b><br>".
		&ui_note($text{'jail_increment_desc'}, 0));

	my $def_increment = &canonical_fail2ban_boolean(
		&find_value("bantime.increment", $def));
	my $increment = &canonical_fail2ban_boolean(
		&find_value("bantime.increment", $jail));
	my $def_increment_text = $def_increment eq "true" ? $text{'yes'} :
								  $text{'no'};
	print &ui_table_row($text{'jail_bantime_increment'},
		&ui_select("bantime_increment", $increment,
			[ [ "", $text{'default'}." (".$def_increment_text.")" ],
			  [ "true", $text{'yes'} ],
			  [ "false", $text{'no'} ] ], 1, 0, 1));

	my $def_factor = &find_value("bantime.factor", $def) || 1;
	my $factor = &find_value("bantime.factor", $jail);
	print &ui_table_row($text{'jail_bantime_factor'},
		&ui_opt_textbox("bantime_factor", $factor, 8,
			$text{'default'}." (".&html_escape($def_factor).")"));

	my $def_maxtime = &find_value("bantime.maxtime", $def);
	my $maxtime = &find_value("bantime.maxtime", $jail);
	my $def_maxtime_text = $def_maxtime ne "" ?
		&html_escape($def_maxtime) : $text{'jail_nolimit'};
	print &ui_table_row($text{'jail_bantime_maxtime'},
		&ui_opt_textbox("bantime_maxtime", $maxtime, 8,
			$text{'default'}." (".$def_maxtime_text.")"));

	my $def_overalljails = &canonical_fail2ban_boolean(
		&find_value("bantime.overalljails", $def));
	my $overalljails = &canonical_fail2ban_boolean(
		&find_value("bantime.overalljails", $jail));
	my $def_overalljails_text = $def_overalljails eq "true" ?
		$text{'yes'} : $text{'no'};
	print &ui_table_row($text{'jail_bantime_overalljails'},
		&ui_select("bantime_overalljails", $overalljails,
			[ [ "", $text{'default'}." (".$def_overalljails_text.")" ],
			  [ "true", $text{'yes'} ],
			  [ "false", $text{'no'} ] ], 1, 0, 1));

	my $def_rndtime = &find_value("bantime.rndtime", $def);
	my $rndtime = &find_value("bantime.rndtime", $jail);
	my $def_rndtime_text = $def_rndtime ne "" ?
		&html_escape($def_rndtime) : $text{'jail_none'};
	print &ui_table_row($text{'jail_bantime_rndtime'},
		&ui_opt_textbox("bantime_rndtime", $rndtime, 8,
			$text{'default'}." (".$def_rndtime_text.")"));
	print &ui_table_hr();
	}

# IPs to ignore
my $def_ignoreip = &find_value("ignoreip", $def) || "127.0.0.1";
my $ignoreip = &find_value("ignoreip", $jail);
print &ui_table_row($text{'jail_ignoreip'},
	&ui_opt_textbox("ignoreip", $ignoreip, 40,
			$text{'default'}." (".$def_ignoreip.")"));

print &ui_table_end();
if ($in{'new'}) {
	print &ui_form_end([ [ undef, $text{'create'} ] ]);
	}
else {
	print &ui_form_end([ [ undef, $text{'save'} ],
			     [ 'delete', $text{'delete'} ] ]);
	}

&ui_print_footer("list_jails.cgi", $text{'jails_return'});

