Handle hostnames with upper-case letters
[webmin.git] / ajaxterm / index.cgi
1 #!/usr/local/bin/perl
2 # Start the Ajaxterm webserver on a random port, then print an iframe for
3 # a URL that proxies to it
4
5 BEGIN { push(@INC, ".."); };
6 use WebminCore;
7 use Socket;
8 &init_config();
9
10 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);
11
12 # Check for python
13 $python = &has_command("python");
14 if (!$python) {
15         &ui_print_endpage(&text('index_epython', "<tt>python</tt>"));
16         }
17
18 # Pick a free port
19 &get_miniserv_config(\%miniserv);
20 $port = $miniserv{'port'} + 1;
21 $proto = getprotobyname('tcp');
22 socket(TEST, PF_INET, SOCK_STREAM, $proto) ||
23         &error("Socket failed : $!");
24 setsockopt(TEST, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
25 while(1) {
26         last if (bind(TEST, sockaddr_in($port, INADDR_ANY)));
27         $port++;
28         }
29 close(TEST);
30
31 # Run the Ajaxterm webserver
32 $pid = fork();
33 if (!$pid) {
34         chdir("$module_root_directory/ajaxterm");
35         $logfile = $ENV{'WEBMIN_VAR'}.'/ajaxterm.log';
36         untie(*STDIN); open(STDIN, "</dev/null");
37         untie(*STDOUT); open(STDOUT, ">$logfile");
38         untie(*STDERR); open(STDERR, ">$logfile");
39         $shell = &has_command("bash") ||
40                  &has_command("sh") || "/bin/sh";
41         @uinfo = getpwnam("root");
42         $home = $uinfo[7] || "/";
43         $shell = "$shell -c ".quotemeta("cd '$home' ; exec $shell");
44         exec($python, "ajaxterm.py", "--port", $port, "--log",
45              $config{'autologin'} ? ("--command", $shell) : ( ));
46         exit(1);
47         }
48
49 # Wait for it to come up
50 $try = 0;
51 while(1) {
52         my $err;
53         &open_socket("localhost", $port, TEST2, \$err);
54         last if (!$err);
55         $try++;
56         if ($try > 30) {
57                 &error(&text('index_estart', 30, $port));
58                 }
59         sleep(1);
60         }
61 close(TEST2);
62
63 # Show the iframe
64 print "<center>\n";
65 print "<iframe src=$gconfig{'webprefix'}/$module_name/proxy.cgi/$port/ ",
66       "width=700 height=500 frameborder=0></iframe><br>\n";
67 print "<input type=button onClick='window.open(\"proxy.cgi/$port/\", \"ajaxterm\", \"toolbar=no,menubar=no,scrollbars=no,resizable=yes,width=700,height=500\")' value='$text{'index_popup'}'><p>\n";
68 print &text('index_credits', 'http://antony.lesuisse.org/software/ajaxterm/'),
69       "<p>\n";
70 print "</center>\n";
71
72 # Fork process that checks for inactivity
73 if (!fork()) {
74         untie(*STDIN); close(STDIN);
75         untie(*STDOUT); close(STDOUT);
76         untie(*STDERR); close(STDERR);
77         $statfile = "$ENV{'WEBMIN_VAR'}/ajaxterm/$port";
78         while(1) {
79                 my @st = stat($statfile);
80                 if (@st && time() - $st[9] > $config{'timeout'}) {
81                         # No activity
82                         last;
83                         }
84                 if (!kill(0, $pid)) {
85                         # Dead
86                         last;
87                         }
88                 sleep(10);
89                 }
90         unlink($statfile);
91         kill('KILL', $pid);
92         exit(0);
93         }
94
95 &ui_print_footer("/", $text{'index'});
96