GeƤndert:
[people/pmueller/ipfire-2.x.git] / config / urlfilter / autoupdate.pl
1 #!/usr/bin/perl
2
3 #
4 # $Id: autoupdate.pl,v 1.0 2005/06/15 00:00:00 marco Exp $
5 #
6 use strict;
7
8 my $make_clean = 1;
9
10 my $swroot = "/var/ipfire";
11 my $target = "$swroot/urlfilter/download";
12 my $dbdir = "$swroot/urlfilter/blacklists";
13
14 my $sourceurlfile = "$swroot/urlfilter/autoupdate/autoupdate.urls";
15 my $updconffile = "$swroot/urlfilter/autoupdate/autoupdate.conf";
16 my $updflagfile = "$swroot/urlfilter/blacklists/.autoupdate.last";
17
18 my %cgiparams;
19 my %updatesettings;
20 my $blacklist_url;
21 my $blacklist_src;
22 my $source_url;
23 my $source_name;
24 my @source_urllist;
25
26 my $blacklist;
27 my $category;
28
29 my $exitcode = 1;
30
31 if (-e "$sourceurlfile")
32 {
33 open(FILE, $sourceurlfile);
34 @source_urllist = <FILE>;
35 close(FILE);
36 }
37
38 if (-e "$updconffile") { &readhash("$updconffile", \%updatesettings); }
39
40 if (@ARGV[0] =~ m@^(f|h)tt?ps?://@) { $updatesettings{'UPDATE_SOURCE'} = @ARGV[0]; }
41
42 if ($updatesettings{'UPDATE_SOURCE'} eq 'custom')
43 {
44 $blacklist_url=$updatesettings{'CUSTOM_UPDATE_URL'};
45 } else {
46 $blacklist_url=$updatesettings{'UPDATE_SOURCE'};
47 foreach (@source_urllist)
48 {
49 chomp;
50 $source_name = substr($_,0,rindex($_,","));
51 $source_url = substr($_,index($_,",")+1);
52 if ($blacklist_url eq $source_url) { $blacklist_src=$source_name; }
53 }
54 }
55
56 if ($blacklist_src eq '') { $blacklist_src="custom source URL"; }
57
58 $blacklist_url =~ s/\&/\\\&/;
59
60 $blacklist=substr($blacklist_url,rindex($blacklist_url,"/")+1);
61 if (($blacklist =~ /\?/) || (!($blacklist =~ /\.t(ar\.)?gz$/))) { $blacklist = 'blacklist.tar.gz'; }
62 $blacklist=$target.'/'.$blacklist;
63
64 unless ($blacklist_url eq '')
65 {
66
67 if (-d $target) { system("rm -rf $target"); }
68 system("mkdir $target");
69
70 system("/usr/bin/wget -o $target/wget.log -O $blacklist $blacklist_url");
71
72 if (-e $blacklist)
73 {
74 system("/bin/tar --no-same-owner -xzf $blacklist -C $target");
75 if (-d "$target/blacklists")
76 {
77 open(FILE, ">$target/update.conf");
78 flock FILE, 2;
79 print FILE "logdir $target\n";
80 print FILE "dbhome $target/blacklists\n\n";
81
82 foreach (<$target/blacklists/*>)
83 {
84 if ((-d $_) && ((-s "$_/domains") || (-s "$_/urls")))
85 {
86 $category=substr($_,rindex($_,"/")+1);
87 print FILE "dest $category {\n";
88 if (-s "$_/domains") { print FILE " domainlist $category/domains\n"; }
89 if (-s "$_/urls") { print FILE " urllist $category/urls\n"; }
90 print FILE "}\n\n";
91 }
92 }
93 print FILE "acl {\n";
94 print FILE " default {\n";
95 print FILE " pass none\n";
96 print FILE " }\n";
97 print FILE "}\n";
98 close FILE;
99
100 system("/usr/sbin/squidGuard -d -c $target/update.conf -C all");
101
102 system("cp -r $target/blacklists/* $dbdir");
103
104 system("chown -R nobody.nobody $dbdir");
105
106 foreach $category (<$dbdir/*>)
107 {
108 if (-d $category)
109 {
110 system("chmod 755 $category &> /dev/null");
111 foreach $blacklist (<$category/*>)
112 {
113 if (-f $blacklist){ system("chmod 644 $blacklist &> /dev/null"); }
114 if (-d $blacklist){ system("chmod 755 $blacklist &> /dev/null"); }
115 }
116 system("chmod 666 $category/*.db &> /dev/null");
117 }
118 }
119
120 system("touch $updflagfile");
121 system("chown nobody.nobody $updflagfile");
122
123 system("/usr/local/bin/restartsquid");
124
125 system("logger -t installpackage[urlfilter] \"URL filter blacklist - Update from $blacklist_src completed\"");
126
127 $exitcode = 0;
128
129 } else {
130 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: Not a valid URL filter blacklist\"");
131 }
132 } else {
133 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: Unable to retrieve blacklist from $blacklist_src\"");
134 }
135
136 } else {
137 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: No update source defined\"");
138 }
139
140 if ((-d $target) && ($make_clean)) { system("rm -rf $target"); }
141
142 exit $exitcode;
143
144 # -------------------------------------------------------------------
145
146 sub readhash
147 {
148 my $filename = $_[0];
149 my $hash = $_[1];
150 my ($var, $val);
151
152 if (-e $filename)
153 {
154 open(FILE, $filename) or die "Unable to read file $filename";
155 while (<FILE>)
156 {
157 chop;
158 ($var, $val) = split /=/, $_, 2;
159 if ($var)
160 {
161 $val =~ s/^\'//g;
162 $val =~ s/\'$//g;
163
164 # Untaint variables read from hash
165 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
166 $val =~ /([\w\W]*)/; $val = $1;
167 $hash->{$var} = $val;
168 }
169 }
170 close FILE;
171 }
172 }
173
174 # -------------------------------------------------------------------