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