Handle hostnames with upper-case letters
[webmin.git] / sentry / save_portsentry.cgi
1 #!/usr/local/bin/perl
2 # save_config.cgi
3 # Update the portsentry config file
4
5 require './sentry-lib.pl';
6 &ReadParse();
7 &error_setup($text{'portsentry_err'});
8 $conf = &get_portsentry_config();
9 &lock_config_files($conf);
10
11 # Validate and save inputs
12 @tports = split(/\s+/, $in{'tports'});
13 foreach $t (@tports) {
14         $t > 0 && $t < 65535 || &error(&text('portsentry_etports', $t));
15         }
16 &save_config($conf, "TCP_PORTS", join(",", @tports));
17 $in{'tadv'} > 0 && $in{'tadv'} < 65535 || &error($text{'portsentry_etadv'});
18 &save_config($conf, "ADVANCED_PORTS_TCP", $in{'tadv'});
19 @texc = split(/\s+/, $in{'texc'});
20 foreach $t (@texc) {
21         $t > 0 && $t < 65535 || &error(&text('portsentry_etexc', $t));
22         }
23 &save_config($conf, "ADVANCED_EXCLUDE_TCP", join(",", @texc));
24
25 @uports = split(/\s+/, $in{'uports'});
26 foreach $t (@uports) {
27         $t > 0 && $t < 65535 || &error(&text('portsentry_euports', $t));
28         }
29 &save_config($conf, "UDP_PORTS", join(",", @uports));
30 $in{'uadv'} > 0 && $in{'uadv'} < 65535 || &error($text{'portsentry_euadv'});
31 &save_config($conf, "ADVANCED_PORTS_UDP", $in{'uadv'});
32 @uexc = split(/\s+/, $in{'uexc'});
33 foreach $t (@uexc) {
34         $t > 0 && $t < 65535 || &error(&text('portsentry_euexc', $t));
35         }
36 &save_config($conf, "ADVANCED_EXCLUDE_UDP", join(",", @uexc));
37
38 &save_config($conf, "BLOCK_TCP", $in{'tblock'});
39 &save_config($conf, "BLOCK_UDP", $in{'ublock'});
40 &save_config($conf, "PORT_BANNER", $in{'banner'});
41
42 $in{'trigger'} =~ /^\d+$/ || &error($text{'portsentry_etrigger'});
43 &save_config($conf, "SCAN_TRIGGER", $in{'trigger'});
44
45 # Save list of ignored hosts
46 if (defined($in{'ignore'})) {
47         if ($config{'portsentry_ignore'}) {
48                 $ign = $config{'portsentry_ignore'};
49                 }
50         else {
51                 $ign = &find_value("IGNORE_FILE", $conf);
52                 }
53         &lock_file($ign);
54         $in{'ignore'} =~ s/\r//g;
55         $in{'ignore'} =~ s/\n*$/\n/;
56         foreach $h (split(/\s+/, $in{'ignore'})) {
57                 &to_ipaddress($h) ||
58                   ($h =~ /^([0-9\.]+)\/(\d+)/ && &check_ipaddress($1)) ||
59                         &error(&text('portsentry_eignore', $h));
60                 }
61         if (defined($in{'editbelow'})) {
62                 open(IGNORE, $ign);
63                 @below = <IGNORE>;
64                 close(IGNORE);
65                 @below = @below[$in{'editbelow'} .. $#below];
66                 }
67         &open_tempfile(IGNORE, ">$ign");
68         &print_tempfile(IGNORE, $in{'ignore'});
69         &print_tempfile(IGNORE, @below);
70         &close_tempfile(IGNORE);
71         &unlock_file($ign);
72         }
73 &flush_file_lines();
74 &unlock_config_files($conf);
75
76 if ($in{'apply'}) {
77         # Restart portsentry
78         &stop_portsentry();
79         $err = &start_portsentry();
80         &error($err) if ($err);
81         }
82 &webmin_log("portsentry");
83
84 &redirect("");
85