]>
Commit | Line | Data |
---|---|---|
8dcebe53 SS |
1 | #!/usr/bin/perl -w |
2 | ############################################################################ | |
3 | # # | |
4 | # This file is part of the IPFire Firewall. # | |
5 | # # | |
6 | # IPFire is free software; you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation; either version 2 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # IPFire is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with IPFire; if not, write to the Free Software # | |
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # | |
19 | # # | |
fd2dccaa | 20 | # Copyright (C) 2018-2019 IPFire Team <info@ipfire.org> # |
8dcebe53 SS |
21 | # # |
22 | ############################################################################ | |
23 | ||
e55fa2f7 SS |
24 | use strict; |
25 | ||
8dcebe53 SS |
26 | package IDS; |
27 | ||
28 | require '/var/ipfire/general-functions.pl'; | |
abffcc99 | 29 | require "${General::swroot}/network-functions.pl"; |
35bc92a3 | 30 | require "${General::swroot}/suricata/ruleset-sources"; |
8dcebe53 | 31 | |
02844177 | 32 | # Location where all config and settings files are stored. |
164eab66 | 33 | our $settingsdir = "${General::swroot}/suricata"; |
02844177 | 34 | |
ddaf8ae1 SS |
35 | # File where the main file for providers ruleset inclusion exists. |
36 | our $suricata_used_providers_file = "$settingsdir/suricata-used-providers.yaml"; | |
37 | ||
88eb5626 SS |
38 | # File for static ruleset inclusions. |
39 | our $suricata_static_rulefiles_file = "$settingsdir/suricata-static-included-rulefiles.yaml"; | |
40 | ||
b02e30fd SS |
41 | # File where the addresses of the homenet are stored. |
42 | our $homenet_file = "$settingsdir/suricata-homenet.yaml"; | |
43 | ||
30ee98e9 SS |
44 | # File where the addresses of the used DNS servers are stored. |
45 | our $dns_servers_file = "$settingsdir/suricata-dns-servers.yaml"; | |
46 | ||
e698090e SS |
47 | # File where the HTTP ports definition is stored. |
48 | our $http_ports_file = "$settingsdir/suricata-http-ports.yaml"; | |
49 | ||
15832b10 SS |
50 | # File which contains includes for provider specific rule modifications. |
51 | our $oinkmaster_provider_includes_file = "$settingsdir/oinkmaster-provider-includes.conf"; | |
52 | ||
b02e30fd SS |
53 | # File which contains wheater the rules should be changed. |
54 | our $modify_sids_file = "$settingsdir/oinkmaster-modify-sids.conf"; | |
55 | ||
56 | # File which stores the configured IPS settings. | |
57 | our $ids_settings_file = "$settingsdir/settings"; | |
58 | ||
a8d36d3e SS |
59 | # File which stores the used and configured ruleset providers. |
60 | our $providers_settings_file = "$settingsdir/providers-settings"; | |
61 | ||
b02e30fd SS |
62 | # File which stores the configured settings for whitelisted addresses. |
63 | our $ignored_file = "$settingsdir/ignored"; | |
64 | ||
788a71f5 SS |
65 | # Location where the downloaded rulesets are stored. |
66 | our $dl_rules_path = "/var/tmp"; | |
67 | ||
3983aebd | 68 | # File to store any errors, which also will be read and displayed by the wui. |
77910792 | 69 | our $storederrorfile = "/tmp/ids_storederror"; |
3983aebd | 70 | |
8076deba SS |
71 | # File to lock the WUI, while the autoupdate script runs. |
72 | our $ids_page_lock_file = "/tmp/ids_page_locked"; | |
73 | ||
298ef5ba | 74 | # Location where the rulefiles are stored. |
21cab141 | 75 | our $rulespath = "/var/lib/suricata"; |
298ef5ba | 76 | |
6983a96e SS |
77 | # Location where the addition config files are stored. |
78 | our $configspath = "/usr/share/suricata"; | |
79 | ||
23b56052 | 80 | # Location of the classification file. |
6983a96e | 81 | our $classification_file = "$configspath/classification.config"; |
23b56052 | 82 | |
8bd74e12 SS |
83 | # Location of the sid to msg mappings file. |
84 | our $sid_msg_file = "$rulespath/sid-msg.map"; | |
85 | ||
612bb2df SS |
86 | # Location to store local rules. This file will not be touched. |
87 | our $local_rules_file = "$rulespath/local.rules"; | |
88 | ||
b02e30fd SS |
89 | # File which contains the rules to whitelist addresses on suricata. |
90 | our $whitelist_file = "$rulespath/whitelist.rules"; | |
91 | ||
bce84f39 SS |
92 | # File which contains a list of all supported ruleset sources. |
93 | # (Sourcefire, Emergingthreads, etc..) | |
94 | our $rulesetsourcesfile = "$settingsdir/ruleset-sources"; | |
95 | ||
796eea21 SS |
96 | # The pidfile of the IDS. |
97 | our $idspidfile = "/var/run/suricata.pid"; | |
98 | ||
5240a809 SS |
99 | # Location of suricatactrl. |
100 | my $suricatactrl = "/usr/local/bin/suricatactrl"; | |
101 | ||
788a71f5 SS |
102 | # Prefix for each downloaded ruleset. |
103 | my $dl_rulesfile_prefix = "idsrules"; | |
104 | ||
0fbfffea SS |
105 | # Temporary directory where the rulesets will be extracted. |
106 | my $tmp_directory = "/tmp/ids_tmp"; | |
107 | ||
6c9d3eee SS |
108 | # Temporary directory where the extracted rules files will be stored. |
109 | my $tmp_rules_directory = "$tmp_directory/rules"; | |
110 | ||
111 | # Temporary directory where the extracted additional config files will be stored. | |
112 | my $tmp_conf_directory = "$tmp_directory/conf"; | |
113 | ||
5240a809 | 114 | # Array with allowed commands of suricatactrl. |
ed06bc81 SS |
115 | my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir', 'cron' ); |
116 | ||
117 | # Array with supported cron intervals. | |
118 | my @cron_intervals = ('off', 'daily', 'weekly' ); | |
5240a809 | 119 | |
e698090e SS |
120 | # Array which contains the HTTP ports, which statically will be declared as HTTP_PORTS in the |
121 | # http_ports_file. | |
122 | my @http_ports = ('80', '81'); | |
123 | ||
88eb5626 SS |
124 | # Array which contains a list of rulefiles which always will be included if they exist. |
125 | my @static_included_rulefiles = ('local.rules', 'whitelist.rules' ); | |
126 | ||
788a71f5 SS |
127 | # Hash which allows to convert the download type (dl_type) to a file suffix. |
128 | my %dl_type_to_suffix = ( | |
129 | "archive" => ".tar.gz", | |
130 | "plain" => ".rules", | |
131 | ); | |
132 | ||
b02e30fd SS |
133 | # |
134 | ## Function to check and create all IDS related files, if the does not exist. | |
135 | # | |
136 | sub check_and_create_filelayout() { | |
137 | # Check if the files exist and if not, create them. | |
15832b10 | 138 | unless (-f "$oinkmaster_provider_includes_file") { &create_empty_file($oinkmaster_provider_includes_file); } |
b02e30fd | 139 | unless (-f "$modify_sids_file") { &create_empty_file($modify_sids_file); } |
ddaf8ae1 | 140 | unless (-f "$suricata_used_providers_file") { &create_empty_file($suricata_used_providers_file); } |
b02e30fd | 141 | unless (-f "$ids_settings_file") { &create_empty_file($ids_settings_file); } |
a8d36d3e | 142 | unless (-f "$providers_settings_file") { &create_empty_file($providers_settings_file); } |
b02e30fd SS |
143 | unless (-f "$ignored_file") { &create_empty_file($ignored_file); } |
144 | unless (-f "$whitelist_file" ) { &create_empty_file($whitelist_file); } | |
145 | } | |
146 | ||
70cc1315 SS |
147 | # |
148 | ## Function to get a list of all available ruleset providers. | |
149 | ## | |
150 | ## They will be returned as a sorted array. | |
151 | # | |
152 | sub get_ruleset_providers() { | |
153 | my @providers; | |
154 | ||
155 | # Loop through the hash of providers. | |
156 | foreach my $provider ( keys %IDS::Ruleset::Providers ) { | |
157 | # Add the provider to the array. | |
158 | push(@providers, $provider); | |
159 | } | |
160 | ||
161 | # Sort and return the array. | |
162 | return sort(@providers); | |
163 | } | |
164 | ||
5e20d6cb SS |
165 | # |
166 | ## Function to get a list of all enabled ruleset providers. | |
167 | ## | |
168 | ## They will be returned as an array. | |
169 | # | |
170 | sub get_enabled_providers () { | |
171 | my %used_providers = (); | |
172 | ||
173 | # Array to store the enabled providers. | |
174 | my @enabled_providers = (); | |
175 | ||
176 | # Read-in the providers config file. | |
177 | &General::readhasharray("$providers_settings_file", \%used_providers); | |
178 | ||
179 | # Loop through the hash of used_providers. | |
180 | foreach my $id (keys %used_providers) { | |
181 | # Skip disabled providers. | |
182 | next unless ($used_providers{$id}[3] eq "enabled"); | |
183 | ||
184 | # Grab the provider handle. | |
185 | my $provider = "$used_providers{$id}[0]"; | |
186 | ||
187 | # Add the provider to the array of enabled providers. | |
188 | push(@enabled_providers, $provider); | |
189 | } | |
190 | ||
191 | # Return the array. | |
192 | return @enabled_providers; | |
193 | } | |
194 | ||
8dcebe53 SS |
195 | # |
196 | ## Function for checking if at least 300MB of free disk space are available | |
197 | ## on the "/var" partition. | |
198 | # | |
199 | sub checkdiskspace () { | |
200 | # Call diskfree to gather the free disk space of /var. | |
81631920 | 201 | my @df = &General::system_output("/bin/df", "-B", "M", "/var"); |
8dcebe53 SS |
202 | |
203 | # Loop through the output. | |
204 | foreach my $line (@df) { | |
205 | # Ignore header line. | |
206 | next if $line =~ m/^Filesystem/; | |
207 | ||
208 | # Search for a line with the device information. | |
209 | if ($line =~ m/dev/ ) { | |
210 | # Split the line into single pieces. | |
211 | my @values = split(' ', $line); | |
212 | my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values; | |
213 | ||
214 | # Check if the available disk space is more than 300MB. | |
215 | if ($available < 300) { | |
434001d0 SS |
216 | # Log error to syslog. |
217 | &_log_to_syslog("Not enough free disk space on /var. Only $available MB from 300 MB available."); | |
8dcebe53 | 218 | |
434001d0 SS |
219 | # Exit function and return "1" - False. |
220 | return 1; | |
8dcebe53 SS |
221 | } |
222 | } | |
223 | } | |
224 | ||
225 | # Everything okay, return nothing. | |
226 | return; | |
227 | } | |
228 | ||
eea2670b | 229 | # |
b3c2c336 SS |
230 | ## This function is responsible for downloading the configured IDS rulesets or if no one is specified |
231 | ## all configured rulesets will be downloaded. | |
eea2670b | 232 | ## |
b3c2c336 SS |
233 | ## * At first it gathers all configured ruleset providers, initialize the downloader and sets an |
234 | ## upstream proxy if configured. | |
235 | ## * After that, the given ruleset or in case all rulesets should be downloaded, it will determine wether it | |
236 | ## is enabled or not. | |
237 | ## * The next step will be to generate the final download url, by obtaining the URL for the desired | |
238 | ## ruleset, add the settings for the upstream proxy. | |
239 | ## * Finally the function will grab all the rules files or tarballs from the servers. | |
eea2670b | 240 | # |
b3c2c336 SS |
241 | sub downloadruleset ($) { |
242 | my ($provider) = @_; | |
243 | ||
244 | # If no provider is given default to "all". | |
245 | $provider //= 'all'; | |
246 | ||
247 | # Hash to store the providers and access id's, for which rules should be downloaded. | |
248 | my %sheduled_providers = (); | |
249 | ||
250 | # Get used provider settings. | |
251 | my %used_providers = (); | |
252 | &General::readhasharray("$providers_settings_file", \%used_providers); | |
eea2670b | 253 | |
be52c68a | 254 | # Check if a ruleset has been configured. |
b3c2c336 | 255 | unless(%used_providers) { |
be52c68a | 256 | # Log that no ruleset has been configured and abort. |
b3c2c336 | 257 | &_log_to_syslog("No ruleset provider has been configured."); |
be52c68a SS |
258 | |
259 | # Return "1". | |
260 | return 1; | |
261 | } | |
262 | ||
eea2670b SS |
263 | # Read proxysettings. |
264 | my %proxysettings=(); | |
265 | &General::readhash("${General::swroot}/proxy/settings", \%proxysettings); | |
266 | ||
267 | # Load required perl module to handle the download. | |
268 | use LWP::UserAgent; | |
269 | ||
270 | # Init the download module. | |
271 | my $downloader = LWP::UserAgent->new; | |
272 | ||
273 | # Set timeout to 10 seconds. | |
274 | $downloader->timeout(10); | |
275 | ||
276 | # Check if an upstream proxy is configured. | |
277 | if ($proxysettings{'UPSTREAM_PROXY'}) { | |
eea2670b SS |
278 | my $proxy_url; |
279 | ||
40407aee | 280 | $proxy_url = "http://"; |
eea2670b | 281 | |
40407aee PM |
282 | # Check if the proxy requires authentication. |
283 | if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) { | |
284 | $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@"; | |
eea2670b SS |
285 | } |
286 | ||
40407aee PM |
287 | # Add proxy server address and port. |
288 | $proxy_url .= $proxysettings{'UPSTREAM_PROXY'}; | |
289 | ||
eea2670b | 290 | # Setup proxy settings. |
6994f001 | 291 | $downloader->proxy(['http', 'https'], $proxy_url); |
eea2670b SS |
292 | } |
293 | ||
b3c2c336 SS |
294 | # Loop through the hash of configured providers. |
295 | foreach my $id ( keys %used_providers ) { | |
296 | # Skip providers which are not enabled. | |
297 | next if ($used_providers{$id}[3] ne "enabled"); | |
eea2670b | 298 | |
b3c2c336 SS |
299 | # Obtain the provider handle. |
300 | my $provider_handle = $used_providers{$id}[0]; | |
eea2670b | 301 | |
b3c2c336 SS |
302 | # Handle update off all providers. |
303 | if (($provider eq "all") || ($provider_handle eq "$provider")) { | |
304 | # Add provider handle and it's id to the hash of sheduled providers. | |
2c02c936 | 305 | $sheduled_providers{$provider_handle} = $id; |
b3c2c336 | 306 | } |
eea2670b SS |
307 | } |
308 | ||
b3c2c336 SS |
309 | # Loop through the hash of sheduled providers. |
310 | foreach my $provider ( keys %sheduled_providers) { | |
311 | # Grab the download url for the provider. | |
312 | my $url = $IDS::Ruleset::Providers{$provider}{'dl_url'}; | |
313 | ||
314 | # Check if the provider requires a subscription. | |
315 | if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") { | |
316 | # Grab the previously stored access id for the provider from hash. | |
317 | my $id = $sheduled_providers{$provider}; | |
318 | ||
319 | # Grab the subscription code. | |
320 | my $subscription_code = $used_providers{$id}[1]; | |
321 | ||
322 | # Add the subscription code to the download url. | |
323 | $url =~ s/\<subscription_code\>/$subscription_code/g; | |
324 | ||
325 | } | |
326 | ||
327 | # Abort if no url could be determined for the provider. | |
328 | unless ($url) { | |
329 | # Log error and abort. | |
330 | &_log_to_syslog("Unable to gather a download URL for the selected ruleset provider."); | |
331 | return 1; | |
332 | } | |
333 | ||
334 | # Variable to store the filesize of the remote object. | |
335 | my $remote_filesize; | |
336 | ||
337 | # The sourcfire (snort rules) does not allow to send "HEAD" requests, so skip this check | |
338 | # for this webserver. | |
339 | # | |
340 | # Check if the ruleset source contains "snort.org". | |
341 | unless ($url =~ /\.snort\.org/) { | |
342 | # Pass the requrested url to the downloader. | |
343 | my $request = HTTP::Request->new(HEAD => $url); | |
344 | ||
345 | # Accept the html header. | |
346 | $request->header('Accept' => 'text/html'); | |
347 | ||
348 | # Perform the request and fetch the html header. | |
349 | my $response = $downloader->request($request); | |
350 | ||
351 | # Check if there was any error. | |
352 | unless ($response->is_success) { | |
353 | # Obtain error. | |
354 | my $error = $response->status_line(); | |
355 | ||
356 | # Log error message. | |
357 | &_log_to_syslog("Unable to download the ruleset. \($error\)"); | |
358 | ||
359 | # Return "1" - false. | |
360 | return 1; | |
361 | } | |
362 | ||
363 | # Assign the fetched header object. | |
364 | my $header = $response->headers(); | |
365 | ||
366 | # Grab the remote file size from the object and store it in the | |
367 | # variable. | |
368 | $remote_filesize = $header->content_length; | |
369 | } | |
370 | ||
371 | # Load perl module to deal with temporary files. | |
372 | use File::Temp; | |
96da5803 | 373 | |
b3c2c336 SS |
374 | # Generate temporary file name, located in "/var/tmp" and with a suffix of ".tmp". |
375 | my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "/var/tmp/", UNLINK => 0 ); | |
376 | my $tmpfile = $tmp->filename(); | |
96da5803 | 377 | |
b3c2c336 SS |
378 | # Pass the requested url to the downloader. |
379 | my $request = HTTP::Request->new(GET => $url); | |
96da5803 | 380 | |
b3c2c336 SS |
381 | # Perform the request and save the output into the tmpfile. |
382 | my $response = $downloader->request($request, $tmpfile); | |
96da5803 | 383 | |
155b3b56 SS |
384 | # Check if there was any error. |
385 | unless ($response->is_success) { | |
386 | # Obtain error. | |
b3c2c336 | 387 | my $error = $response->content; |
96da5803 | 388 | |
155b3b56 SS |
389 | # Log error message. |
390 | &_log_to_syslog("Unable to download the ruleset. \($error\)"); | |
391 | ||
392 | # Return "1" - false. | |
393 | return 1; | |
394 | } | |
96da5803 | 395 | |
b3c2c336 SS |
396 | # Load perl stat module. |
397 | use File::stat; | |
96da5803 | 398 | |
b3c2c336 SS |
399 | # Perform stat on the tmpfile. |
400 | my $stat = stat($tmpfile); | |
96da5803 | 401 | |
b3c2c336 SS |
402 | # Grab the local filesize of the downloaded tarball. |
403 | my $local_filesize = $stat->size; | |
25b6545a | 404 | |
b3c2c336 SS |
405 | # Check if both file sizes match. |
406 | if (($remote_filesize) && ($remote_filesize ne $local_filesize)) { | |
407 | # Log error message. | |
408 | &_log_to_syslog("Unable to completely download the ruleset. "); | |
409 | &_log_to_syslog("Only got $local_filesize Bytes instead of $remote_filesize Bytes. "); | |
eea2670b | 410 | |
b3c2c336 SS |
411 | # Delete temporary file. |
412 | unlink("$tmpfile"); | |
88daf7eb | 413 | |
b3c2c336 SS |
414 | # Return "1" - false. |
415 | return 1; | |
416 | } | |
434001d0 | 417 | |
b3c2c336 SS |
418 | # Genarate and assign file name and path to store the downloaded rules file. |
419 | my $dl_rulesfile = &_get_dl_rulesfile($provider); | |
eea2670b | 420 | |
b3c2c336 SS |
421 | # Check if a file name could be obtained. |
422 | unless ($dl_rulesfile) { | |
423 | # Log error message. | |
424 | &_log_to_syslog("Unable to store the downloaded rules file. "); | |
96da5803 | 425 | |
b3c2c336 SS |
426 | # Delete downloaded temporary file. |
427 | unlink("$tmpfile"); | |
96da5803 | 428 | |
b3c2c336 SS |
429 | # Return "1" - false. |
430 | } | |
96da5803 | 431 | |
b3c2c336 SS |
432 | # Load file copy module, which contains the move() function. |
433 | use File::Copy; | |
96da5803 | 434 | |
b3c2c336 SS |
435 | # Overwrite the may existing rulefile or tarball with the downloaded one. |
436 | move("$tmpfile", "$dl_rulesfile"); | |
25b6545a | 437 | |
ae226132 SS |
438 | # Delete temporary file. |
439 | unlink("$tmpfile"); | |
440 | ||
b3c2c336 SS |
441 | # Set correct ownership for the tarball. |
442 | set_ownership("$dl_rulesfile"); | |
96da5803 SS |
443 | } |
444 | ||
eea2670b SS |
445 | # If we got here, everything worked fine. Return nothing. |
446 | return; | |
447 | } | |
8dcebe53 | 448 | |
0fbfffea SS |
449 | # |
450 | ## Function to extract a given ruleset. | |
1b5aec1b SS |
451 | ## |
452 | ## In case the ruleset provider offers a plain file, it simply will | |
453 | ## be copied. | |
0fbfffea SS |
454 | # |
455 | sub extractruleset ($) { | |
456 | my ($provider) = @_; | |
457 | ||
458 | # Load perl module to deal with archives. | |
459 | use Archive::Tar; | |
460 | ||
461 | # Load perl module to deal with files and path. | |
462 | use File::Basename; | |
463 | ||
1b5aec1b SS |
464 | # Load perl module for file copying. |
465 | use File::Copy; | |
466 | ||
0fbfffea SS |
467 | # Get full path and downloaded rulesfile for the given provider. |
468 | my $tarball = &_get_dl_rulesfile($provider); | |
469 | ||
470 | # Check if the file exists. | |
471 | unless (-f $tarball) { | |
1b5aec1b | 472 | &_log_to_syslog("Could not find ruleset file: $tarball"); |
0fbfffea SS |
473 | |
474 | # Return nothing. | |
475 | return; | |
476 | } | |
477 | ||
0fbfffea SS |
478 | # Check if the temporary directories exist, otherwise create them. |
479 | mkdir("$tmp_directory") unless (-d "$tmp_directory"); | |
6c9d3eee SS |
480 | mkdir("$tmp_rules_directory") unless (-d "$tmp_rules_directory"); |
481 | mkdir("$tmp_conf_directory") unless (-d "$tmp_conf_directory"); | |
0fbfffea | 482 | |
1b5aec1b SS |
483 | # Omit the type (dl_type) of the stored ruleset. |
484 | my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'}; | |
0fbfffea | 485 | |
1b5aec1b SS |
486 | # Handle the different ruleset types. |
487 | if ($type eq "plain") { | |
488 | # Generate destination filename an full path. | |
489 | my $destination = "$tmp_rules_directory/$provider\-ruleset.rules"; | |
0fbfffea | 490 | |
1b5aec1b SS |
491 | # Copy the file into the temporary rules directory. |
492 | copy($tarball, $destination); | |
0fbfffea | 493 | |
1b5aec1b SS |
494 | } elsif ( $type eq "archive") { |
495 | # Initialize the tar module. | |
496 | my $tar = Archive::Tar->new($tarball); | |
0fbfffea | 497 | |
1b5aec1b SS |
498 | # Get the filelist inside the tarball. |
499 | my @packed_files = $tar->list_files; | |
0fbfffea | 500 | |
1b5aec1b SS |
501 | # Loop through the filelist. |
502 | foreach my $packed_file (@packed_files) { | |
503 | my $destination; | |
0fbfffea | 504 | |
1b5aec1b SS |
505 | # Splitt the packed file into chunks. |
506 | my $file = fileparse($packed_file); | |
0fbfffea | 507 | |
1b5aec1b SS |
508 | # Handle msg-id.map file. |
509 | if ("$file" eq "sid-msg.map") { | |
510 | # Set extract destination to temporary config_dir. | |
511 | $destination = "$tmp_conf_directory/$provider\-sid-msg.map"; | |
0fbfffea | 512 | |
1b5aec1b SS |
513 | # Handle classification.conf |
514 | } elsif ("$file" eq "classification.config") { | |
515 | # Set extract destination to temporary config_dir. | |
516 | $destination = "$tmp_conf_directory/$provider\-classification.config"; | |
0fbfffea | 517 | |
1b5aec1b SS |
518 | # Handle rules files. |
519 | } elsif ($file =~ m/\.rules$/) { | |
520 | my $rulesfilename; | |
0fbfffea | 521 | |
1b5aec1b SS |
522 | # Splitt the filename into chunks. |
523 | my @filename = split("-", $file); | |
0fbfffea | 524 | |
1b5aec1b SS |
525 | # Reverse the array. |
526 | @filename = reverse(@filename); | |
0fbfffea | 527 | |
1b5aec1b SS |
528 | # Get the amount of elements in the array. |
529 | my $elements = @filename; | |
0fbfffea | 530 | |
1b5aec1b SS |
531 | # Remove last element of the hash. |
532 | # It contains the vendor name, which will be replaced. | |
533 | if ($elements >= 3) { | |
534 | # Remove last element from hash. | |
535 | pop(@filename); | |
536 | } | |
537 | ||
538 | # Check if the last element of the filename does not | |
539 | # contain the providers name. | |
540 | if ($filename[-1] ne "$provider") { | |
541 | # Add provider name as last element. | |
542 | push(@filename, $provider); | |
543 | } | |
544 | ||
545 | # Reverse the array back. | |
546 | @filename = reverse(@filename); | |
547 | ||
548 | # Generate the name for the rulesfile. | |
549 | $rulesfilename = join("-", @filename); | |
550 | ||
551 | # Set extract destination to temporaray rules_dir. | |
552 | $destination = "$tmp_rules_directory/$rulesfilename"; | |
553 | } else { | |
554 | # Skip all other files. | |
555 | next; | |
556 | } | |
557 | ||
558 | # Extract the file to the temporary directory. | |
559 | $tar->extract_file("$packed_file", "$destination"); | |
560 | } | |
0fbfffea SS |
561 | } |
562 | } | |
563 | ||
25f5cb0d | 564 | # |
b953677b SS |
565 | ## A wrapper function to call the oinkmaster script, setup the rules structues and |
566 | ## call the functions to merge the additional config files. (classification, sid-msg, etc.). | |
25f5cb0d SS |
567 | # |
568 | sub oinkmaster () { | |
330759d8 SS |
569 | # Check if the files in rulesdir have the correct permissions. |
570 | &_check_rulesdir_permissions(); | |
571 | ||
b953677b | 572 | # Cleanup the rules directory before filling it with the new rulests. |
883820bd SS |
573 | &_cleanup_rulesdir(); |
574 | ||
0130e0d1 | 575 | # Get all enabled providers. |
e31458de | 576 | my @enabled_providers = &get_enabled_providers(); |
b953677b | 577 | |
0130e0d1 SS |
578 | # Loop through the array of enabled providers. |
579 | foreach my $provider (@enabled_providers) { | |
1b5aec1b SS |
580 | # Call the extractruleset function. |
581 | &extractruleset($provider); | |
b953677b SS |
582 | } |
583 | ||
0e40e1e7 SS |
584 | # Load perl module to talk to the kernel syslog. |
585 | use Sys::Syslog qw(:DEFAULT setlogsock); | |
586 | ||
587 | # Establish the connection to the syslog service. | |
588 | openlog('oinkmaster', 'cons,pid', 'user'); | |
589 | ||
25f5cb0d | 590 | # Call oinkmaster to generate ruleset. |
b953677b | 591 | open(OINKMASTER, "/usr/local/bin/oinkmaster.pl -s -u dir://$tmp_rules_directory -C $settingsdir/oinkmaster.conf -o $rulespath 2>&1 |") or die "Could not execute oinkmaster $!\n"; |
0e40e1e7 SS |
592 | |
593 | # Log output of oinkmaster to syslog. | |
594 | while(<OINKMASTER>) { | |
595 | # The syslog function works best with an array based input, | |
596 | # so generate one before passing the message details to syslog. | |
597 | my @syslog = ("INFO", "$_"); | |
598 | ||
599 | # Send the log message. | |
600 | syslog(@syslog); | |
601 | } | |
602 | ||
603 | # Close the pipe to oinkmaster process. | |
604 | close(OINKMASTER); | |
605 | ||
606 | # Close the log handle. | |
607 | closelog(); | |
b953677b | 608 | |
b953677b SS |
609 | # Call function to merge the classification files. |
610 | &merge_classifications(@enabled_providers); | |
611 | ||
612 | # Call function to merge the sid to message mapping files. | |
613 | &merge_sid_msg(@enabled_providers); | |
614 | ||
615 | # Cleanup temporary directory. | |
16b2d281 | 616 | &cleanup_tmp_directory(); |
25f5cb0d SS |
617 | } |
618 | ||
23b56052 SS |
619 | # |
620 | ## Function to merge the classifications for a given amount of providers and write them | |
621 | ## to the classifications file. | |
622 | # | |
623 | sub merge_classifications(@) { | |
624 | my @providers = @_; | |
625 | ||
626 | # Hash to store all collected classifications. | |
627 | my %classifications = (); | |
628 | ||
629 | # Loop through the given array of providers. | |
630 | foreach my $provider (@providers) { | |
631 | # Generate full path to classification file. | |
6c9d3eee | 632 | my $classification_file = "$tmp_conf_directory/$provider\-classification.config"; |
23b56052 SS |
633 | |
634 | # Skip provider if no classification file exists. | |
635 | next unless (-f "$classification_file"); | |
636 | ||
637 | # Open the classification file. | |
638 | open(CLASSIFICATION, $classification_file) or die "Could not open file $classification_file. $!\n"; | |
639 | ||
640 | # Loop through the file content. | |
641 | while(<CLASSIFICATION>) { | |
642 | # Parse the file and grab the classification details. | |
643 | if ($_ =~/.*config classification\: (.*)/) { | |
644 | # Split the grabbed details. | |
645 | my ($short_name, $short_desc, $priority) = split("\,", $1); | |
646 | ||
647 | # Check if the grabbed classification is allready known and the priority value is greater | |
648 | # than the stored one (which causes less priority in the IDS). | |
649 | if (($classifications{$short_name}) && ($classifications{$short_name}[1] >= $priority)) { | |
650 | #Change the priority value to the stricter one. | |
651 | $classifications{$short_name} = [ "$classifications{$short_name}[0]", "$priority" ]; | |
652 | } else { | |
653 | # Add the classification to the hash. | |
654 | $classifications{$short_name} = [ "$short_desc", "$priority" ]; | |
655 | } | |
656 | } | |
657 | } | |
658 | ||
659 | # Close the file. | |
660 | close(CLASSIFICATION); | |
661 | } | |
662 | ||
663 | # Open classification file for writing. | |
664 | open(FILE, ">", "$classification_file") or die "Could not write to $classification_file. $!\n"; | |
665 | ||
666 | # Print notice about autogenerated file. | |
667 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n\n"; | |
668 | ||
669 | # Sort and loop through the hash of classifications. | |
670 | foreach my $key (sort keys %classifications) { | |
671 | # Assign some nice variable names for the items. | |
672 | my $short_name = $key; | |
673 | my $short_desc = $classifications{$key}[0]; | |
674 | my $priority = $classifications{$key}[1]; | |
675 | ||
676 | # Write the classification to the file. | |
677 | print FILE "config classification: $short_name,$short_desc,$priority\n"; | |
678 | } | |
679 | ||
680 | # Close file handle. | |
681 | close(FILE); | |
682 | } | |
683 | ||
8bd74e12 SS |
684 | # |
685 | ## Function to merge the "sid to message mapping" files of various given providers. | |
686 | # | |
687 | sub merge_sid_msg (@) { | |
688 | my @providers = @_; | |
689 | ||
690 | # Hash which contains all the sid to message mappings. | |
691 | my %mappings = (); | |
692 | ||
693 | # Loop through the array of given providers. | |
694 | foreach my $provider (@providers) { | |
695 | # Generate full path and filename. | |
6c9d3eee | 696 | my $sid_msg_file = "$tmp_conf_directory/$provider\-sid-msg.map"; |
8bd74e12 SS |
697 | |
698 | # Skip provider if no sid to msg mapping file for this provider exists. | |
699 | next unless (-f $sid_msg_file); | |
700 | ||
701 | # Open the file. | |
702 | open(MAPPING, $sid_msg_file) or die "Could not open $sid_msg_file. $!\n"; | |
703 | ||
704 | # Loop through the file content. | |
705 | while (<MAPPING>) { | |
706 | # Remove newlines. | |
707 | chomp($_); | |
708 | ||
709 | # Skip lines which do not start with a number, | |
710 | next unless ($_ =~ /^\d+/); | |
711 | ||
712 | # Split line content and assign it to an array. | |
713 | my @line = split(/ \|\| /, $_); | |
714 | ||
715 | # Grab the first element (and remove it) from the line array. | |
716 | # It contains the sid. | |
717 | my $sid = shift(@line); | |
718 | ||
719 | # Store the grabbed sid and the remain array as hash value. | |
720 | # It still contains the messages, references etc. | |
721 | $mappings{$sid} = [@line]; | |
722 | } | |
723 | ||
724 | # Close file handle. | |
725 | close(MAPPING); | |
726 | } | |
727 | ||
728 | # Open mappings file for writing. | |
729 | open(FILE, ">", $sid_msg_file) or die "Could not write $sid_msg_file. $!\n"; | |
730 | ||
731 | # Write notice about autogenerated file. | |
732 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n\n"; | |
733 | ||
734 | # Loop through the hash of mappings. | |
735 | foreach my $sid ( sort keys %mappings) { | |
736 | # Grab data for the sid. | |
737 | my @data = @{$mappings{$sid}}; | |
738 | ||
739 | # Add the sid to the data array. | |
740 | unshift(@data, $sid); | |
741 | ||
742 | # Generate line. | |
743 | my $line = join(" \|\| ", @data); | |
744 | ||
745 | print FILE "$line\n"; | |
746 | ||
747 | } | |
748 | ||
749 | # Close file handle. | |
750 | close(FILE); | |
751 | } | |
752 | ||
50f348f6 SS |
753 | # |
754 | ## A very tiny function to move an extracted ruleset from the temporary directory into | |
755 | ## the rules directory. | |
756 | # | |
757 | sub move_tmp_ruleset() { | |
758 | # Load perl module. | |
759 | use File::Copy; | |
760 | ||
761 | # Do a directory listing of the temporary directory. | |
762 | opendir DH, $tmp_rules_directory; | |
763 | ||
764 | # Loop over all files. | |
765 | while(my $file = readdir DH) { | |
766 | # Move them to the rules directory. | |
767 | move "$tmp_rules_directory/$file" , "$rulespath/$file"; | |
768 | } | |
769 | ||
770 | # Close directory handle. | |
771 | closedir DH; | |
772 | } | |
773 | ||
16b2d281 SS |
774 | # |
775 | ## Function to cleanup the temporary IDS directroy. | |
776 | # | |
777 | sub cleanup_tmp_directory () { | |
778 | # Load rmtree() function from file path perl module. | |
779 | use File::Path 'rmtree'; | |
780 | ||
781 | # Delete temporary directory and all containing files. | |
782 | rmtree([ "$tmp_directory" ]); | |
783 | } | |
784 | ||
3983aebd SS |
785 | # |
786 | ## Function to do all the logging stuff if the downloading or updating of the ruleset fails. | |
787 | # | |
788 | sub log_error ($) { | |
789 | my ($error) = @_; | |
790 | ||
791 | # Remove any newline. | |
792 | chomp($error); | |
793 | ||
eb5592c1 SS |
794 | # Call private function to log the error message to syslog. |
795 | &_log_to_syslog($error); | |
796 | ||
3983aebd SS |
797 | # Call private function to write/store the error message in the storederrorfile. |
798 | &_store_error_message($error); | |
799 | } | |
800 | ||
eb5592c1 SS |
801 | # |
802 | ## Function to log a given error message to the kernel syslog. | |
803 | # | |
804 | sub _log_to_syslog ($) { | |
805 | my ($message) = @_; | |
806 | ||
807 | # Load perl module to talk to the kernel syslog. | |
808 | use Sys::Syslog qw(:DEFAULT setlogsock); | |
809 | ||
810 | # The syslog function works best with an array based input, | |
811 | # so generate one before passing the message details to syslog. | |
812 | my @syslog = ("ERR", "<ERROR> $message"); | |
813 | ||
814 | # Establish the connection to the syslog service. | |
815 | openlog('oinkmaster', 'cons,pid', 'user'); | |
816 | ||
817 | # Send the log message. | |
818 | syslog(@syslog); | |
819 | ||
820 | # Close the log handle. | |
821 | closelog(); | |
822 | } | |
823 | ||
3983aebd SS |
824 | # |
825 | ## Private function to write a given error message to the storederror file. | |
826 | # | |
827 | sub _store_error_message ($) { | |
828 | my ($message) = @_; | |
829 | ||
830 | # Remove any newline. | |
831 | chomp($message); | |
832 | ||
833 | # Open file for writing. | |
834 | open (ERRORFILE, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n"; | |
835 | ||
836 | # Write error to file. | |
837 | print ERRORFILE "$message\n"; | |
838 | ||
839 | # Close file. | |
840 | close (ERRORFILE); | |
3c59b1fa SS |
841 | |
842 | # Set correct ownership for the file. | |
843 | &set_ownership("$storederrorfile"); | |
3983aebd SS |
844 | } |
845 | ||
788a71f5 SS |
846 | # |
847 | ## Private function to get the path and filename for a downloaded ruleset by a given provider. | |
848 | # | |
849 | sub _get_dl_rulesfile($) { | |
850 | my ($provider) = @_; | |
851 | ||
852 | # Gather the download type for the given provider. | |
853 | my $dl_type = $IDS::Ruleset::Providers{$provider}{'dl_type'}; | |
854 | ||
855 | # Obtain the file suffix for the download file type. | |
856 | my $suffix = $dl_type_to_suffix{$dl_type}; | |
857 | ||
858 | # Check if a suffix has been found. | |
859 | unless ($suffix) { | |
860 | # Abort return - nothing. | |
861 | return; | |
862 | } | |
863 | ||
864 | # Generate the full filename and path for the stored rules file. | |
865 | my $rulesfile = "$dl_rules_path/$dl_rulesfile_prefix-$provider$suffix"; | |
866 | ||
577e3304 SS |
867 | # Check if the file exists. |
868 | unless (-f "$rulesfile") { | |
869 | # Abort return - nothing. | |
870 | return; | |
871 | } | |
872 | ||
788a71f5 SS |
873 | # Return the generated filename. |
874 | return $rulesfile; | |
875 | } | |
876 | ||
61b92664 SS |
877 | # |
878 | ## Tiny function to delete the stored ruleset file or tarball for a given provider. | |
879 | # | |
880 | sub drop_dl_rulesfile ($) { | |
881 | my ($provider) = @_; | |
882 | ||
883 | # Gather the full path and name of the stored rulesfile. | |
884 | my $rulesfile = &_get_dl_rulesfile($provider); | |
885 | ||
886 | # Check if the given rulesfile exists. | |
887 | if (-f $rulesfile) { | |
888 | # Delete the stored rulesfile. | |
889 | unlink($rulesfile) or die "Could not delete $rulesfile. $!\n"; | |
890 | } | |
891 | } | |
892 | ||
5b9d877d SS |
893 | # |
894 | ## Tiny function to get/generate the full path and filename for the providers oinkmaster | |
895 | ## modified sids file. | |
896 | # | |
897 | sub get_oinkmaster_provider_modified_sids_file ($) { | |
898 | my ($provider) = @_; | |
899 | ||
900 | # Generate the filename. | |
901 | my $filename = "$settingsdir/oinkmaster-$provider-modified-sids.conf"; | |
902 | ||
903 | # Return the filename. | |
904 | return $filename; | |
905 | } | |
906 | ||
d878d9c0 SS |
907 | # |
908 | ## Function to directly altering the oinkmaster provider includes file. | |
909 | ## | |
910 | ## Requires tha acition "remove" or "add" and a provider handle. | |
911 | # | |
912 | sub alter_oinkmaster_provider_includes_file ($$) { | |
913 | my ($action, $provider) = @_; | |
914 | ||
915 | # Call function to get the path and name for the given providers | |
916 | # oinkmaster modified sids file. | |
917 | my $provider_modified_sids_file = &get_oinkmaster_provider_modified_sids_file($provider); | |
918 | ||
919 | # Open the file for reading.. | |
920 | open (FILE, $oinkmaster_provider_includes_file) or die "Could not read $oinkmaster_provider_includes_file. $!\n"; | |
921 | ||
922 | # Read-in file content. | |
923 | my @lines = <FILE>; | |
924 | ||
925 | # Close file after reading. | |
926 | close(FILE); | |
927 | ||
928 | # Re-open the file for writing. | |
929 | open(FILE, ">", $oinkmaster_provider_includes_file) or die "Could not write to $oinkmaster_provider_includes_file. $!\n"; | |
930 | ||
931 | # Loop through the file content. | |
932 | foreach my $line (@lines) { | |
933 | # Remove newlines. | |
934 | chomp($line); | |
935 | ||
936 | # Skip line if we found our given provider and the action should be remove. | |
937 | next if (($line =~ /$provider/) && ($action eq "remove")); | |
938 | ||
939 | # Write the read-in line back to the file. | |
940 | print FILE "$line\n"; | |
941 | } | |
942 | ||
1d860d89 SS |
943 | # Check if the file exists and add the provider if requested. |
944 | if ((-f $provider_modified_sids_file) && ($action eq "add")) { | |
d878d9c0 SS |
945 | print FILE "include $provider_modified_sids_file\n"; |
946 | } | |
947 | ||
948 | # Close file handle. | |
949 | close(FILE); | |
950 | } | |
951 | ||
69b3156f SS |
952 | # |
953 | ## Function to read-in the given enabled or disables sids file. | |
954 | # | |
955 | sub read_enabled_disabled_sids_file($) { | |
956 | my ($file) = @_; | |
957 | ||
958 | # Temporary hash to store the sids and their state. It will be | |
959 | # returned at the end of this function. | |
960 | my %temphash; | |
961 | ||
962 | # Open the given filename. | |
963 | open(FILE, "$file") or die "Could not open $file. $!\n"; | |
964 | ||
965 | # Loop through the file. | |
966 | while(<FILE>) { | |
967 | # Remove newlines. | |
968 | chomp $_; | |
969 | ||
970 | # Skip blank lines. | |
971 | next if ($_ =~ /^\s*$/); | |
972 | ||
973 | # Skip coments. | |
974 | next if ($_ =~ /^\#/); | |
975 | ||
976 | # Splitt line into sid and state part. | |
977 | my ($state, $sid) = split(" ", $_); | |
978 | ||
979 | # Skip line if the sid is not numeric. | |
980 | next unless ($sid =~ /\d+/ ); | |
981 | ||
982 | # Check if the sid was enabled. | |
983 | if ($state eq "enablesid") { | |
984 | # Add the sid and its state as enabled to the temporary hash. | |
985 | $temphash{$sid} = "enabled"; | |
986 | # Check if the sid was disabled. | |
987 | } elsif ($state eq "disablesid") { | |
988 | # Add the sid and its state as disabled to the temporary hash. | |
989 | $temphash{$sid} = "disabled"; | |
990 | # Invalid state - skip the current sid and state. | |
991 | } else { | |
992 | next; | |
993 | } | |
994 | } | |
995 | ||
996 | # Close filehandle. | |
997 | close(FILE); | |
998 | ||
999 | # Return the hash. | |
1000 | return %temphash; | |
1001 | } | |
1002 | ||
796eea21 SS |
1003 | # |
1004 | ## Function to check if the IDS is running. | |
1005 | # | |
1006 | sub ids_is_running () { | |
1007 | if(-f $idspidfile) { | |
1008 | # Open PID file for reading. | |
1009 | open(PIDFILE, "$idspidfile") or die "Could not open $idspidfile. $!\n"; | |
1010 | ||
1011 | # Grab the process-id. | |
1012 | my $pid = <PIDFILE>; | |
1013 | ||
1014 | # Close filehandle. | |
1015 | close(PIDFILE); | |
1016 | ||
1017 | # Remove any newline. | |
1018 | chomp($pid); | |
1019 | ||
1020 | # Check if a directory for the process-id exists in proc. | |
1021 | if(-d "/proc/$pid") { | |
1022 | # The IDS daemon is running return the process id. | |
1023 | return $pid; | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | # Return nothing - IDS is not running. | |
1028 | return; | |
1029 | } | |
1030 | ||
5240a809 SS |
1031 | # |
1032 | ## Function to call suricatactrl binary with a given command. | |
1033 | # | |
1034 | sub call_suricatactrl ($) { | |
1035 | # Get called option. | |
ed06bc81 | 1036 | my ($option, $interval) = @_; |
5240a809 SS |
1037 | |
1038 | # Loop through the array of supported commands and check if | |
1039 | # the given one is part of it. | |
1040 | foreach my $cmd (@suricatactrl_cmds) { | |
1041 | # Skip current command unless the given one has been found. | |
1042 | next unless($cmd eq $option); | |
1043 | ||
ed06bc81 SS |
1044 | # Check if the given command is "cron". |
1045 | if ($option eq "cron") { | |
1046 | # Check if an interval has been given. | |
1047 | if ($interval) { | |
1048 | # Check if the given interval is valid. | |
1049 | foreach my $element (@cron_intervals) { | |
1050 | # Skip current element until the given one has been found. | |
1051 | next unless($element eq $interval); | |
1052 | ||
1053 | # Call the suricatactrl binary and pass the "cron" command | |
1054 | # with the requrested interval. | |
81631920 | 1055 | &General::system("$suricatactrl", "$option", "$interval"); |
ed06bc81 SS |
1056 | |
1057 | # Return "1" - True. | |
1058 | return 1; | |
1059 | } | |
1060 | } | |
5240a809 | 1061 | |
ed06bc81 SS |
1062 | # If we got here, the given interval is not supported or none has been given. - Return nothing. |
1063 | return; | |
1064 | } else { | |
1065 | # Call the suricatactrl binary and pass the requrested | |
1066 | # option to it. | |
81631920 | 1067 | &General::system("$suricatactrl", "$option"); |
ed06bc81 SS |
1068 | |
1069 | # Return "1" - True. | |
1070 | return 1; | |
1071 | } | |
5240a809 SS |
1072 | } |
1073 | ||
1074 | # Command not found - return nothing. | |
1075 | return; | |
1076 | } | |
1077 | ||
308ba5e7 SS |
1078 | # |
1079 | ## Function to create a new empty file. | |
1080 | # | |
1081 | sub create_empty_file($) { | |
1082 | my ($file) = @_; | |
1083 | ||
1084 | # Check if the given file exists. | |
1085 | if(-e $file) { | |
1086 | # Do nothing to prevent from overwriting existing files. | |
1087 | return; | |
1088 | } | |
1089 | ||
1090 | # Open the file for writing. | |
1091 | open(FILE, ">$file") or die "Could not write to $file. $!\n"; | |
1092 | ||
1093 | # Close file handle. | |
1094 | close(FILE); | |
1095 | ||
1096 | # Return true. | |
1097 | return 1; | |
1098 | } | |
1099 | ||
330759d8 SS |
1100 | # |
1101 | ## Private function to check if the file permission of the rulespath are correct. | |
1102 | ## If not, call suricatactrl to fix them. | |
1103 | # | |
1104 | sub _check_rulesdir_permissions() { | |
e568796b SS |
1105 | # Check if the rulepath main directory is writable. |
1106 | unless (-W $rulespath) { | |
1107 | # If not call suricatctrl to fix it. | |
1108 | &call_suricatactrl("fix-rules-dir"); | |
1109 | } | |
1110 | ||
330759d8 SS |
1111 | # Open snort rules directory and do a directory listing. |
1112 | opendir(DIR, $rulespath) or die $!; | |
1113 | # Loop through the direcory. | |
1114 | while (my $file = readdir(DIR)) { | |
1115 | # We only want files. | |
1116 | next unless (-f "$rulespath/$file"); | |
1117 | ||
1118 | # Check if the file is writable by the user. | |
1119 | if (-W "$rulespath/$file") { | |
1120 | # Everything is okay - go on to the next file. | |
1121 | next; | |
1122 | } else { | |
1123 | # There are wrong permissions, call suricatactrl to fix it. | |
1124 | &call_suricatactrl("fix-rules-dir"); | |
1125 | } | |
1126 | } | |
1127 | } | |
1128 | ||
b59cdbee SS |
1129 | # |
1130 | ## Private function to cleanup the directory which contains | |
1131 | ## the IDS rules, before extracting and modifing the new ruleset. | |
1132 | # | |
1133 | sub _cleanup_rulesdir() { | |
8cf04a16 SS |
1134 | # Open rules directory and do a directory listing. |
1135 | opendir(DIR, $rulespath) or die $!; | |
1136 | ||
1137 | # Loop through the direcory. | |
1138 | while (my $file = readdir(DIR)) { | |
1139 | # We only want files. | |
1140 | next unless (-f "$rulespath/$file"); | |
1141 | ||
52071c0e SS |
1142 | # Skip element if it has config as file extension. |
1143 | next if ($file =~ m/\.config$/); | |
1144 | ||
fefb5173 SS |
1145 | # Skip rules file for whitelisted hosts. |
1146 | next if ("$rulespath/$file" eq $whitelist_file); | |
1147 | ||
612bb2df SS |
1148 | # Skip rules file with local rules. |
1149 | next if ("$rulespath/$file" eq $local_rules_file); | |
1150 | ||
8cf04a16 | 1151 | # Delete the current processed file, if not, exit this function |
b59cdbee | 1152 | # and return an error message. |
1201c1e7 | 1153 | unlink("$rulespath/$file") or return "Could not delete $rulespath/$file. $!\n"; |
b59cdbee SS |
1154 | } |
1155 | ||
4ce42488 | 1156 | # Return nothing; |
b59cdbee SS |
1157 | return; |
1158 | } | |
1159 | ||
b02e30fd SS |
1160 | # |
1161 | ## Function to generate the file which contains the home net information. | |
1162 | # | |
1163 | sub generate_home_net_file() { | |
1164 | my %netsettings; | |
1165 | ||
1166 | # Read-in network settings. | |
1167 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
1168 | ||
1169 | # Get available network zones. | |
abffcc99 | 1170 | my @network_zones = &Network::get_available_network_zones(); |
b02e30fd SS |
1171 | |
1172 | # Temporary array to store network address and prefix of the configured | |
1173 | # networks. | |
1174 | my @networks; | |
1175 | ||
1176 | # Loop through the array of available network zones. | |
1177 | foreach my $zone (@network_zones) { | |
bcbc9897 SS |
1178 | # Check if the current processed zone is red. |
1179 | if($zone eq "red") { | |
1180 | # Grab the IP-address of the red interface. | |
1181 | my $red_address = &get_red_address(); | |
b02e30fd | 1182 | |
bcbc9897 SS |
1183 | # Check if an address has been obtained. |
1184 | if ($red_address) { | |
1185 | # Generate full network string. | |
1186 | my $red_network = join("/", $red_address, "32"); | |
b02e30fd | 1187 | |
bcbc9897 SS |
1188 | # Add the red network to the array of networks. |
1189 | push(@networks, $red_network); | |
1190 | } | |
23c0347a | 1191 | |
23c0347a SS |
1192 | # Check if the configured RED_TYPE is static. |
1193 | if ($netsettings{'RED_TYPE'} eq "STATIC") { | |
1194 | # Get configured and enabled aliases. | |
1195 | my @aliases = &get_aliases(); | |
1196 | ||
1197 | # Loop through the array. | |
1198 | foreach my $alias (@aliases) { | |
1199 | # Add "/32" prefix. | |
1200 | my $network = join("/", $alias, "32"); | |
1201 | ||
1202 | # Add the generated network to the array of networks. | |
1203 | push(@networks, $network); | |
1204 | } | |
1205 | } | |
bcbc9897 SS |
1206 | # Process remaining network zones. |
1207 | } else { | |
1208 | # Convert current zone name into upper case. | |
1209 | $zone = uc($zone); | |
1210 | ||
1211 | # Generate key to access the required data from the netsettings hash. | |
1212 | my $zone_netaddress = $zone . "_NETADDRESS"; | |
1213 | my $zone_netmask = $zone . "_NETMASK"; | |
1214 | ||
1215 | # Obtain the settings from the netsettings hash. | |
1216 | my $netaddress = $netsettings{$zone_netaddress}; | |
1217 | my $netmask = $netsettings{$zone_netmask}; | |
1218 | ||
1219 | # Convert the subnetmask into prefix notation. | |
1220 | my $prefix = &Network::convert_netmask2prefix($netmask); | |
1221 | ||
1222 | # Generate full network string. | |
1223 | my $network = join("/", $netaddress,$prefix); | |
1224 | ||
1225 | # Check if the network is valid. | |
1226 | if(&Network::check_subnet($network)) { | |
1227 | # Add the generated network to the array of networks. | |
1228 | push(@networks, $network); | |
1229 | } | |
23c0347a | 1230 | } |
b02e30fd SS |
1231 | } |
1232 | ||
1233 | # Format home net declaration. | |
7479c993 | 1234 | my $line = "\"[" . join(',', @networks) . "]\""; |
b02e30fd SS |
1235 | |
1236 | # Open file to store the addresses of the home net. | |
1237 | open(FILE, ">$homenet_file") or die "Could not open $homenet_file. $!\n"; | |
1238 | ||
1239 | # Print yaml header. | |
1240 | print FILE "%YAML 1.1\n"; | |
1241 | print FILE "---\n\n"; | |
1242 | ||
1243 | # Print notice about autogenerated file. | |
1244 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
1245 | ||
1246 | # Print the generated and required HOME_NET declaration to the file. | |
1247 | print FILE "HOME_NET:\t$line\n"; | |
1248 | ||
1249 | # Close file handle. | |
1250 | close(FILE); | |
1251 | } | |
1252 | ||
30ee98e9 SS |
1253 | # |
1254 | # Function to generate and write the file which contains the configured and used DNS servers. | |
1255 | # | |
1256 | sub generate_dns_servers_file() { | |
12c49915 SS |
1257 | # Get the used DNS servers. |
1258 | my @nameservers = &General::get_nameservers(); | |
30ee98e9 | 1259 | |
7b97359b SS |
1260 | # Get network settings. |
1261 | my %netsettings; | |
1262 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
1263 | ||
30ee98e9 | 1264 | # Format dns servers declaration. |
b76118c3 | 1265 | my $line = ""; |
30ee98e9 | 1266 | |
12c49915 SS |
1267 | # Check if the system has configured nameservers. |
1268 | if (@nameservers) { | |
7b97359b SS |
1269 | # Add the GREEN address as DNS servers. |
1270 | push(@nameservers, $netsettings{'GREEN_ADDRESS'}); | |
1271 | ||
1272 | # Check if a BLUE zone exists. | |
1273 | if ($netsettings{'BLUE_ADDRESS'}) { | |
1274 | # Add the BLUE address to the array of nameservers. | |
1275 | push(@nameservers, $netsettings{'BLUE_ADDRESS'}); | |
1276 | } | |
1277 | ||
1278 | # Generate the line which will be written to the DNS servers file. | |
b76118c3 | 1279 | $line = join(",", @nameservers); |
12c49915 | 1280 | } else { |
7b97359b | 1281 | # External net simply contains (any). |
99cadb74 | 1282 | $line = "\$EXTERNAL_NET"; |
fd2dccaa | 1283 | } |
30ee98e9 | 1284 | |
30ee98e9 SS |
1285 | # Open file to store the used DNS server addresses. |
1286 | open(FILE, ">$dns_servers_file") or die "Could not open $dns_servers_file. $!\n"; | |
1287 | ||
1288 | # Print yaml header. | |
1289 | print FILE "%YAML 1.1\n"; | |
1290 | print FILE "---\n\n"; | |
1291 | ||
1292 | # Print notice about autogenerated file. | |
1293 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
1294 | ||
1295 | # Print the generated DNS declaration to the file. | |
b76118c3 | 1296 | print FILE "DNS_SERVERS:\t\"[$line]\"\n"; |
30ee98e9 SS |
1297 | |
1298 | # Close file handle. | |
1299 | close(FILE); | |
1300 | } | |
1301 | ||
e698090e SS |
1302 | # |
1303 | # Function to generate and write the file which contains the HTTP_PORTS definition. | |
1304 | # | |
1305 | sub generate_http_ports_file() { | |
1306 | my %proxysettings; | |
1307 | ||
1308 | # Read-in proxy settings | |
1309 | &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings); | |
1310 | ||
1311 | # Check if the proxy is enabled. | |
1312 | if (( -e "${General::swroot}/proxy/enable") || (-e "${General::swroot}/proxy/enable_blue")) { | |
1313 | # Add the proxy port to the array of HTTP ports. | |
1314 | push(@http_ports, $proxysettings{'PROXY_PORT'}); | |
1315 | } | |
1316 | ||
1317 | # Check if the transparent mode of the proxy is enabled. | |
1318 | if ((-e "${General::swroot}/proxy/transparent") || (-e "${General::swroot}/proxy/transparent_blue")) { | |
1319 | # Add the transparent proxy port to the array of HTTP ports. | |
1320 | push(@http_ports, $proxysettings{'TRANSPARENT_PORT'}); | |
1321 | } | |
1322 | ||
1323 | # Format HTTP_PORTS declaration. | |
1324 | my $line = ""; | |
1325 | ||
1326 | # Generate line which will be written to the http ports file. | |
1327 | $line = join(",", @http_ports); | |
1328 | ||
1329 | # Open file to store the HTTP_PORTS. | |
1330 | open(FILE, ">$http_ports_file") or die "Could not open $http_ports_file. $!\n"; | |
1331 | ||
1332 | # Print yaml header. | |
1333 | print FILE "%YAML 1.1\n"; | |
1334 | print FILE "---\n\n"; | |
1335 | ||
1336 | # Print notice about autogenerated file. | |
1337 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
1338 | ||
1339 | # Print the generated HTTP_PORTS declaration to the file. | |
1340 | print FILE "HTTP_PORTS:\t\"[$line]\"\n"; | |
1341 | ||
1342 | # Close file handle. | |
1343 | close(FILE); | |
1344 | } | |
1345 | ||
b02e30fd | 1346 | # |
ddaf8ae1 SS |
1347 | ## Function to generate and write the file for used rulefiles file for a given provider. |
1348 | ## | |
1349 | ## The function requires as first argument a provider handle, and as second an array with files. | |
b02e30fd | 1350 | # |
ddaf8ae1 SS |
1351 | sub write_used_provider_rulefiles_file($@) { |
1352 | my ($provider, @files) = @_; | |
1353 | ||
1354 | # Get the path and file for the provider specific used rulefiles file. | |
1355 | my $used_provider_rulesfile_file = &get_used_provider_rulesfile_file($provider); | |
b02e30fd SS |
1356 | |
1357 | # Open file for used rulefiles. | |
a081f203 | 1358 | open (FILE, ">", "$used_provider_rulesfile_file") or die "Could not write to $used_provider_rulesfile_file. $!\n"; |
b02e30fd SS |
1359 | |
1360 | # Write yaml header to the file. | |
1361 | print FILE "%YAML 1.1\n"; | |
1362 | print FILE "---\n\n"; | |
1363 | ||
1364 | # Write header to file. | |
1365 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
1366 | ||
b02e30fd SS |
1367 | # Loop through the array of given files. |
1368 | foreach my $file (@files) { | |
1369 | # Check if the given filename exists and write it to the file of used rulefiles. | |
1370 | if(-f "$rulespath/$file") { | |
1371 | print FILE " - $file\n"; | |
1372 | } | |
1373 | } | |
1374 | ||
1375 | # Close file after writing. | |
1376 | close(FILE); | |
1377 | } | |
1378 | ||
ddaf8ae1 SS |
1379 | # |
1380 | ## Function to write the main file for provider rulesfiles inclusions. | |
1381 | ## | |
1382 | ## This function requires an array of provider handles. | |
1383 | # | |
1384 | sub write_main_used_rulefiles_file (@) { | |
1385 | my (@providers) = @_; | |
1386 | ||
88eb5626 SS |
1387 | # Call function to write the static rulefiles file. |
1388 | &_write_static_rulefiles_file(); | |
1389 | ||
ddaf8ae1 SS |
1390 | # Open file for used rulefils inclusion. |
1391 | open (FILE, ">", "$suricata_used_providers_file") or die "Could not write to $suricata_used_providers_file. $!\n"; | |
1392 | ||
1393 | # Write yaml header to the file. | |
1394 | print FILE "%YAML 1.1\n"; | |
1395 | print FILE "---\n\n"; | |
1396 | ||
1397 | # Write header to file. | |
1398 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
1399 | ||
1400 | # Loop through the list of given providers. | |
1401 | foreach my $provider (@providers) { | |
1402 | # Call function to get the providers used rulefiles file. | |
1403 | my $filename = &get_used_provider_rulesfile_file($provider); | |
1404 | ||
1b0e555f SS |
1405 | # Check if the file exists and write it into the used rulefiles file. |
1406 | if (-f $filename) { | |
1407 | # Print the provider to the file. | |
1408 | print FILE "include\: $filename\n"; | |
1409 | } | |
ddaf8ae1 SS |
1410 | } |
1411 | ||
88eb5626 SS |
1412 | # Always include the file which hold the static includes. |
1413 | print FILE "include\: $suricata_static_rulefiles_file\n"; | |
ddaf8ae1 SS |
1414 | |
1415 | # Close the filehandle after writing. | |
1416 | close(FILE); | |
1417 | } | |
1418 | ||
88eb5626 SS |
1419 | sub _write_static_rulefiles_file () { |
1420 | # Open file. | |
1421 | open (FILE, ">", $suricata_static_rulefiles_file) or die "Could not write to $suricata_static_rulefiles_file. $!\n"; | |
1422 | ||
1423 | # Write yaml header to the file. | |
1424 | print FILE "%YAML 1.1\n"; | |
1425 | print FILE "---\n\n"; | |
1426 | ||
1427 | # Write notice about autogenerated file. | |
1428 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
1429 | ||
1430 | # Loop through the array of static included rulesfiles. | |
1431 | foreach my $file (@static_included_rulefiles) { | |
1432 | # Check if the file exists. | |
1433 | if (-f "$rulespath/$file") { | |
1434 | # Write the rulesfile name to the file. | |
1435 | print FILE " - $file\n"; | |
1436 | } | |
1437 | } | |
1438 | ||
1439 | # Close the file handle | |
1440 | close(FILE); | |
1441 | } | |
1442 | ||
ddaf8ae1 SS |
1443 | # |
1444 | ## Tiny function to generate the full path and name for the used_provider_rulesfile file of a given provider. | |
1445 | # | |
1446 | sub get_used_provider_rulesfile_file ($) { | |
1447 | my ($provider) = @_; | |
1448 | ||
1449 | my $filename = "$settingsdir/suricata\-$provider\-used\-rulefiles.yaml"; | |
1450 | ||
1451 | # Return the gernerated file. | |
1452 | return $filename; | |
1453 | } | |
1454 | ||
74cc8f5a SS |
1455 | # |
1456 | ## Function to generate and write the file for modify the ruleset. | |
1457 | # | |
81bae51f SS |
1458 | sub write_modify_sids_file() { |
1459 | # Get configured settings. | |
1460 | my %idssettings=(); | |
81bae51f | 1461 | &General::readhash("$ids_settings_file", \%idssettings); |
74cc8f5a SS |
1462 | |
1463 | # Open modify sid's file for writing. | |
2ee51088 | 1464 | open(FILE, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n"; |
74cc8f5a SS |
1465 | |
1466 | # Write file header. | |
1467 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
1468 | ||
1469 | # Check if the traffic only should be monitored. | |
81bae51f | 1470 | unless($idssettings{'MONITOR_TRAFFIC_ONLY'} eq 'on') { |
a5ba473c TF |
1471 | # Suricata is in IPS mode, which means that the rule actions have to be changed |
1472 | # from 'alert' to 'drop', however not all rules should be changed. Some rules | |
1473 | # exist purely to set a flowbit which is used to convey other information, such | |
1474 | # as a specific type of file being downloaded, to other rulewhich then check for | |
1475 | # malware in that file. Rules which fall into the first category should stay as | |
1476 | # alert since not all flows of that type contain malware. | |
1477 | ||
aac869c4 SS |
1478 | # These types of rulesfiles contain meta-data which gives the action that should |
1479 | # be used when in IPS mode. Do the following: | |
1480 | # | |
1481 | # 1. Disable all rules and set the action to 'drop' | |
1482 | # 2. Set the action back to 'alert' if the rule contains 'flowbits:noalert;' | |
1483 | # This should give rules not in the policy a reasonable default if the user | |
1484 | # manually enables them. | |
1485 | # 3. Enable rules and set actions according to the meta-data strings. | |
a5ba473c | 1486 | |
aac869c4 | 1487 | my $policy = 'balanced'; # Placeholder to allow policy to be changed. |
a5ba473c TF |
1488 | |
1489 | print FILE <<END; | |
a5ba473c TF |
1490 | modifysid * "^#(?:alert|drop)(.+policy $policy-ips alert)" | "alert\${1}" |
1491 | modifysid * "^#(?:alert|drop)(.+policy $policy-ips drop)" | "drop\${1}" | |
a5ba473c TF |
1492 | modifysid * "^(#?)(?:alert|drop)" | "\${1}drop" |
1493 | modifysid * "^(#?)drop(.+flowbits:noalert;)" | "\${1}alert\${2}" | |
1494 | END | |
1495 | } | |
74cc8f5a SS |
1496 | |
1497 | # Close file handle. | |
1498 | close(FILE); | |
1499 | } | |
1500 | ||
f580aa8c SS |
1501 | # |
1502 | ## Function to get the ruleset date for a given provider. | |
1503 | ## | |
1504 | ## The function simply return the creation date in a human read-able format | |
1505 | ## of the stored providers rulesfile. | |
1506 | # | |
1507 | sub get_ruleset_date($) { | |
1508 | my ($provider) = @_; | |
fc685a36 | 1509 | my $date; |
c68bcbb2 | 1510 | my $mtime; |
f580aa8c SS |
1511 | |
1512 | # Load neccessary perl modules for file stat and to format the timestamp. | |
1513 | use File::stat; | |
1514 | use POSIX qw( strftime ); | |
1515 | ||
1516 | # Get the stored rulesfile for this provider. | |
1517 | my $stored_rulesfile = &_get_dl_rulesfile($provider); | |
1518 | ||
c68bcbb2 SS |
1519 | # Check if we got a file. |
1520 | if ($stored_rulesfile) { | |
1521 | # Call stat on the rulestarball. | |
1522 | my $stat = stat("$stored_rulesfile"); | |
f580aa8c | 1523 | |
c68bcbb2 SS |
1524 | # Get timestamp the file creation. |
1525 | $mtime = $stat->mtime; | |
1526 | } | |
f580aa8c | 1527 | |
057e8953 SS |
1528 | # Check if the timestamp has not been grabbed. |
1529 | unless ($mtime) { | |
1530 | # Return N/A for Not available. | |
1531 | return "N/A"; | |
1532 | } | |
1533 | ||
f580aa8c | 1534 | # Convert into human read-able format. |
fc685a36 SS |
1535 | $date = strftime('%Y-%m-%d %H:%M:%S', localtime($mtime)); |
1536 | ||
f580aa8c SS |
1537 | # Return the date. |
1538 | return $date; | |
1539 | } | |
1540 | ||
04a0d07c SS |
1541 | # |
1542 | ## Function to gather the version of suricata. | |
1543 | # | |
1544 | sub get_suricata_version($) { | |
1545 | my ($format) = @_; | |
1546 | ||
1547 | # Execute piped suricata command and return the version information. | |
1548 | open(SURICATA, "suricata -V |") or die "Couldn't execute program: $!"; | |
1549 | ||
1550 | # Grab and store the output of the piped program. | |
1551 | my $version_string = <SURICATA>; | |
1552 | ||
1553 | # Close pipe. | |
1554 | close(SURICATA); | |
1555 | ||
1556 | # Remove newlines. | |
1557 | chomp($version_string); | |
1558 | ||
fd2dccaa | 1559 | # Grab the version from the version string. |
04a0d07c SS |
1560 | $version_string =~ /([0-9]+([.][0-9]+)+)/; |
1561 | ||
1562 | # Splitt the version into single chunks. | |
1563 | my ($major_ver, $minor_ver, $patchlevel) = split(/\./, $1); | |
1564 | ||
1565 | # Check and return the requested version sheme. | |
1566 | if ($format eq "major") { | |
1567 | # Return the full version. | |
1568 | return "$major_ver"; | |
1569 | } elsif ($format eq "minor") { | |
1570 | # Return the major and minor part. | |
1571 | return "$major_ver.$minor_ver"; | |
1572 | } else { | |
1573 | # Return the full version string. | |
1574 | return "$major_ver.$minor_ver.$patchlevel"; | |
fd2dccaa | 1575 | } |
04a0d07c SS |
1576 | } |
1577 | ||
9283e9b9 SS |
1578 | # |
1579 | ## Function to generate the rules file with whitelisted addresses. | |
1580 | # | |
1581 | sub generate_ignore_file() { | |
1582 | my %ignored = (); | |
1583 | ||
1584 | # SID range 1000000-1999999 Reserved for Local Use | |
1585 | # Put your custom rules in this range to avoid conflicts | |
1586 | my $sid = 1500000; | |
1587 | ||
1588 | # Read-in ignoredfile. | |
1589 | &General::readhasharray($IDS::ignored_file, \%ignored); | |
1590 | ||
1591 | # Open ignorefile for writing. | |
1592 | open(FILE, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n"; | |
1593 | ||
1594 | # Config file header. | |
1595 | print FILE "# Autogenerated file.\n"; | |
1596 | print FILE "# All user modifications will be overwritten.\n\n"; | |
1597 | ||
1598 | # Add all user defined addresses to the whitelist. | |
1599 | # | |
1600 | # Check if the hash contains any elements. | |
1601 | if (keys (%ignored)) { | |
1602 | # Loop through the entire hash and write the host/network | |
1603 | # and remark to the ignore file. | |
1604 | while ( (my $key) = each %ignored) { | |
1605 | my $address = $ignored{$key}[0]; | |
1606 | my $remark = $ignored{$key}[1]; | |
1607 | my $status = $ignored{$key}[2]; | |
1608 | ||
1609 | # Check if the status of the entry is "enabled". | |
1610 | if ($status eq "enabled") { | |
1611 | # Check if the address/network is valid. | |
1612 | if ((&General::validip($address)) || (&General::validipandmask($address))) { | |
1613 | # Write rule line to the file to pass any traffic from this IP | |
1614 | print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n"; | |
1615 | ||
1616 | # Increment sid. | |
1617 | $sid++; | |
1618 | } | |
1619 | } | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | close(FILE); | |
1624 | } | |
1625 | ||
1fedede6 SS |
1626 | # |
1627 | ## Function to set correct ownership for single files and directories. | |
1628 | # | |
1629 | ||
1630 | sub set_ownership($) { | |
1631 | my ($target) = @_; | |
1632 | ||
1633 | # User and group of the WUI. | |
1634 | my $uname = "nobody"; | |
1635 | my $grname = "nobody"; | |
1636 | ||
1637 | # The chown function implemented in perl requies the user and group as nummeric id's. | |
1638 | my $uid = getpwnam($uname); | |
1639 | my $gid = getgrnam($grname); | |
1640 | ||
1641 | # Check if the given target exists. | |
1642 | unless ($target) { | |
1643 | # Stop the script and print error message. | |
1644 | die "The $target does not exist. Cannot change the ownership!\n"; | |
1645 | } | |
1646 | ||
1647 | # Check weather the target is a file or directory. | |
1648 | if (-f $target) { | |
1649 | # Change ownership ot the single file. | |
1650 | chown($uid, $gid, "$target"); | |
1651 | } elsif (-d $target) { | |
1652 | # Do a directory listing. | |
1653 | opendir(DIR, $target) or die $!; | |
1654 | # Loop through the direcory. | |
1655 | while (my $file = readdir(DIR)) { | |
1656 | ||
1657 | # We only want files. | |
1658 | next unless (-f "$target/$file"); | |
1659 | ||
1660 | # Set correct ownership for the files. | |
1661 | chown($uid, $gid, "$target/$file"); | |
1662 | } | |
1663 | ||
1664 | closedir(DIR); | |
1665 | ||
1666 | # Change ownership of the directory. | |
1667 | chown($uid, $gid, "$target"); | |
1668 | } | |
1669 | } | |
77c31301 SS |
1670 | |
1671 | # | |
1672 | ## Function to read-in the aliases file and returns all configured and enabled aliases. | |
1673 | # | |
1674 | sub get_aliases() { | |
1675 | # Location of the aliases file. | |
1676 | my $aliases_file = "${General::swroot}/ethernet/aliases"; | |
1677 | ||
1678 | # Array to store the aliases. | |
1679 | my @aliases; | |
1680 | ||
1681 | # Check if the file is empty. | |
1682 | if (-z $aliases_file) { | |
1683 | # Abort nothing to do. | |
1684 | return; | |
1685 | } | |
1686 | ||
1687 | # Open the aliases file. | |
1688 | open(ALIASES, $aliases_file) or die "Could not open $aliases_file. $!\n"; | |
1689 | ||
1690 | # Loop through the file content. | |
1691 | while (my $line = <ALIASES>) { | |
1692 | # Remove newlines. | |
1693 | chomp($line); | |
1694 | ||
1695 | # Splitt line content into single chunks. | |
1696 | my ($address, $state, $remark) = split(/\,/, $line); | |
1697 | ||
1698 | # Check if the state of the current processed alias is "on". | |
1699 | if ($state eq "on") { | |
1700 | # Check if the address is valid. | |
1701 | if(&Network::check_ip_address($address)) { | |
1702 | # Add the alias to the array of aliases. | |
1703 | push(@aliases, $address); | |
1704 | } | |
1705 | } | |
1706 | } | |
1707 | ||
1708 | # Close file handle. | |
1709 | close(ALIASES); | |
1710 | ||
1711 | # Return the array. | |
1712 | return @aliases; | |
1713 | } | |
1714 | ||
de8e1e5b SS |
1715 | # |
1716 | ## Function to grab the current assigned IP-address on red. | |
1717 | # | |
1718 | sub get_red_address() { | |
1719 | # File, which contains the current IP-address of the red interface. | |
1720 | my $file = "${General::swroot}/red/local-ipaddress"; | |
1721 | ||
1722 | # Check if the file exists. | |
1723 | if (-e $file) { | |
1724 | # Open the given file. | |
1725 | open(FILE, "$file") or die "Could not open $file."; | |
1726 | ||
1727 | # Obtain the address from the first line of the file. | |
1728 | my $address = <FILE>; | |
1729 | ||
1730 | # Close filehandle | |
1731 | close(FILE); | |
1732 | ||
1733 | # Remove newlines. | |
1734 | chomp $address; | |
1735 | ||
1736 | # Check if the grabbed address is valid. | |
1737 | if (&General::validip($address)) { | |
1738 | # Return the address. | |
1739 | return $address; | |
1740 | } | |
1741 | } | |
1742 | ||
1743 | # Return nothing. | |
1744 | return; | |
1745 | } | |
77c31301 | 1746 | |
6563d449 | 1747 | # |
ddaf8ae1 | 1748 | ## Function to get the used rules files of a given provider. |
6563d449 | 1749 | # |
ddaf8ae1 SS |
1750 | sub read_used_provider_rulesfiles($) { |
1751 | my ($provider) = @_; | |
1752 | ||
6563d449 SS |
1753 | # Array to store the used rulefiles. |
1754 | my @used_rulesfiles = (); | |
1755 | ||
ddaf8ae1 SS |
1756 | # Get the used rulesefile file for the provider. |
1757 | my $rulesfile_file = &get_used_provider_rulesfile_file($provider); | |
1758 | ||
01fc880c SS |
1759 | # Check if the a used rulesfile exists for this provider. |
1760 | if (-f $rulesfile_file) { | |
6563d449 | 1761 | # Open the file or used rulefiles and read-in content. |
ddaf8ae1 | 1762 | open(FILE, $rulesfile_file) or die "Could not open $rulesfile_file. $!\n"; |
6563d449 SS |
1763 | |
1764 | while (<FILE>) { | |
1765 | # Assign the current line to a nice variable. | |
1766 | my $line = $_; | |
1767 | ||
1768 | # Remove newlines. | |
1769 | chomp($line); | |
1770 | ||
1771 | # Skip comments. | |
1772 | next if ($line =~ /\#/); | |
1773 | ||
1774 | # Skip blank lines. | |
1775 | next if ($line =~ /^\s*$/); | |
1776 | ||
1777 | # Gather the rulefile. | |
1778 | if ($line =~ /.*- (.*)/) { | |
1779 | my $rulefile = $1; | |
1780 | ||
1781 | # Add the rulefile to the array of used rulesfiles. | |
1782 | push(@used_rulesfiles, $rulefile); | |
1783 | } | |
1784 | } | |
1785 | ||
1786 | # Close the file. | |
1787 | close(FILE); | |
1788 | } | |
1789 | ||
1790 | # Return the array of used rulesfiles. | |
1791 | return @used_rulesfiles; | |
1792 | } | |
1793 | ||
8076deba SS |
1794 | # |
1795 | ## Function to write the lock file for locking the WUI, while | |
1796 | ## the autoupdate script runs. | |
1797 | # | |
1798 | sub lock_ids_page() { | |
1799 | # Call subfunction to create the file. | |
1800 | &create_empty_file($ids_page_lock_file); | |
1801 | } | |
1802 | ||
1803 | # | |
1804 | ## Function to release the lock of the WUI, again. | |
1805 | # | |
1806 | sub unlock_ids_page() { | |
1807 | # Delete lock file. | |
1808 | unlink($ids_page_lock_file); | |
1809 | } | |
1810 | ||
8dcebe53 | 1811 | 1; |