forked from virtualmin/virtualmin-gpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-scripts.pl
executable file
·262 lines (238 loc) · 6.01 KB
/
check-scripts.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/local/bin/perl
=head1 check-scripts.pl
Verify scripts available in Virtualmin
Makes sure all scripts installers have valid files, and that the latest
version is available in Virtualmin. This is for internal use, and shouldn't
be called.
=cut
package virtual_server;
if (!$module_name) {
$main::no_acl_check++;
$ENV{'WEBMIN_CONFIG'} ||= "/etc/webmin";
$ENV{'WEBMIN_VAR'} ||= "/var/webmin";
if ($0 =~ /^(.*)\/[^\/]+$/) {
chdir($pwd = $1);
}
else {
chop($pwd = `pwd`);
}
$0 = "$pwd/check-scripts.pl";
require './virtual-server-lib.pl';
$< == 0 || die "check-scripts.pl must be run as root";
}
# Parse command-line args
while(@ARGV) {
$a = shift(@ARGV);
if ($a eq "--debug") {
$debug = 1;
}
elsif ($a eq "--multiline") {
$multiline = 1;
}
elsif ($a eq "--help") {
&usage();
}
elsif ($a !~ /^\-/) {
push(@scripts, $a);
}
else {
&usage("Unknown parameter $a");
}
}
if (!@scripts) {
@scripts = &list_scripts();
}
foreach $s (@scripts) {
$script = &get_script($s);
next if ($script->{'nocheck'});
# Make sure all of the versions are available
foreach $v (@{$script->{'versions'}}) {
print "Checking $s $v ..\n";
@files = &{$script->{'files_func'}}(undef, $v, undef, undef);
foreach $f (@files) {
# Try a download
if ($f->{'nocheck'}) {
print ".. checking disabled\n";
next;
}
($url, $def) = &convert_osdn_url($f->{'url'});
if ($def == 1) {
# Couldn't find OSDN file
print ".. no such file\n";
push(@errs, [ $script, $v, $url, "No such file" ]);
next;
}
if ($url =~ /^ftp:\/\/([^\/]+)(\/.*)$/) {
# Check FTP file
print "Trying $url ..\n";
$size = undef;
for($i=0; $i<10; $i++) {
$size = &ftp_size($1, $2);
last if ($size);
sleep($i*5);
}
if (!$size) {
print ".. FTP file not found\n";
push(@errs, [ $script, $v, $url, "FTP file not found" ]);
}
else {
print ".. OK\n";
}
}
else {
# Do HTTP download
print "Trying $url ..\n";
($host, $port, $page, $ssl) = &parse_http_url($url);
$h = &make_http_connection(
$host, $port, $ssl,
$f->{'method'} || "HEAD", $page,
[ [ 'Host', $host ],
[ 'User-agent', 'Webmin' ],
[ 'Accept-language', 'en' ] ]);
if (!ref($h)) {
print ".. failed : $h\n";
push(@errs, [ $script, $v, $url, $h ]);
next;
}
# Make sure the file exists
$line = &read_http_connection($h);
$line =~ s/\r|\n//g;
if ($line !~ /^HTTP\/1\..\s+(200|30[0-9])\s+/) {
print ".. HTTP error : $line\n";
push(@errs, [ $script, $v, $url, $line ]);
}
else {
print ".. OK\n";
}
&close_http_connection($h);
}
}
}
# Make sure Virtualmin has the latest version
$lfunc = $script->{'latest_func'};
$url = undef;
if (defined(&$lfunc)) {
foreach $v (@{$script->{'versions'}}) {
($url, $re, $prefix, $suffix) = &$lfunc($v);
next if (!$url || !$re);
print "Checking $script->{'name'} website for $v ..\n";
($host, $port, $page, $ssl) = &parse_http_url($url);
$data = $err = undef;
&http_download($host, $port, $page, \$data, \$err,
undef, $ssl, undef, undef, undef, 0, 1);
if ($err || !$data) {
push(@errs, [ $script, $v, $url,
"Failed to find latest version" ]);
print ".. Download failed : $err\n";
next;
}
# Extract all the versions
local @vers;
if (ref($re)) {
# By callback func on data
@vers = &$re($data, $v);
}
else {
# Using regexp
while($data =~ /$re(.*)/is) {
push(@vers, $prefix.$1.$suffix);
$data = $2;
}
}
if (@vers) {
@vers = sort { &compare_versions($b, $a, $script) }
&unique(@vers);
$lver = $vers[0];
if (&compare_versions($lver, $v, $script) > 0) {
push(@errs, [ $script, $v, $url,
"Version $lver is available" ]);
print ".. found newer version $lver\n";
}
elsif (&compare_versions($lver, $v, $script) < 0) {
push(@errs, [ $script, $v, $url,
"Version $lver is older than $v" ]);
print ".. found older version $lver\n";
}
else {
print ".. OK\n";
}
}
else {
push(@errs, [ $script, $v, $url,
"Failed to find version number" ]);
print ".. Failed to find version\n";
}
}
}
# Check for versions by querying osdn
$clfunc = $script->{'check_latest_func'};
if (defined(&$clfunc)) {
foreach $v (@{$script->{'versions'}}) {
print "Checking $script->{'name'} versions for $v ..\n";
$lver = &$clfunc($v);
if ($lver) {
push(@errs, [ $script, $v, $url,
"Latest version is $lver" ]);
print ".. found newer version : $lver\n";
}
else {
print ".. OK\n";
}
}
}
}
# Send off any errors via email
if (@errs) {
&foreign_require("mailboxes");
$body = "The following errors were detected downloading script files:\n";
foreach $e (@errs) {
$body .= "\n";
$body .= "Script: $e->[0]->{'name'}\n";
$body .= "Version: $e->[1]\n";
$body .= "URL: $e->[2]\n" if ($e->[2]);
$body .= "Error: $e->[3]\n";
}
if ($debug) {
print STDERR $body;
exit(1);
}
else {
&mailboxes::send_text_mail(&get_global_from_address(),
"jcameron\@webmin.com",
undef,
"Virtualmin script errors",
$body);
}
}
sub ftp_size
{
local ($host, $file) = @_;
# connect to host and login
local $error;
&open_socket($host, 21, "SOCK", \$error) || return 0;
alarm(0);
if ($download_timed_out) {
return 0;
}
&ftp_command("", 2, \$error) || return 0;
# Login as anonymous
local @urv = &ftp_command("USER anonymous", [ 2, 3 ], \$error);
@urv || return 0;
if (int($urv[1]/100) == 3) {
&ftp_command("PASS root\@".&get_system_hostname(), 2,
\$error) || return 0;
}
# get the file size and tell the callback
&ftp_command("TYPE I", 2, \$error) || return 0;
local $size = &ftp_command("SIZE $file", 2, \$error);
&ftp_command("QUIT", 2, \$error) || return 0;
return $size;
}
sub usage
{
print "$_[0]\n\n" if ($_[0]);
print "Verifies that Virtualmin scripts are available\n";
print "\n";
print "virtualmin check-scripts [--debug] [scriptname]*\n";
exit(1);
}