Handle hostnames with upper-case letters
[webmin.git] / itsecur-firewall / import_rules.cgi
1 #!/usr/bin/perl
2 # Actually do an import
3
4 require './itsecur-lib.pl';
5 &can_edit_error("import");
6 &error_setup($text{'import_err'});
7 &ReadParseMime();
8
9 if (&foreign_check("net")) {
10         &foreign_require("net", "net-lib.pl");
11         foreach $i (&net::active_interfaces(), &net::boot_interfaces()) {
12                 $iface{$i->{'fullname'}} = $i;
13                 }
14         }
15 %services = map { $_->{'name'}, $_ } &list_services();
16 %times = map { $_->{'name'}, $_ } &list_times();
17
18 # Validate inputs
19 if (!$in{'src_def'}) {
20         -r $in{'src'} || &error_cleanup($text{'restore_esrc'});
21         $data = `cat $in{'src'}`;
22         }
23 else {
24         $in{'file'} || &error_cleanup($text{'restore_efile'});
25         $data = $in{'file'};
26         }
27
28 # Parse the CSV data
29 $data =~ s/\r//g;
30 $i = 0;
31 foreach $line (split(/\n/, $data)) {
32         # Split into columns
33         $oldline = $line;
34         $i++;
35         next if (!$line);
36         local @row;
37         while($line && $line =~ /^,?("([^"]*)"|([^,]*))(.*)$/) {
38                 push(@row, $2 || $3);
39                 $line = $4;
40                 }
41         @row >= 4 || &error(&text('import_erow', $i, $oldline));
42
43         # Create a rule
44         $rule = { 'enabled' => 1 };
45         $rule->{'source'} = &parse_srcdest($row[0]);
46         $rule->{'source'} || &error(text('import_esource', $i, $row[0]));
47         $rule->{'dest'} = &parse_srcdest($row[1]);
48         $rule->{'dest'} || &error(text('import_edest', $i, $row[1]));
49         @servs = split(/\s+/, $row[2]);
50         foreach $s (@servs) {
51                 $services{$s} || &error(text('import_eservice', $i, $s));
52                 }
53         $rule->{'service'} = @servs ? join(",", @servs) : "*";
54         if ($row[3] =~ s/\s+log$//i) {
55                 $rule->{'log'} = 1;
56                 }
57         else {
58                 $rule->{'log'} = 0;
59                 }
60         &indexof(lc($row[3]), @actions) >= 0 ||
61                 &error(text('import_eaction', $i, $row[3]));
62         $rule->{'action'} = lc($row[3]);
63         $rule->{'desc'} = $row[4] || "*";
64         if ($row[5]) {
65                 $times{$row[5]} || &error(text('import_etime', $i, $row[5]));
66                 $rule->{'time'} = $row[5];
67                 }
68         else {
69                 $rule->{'time'} = "*";
70                 }
71         push(@newrules, $rule);
72         }
73
74 # Ensure that new rules are sane
75
76 # Save the rules
77 &lock_itsecur_files();
78 @rules = &list_rules();
79 push(@rules, @newrules);
80 &automatic_backup();
81 &save_rules(@rules);
82 &unlock_itsecur_files();
83
84 # Tell the user
85 &header($text{'import_title'}, "",
86         undef, undef, undef, undef, &apply_button());
87 print "<hr>\n";
88
89 print "<p>",&text('import_done1', scalar(@newrules)),"<p>\n";
90
91 print "<hr>\n";
92 &footer("", $text{'index_return'});
93 &remote_webmin_log("import", "rules", $in{'src_def'} ? undef : $in{'src'});
94
95 sub parse_srcdest
96 {
97 if ($_[0] eq "") {
98         return "*";
99         }
100 elsif (&valid_host($_[0])) {
101         return $_[0];
102         }
103 elsif ($iface{lc($_[0])}) {
104         return "%".lc($_[0]);
105         }
106 else {
107         return undef;
108         }
109 }