Handle hostnames with upper-case letters
[webmin.git] / servers / save_auto.cgi
1 #!/usr/local/bin/perl
2 # Save scheduled finding of servers
3 # Thanks to OpenCountry for sponsoring this feature
4
5 use strict;
6 use warnings;
7 require './servers-lib.pl';
8 our (%in, %text, $module_name, $cron_cmd, @cluster_modules, %config, %access);
9 $access{'auto'} || &error($text{'auto_ecannot'});
10 &ReadParse();
11 &error_setup($text{'auto_err'});
12
13 my $job = &find_cron_job();
14
15 # Validate inputs
16 if ($in{'sched'}) {
17         $in{'mins'} =~ /^\d+$/ || &error($text{'auto_emins'});
18         if ($in{'net_def'} == 1) {
19                 $config{'auto_net'} = undef;
20                 }
21         elsif ($in{'net_def'} == 0) {
22                 my @nets = split(/\s+/, $in{'net'});
23                 foreach my $n (@nets) {
24                         &check_ipaddress($n) ||
25                                 &error($text{'auto_enet'});
26                         }
27                 $config{'auto_net'} = $in{'net'};
28                 }
29         else {
30                 $in{'iface'} =~ /^[a-z]+\d*(:\d+)?$/ ||
31                         &error($text{'auto_eiface'});
32                 $config{'auto_net'} = $in{'iface'};
33                 }
34         $in{'auser'} =~ /\S/ || &error($text{'auto_euser'});
35         $config{'auto_user'} = $in{'auser'};
36         $config{'auto_pass'} = $in{'apass'};
37         $config{'auto_type'} = $in{'type'};
38         foreach my $m (@cluster_modules) {
39                 if (&foreign_available($m)) {
40                         $config{'auto_'.$m} = $in{$m};
41                         }
42                 }
43         $config{'auto_remove'} = $in{'remove'};
44         $config{'auto_self'} = $in{'self'};
45         $in{'email_def'} || $in{'email'} =~ /\S/ ||&error($text{'auto_eemail'});
46         $config{'auto_email'} = $in{'email_def'} ? undef : $in{'email'};
47         $in{'smtp_def'} || &to_ipaddress($in{'smtp'}) ||
48             &to_ip6address($in{'smtp'}) ||
49                 &error($text{'auto_esmtp'});
50         $config{'auto_smtp'} = $in{'smtp_def'} ? undef : $in{'smtp'};
51         &save_module_config();
52         }
53
54 # Create or update Cron job
55 &cron::create_wrapper($cron_cmd, $module_name, "auto.pl");
56 my @mins;
57 if ($in{'sched'}) {
58         # Work out minutes
59         for(my $i=0; $i<60; $i+=$in{'mins'}) {
60                 push(@mins, $i);
61                 }
62         }
63 if ($in{'sched'} && !$job) {
64         # Create the job
65         $job = { 'user' => 'root',
66                  'command' => $cron_cmd,
67                  'active' => 1,
68                  'mins' => join(",", @mins),
69                  'hours' => '*',
70                  'days' => '*',
71                  'months' => '*',
72                  'weekdays' => '*',
73                 };
74         &cron::create_cron_job($job);
75         }
76 elsif (!$in{'sched'} && $job) {
77         # Delete the job
78         &cron::delete_cron_job($job);
79         }
80 elsif ($in{'sched'} && $job) {
81         # Update the job
82         $job->{'mins'} = join(",", @mins);
83         $job->{'hours'} = $job->{'days'} =
84             $job->{'months'} = $job->{'weekdays'} = '*';
85         &cron::change_cron_job($job);
86         }
87
88 &redirect("");
89