Handle hostnames with upper-case letters
[webmin.git] / status / ftp-monitor.pl
1 # ftp-monitor.pl
2 # Monitor a remote FTP server by doing a test download
3
4 sub get_ftp_status
5 {
6 local $up = 0;
7 local $st = time();
8 local $error;
9 local $temp = &transname();
10 eval {
11         local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
12         alarm($_[0]->{'alarm'} ? $_[0]->{'alarm'} : 10);
13
14         if ($_[0]->{'tls'}) {
15                 # Connect using TLS-capable perl module
16                 eval "use Net::FTPSSL";
17                 my $ftps;
18                 eval { $ftps = Net::FTPSSL->new($_[0]->{'host'},
19                                             Port => $_[0]->{'port'} || 21) };
20                 if ($@) {
21                         $error = &text('ftp_econn2', &html_escape($@));
22                         return 0;
23                         }
24                 elsif (!$ftps) {
25                         $error = $text{'ftp_econn'};
26                         return 0;
27                         }
28
29                 # Login with username and password, or anonymous
30                 my $ok;
31                 if ($_[0]->{'user'}) {
32                         $ok = $ftps->login($_[0]->{'user'}, $_[0]->{'pass'});
33                         }
34                 else {
35                         $ok = $ftps->login("anonymous",
36                                      "root\@".&get_system_hostname());
37                         }
38                 if (!$ok) {
39                         $error = &text('ftp_elogin', $ftps->last_message);
40                         return 0;
41                         }
42
43                 # Get the file
44                 if ($_[0]->{'file'}) {
45                         $ok = $ftps->get($_[0]->{'file'}, $temp);
46                         if (!$ok) {
47                                 $error = &text('ftp_etlsfile',
48                                                $ftps->last_message);
49                                 return 0;
50                                 }
51                         }
52                 $ftps->quit();
53                 }
54         else {
55                 # Use Webmin's built in FTP code
56                 &ftp_download($_[0]->{'host'}, $_[0]->{'file'}, $temp, \$error,
57                               undef, $_[0]->{'user'}, $_[0]->{'pass'},
58                               $_[0]->{'port'});
59                 }
60         alarm(0);
61         $up = $error ? 0 : 1;
62         };
63
64 if ($@) {
65         die unless $@ eq "alarm\n";   # propagate unexpected errors
66         return { 'up' => 0 };
67         }
68 else { 
69         return { 'up' => $up, 'time' => time() - $st,
70                  'desc' => $up ? undef : $error };
71         }
72 }
73
74 sub show_ftp_dialog
75 {
76 print &ui_table_row($text{'ftp_host'},
77         &ui_textbox("host", $_[0]->{'host'}, 30));
78
79 print &ui_table_row($text{'ftp_port'},
80         &ui_textbox("port", $_[0]->{'port'} || 21, 5));
81
82 print &ui_table_row($text{'ftp_user'},
83         &ui_opt_textbox("ftpuser", $_[0]->{'user'}, 15, $text{'ftp_anon'}));
84
85 print &ui_table_row($text{'ftp_pass'},
86         &ui_password("ftppass", $_[0]->{'pass'}, 15));
87
88 print &ui_table_row($text{'ftp_file'},
89         &ui_opt_textbox("file", $_[0]->{'file'}, 50, $text{'ftp_none'}), 3);
90
91 print &ui_table_row($text{'http_alarm'},
92         &ui_opt_textbox("alarm", $_[0]->{'alarm'}, 5, $text{'default'}));
93
94 print &ui_table_row($text{'ftp_tls'},
95         &ui_yesno_radio("tls", $_[0]->{'tls'}));
96 }
97
98 sub parse_ftp_dialog
99 {
100 $in{'host'} =~ /^[a-z0-9\.\-\_]+$/i || &error($text{'ftp_ehost'});
101 $_[0]->{'host'} = $in{'host'};
102
103 $in{'port'} =~ /^\d+$/i || &error($text{'ftp_eport'});
104 $_[0]->{'port'} = $in{'port'};
105
106 $in{'ftpuser_def'} || $in{'ftpuser'} =~ /^\S+$/ || &error($text{'ftp_euser'});
107 $_[0]->{'user'} = $in{'ftpuser_def'} ? undef : $in{'ftpuser'};
108 $_[0]->{'pass'} = $in{'ftppass'};
109
110 $in{'file_def'} || $in{'file'} =~ /^\S+$/ || &error($text{'ftp_efile'});
111 $_[0]->{'file'} = $in{'file_def'} ? undef : $in{'file'};
112
113 if ($in{'alarm_def'}) {
114         delete($_[0]->{'alarm'});
115         }
116 else {
117         $in{'alarm'} =~ /^\d+$/ || &error($text{'http_ealarm'});
118         $_[0]->{'alarm'} = $in{'alarm'};
119         }
120
121 $_[0]->{'tls'} = $in{'tls'};
122 if ($in{'tls'}) {
123         eval "use Net::FTPSSL";
124         if ($@) {
125                 &error(&text('ftp_etls', '<tt>Net::FTPSSL</tt>'));
126                 }
127         }
128 }
129