Handle hostnames with upper-case letters
[webmin.git] / itsecur-firewall / import_servs.cgi
1 #!/usr/bin/perl
2 # Actually do an import of services
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 %services = map { $_->{'name'}, $_ } &list_services();
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 >= 3 || &error(&text('import_erow', $i, $oldline));
35
36         # Create a service
37         $row[0] =~ /\S/ || &error(text('import_eservname', $i));
38         $services{$row[0]} && &error(text('import_eservclash', $i, $row[0]));
39         $serv = { 'name' => $row[0] };
40         for($i=1; $i<@row; $i+=2) {
41                 getprotobyname($row[$i]) ||
42                         &error(text('import_eproto', $i, $row[$i]));
43                 $row[$i+1] =~ /^\d+$/ ||
44                         &error(text('import_eservnum', $i, $row[$i]));
45                 push(@{$serv->{'protos'}}, $row[$i]);
46                 push(@{$serv->{'ports'}}, $row[$i+1]);
47                 }
48         push(@newservs, $serv);
49         }
50
51 # Save the services
52 &lock_itsecur_files();
53 @servs = &list_services();
54 push(@servs, @newservs);
55 &automatic_backup();
56 &save_services(@servs);
57 &unlock_itsecur_files();
58
59 # Tell the user
60 &header($text{'import_title'}, "",
61         undef, undef, undef, undef, &apply_button());
62 print "<hr>\n";
63
64 print "<p>",&text('import_done2', scalar(@newservs)),"<p>\n";
65
66 print "<hr>\n";
67 &footer("", $text{'index_return'});
68 &remote_webmin_log("import", "services", $in{'src_def'} ? undef : $in{'src'});
69