]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/updxlrator/checkup
35c4953edc8a83a46f1dada8fd67f409a990d585
[people/pmueller/ipfire-2.x.git] / config / updxlrator / checkup
1 #!/usr/bin/perl
2 #
3 # This code is distributed under the terms of the GPL
4 #
5 # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
6 #
7 # $Id: checkup,v 2.0 2007/06/17 00:00:00 marco.s Exp $
8 #
9
10 use strict;
11
12 use HTTP::Date;
13
14 my $swroot='/var/ipfire';
15 my $apphome="/var/ipfire/updatexlrator";
16 my $logfile="/var/log/updatexlrator/checkup.log";
17 my $repository='/var/updatecache';
18 my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
19 my %proxysettings=();
20 my %xlratorsettings=();
21 my $download=0;
22 my $updatefile='';
23 my $sourceurl='';
24 my @sources=();
25 my @updatelist=();
26 my $logging=0;
27
28 my $sfUnknown = "0";
29 my $sfOk = "1";
30 my $sfOutdated = "2";
31 my $sfNoSource = "3";
32
33 if (-e "$swroot/updatexlrator/settings")
34 {
35 &readhash("$swroot/updatexlrator/settings", \%xlratorsettings);
36 if ($xlratorsettings{'FULL_AUTOSYNC'} eq 'on') { $download=1; };
37 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
38 }
39
40 if (-e "$swroot/proxy/settings") { &readhash("$swroot/proxy/settings", \%proxysettings); }
41
42 if (-e "$swroot/proxy/advanced/settings")
43 {
44 %proxysettings=();
45 &readhash("$swroot/proxy/advanced/settings", \%proxysettings);
46 }
47
48 foreach (<$repository/*>)
49 {
50 if (-d $_)
51 {
52 unless (/^$repository\/download$/) { push(@sources,$_); }
53 }
54 }
55
56 foreach (@sources)
57 {
58 @updatelist=<$_/*>;
59 foreach(@updatelist)
60 {
61 if (-e "$_/source.url")
62 {
63 open (FILE,"$_/source.url");
64 $sourceurl=<FILE>;
65 close FILE;
66 chomp($sourceurl);
67 $updatefile = substr($sourceurl,rindex($sourceurl,'/')+1,length($sourceurl));
68 &checksource($_);
69 }
70 }
71 }
72
73 # -------------------------------------------------------------------
74
75 sub readhash
76 {
77 my $filename = $_[0];
78 my $hash = $_[1];
79 my ($var, $val);
80
81 if (-e $filename)
82 {
83 open(FILE, $filename) or die "Unable to read file $filename";
84 while (<FILE>)
85 {
86 chop;
87 ($var, $val) = split /=/, $_, 2;
88 if ($var)
89 {
90 $val =~ s/^\'//g;
91 $val =~ s/\'$//g;
92
93 # Untaint variables read from hash
94 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
95 $val =~ /([\w\W]*)/; $val = $1;
96 $hash->{$var} = $val;
97 }
98 }
99 close FILE;
100 }
101 }
102
103 # -------------------------------------------------------------------
104
105 sub getmtime
106 {
107 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_[0]);
108
109 return $mtime;
110 }
111
112 # -------------------------------------------------------------------
113
114 sub writelog
115 {
116 print "$_[0]\n";
117 if ($logging)
118 {
119 open (LOGFILE,">>$logfile");
120 my @now = localtime(time);
121 printf LOGFILE "%04d-%02d-%02d %02d:%02d:%02d %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$_[0];
122 close LOGFILE;
123 }
124 }
125
126 # -------------------------------------------------------------------
127
128 sub setcachestatus
129 {
130 open (FILE,">$_[0]");
131 print FILE "$_[1]\n";
132 close FILE;
133 }
134
135 # -------------------------------------------------------------------
136
137 sub checksource
138 {
139 my @http_header=();
140 my $http_result='000 n/a';
141 my $returncode=0;
142 my $localfile='';
143 my $remote_size=0;
144 my $remote_mtime=0;
145 my $login='';
146 my $url='';
147 my $cdir=$_[0];
148
149 open (FILE,"$cdir/source.url");
150 $url=<FILE>;
151 close FILE;
152 chomp($url);
153
154 $localfile = $cdir . substr($url,rindex($url,'/'),length($url));
155
156 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
157 {
158 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
159 if ($proxysettings{'UPSTREAM_PASSWORD'})
160 {
161 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
162 }
163 }
164
165 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
166 @http_header = `wget $login --user-agent="$useragent" --spider -S $url 2>&1`;
167 $ENV{'http_proxy'} = '';
168 &writelog(@http_header);
169
170 foreach (@http_header)
171 {
172 chomp;
173 if (/^\s*HTTP\/\d+\.\d+\s\d+\s+\w+/) { $http_result = $_; $http_result =~ s/^\s*HTTP\/\d+\.\d+\s+//; }
174 if (/^\s*Content-Length:\s/) { $remote_size = $_; $remote_size =~ s/[^0-9]//g; }
175 if (/^\s*Last-Modified:\s/) { $remote_mtime = $_; $remote_mtime =~ s/^\s*Last-Modified:\s//; $remote_mtime = HTTP::Date::str2time($remote_mtime) }
176 }
177
178 &writelog($localfile);
179 &writelog("HTTP result: $http_result");
180 &writelog("Source size: $remote_size");
181 &writelog("Cached size: " . (-s $localfile));
182 &writelog("Source time: $remote_mtime");
183 &writelog("Cached time: " . getmtime($localfile));
184
185 if ($http_result =~ /\d+\s+OK$/)
186 {
187 if (($remote_size == -s $localfile) && ($remote_mtime == getmtime($localfile)))
188 {
189 &writelog("Status: Ok");
190 &setcachestatus("$cdir/status",$sfOk);
191 } else {
192 &writelog("Status: Outdated");
193 &setcachestatus("$cdir/status",$sfOutdated);
194 if ($download)
195 {
196 &writelog("Retrieving file from source: $remote_size bytes");
197 $_ = system("wget $login --user-agent=\"$useragent\" -q -O $localfile $url");
198 &writelog("Download finished with code: $_");
199 if ($_ == 0) { &setcachestatus("$cdir/status",$sfOk); }
200 }
201 }
202 } else {
203 $_ = $http_result;
204 s/\D+//;
205 if ($_ eq '404')
206 {
207 &writelog("Status: No source");
208 &setcachestatus("$cdir/status",$sfNoSource);
209 } else {
210 &writelog("Status: Error");
211 &setcachestatus("$cdir/status",$sfUnknown);
212 }
213 }
214
215 &setcachestatus("$cdir/checkup.log",time);
216 }
217
218 # -------------------------------------------------------------------