Handle hostnames with upper-case letters
[webmin.git] / cron / exec_cron.cgi.bak
1 #!/usr/local/bin/perl
2 # exec_cron.cgi
3 # Execute an existing cron job, and display the output
4
5 require './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 $job->{'command'} =~ s/\\%/\0/g;
15 @lines = split(/%/ , $job->{'command'});
16 foreach (@lines) { s/\0/%/g; }
17 for($i=1; $i<@lines; $i++) {
18         $input .= $lines[$i]."\n";
19         }
20
21 $| = 1;
22 $theme_no_table++;
23 &header($text{'exec_title'}, "");
24 print "<hr>\n";
25 &additional_log('exec', undef, $lines[0]);
26 &webmin_log("exec", "cron", $job->{'user'}, $job);
27
28 # Remove variables that wouldn't be in the 'real' cron
29 foreach $e ('SERVER_PORT', 'GATEWAY_INTERFACE', 'WEBMIN_VAR', 'SERVER_ROOT',
30             'REMOTE_USER', 'SERVER_ADMIN', 'REQUEST_METHOD', 'REMOTE_HOST',
31             'REMOTE_ADDR', 'SERVER_SOFTWARE', 'PATH_TRANSLATED', 'QUERY_STRING',
32             'MINISERV_CONFIG', 'SERVER_NAME', 'SERVER_PROTOCOL', 'REQUEST_URI',
33             'DOCUMENT_ROOT', 'WEBMIN_CONFIG', 'SESSION_ID', 'PATH_INFO',
34             'BASE_REMOTE_USER') {
35         delete($ENV{$e});
36         }
37 foreach $e (keys %ENV) {
38         delete($ENV{$e}) if ($e =~ /^HTTP_/);
39         }
40
41 # Set cron environment variables
42 foreach $e (&read_envs($job->{'user'})) {
43         $ENV{$1} = $2 if ($e =~ /^(\S+)\s+(.*)$/);
44         }
45
46 # Get command and switch uid/gid
47 @uinfo = getpwnam($job->{'user'});
48 $ENV{"HOME"} = $uinfo[7];
49 $ENV{"SHELL"} = "/bin/sh";
50 $ENV{"LOGNAME"} = $ENV{"USER"} = $job->{'user'};
51 $( = $) = $uinfo[3];
52 ($>, $<) = ($uinfo[2], $uinfo[2]);
53
54 # Execute cron command and display output..
55 print &text('exec_cmd', "<tt>$lines[0]</tt>"),"<p>\n";
56 print "<pre>";
57 $got = &foreign_call("proc", "safe_process_exec",
58                      $lines[0], 0, 0, STDOUT, $input, 1);
59 print "<i>$text{'exec_none'}</i>\n" if (!$got);
60 print "</pre>\n";
61
62 print "<hr>\n";
63 &footer("edit_cron.cgi?idx=$in{'idx'}", $text{'edit_return'},
64         "", $text{'index_return'});
65