Handle hostnames with upper-case letters
[webmin.git] / itsecur-firewall / backup.cgi
1 #!/usr/bin/perl
2 # Actually do a backup
3
4 require './itsecur-lib.pl';
5 &can_edit_error("backup");
6 &error_setup($text{'backup_err'});
7 &ReadParse();
8
9 # Validate inputs
10 if ($in{'dest_mode'} == 0) {
11         $file = &tempname();
12         }
13 elsif ($in{'dest_mode'} == 1) {
14         $orig_dest = $in{'dest'};
15         if (-d $in{'dest'}) {
16                 $in{'dest'} .= "/firewall.zip";
17                 }
18         $in{'dest'} =~ /^(.*)\// || &error($text{'backup_edest'});
19         -d $1 || &error($text{'backup_edestdir'});
20         $file = $in{'dest'};
21         $done = &text('backup_done1', $file);
22         }
23 elsif ($in{'dest_mode'} == 2) {
24         gethostbyname($in{'ftphost'}) || &error($text{'backup_eftphost'});
25         $in{'ftpfile'} =~ /^\/\S+/ || &error($text{'backup_eftpfile'});
26         $in{'ftpuser'} =~ /\S/ || &error($text{'backup_eftpuser'});
27         $file = "ftp://$in{'ftpuser'}:$in{'ftppass'}\@$in{'ftphost'}$in{'ftpfile'}";
28         $done = &text('backup_done2', $in{'ftphost'}, $in{'ftpfile'});
29         }
30 elsif ($in{'dest_mode'} == 3) {
31         $in{'email'} =~ /^\S+\@\S+$/ || &error($text{'backup_eemail'});
32         $file = "mailto:$in{'email'}";
33         $done = &text('backup_done3', $in{'email'});
34         }
35 if (!$in{'pass_def'}) {
36         $in{'pass'} || &error($text{'backup_epass'});
37         }
38 @what = split(/\0/, $in{'what'});
39 @what || &error($text{'backup_ewhat'});
40
41 if (!$in{'save'}) {
42         # Create the tar file
43         $err = &backup_firewall(\@what, $file, $in{'pass_def'} ? undef
44                                                                : $in{'pass'});
45         &error($err) if ($err);
46         }
47
48 # Save settings
49 $config{'backup_dest'} = $in{'dest_mode'} == 0 ? undef : $file;
50 $config{'backup_what'} = join(" ", @what);
51 $config{'backup_pass'} = $in{'pass_def'} ? undef : $in{'pass'};
52 &write_file($module_config_file, \%config);
53
54 if ($in{'save'}) {
55         # Tell the user about the cron job
56         &header($text{'backup_title'}, "",
57                 undef, undef, undef, undef, &apply_button());
58         print "<hr>\n";
59
60         print "<p>",&text('backup_donesched'),"<p>\n";
61
62         print "<hr>\n";
63         &footer("", $text{'index_return'});
64         }
65 elsif ($in{'dest_mode'} == 0) {
66         # Send to browser
67         print "Content-type: application/octet-stream\n\n";
68         open(FILE, $file);
69         while(<FILE>) {
70                 print;
71                 }
72         close(FILE);
73         unlink($file);
74         &remote_webmin_log("backup");
75         }
76 else {
77         # Tell the user
78         &header($text{'backup_title'}, "",
79                 undef, undef, undef, undef, &apply_button());
80         print "<hr>\n";
81
82         print "<p>$done<p>\n";
83
84         print "<hr>\n";
85         &footer("", $text{'index_return'});
86         &remote_webmin_log("backup", undef, $in{'dest'});
87         }
88
89 # Setup cron job
90 $job = &find_backup_job();
91 if ($job) {
92         &cron::delete_cron_job($job);
93         }
94 if (!$in{'sched_def'}) {
95         $job = { 'special' => $in{'sched'},
96                  'user' => 'root',
97                  'command' => $cron_cmd,
98                  'active' => 1 };
99         &cron::create_wrapper($cron_cmd, $module_name, "backup.pl");
100         &cron::create_cron_job($job);
101         }
102