]> git.ipfire.org Git - ipfire-2.x.git/blame - config/urlfilter/autoupdate.pl
suricata: Change midstream policy to "pass-flow"
[ipfire-2.x.git] / config / urlfilter / autoupdate.pl
CommitLineData
10e4f239 1#!/usr/bin/perl
10e4f239 2#
1465b127
MT
3# This code is distributed under the terms of the GPL
4#
5# (c) 2004-2007 marco.s - http://www.urlfilter.net
6#
7# $Id: autoupdate.pl,v 1.1 2007/03/14 00:00:00 marco.s Exp $
10e4f239
MT
8#
9use strict;
10
5428eeee
MT
11require "/var/ipfire/general-functions.pl";
12
10e4f239
MT
13my $make_clean = 1;
14
15my $swroot = "/var/ipfire";
16my $target = "$swroot/urlfilter/download";
1465b127 17my $tempdb = "$target/blacklists";
10e4f239
MT
18my $dbdir = "$swroot/urlfilter/blacklists";
19
20my $sourceurlfile = "$swroot/urlfilter/autoupdate/autoupdate.urls";
21my $updconffile = "$swroot/urlfilter/autoupdate/autoupdate.conf";
22my $updflagfile = "$swroot/urlfilter/blacklists/.autoupdate.last";
23
24my %cgiparams;
25my %updatesettings;
10e4f239
MT
26my $blacklist_url;
27my $blacklist_src;
28my $source_url;
29my $source_name;
30my @source_urllist;
31
1465b127 32my @categories;
10e4f239
MT
33my $blacklist;
34my $category;
35
36my $exitcode = 1;
37
38if (-e "$sourceurlfile")
39{
40 open(FILE, $sourceurlfile);
41 @source_urllist = <FILE>;
42 close(FILE);
43}
44
45if (-e "$updconffile") { &readhash("$updconffile", \%updatesettings); }
46
47if (@ARGV[0] =~ m@^(f|h)tt?ps?://@) { $updatesettings{'UPDATE_SOURCE'} = @ARGV[0]; }
48
49if ($updatesettings{'UPDATE_SOURCE'} eq 'custom')
50{
51 $blacklist_url=$updatesettings{'CUSTOM_UPDATE_URL'};
52} else {
53 $blacklist_url=$updatesettings{'UPDATE_SOURCE'};
54 foreach (@source_urllist)
55 {
56 chomp;
57 $source_name = substr($_,0,rindex($_,","));
58 $source_url = substr($_,index($_,",")+1);
59 if ($blacklist_url eq $source_url) { $blacklist_src=$source_name; }
60 }
61}
62
63if ($blacklist_src eq '') { $blacklist_src="custom source URL"; }
64
65$blacklist_url =~ s/\&/\\\&/;
66
67$blacklist=substr($blacklist_url,rindex($blacklist_url,"/")+1);
68if (($blacklist =~ /\?/) || (!($blacklist =~ /\.t(ar\.)?gz$/))) { $blacklist = 'blacklist.tar.gz'; }
69$blacklist=$target.'/'.$blacklist;
70
71unless ($blacklist_url eq '')
72{
73
74 if (-d $target) { system("rm -rf $target"); }
75 system("mkdir $target");
76
5428eeee 77 &Network::setup_upstream_proxy();
10e4f239
MT
78 system("/usr/bin/wget -o $target/wget.log -O $blacklist $blacklist_url");
79
80 if (-e $blacklist)
81 {
82 system("/bin/tar --no-same-owner -xzf $blacklist -C $target");
1465b127
MT
83 if (-d "$target/BL") { system ("mv $target/BL $target/blacklists"); }
84 if (-d "$tempdb")
10e4f239 85 {
1465b127
MT
86 undef(@categories);
87 &getblockcategory ($tempdb);
88 foreach (@categories) { $_ = substr($_,length($tempdb)+1); }
89
10e4f239
MT
90 open(FILE, ">$target/update.conf");
91 flock FILE, 2;
92 print FILE "logdir $target\n";
1465b127
MT
93 print FILE "dbhome $tempdb\n\n";
94
95 foreach $category (@categories) {
96 $blacklist = $category;
97 $category =~ s/\//_/g;
98 print FILE "dest $category {\n";
99 if (-s "$tempdb/$blacklist/domains") {
100 print FILE " domainlist $blacklist\/domains\n";
10e4f239 101 }
1465b127
MT
102 if (-s "$tempdb/$blacklist/urls") {
103 print FILE " urllist $blacklist\/urls\n";
104 }
105 print FILE "}\n\n";
106 $category = $blacklist;
10e4f239 107 }
1465b127 108
10e4f239
MT
109 print FILE "acl {\n";
110 print FILE " default {\n";
111 print FILE " pass none\n";
112 print FILE " }\n";
113 print FILE "}\n";
114 close FILE;
115
0bd7d00b 116 system("/usr/bin/squidGuard -d -c $target/update.conf -C all");
10e4f239
MT
117
118 system("cp -r $target/blacklists/* $dbdir");
119
120 system("chown -R nobody.nobody $dbdir");
121
1465b127 122 &setpermissions ($dbdir);
10e4f239
MT
123
124 system("touch $updflagfile");
125 system("chown nobody.nobody $updflagfile");
126
0bd7d00b 127 system("/etc/init.d/squid restart");
10e4f239
MT
128
129 system("logger -t installpackage[urlfilter] \"URL filter blacklist - Update from $blacklist_src completed\"");
130
131 $exitcode = 0;
132
133 } else {
134 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: Not a valid URL filter blacklist\"");
135 }
136 } else {
137 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: Unable to retrieve blacklist from $blacklist_src\"");
138 }
139
140} else {
141 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: No update source defined\"");
142}
143
144if ((-d $target) && ($make_clean)) { system("rm -rf $target"); }
145
146exit $exitcode;
147
148# -------------------------------------------------------------------
149
150sub readhash
151{
152 my $filename = $_[0];
153 my $hash = $_[1];
154 my ($var, $val);
155
156 if (-e $filename)
157 {
158 open(FILE, $filename) or die "Unable to read file $filename";
159 while (<FILE>)
160 {
161 chop;
162 ($var, $val) = split /=/, $_, 2;
163 if ($var)
164 {
165 $val =~ s/^\'//g;
166 $val =~ s/\'$//g;
167
168 # Untaint variables read from hash
169 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
170 $val =~ /([\w\W]*)/; $val = $1;
171 $hash->{$var} = $val;
172 }
173 }
174 close FILE;
175 }
176}
177
178# -------------------------------------------------------------------
1465b127
MT
179
180sub getblockcategory
181{
182 foreach $category (<$_[0]/*>)
183 {
184 if (-d $category)
185 {
186 if ((-s "$category/domains") || (-s "$category/urls"))
187 {
188 unless ($category =~ /\bcustom\b/) { push(@categories,$category); }
189 }
190 &getblockcategory ($category);
191 }
192 }
193}
194
195# -------------------------------------------------------------------
196
197sub setpermissions
198{
199 my $bldir = $_[0];
200
201 foreach $category (<$bldir/*>)
202 {
203 if (-d $category){
204 system("chmod 755 $category &> /dev/null");
205 foreach $blacklist (<$category/*>)
206 {
207 if (-f $blacklist) { system("chmod 644 $blacklist &> /dev/null"); }
208 if (-d $blacklist) { system("chmod 755 $blacklist &> /dev/null"); }
209 }
210 system("chmod 666 $category/*.db &> /dev/null");
211 &setpermissions ($category);
212 }
213 }
214}
215
216# -------------------------------------------------------------------