Gro├čes Update:
[ipfire-2.x.git] / html / cgi-bin / ids.cgi
1 #!/usr/bin/perl
2 #
3 # SmoothWall CGIs
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 # (c) The SmoothWall Team
8 #
9 # $Id: ids.cgi,v 1.8.2.18 2005/07/27 21:35:22 franck78 Exp $
10 #
11
12 use LWP::UserAgent;
13 use File::Copy;
14 use File::Temp qw/ tempfile tempdir /;
15 use strict;
16
17 # enable only the following on debugging purpose
18 #use warnings;
19 #use CGI::Carp 'fatalsToBrowser';
20
21 require 'CONFIG_ROOT/general-functions.pl';
22 require "${General::swroot}/lang.pl";
23 require "${General::swroot}/header.pl";
24
25 my %snortsettings=();
26 my %checked=();
27 my %netsettings=();
28 our $errormessage = '';
29 our $md5 = '0';# not '' to avoid displaying the wrong message when INSTALLMD5 not set
30 our $realmd5 = '';
31 our $results = '';
32 our $tempdir = '';
33 our $url='';
34 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
35
36 &Header::showhttpheaders();
37
38 $snortsettings{'ENABLE_SNORT'} = 'off';
39 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
40 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
41 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
42 $snortsettings{'ACTION'} = '';
43 $snortsettings{'RULESTYPE'} = '';
44 $snortsettings{'OINKCODE'} = '';
45 $snortsettings{'INSTALLDATE'} = '';
46 $snortsettings{'INSTALLMD5'} = '';
47
48 &Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
49
50 if ($snortsettings{'RULESTYPE'} eq 'subscripted') {
51         $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-2.3_s.tar.gz";
52 } else {
53         $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-2.3.tar.gz";
54 }
55
56 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'})
57 {
58         $errormessage = $Lang::tr{'invalid input for oink code'} unless (
59             ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/)  ||
60             ($snortsettings{'RULESTYPE'} eq 'nothing' )       );
61
62         &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
63         if ($snortsettings{'ENABLE_SNORT'} eq 'on')
64         {
65                 system ('/bin/touch', "${General::swroot}/snort/enable");
66         } else {
67                 unlink "${General::swroot}/snort/enable";
68         } 
69         if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
70         {
71                 system ('/bin/touch', "${General::swroot}/snort/enable_green");
72         } else {
73                 unlink "${General::swroot}/snort/enable_green";
74         } 
75         if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
76         {
77                 system ('/bin/touch', "${General::swroot}/snort/enable_blue");
78         } else {
79                 unlink "${General::swroot}/snort/enable_blue";
80         } 
81         if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
82         {
83                 system ('/bin/touch', "${General::swroot}/snort/enable_orange");
84         } else {
85                 unlink "${General::swroot}/snort/enable_orange";
86         }
87
88         system('/usr/local/bin/restartsnort','red','orange','blue','green');
89 } else {
90          # INSTALLMD5 is not in the form, so not retrieved by getcgihash
91         &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
92 }
93
94 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
95         $md5 = &getmd5;
96         if (($snortsettings{'INSTALLMD5'} ne $md5) && defined $md5 ) {
97                 chomp($md5);
98                 my $filename = &downloadrulesfile();
99                 if (defined $filename) {
100                         # Check MD5sum
101                         $realmd5 = `/usr/bin/md5sum $filename`;
102                         chomp ($realmd5);
103                         $realmd5 =~ s/^(\w+)\s.*$/$1/;
104                         if ($md5 ne $realmd5) {
105                                 $errormessage = "$Lang::tr{'invalid md5sum'}";
106                         } else {
107                                 $results = "<b>$Lang::tr{'installed updates'}</b>\n<pre>";
108                                 $results .=`/usr/local/bin/oinkmaster.pl -s -u file://$filename -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort 2>&1`;
109                                 $results .= "</pre>";
110                         }
111                         unlink ($filename);
112                 }
113         }
114 }
115
116 $checked{'ENABLE_SNORT'}{'off'} = '';
117 $checked{'ENABLE_SNORT'}{'on'} = '';
118 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
119 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
120 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
121 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
122 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
123 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
124 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
125 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
126 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
127 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
128 $checked{'RULESTYPE'}{'nothing'} = '';
129 $checked{'RULESTYPE'}{'registered'} = '';
130 $checked{'RULESTYPE'}{'subscripted'} = '';
131 $checked{'RULESTYPE'}{$snortsettings{'RULESTYPE'}} = "checked='checked'";
132
133 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
134
135 &Header::openbigbox('100%', 'left', '', $errormessage);
136
137 if ($errormessage) {
138         &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
139         print "<class name='base'>$errormessage\n";
140         print "&nbsp;</class>\n";
141         &Header::closebox();
142 }
143
144 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system2'});
145 print <<END
146 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
147 <tr>
148         <td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />
149                 GREEN Snort</td>
150 </tr>
151 END
152 ;
153 if ($netsettings{'BLUE_DEV'} ne '') {
154 print <<END
155 <tr>
156         <td class='base'><input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />
157                 BLUE Snort</td>
158 </tr>
159 END
160 ;
161 }
162 if ($netsettings{'ORANGE_DEV'} ne '') {
163 print <<END
164 <tr>
165         <td class='base'><input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />
166                 ORANGE Snort</td>
167 </tr>
168 END
169 ;
170 }
171 print <<END
172 <tr>
173         <td class='base'><input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} />
174                 RED Snort</td>
175 </tr>
176 <tr>
177         <td><hr /></td>
178 </tr>
179 <tr>
180         <td><b>$Lang::tr{'ids rules update'}</b></td>
181 </tr>
182 <tr>
183         <td><input type='radio' name='RULESTYPE' value='nothing' $checked{'RULESTYPE'}{'nothing'} />
184                 $Lang::tr{'no'}</td>
185 </tr>
186 <tr>
187         <td><input type='radio' name='RULESTYPE' value='registered' $checked{'RULESTYPE'}{'registered'} />
188                 $Lang::tr{'registered user rules'}</td>
189 </tr>
190 <tr>
191         <td><input type='radio' name='RULESTYPE' value='subscripted' $checked{'RULESTYPE'}{'subscripted'} />
192                 $Lang::tr{'subscripted user rules'}</td>
193 </tr>
194 <tr>
195         <td><br />
196                 $Lang::tr{'ids rules license'} <a href='http://www.snort.org/' target='_blank'>http://www.snort.org</a>.<br />
197                 <br />
198                 $Lang::tr{'ids rules license2'} <a href='http://www.snort.org/reg-bin/userprefs.cgi' target='_blank'>USER PREFERENCES</a>, $Lang::tr{'ids rules license3'}<br />
199         </td>
200 </tr>
201 <tr>
202         <td nowrap='nowrap'>Oink Code:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
203 </tr>
204 <tr>
205         <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
206 END
207 ;
208
209 if ($snortsettings{'INSTALLMD5'} eq $md5) {
210         print "&nbsp;$Lang::tr{'rules already up to date'}</td>";
211 } else {
212         if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} && $md5 eq $realmd5 ) {
213                 $snortsettings{'INSTALLMD5'} = $realmd5;
214                 $snortsettings{'INSTALLDATE'} = `/bin/date +'%Y-%m-%d'`;
215                 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
216         }
217         print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
218 }
219 print <<END
220 </tr>
221 </table>
222 <hr />
223 <table width='100%'>
224 <tr>
225         <td width='55%'>&nbsp;</td>
226         <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
227         <td width='5%'>
228                 &nbsp; <!-- space for future online help link -->
229         </td>
230 </tr>
231 </table>
232 </form>
233 END
234 ;
235
236 if ($results ne '') {
237         print "$results";
238 }
239
240 &Header::closebox();
241 &Header::closebigbox();
242 &Header::closepage();
243
244 sub getmd5 {
245         # Retrieve MD5 sum from $url.md5 file
246         #
247         my $md5buf = &geturl("$url.md5");
248         return undef unless $md5buf;
249
250         if (0) { # 1 to debug
251                 my $filename='';
252                 my $fh='';
253                 ($fh, $filename) = tempfile('/tmp/XXXXXXXX',SUFFIX => '.md5' );
254                 binmode ($fh);
255                 syswrite ($fh, $md5buf->content);
256                 close($fh);
257         }
258         return $md5buf->content;
259 }
260 sub downloadrulesfile {
261         my $return = &geturl($url);
262         return undef unless $return;
263
264         if (index($return->content, "\037\213") == -1 ) { # \037\213 is .gz beginning
265                 $errormessage = $Lang::tr{'invalid loaded file'};
266                 return undef;
267         }
268
269         my $filename='';
270         my $fh='';
271         ($fh, $filename) = tempfile('/tmp/XXXXXXXX',SUFFIX => '.tar.gz' );#oinkmaster work only with this extension
272         binmode ($fh);
273         syswrite ($fh, $return->content);
274         close($fh);
275         return $filename;
276 }
277
278 sub geturl ($) {
279         my $url=$_[0];
280
281         unless (-e "${General::swroot}/red/active") {
282                 $errormessage = $Lang::tr{'could not download latest updates'};
283                 return undef;
284         }
285
286         my $downloader = LWP::UserAgent->new;
287         $downloader->timeout(5);
288
289         my %proxysettings=();
290         &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
291
292         if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
293                 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
294                 if ($proxysettings{'UPSTREAM_USER'}) {
295                         $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/");
296                 } else {
297                         $downloader->proxy("http","http://$peer:$peerport/");
298                 }
299         }
300
301         my $return = $downloader->get($url,'Cache-Control','no-cache');
302
303         if ($return->code == 403) {
304                 $errormessage = $Lang::tr{'access refused with this oinkcode'};
305                 return undef;
306         } elsif (!$return->is_success()) {
307                 $errormessage = $Lang::tr{'could not download latest updates'};
308                 return undef;
309         }
310
311         return $return;
312
313 }