Handle hostnames with upper-case letters
[webmin.git] / vnc / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # Display the vnc applet
4
5 BEGIN { push(@INC, ".."); };
6 use WebminCore;
7 use Socket;
8 &init_config();
9
10 &ui_print_header(undef, $text{'index_title'}, "", undef, &get_product_name() eq 'webmin', 1);
11
12 if ($config{'program'}) {
13         # Check if Xvnc is installed
14         if (!&has_command("Xvnc")) {
15                 &error_exit(&text('index_ecmd', "<tt>Xvnc</tt>"));
16                 }
17
18         # Pick a free VNC number
19         for($num=1; $num<1000; $num++) {
20                 $port = 5900+$num;
21                 last;
22                 # XXX need to test
23                 }
24
25         # Generate a password using vncpasswd
26         # XXX
27
28         # Start Xvnc in a background process, and kill it after one client
29         if (!fork()) {
30                 close(STDIN);
31                 close(STDOUT);
32                 local $pid = open(VNC, "Xvnc :$num 2>&1 |");
33                 while(<VNC>) {
34                         if (/Client\s+(\S+)\s+gone/i) {
35                                 kill(TERM, $pid);
36                                 last;
37                                 }
38                         }
39                 close(VNC);
40                 exit;
41                 }
42
43         # Run the specified program, using the selected display
44         $ENV{'DISPLAY'} = "localhost:$num";
45         system("$config{'program'} >/dev/null 2>&1 </dev/null &");
46
47         # XXX what about security?
48         # XXX how to ensure exit?       -rfbwait
49         # XXX what about window manager? or none?
50         # XXX what user to run program as? need option for current user
51         # XXX need to generate random password, and pass to java
52         }
53 else {
54         $addr = $config{'host'} ? $config{'host'} :
55                 $ENV{'SERVER_NAME'} ? $ENV{'SERVER_NAME'} :
56                                       &to_ipaddress(&get_system_hostname());
57         $SIG{ALRM} = "connect_timeout";
58         alarm(10);
59         &open_socket($addr, $port, STEST, \$err);
60         close(STEST);
61         $err && &error_exit(&text('index_esocket', $addr, $config{'port'}));
62         $port = $config{'port'};
63         }
64
65 if ($ENV{'HTTPS'} eq 'ON') {
66         print "<center><font color=#ff0000>$text{'index_warn'}",
67               "</font></center><br>\n";
68         }
69
70 print "<center><applet archive=vncviewer.jar code=vncviewer.class ",
71       "width=$config{'width'} height=$config{'height'}>\n";
72 print "<param name=port value='$port'>\n";
73 if ($config{'host'}) {
74         print "<param name=host value='$config{'host'}'>\n";
75         }
76 print "$text{'index_nojava'} <p>\n";
77 print "</applet><br>\n";
78 print "$text{'index_credits'}</center>\n";
79
80 &ui_print_footer("/", $text{'index'});
81
82 sub connect_timeout
83 {
84 }
85
86 sub error_exit
87 {
88 print "<p>",@_,"<p>\n";
89 &ui_print_footer("/", $text{'index'});
90 exit;
91 }
92