Handle hostnames with upper-case letters
[webmin.git] / itsecur-firewall / import_groups.cgi
1 #!/usr/bin/perl
2 # Actually do an import of host groups
3
4 require './itsecur-lib.pl';
5 &can_edit_error("import");
6 &error_setup($text{'import_err'});
7 &ReadParseMime();
8
9 # Validate inputs
10 if (!$in{'src_def'}) {
11         -r $in{'src'} || &error_cleanup($text{'restore_esrc'});
12         $data = `cat $in{'src'}`;
13         }
14 else {
15         $in{'file'} || &error_cleanup($text{'restore_efile'});
16         $data = $in{'file'};
17         }
18
19 %groups = map { $_->{'name'}, $_ } &list_groups();
20
21 # Parse the CSV data
22 $data =~ s/\r//g;
23 $i = 0;
24 foreach $line (split(/\n/, $data)) {
25         # Split into columns
26         $oldline = $line;
27         $i++;
28         next if (!$line);
29         local @row;
30         while($line && $line =~ /^,?("([^"]*)"|([^,]*))(.*)$/) {
31                 push(@row, $2 || $3);
32                 $line = $4;
33                 }
34         @row >= 1 || &error(&text('import_erow', $i, $oldline));
35
36         # Create a service
37         $row[0] =~ /\S/ || &error(text('import_egroupname', $i));
38         $groups{$row[0]} && &error(text('import_egroupclash', $i, $row[0]));
39         $group = { 'name' => $row[0] };
40         if (@row == 1) {
41                 # Group name is the host name
42                 &valid_host($row[0]) ||
43                         &error(text('import_ehost', $i, $row[0]));
44                 $group->{'members'} = [ $row[0] ];
45                 }
46         else {
47                 # Hosts are listed
48                 for($i=1; $i<@row; $i++) {
49                         &valid_host($row[$i]) ||
50                                 &error(text('import_ehost', $i, $row[$i]));
51                         push(@{$group->{'members'}}, $row[$i]);
52                         }
53                 }
54         push(@newgroups, $group);
55         }
56
57 # Save the groups
58 &lock_itsecur_files();
59 @groups = &list_groups();
60 push(@groups, @newgroups);
61 &automatic_backup();
62 &save_groups(@groups);
63 &unlock_itsecur_files();
64
65 # Tell the user
66 &header($text{'import_title'}, "",
67         undef, undef, undef, undef, &apply_button());
68 print "<hr>\n";
69
70 print "<p>",&text('import_done3', scalar(@newgroups)),"<p>\n";
71
72 print "<hr>\n";
73 &footer("", $text{'index_return'});
74 &remote_webmin_log("import", "services", $in{'src_def'} ? undef : $in{'src'});
75