1#!/usr/local/bin/perl
2# exec_cron.cgi
3# Execute an existing cron job, and display the output
4
5require './cron-lib.pl';
6&ReadParse();
7
8@jobs = &list_cron_jobs();
9$job = $jobs[$in{'idx'}];
10&can_edit_user(\%access, $job->{'user'}) || &error($text{'exec_ecannot'});
11&foreign_require("proc", "proc-lib.pl");
12
13# split command into command and input
14&convert_range($job);
15&convert_comment($job);
16$job->{'command'} =~ s/\\%/\0/g;
17@lines = split(/%/ , $job->{'command'});
18foreach (@lines) { s/\0/%/g; }
19for($i=1; $i<@lines; $i++) {
20	$input .= $lines[$i]."\n";
21	}
22
23if ($in{'bg'}) {
24	&ui_print_header(undef, $text{'exec_title'}, "");
25	}
26else {
27	&ui_print_unbuffered_header(undef, $text{'exec_title'}, "");
28	}
29&additional_log('exec', undef, $lines[0]);
30&webmin_log("exec", "cron", $job->{'user'}, $job);
31
32# Remove variables that wouldn't be in the 'real' cron
33&clean_environment();
34
35# Set cron environment variables
36foreach $e (&read_envs($job->{'user'})) {
37	$ENV{$1} = $2 if ($e =~ /^(\S+)\s+(.*)$/);
38	}
39
40if (&supports_users()) {
41	# Get command and switch uid/gid
42	@uinfo = getpwnam($job->{'user'});
43	$ENV{"HOME"} = $uinfo[7];
44	$ENV{"SHELL"} = "/bin/sh";
45	$ENV{"LOGNAME"} = $ENV{"USER"} = $job->{'user'};
46	&switch_to_unix_user(\@uinfo);
47	}
48
49if ($in{'bg'}) {
50	# Run in background
51	print &text('exec_cmdbg',
52		    "<tt>".&html_escape($lines[0])."</tt>"),"<p>\n";
53	if (defined($input)) {
54		local $temp = &tempname();
55		&open_tempfile(TEMP, ">$temp");
56		&print_tempfile(TEMP, $input);
57		&close_tempfile(TEMP);
58		&execute_command("(($lines[0]) ; rm -f $temp) &", $temp,
59				 undef, undef);
60		}
61	else {
62		&execute_command("($lines[0]) &", undef, undef, undef);
63		}
64	}
65else {
66	# Execute cron command and display output..
67	print &text('exec_cmd',
68		    "<tt>".&html_escape($lines[0])."</tt>"),"<p>\n";
69	print "<pre>";
70	$got = &foreign_call("proc", "safe_process_exec",
71			     $lines[0], 0, 0, STDOUT, $input, 1);
72	print "<i>$text{'exec_none'}</i>\n" if (!$got);
73	print "</pre>\n";
74	}
75
76&ui_print_footer("edit_cron.cgi?idx=$in{'idx'}", $text{'edit_return'},
77	"", $text{'index_return'});
78
79