Handle hostnames with upper-case letters
[webmin.git] / cron / save_cron.cgi
1 #!/usr/local/bin/perl
2 # save_cron.cgi
3 # Save an existing cron job, or create a new one
4
5 require './cron-lib.pl';
6 &error_setup($text{'save_err'});
7 &ReadParse();
8
9 @jobs = &list_cron_jobs();
10 if ($in{'new'}) {
11         $access{'create'} || &error($text{'save_ecannot2'});
12         $job = { 'type' => 0 };
13         }
14 else {
15         $oldjob = $jobs[$in{'idx'}];
16         $job->{'type'} = $oldjob->{'type'};
17         $job->{'file'} = $oldjob->{'file'};
18         $job->{'line'} = $oldjob->{'line'};
19         $job->{'nolog'} = $oldjob->{'nolog'};
20         }
21
22 @files = &unique((map { $_->{'file'} } @jobs),
23                  "$config{'cron_dir'}/$in{'user'}");
24 foreach $f (@files) { &lock_file($f); }
25
26 # Check and parse inputs
27 if ($in{"cmd"} !~ /\S/ && $access{'command'}) {
28         &error($text{'save_ecmd'});
29         }
30 if (&supports_users()) {
31         if (!$in{'user'}) {
32                 &error($text{'save_euser'});
33                 }
34         if (!defined(getpwnam($in{'user'}))) {
35                 &error(&text('save_euser2', $in{'user'}));
36                 }
37         }
38 &parse_times_input($job, \%in);
39 $in{input} =~ s/\r//g; $in{input} =~ s/%/\\%/g;
40 $in{cmd} =~ s/%/\\%/g;
41 $job->{'active'} = $in{'active'};
42 if ($access{'command'}) {
43         $job->{'command'} = $in{'cmd'};
44         if ($in{input} =~ /\S/) {
45                 @inlines = split(/\n/ , $in{input});
46                 $job->{'command'} .= '%'.join('%' , @inlines);
47                 }
48         }
49 else {
50         $job->{'command'} = $oldjob->{'command'};
51         }
52 $job->{'comment'} = $in{'comment'};
53 &unconvert_comment($job);
54
55 if (&supports_users()) {
56         # Check if this user is allowed to execute cron jobs
57         &can_use_cron($in{'user'}) ||
58                 &error(&text('save_eallow', $in{'user'}));
59         $job->{'user'} = $in{'user'};
60         }
61
62 if (defined($in{'range_def'})) {
63         # Save range to run
64         &parse_range_input($job, \%in);
65         &unconvert_range($job);
66         }
67
68 # Check module access control
69 &can_edit_user(\%access, $in{'user'}) ||
70         &error(&text('save_ecannot', $in{'user'}));
71
72 if (!$in{'new'}) {
73         # Editing an existing job
74         &can_edit_user(\%access, $oldjob->{'user'}) ||
75                 &error(&text('save_ecannot', $oldjob->{'user'}));
76         if ($job->{'user'} eq $oldjob->{'user'}) {
77                 &change_cron_job($job);
78                 }
79         else {
80                 &delete_cron_job($oldjob);
81                 &create_cron_job($job);
82
83                 # Find new index, which will change due to user move
84                 undef(@cron_jobs_cache);
85                 $in{'idx'} = undef;
86                 foreach $newjob (&list_cron_jobs()) {
87                         if ($newjob->{'user'} eq $job->{'user'} &&
88                             $newjob->{'active'} eq $job->{'active'} &&
89                             $newjob->{'command'} eq $job->{'command'}) {
90                                 $in{'idx'} = $newjob->{'index'};
91                                 }
92                         }
93                 }
94         }
95 else {
96         # Creating a new job
97         &create_cron_job($job);
98         }
99
100 foreach $f (@files) { &unlock_file($f); }
101 if ($in{'new'}) {
102         &webmin_log("create", "cron", $in{'user'}, \%in);
103         }
104 else {
105         &webmin_log("modify", "cron", $in{'user'}, \%in);
106         }
107
108 if ($in{'saverun'}) {
109         # Redirect to execute form
110         defined($in{'idx'}) || &error($text{'save_eidx'});
111         &redirect("exec_cron.cgi?idx=$in{'idx'}");
112         }
113 else {
114         # Just go back to main menu
115         &redirect("");
116         }
117
118