Handle hostnames with upper-case letters
[webmin.git] / wuftpd / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # Display the wuFTPd main menu
4
5 require './wuftpd-lib.pl';
6 use Socket;
7 $| = 1;
8
9 # Check if wuftpd is installed
10 if (!-x $config{'ftpd_path'}) {
11         &ui_print_header(undef, $text{'index_title'}, "", "intro", 1, 1, 0,
12                 &help_search_link("wu-ftpd", "man", "doc", "google"));
13         print &text('index_eftpd', "<tt>$config{'ftpd_path'}</tt>",
14                   "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
15         &ui_print_footer("/", $text{'index'});
16         exit;
17         }
18
19 # Check if the ftpaccess file exists
20 if (!-r $config{'ftpaccess'}) {
21         &ui_print_header(undef, $text{'index_title'}, "", "intro", 1, 1, 0,
22                 &help_search_link("wu-ftpd", "man", "doc", "google"));
23         print &text('index_eftpaccess', "<tt>$config{'ftpaccess'}</tt>",
24                   "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
25         &ui_print_footer("/", $text{'index'});
26         exit;
27         }
28
29 @st = stat($config{'ftpd_path'});
30 &read_file("$module_config_directory/ftpd", \%ftpd);
31 if ($ftpd{'size'} != $st[7] || $ftpd{'mtime'} != $st[9]) {
32         # Run the ftpd to check if it is really wuftpd, by starting it
33         # in a separate TCP server process
34         $proto = getprotobyname('tcp');
35         socket(MAIN, PF_INET, SOCK_STREAM, $proto) ||
36                 &error("socket failed : $!");
37         setsockopt(MAIN, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
38         $port = 10000;
39         while(1) {
40                 $port++;
41                 last if (bind(MAIN, pack_sockaddr_in($port, INADDR_ANY)));
42                 }
43         listen(MAIN, SOMAXCONN);
44         if (!($pid = fork())) {
45                 accept(SOCK, MAIN) || exit(1);
46                 untie(*STDIN);
47                 untie(*STDOUT);
48                 untie(*STDERR);
49                 open(STDIN, "<&SOCK");
50                 open(STDOUT, ">&SOCK");
51                 open(STDERR, ">&SOCK");
52                 exec("$config{'ftpd_path'} -A");
53                 print "Exec failed : $!\n";
54                 exit;
55                 }
56         close(MAIN);
57         &open_socket("localhost", $port, CONN);
58         select(CONN); $| = 1; select(STDOUT);
59         print CONN "quit\n";
60         local $out;
61         while(<CONN>) {
62                 $version = $1 if (/Version\s+wu-(\d+\.\d+)/i);
63                 $out .= $_;
64                 }
65         close(CONN);
66         waitpid($pid, 0);
67         if (!$version) {
68                 &ui_print_header(undef, $text{'index_title'}, "", "intro", 1, 1, 0,
69                         &help_search_link("wu-ftpd", "man", "doc", "google"));
70                 print &text('index_eversion',
71                           "<tt>$config{'ftpd_path'}</tt>",
72                           "$gconfig{'webprefix'}/config.cgi?$module_name",
73                           "<pre>$out</pre>"),"<p>\n";
74                 &ui_print_footer("/", $text{'index'});
75                 exit;
76                 }
77
78         # Save version information
79         $ftpd{'size'} = $st[7];
80         $ftpd{'mtime'} = $st[9];
81         $ftpd{'version'} = $version;
82         &write_file("$module_config_directory/ftpd", \%ftpd);
83         }
84
85 &ui_print_header(undef, $text{'index_title'}, "", "intro", 1, 1, 0,
86         &help_search_link("wu-ftpd", "man", "doc", "google"), undef, undef,
87         &text('index_version', $ftpd{'version'}));
88
89 # Display table of icons
90 @names = ( 'class', 'message', 'acl', 'net', 'log',
91            'alias', 'anon', 'perm', 'misc' );
92 @links = map { "edit_${_}.cgi" } @names;
93 @titles = map { $text{"${_}_title"} } @names;
94 @icons = map { "images/${_}.gif" } @names;
95 &icons_table(\@links, \@titles, \@icons, 5);
96
97 ($inet, $inet_mod) = &running_under_inetd();
98 if (!$inet) {
99         # Get the FTP server pid
100         $pid = &check_pid_file($config{'pid_file'});
101         }
102
103 if (!$inet && $pid) {
104         print &ui_hr();
105         print "<form action=restart.cgi>\n";
106         print "<input type=hidden name=pid value='$pid'>\n";
107         print "<table width=100%><tr>\n";
108         print "<td><input type=submit value=\"$text{'index_apply'}\"></td>\n";
109         print "<td>$text{'index_applymsg'}</td>\n";
110         print "</tr></table></form>\n";
111         }
112 elsif (!$inet && !$pid) {
113         print &ui_hr();
114         print "<form action=start.cgi>\n";
115         print "<table width=100%><tr>\n";
116         print "<td><input type=submit value=\"$text{'index_start'}\"></td>\n";
117         if ($inet_mod) {
118                 print "<td>",&text('index_startmsg', "/$inet_mod/"),"</td>\n";
119                 }
120         else {
121                 print "<td>$text{'index_startmsg2'}</td>\n";
122                 }
123         print "</tr></table></form>\n";
124         }
125
126 &ui_print_footer("/", $text{'index'});
127