Handle hostnames with upper-case letters
[webmin.git] / shell / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # Show the shell user interface
4
5 $unsafe_index_cgi = 1;
6 require './shell-lib.pl';
7 %access = &get_module_acl();
8 &ReadParseMime() if ($ENV{'REQUEST_METHOD'} ne 'GET');
9 &ui_print_header(undef, $text{'index_title'}, "", undef,
10                  $module_info{'usermin'} ? 0 : 1, 1,
11                  undef, undef, undef,
12         "onLoad='window.scroll(0, 10000); document.forms[0].cmd.focus()'");
13
14 $prevfile = "$module_config_directory/previous.$remote_user";
15 if ($in{'clearcmds'}) {
16         &lock_file($prevfile);
17         unlink($prevfile);
18         &unlock_file($prevfile);
19         &webmin_log("clear");
20         }
21 else {
22         open(PREVFILE, $prevfile);
23         chop(@previous = <PREVFILE>);
24         close(PREVFILE);
25         }
26 $cmd = $in{'doprev'} ? $in{'pcmd'} : $in{'cmd'};
27
28 if ($in{'pwd'}) {
29         $pwd = $in{'pwd'};
30         }
31 else {
32         if ($gconfig{'os_type'} eq 'windows') {
33                 # Initial directory is c:/
34                 $pwd = "c:/";
35                 }
36         else {
37                 # Initial directory is user's home
38                 local @uinfo = getpwnam($access{'user'} || $remote_user);
39                 $pwd = scalar(@uinfo) && -d $uinfo[7] ? $uinfo[7] : "/";
40                 }
41         }
42 if (!$in{'clear'}) {
43         $history = &un_urlize($in{'history'});
44         if ($cmd) {
45                 # Execute the latest command
46                 $fullcmd = $cmd;
47                 $ok = chdir($pwd);
48                 $history .= "<b>&gt; ".&html_escape($cmd, 1)."</b>\n";
49                 if ($cmd =~ /^cd\s+"([^"]+)"\s*(;?\s*(.*))$/ ||
50                     $cmd =~ /^cd\s+'([^']+)'\s*(;?\s*(.*))$/ ||
51                     $cmd =~ /^cd\s+([^; ]*)\s*(;?\s*(.*))$/) {
52                         $cmd = undef;
53                         if (!chdir($1)) {
54                                 $history .= &html_escape("$1: $!\n", 1);
55                                 }
56                         else {
57                                 $cmd = $3 if ($2);
58                                 $pwd = &get_current_dir();
59                                 }
60                         }
61                 if ($cmd) {
62                         local $user = $access{'user'} || $remote_user;
63                         &clean_environment() if ($config{'clear_envs'});
64                         delete($ENV{'SCRIPT_NAME'});    # So that called Webmin
65                                                         # programs get the right
66                                                         # module, not this one!
67                         if (&supports_users()) {
68                                 $out = &backquote_logged(
69                                     &command_as_user($user, 0, $cmd)." 2>&1");
70                                 }
71                         else {
72                                 $out = &backquote_logged("($cmd) 2>&1");
73                                 }
74                         &reset_environment() if ($config{'clear_envs'});
75                         $out = &html_escape($out, 1);
76                         $history .= $out;
77                         }
78                 @previous = &unique(@previous, $fullcmd);
79                 &lock_file($prevfile);
80                 &open_tempfile(PREVFILE, ">>$prevfile");
81                 &print_tempfile(PREVFILE, $fullcmd,"\n");
82                 &close_tempfile(PREVFILE);
83                 &unlock_file($prevfile);
84                 &webmin_log("run", undef, undef, { 'cmd' => $fullcmd });
85                 }
86         }
87
88 # Show the history and command input
89 if ($history) {
90         print &ui_table_start($text{'shell_history'}, "width=100%", 2);
91         print &ui_table_row(undef, "<pre>$history</pre>", 2);
92         print &ui_table_end();
93         print &ui_hr();
94         }
95
96 print "$text{'index_desc'}<br>\n";
97 print &ui_form_start("index.cgi", "form-data");
98
99 print "<table width=100%><tr>\n";
100
101 # Command to run
102 print "<td width=10%>",&ui_submit($text{'index_ok'}),"</td>\n";
103 print "<td>",&ui_textbox("cmd", undef, 50, 0, undef,
104                          "style='width:100%'"),"</td>\n";
105 print "<td align=right width=10%>",&ui_submit($text{'index_clear'}, "clear"),
106       "</td>\n";
107 print "</tr>\n";
108
109 print &ui_hidden("pwd", $pwd);
110 print &ui_hidden("history", &urlize($history));
111 foreach $p (@previous) {
112         print &ui_hidden("previous", $p);
113         }
114
115 # Previous command menu
116 if (@previous) {
117         print "<tr>\n";
118         print "<td width=10%>",&ui_submit($text{'index_pok'}, "doprev"),
119               "</td>\n";
120         print "<td>",&ui_select("pcmd", undef,
121                         [ reverse(@previous) ]);
122         print "<input type=button name=movecmd ",
123               "value='$text{'index_edit'}' ",
124               "onClick='cmd.value = pcmd.options[pcmd.selectedIndex].value'>\n";
125         print "</td>\n";
126         print "<td align=right width=10%>",
127               &ui_submit($text{'index_clearcmds'}, "clearcmds"),"</td>\n";
128         print "</tr>\n";
129         }
130 print "</table>\n";
131 print &ui_form_end();
132
133 &ui_print_footer("/", $text{'index'});
134