Handle hostnames with upper-case letters
[webmin.git] / ipsec / import.cgi
1 #!/usr/local/bin/perl
2 # import.cgi
3 # Add an imported connection to the config file
4
5 require './ipsec-lib.pl';
6 &ReadParseMime();
7 &error_setup($text{'import_err'});
8
9 # Get the file
10 if ($in{'mode'} == 0) {
11         $in{'upload'} || &error($text{'import_eupload'});
12         $in{'upload'} =~ s/\r//g;
13         @data = split(/\n/, $in{'upload'});
14         }
15 else {
16         $in{'file'} || &error($text{'import_efile'});
17         open(FILE, $in{'file'}) || &error($text{'import_eopen'});
18         while(<FILE>) {
19                 s/\r|\n//g;
20                 push(@data, $_);
21                 }
22         close(FILE);
23         }
24
25 # Read and validate it
26 $temp = &transname();
27 open(TEMP, ">$temp");
28 print TEMP map { "$_\n" } @data;
29 close(TEMP);
30 @iconf = &get_config($temp);
31 unlink($temp);
32 if (@iconf != 1 || $iconf[0]->{'name'} ne 'conn') {
33         &error($text{'import_eformat'});
34         }
35 @conf = &get_config();
36 foreach $c (@conf) {
37         if (lc($c->{'value'}) eq lc($iconf[0]->{'value'})) {
38                 $clash = $c;
39                 if (!$in{'over'}) {
40                         &error(&text('import_eclash',
41                                      "<tt>$iconf[0]->{'value'}</tt>"));
42                         }
43                 }
44         }
45
46 # Add to the real config file
47 if ($clash) {
48         &lock_file($clash->{'file'});
49         $lref = &read_file_lines($clash->{'file'});
50         splice(@$lref, $clash->{'line'},
51                $clash->{'eline'} - $clash->{'line'} + 1, @data);
52         }
53 else {
54         &lock_file($config{'file'});
55         $lref = &read_file_lines($config{'file'});
56         push(@$lref, @data);
57         }
58 &flush_file_lines();
59 &unlock_all_files();
60
61 # Tell the user
62 &ui_print_header(undef, $text{'import_title'}, "");
63
64 print "<p>",&text($clash ? 'import_done2' : 'import_done1',
65                   "<tt>$iconf[0]->{'value'}</tt>"),"<p>\n";
66
67 &ui_print_footer("", $text{'index_return'});
68
69 # All done
70 &webmin_log("import", "conn", $iconf[0]->{'value'}, $iconf[0]->{'values'});
71