]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/perl | |
2 | # | |
3 | # This code is distributed under the terms of the GPL | |
4 | # | |
5 | # (c) 2006 marco.s | |
6 | # | |
7 | # $Id: checkup,v 1.0 2006/08/30 00:00:00 marco.s Exp $ | |
8 | # | |
9 | ||
10 | use strict; | |
11 | ||
12 | use IO::Socket; | |
13 | use HTTP::Date; | |
14 | ||
15 | my $swroot='/var/ipfire'; | |
16 | my $scriptpath=substr($0,0,rindex($0,"/")); | |
17 | my $apphome="/var/ipfire/updatexlrator"; | |
18 | my $logfile="/var/log/updatexlrator/checkup.log"; | |
19 | my $debug=(-e "$apphome/debug"); | |
20 | my $repository='/srv/web/ipfire/html/updatecache'; | |
21 | my %xlratorsettings=(); | |
22 | my $download=0; | |
23 | my $updatefile=''; | |
24 | my $sourceurl=''; | |
25 | my $remote_size=0; | |
26 | my $local_size=0; | |
27 | my $remote_mtime=0; | |
28 | my $local_mtime=0; | |
29 | my @updatelist=(); | |
30 | my @metadata=(); | |
31 | ||
32 | @updatelist = <$repository/*>; | |
33 | ||
34 | my $sfUnknown = "0"; | |
35 | my $sfOk = "1"; | |
36 | my $sfOutdated = "2"; | |
37 | ||
38 | if (-e "$swroot/updatexlrator/settings") | |
39 | { | |
40 | &readhash("$swroot/updatexlrator/settings", \%xlratorsettings); | |
41 | if ($xlratorsettings{'FULL_AUTOSYNC'} eq 'on') { $download=1; }; | |
42 | } | |
43 | ||
44 | foreach (@updatelist) | |
45 | { | |
46 | if (!-d $_) | |
47 | { | |
48 | $updatefile = substr($_,rindex($_,"/")+1); | |
49 | if (-e "$repository/metadata/$updatefile") | |
50 | { | |
51 | open (FILE,"$repository/metadata/$updatefile"); | |
52 | @metadata = <FILE>; | |
53 | close FILE; | |
54 | chomp(@metadata); | |
55 | $sourceurl = $metadata[0]; | |
56 | ||
57 | $remote_size = &getdownloadsize($sourceurl); | |
58 | $local_size = (-s "$repository/$updatefile"); | |
59 | ||
60 | $remote_mtime = &getlastmod($sourceurl); | |
61 | $local_mtime = &getmtime("$repository/$updatefile"); | |
62 | ||
63 | if ($remote_mtime eq 0) | |
64 | { | |
65 | $metadata[2] = $sfUnknown; | |
66 | if ($debug) { &writelog("$updatefile - WARNING: Source not found"); } | |
67 | print "$updatefile - WARNING: Source not found\n"; | |
68 | } | |
69 | elsif (($local_mtime eq $remote_mtime) && ($local_size == $remote_size)) | |
70 | { | |
71 | $metadata[2] = $sfOk; | |
72 | $metadata[3] = time; | |
73 | if ($debug) { &writelog("$updatefile"); } | |
74 | print "$updatefile\n"; | |
75 | } | |
76 | else | |
77 | { | |
78 | $metadata[2] = $sfOutdated; | |
79 | $metadata[3] = time; | |
80 | if ($debug) { &writelog("$updatefile - WARNING: Out of date"); } | |
81 | print "$updatefile - WARNING: Out of date\n"; | |
82 | if ($download) | |
83 | { | |
84 | if ($debug) | |
85 | { | |
86 | 1 while $remote_size =~ s/^(-?\d+)(\d{3})/$1.$2/; | |
87 | print "Please wait, retrieving file ($remote_size Byte) from source ..."; | |
88 | `$scriptpath/../bin/wget -nd -nv -O $repository/$updatefile $sourceurl >>$logfile 2>&1`; | |
89 | print "\n"; | |
90 | } else | |
91 | { | |
92 | `$scriptpath/../bin/wget -nd -nv -O $repository/$updatefile $sourceurl 2>&1`; | |
93 | } | |
94 | $local_mtime = &getmtime("$repository/$updatefile"); | |
95 | if ($local_mtime eq $remote_mtime) { $metadata[2] = $sfOk; } | |
96 | } | |
97 | } | |
98 | open (FILE,">$repository/metadata/$updatefile"); | |
99 | foreach (@metadata) { print FILE "$_\n"; } | |
100 | close FILE; | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | # ------------------------------------------------------------------- | |
106 | ||
107 | sub readhash | |
108 | { | |
109 | my $filename = $_[0]; | |
110 | my $hash = $_[1]; | |
111 | my ($var, $val); | |
112 | ||
113 | if (-e $filename) | |
114 | { | |
115 | open(FILE, $filename) or die "Unable to read file $filename"; | |
116 | while (<FILE>) | |
117 | { | |
118 | chop; | |
119 | ($var, $val) = split /=/, $_, 2; | |
120 | if ($var) | |
121 | { | |
122 | $val =~ s/^\'//g; | |
123 | $val =~ s/\'$//g; | |
124 | ||
125 | # Untaint variables read from hash | |
126 | $var =~ /([A-Za-z0-9_-]*)/; $var = $1; | |
127 | $val =~ /([\w\W]*)/; $val = $1; | |
128 | $hash->{$var} = $val; | |
129 | } | |
130 | } | |
131 | close FILE; | |
132 | } | |
133 | } | |
134 | ||
135 | # ------------------------------------------------------------------- | |
136 | ||
137 | sub getmtime | |
138 | { | |
139 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_[0]); | |
140 | ||
141 | return $mtime; | |
142 | } | |
143 | ||
144 | # ------------------------------------------------------------------- | |
145 | ||
146 | sub getlastmod | |
147 | { | |
148 | my $remote=0; | |
149 | my @response=(); | |
150 | my $lastmoddate=0; | |
151 | ||
152 | my $url = $_[0]; | |
153 | ||
154 | $url =~ s@^(.*)://([^/]*)@@; | |
155 | ||
156 | my $proto = $1; | |
157 | my $fqhn = $2; | |
158 | ||
159 | if ((-e "$swroot/red/active") && ($proto eq 'http')) | |
160 | { | |
161 | $remote = IO::Socket::INET->new( | |
162 | PeerHost => $fqhn, | |
163 | PeerPort => 'http(80)', | |
164 | Timeout => 1 | |
165 | ); | |
166 | } | |
167 | ||
168 | if ($remote) | |
169 | { | |
170 | print $remote "HEAD $url HTTP/1.0\n"; | |
171 | print $remote "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n"; | |
172 | print $remote "Host: $fqhn\n"; | |
173 | print $remote "Accept: */*\n\n"; | |
174 | while (<$remote>) { push(@response,$_); } | |
175 | close $remote; | |
176 | if ($response[0] =~ /^HTTP\/\d+\.\d+\s\d+\sOK\s*$/) | |
177 | { | |
178 | foreach (@response) | |
179 | { | |
180 | if (/^Last-Modified: /i) | |
181 | { | |
182 | s/^Last-Modified: //i; | |
183 | $lastmoddate=HTTP::Date::str2time($_); | |
184 | } | |
185 | } | |
186 | } | |
187 | } | |
188 | return $lastmoddate; | |
189 | } | |
190 | ||
191 | # ------------------------------------------------------------------- | |
192 | ||
193 | sub getdownloadsize | |
194 | { | |
195 | my $remote=0; | |
196 | my @response=(); | |
197 | my $contentlength=0; | |
198 | ||
199 | my $url = $_[0]; | |
200 | ||
201 | $url =~ s@^(.*)://([^/]*)@@; | |
202 | ||
203 | my $proto = $1; | |
204 | my $fqhn = $2; | |
205 | ||
206 | if ((-e "$swroot/red/active") && ($proto eq 'http')) | |
207 | { | |
208 | $remote = IO::Socket::INET->new( | |
209 | PeerHost => $fqhn, | |
210 | PeerPort => 'http(80)', | |
211 | Timeout => 1 | |
212 | ); | |
213 | } | |
214 | ||
215 | if ($remote) | |
216 | { | |
217 | print $remote "HEAD $url HTTP/1.0\n"; | |
218 | print $remote "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n"; | |
219 | print $remote "Host: $fqhn\n"; | |
220 | print $remote "Accept: */*\n\n"; | |
221 | while (<$remote>) { push(@response,$_); } | |
222 | close $remote; | |
223 | if ($response[0] =~ /^HTTP\/\d+\.\d+\s\d+\sOK\s*$/) | |
224 | { | |
225 | foreach (@response) | |
226 | { | |
227 | if (/^Content-Length: /i) | |
228 | { | |
229 | s/^Content-Length: //i; | |
230 | $contentlength=int($_); | |
231 | } | |
232 | } | |
233 | } | |
234 | } | |
235 | return $contentlength; | |
236 | } | |
237 | ||
238 | # ------------------------------------------------------------------- | |
239 | ||
240 | sub writelog | |
241 | { | |
242 | open (LOGFILE,">>$logfile"); | |
243 | my @now = localtime(time); | |
244 | printf LOGFILE "%02d:%02d:%02d %s\n",$now[2],$now[1],$now[0],$_[0]; | |
245 | close LOGFILE; | |
246 | } | |
247 | ||
248 | # ------------------------------------------------------------------- |