]>
Commit | Line | Data |
---|---|---|
82979dec SS |
1 | #!/usr/bin/perl |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
66c36198 | 5 | # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> # |
82979dec SS |
6 | # # |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | use strict; | |
72ab7196 | 23 | use POSIX; |
82979dec SS |
24 | |
25 | require '/var/ipfire/general-functions.pl'; | |
26 | require "${General::swroot}/ids-functions.pl"; | |
27 | require "${General::swroot}/lang.pl"; | |
28 | ||
d1f75426 SS |
29 | # Load perl module to talk to the kernel syslog. |
30 | use Sys::Syslog qw(:DEFAULT setlogsock); | |
31 | ||
a956712e SS |
32 | # Variable to store if the process has written a lockfile. |
33 | my $locked; | |
34 | ||
6875f9ce SS |
35 | # Hash to store the configured providers. |
36 | my %providers = (); | |
37 | ||
72ab7196 SS |
38 | # The user and group name as which this script should be run. |
39 | my $run_as = 'nobody'; | |
40 | ||
41 | # Get user and group id of the user. | |
42 | my ( $uid, $gid ) = ( getpwnam $run_as )[ 2, 3 ]; | |
43 | ||
44 | # Check if the script currently runs as root. | |
45 | if ( $> == 0 ) { | |
46 | # Drop privileges and switch to the specified user and group. | |
47 | POSIX::setgid( $gid ); | |
48 | POSIX::setuid( $uid ); | |
49 | } | |
50 | ||
d1f75426 SS |
51 | # Establish the connection to the syslog service. |
52 | openlog('oinkmaster', 'cons,pid', 'user'); | |
53 | ||
27671216 SS |
54 | # Check if the IDS lock file exists. |
55 | # In this case the WUI or another instance currently is altering the | |
56 | # ruleset. | |
57 | if (-f "$IDS::ids_page_lock_file") { | |
58 | # Store notice to the syslog. | |
c9c3eadb | 59 | &_log_to_syslog("<INFO> Autoupdate skipped - Another process was altering the IDS ruleset."); |
27671216 SS |
60 | |
61 | # Exit. | |
62 | exit 0; | |
63 | } | |
64 | ||
82979dec SS |
65 | # Check if the red device is active. |
66 | unless (-e "${General::swroot}/red/active") { | |
67 | # Store notice in the syslog. | |
9a3f9c2b | 68 | &_log_to_syslog("<ERROR> Could not update any ruleset - The system is offline."); |
82979dec SS |
69 | |
70 | # Exit. | |
71 | exit 0; | |
72 | } | |
73 | ||
74 | # Check if enought free disk space is availabe. | |
9a3f9c2b SS |
75 | my $return = &IDS::checkdiskspace(); |
76 | ||
77 | # Handle error. | |
78 | if ($return) { | |
79 | # Store error in syslog. | |
80 | &_log_to_syslog("<ERROR> Not enough free disk space, only $return of 300MB are available."); | |
82979dec SS |
81 | |
82 | # Exit. | |
83 | exit 0; | |
84 | } | |
85 | ||
5206a335 SS |
86 | # Lock the IDS page. |
87 | &IDS::lock_ids_page(); | |
88 | ||
a956712e SS |
89 | # The script has requested a lock, so set locket to "1". |
90 | $locked = "1"; | |
91 | ||
6875f9ce SS |
92 | # Grab the configured providers. |
93 | &General::readhasharray("$IDS::providers_settings_file", \%providers); | |
82979dec | 94 | |
6875f9ce SS |
95 | # Loop through the array of available providers. |
96 | foreach my $id (keys %providers) { | |
97 | # Assign some nice variabled. | |
98 | my $provider = $providers{$id}[0]; | |
74019d30 | 99 | my $enabled_status = $providers{$id}[2]; |
6875f9ce | 100 | my $autoupdate_status = $providers{$id}[3]; |
84227f7a | 101 | |
74019d30 SS |
102 | # Skip the provider if it is not enabled. |
103 | next unless($enabled_status eq "enabled"); | |
104 | ||
6875f9ce SS |
105 | # Skip the provider if autoupdate is not enabled. |
106 | next unless($autoupdate_status eq "enabled"); | |
82979dec | 107 | |
c9c3eadb SS |
108 | # Log notice about update. |
109 | &_log_to_syslog("<INFO> Performing update for $provider."); | |
6875f9ce | 110 | |
c9c3eadb SS |
111 | # Call the download function and gather the new ruleset for the current processed provider. |
112 | my $return = &IDS::downloadruleset($provider); | |
113 | ||
114 | # Check if we got a return code. | |
115 | if ($return) { | |
116 | # Handle different return codes. | |
ff780d8b | 117 | if ($return eq "not modified") { |
c9c3eadb SS |
118 | # Log notice to syslog. |
119 | &_log_to_syslog("<INFO> Skipping $provider - The ruleset is up-to-date"); | |
120 | ||
121 | } elsif ($return eq "no url") { | |
122 | # Log error to the syslog. | |
123 | &_log_to_syslog("<ERROR> Could not determine a download URL for $provider."); | |
124 | ||
125 | } else { | |
126 | # Log error to syslog. | |
127 | &_log_to_syslog("<ERROR> $return"); | |
128 | } | |
129 | } else { | |
130 | # Log successfull update. | |
131 | &_log_to_syslog("<INFO> Successfully updated ruleset for $provider."); | |
132 | ||
133 | # Get path and name of the stored rules file or archive. | |
134 | my $stored_file = &IDS::_get_dl_rulesfile($provider); | |
135 | ||
136 | # Set correct ownership for the downloaded tarball. | |
137 | &IDS::set_ownership("$stored_file"); | |
6875f9ce | 138 | } |
6875f9ce | 139 | } |
50b35e0f | 140 | |
82979dec SS |
141 | # Call oinkmaster to alter the ruleset. |
142 | &IDS::oinkmaster(); | |
143 | ||
ca8c9210 SS |
144 | # Set correct ownership for the rulesdir and files. |
145 | &IDS::set_ownership("$IDS::rulespath"); | |
146 | ||
82979dec SS |
147 | # Check if the IDS is running. |
148 | if(&IDS::ids_is_running()) { | |
149 | # Call suricatactrl to perform a reload. | |
150 | &IDS::call_suricatactrl("reload"); | |
151 | } | |
152 | ||
d1f75426 SS |
153 | # |
154 | # Tiny function to sent the error message to the syslog. | |
155 | # | |
156 | sub _log_to_syslog($) { | |
157 | my ($message) = @_; | |
158 | ||
159 | # The syslog function works best with an array based input, | |
160 | # so generate one before passing the message details to syslog. | |
161 | my @syslog = ("ERR", "$message"); | |
162 | ||
163 | # Send the log message. | |
164 | syslog(@syslog); | |
165 | } | |
166 | ||
a956712e | 167 | END { |
d1f75426 SS |
168 | # Close connection to syslog. |
169 | closelog(); | |
170 | ||
a956712e SS |
171 | # Check if a lock has been requested. |
172 | if ($locked) { | |
173 | # Unlock the IDS page. | |
174 | &IDS::unlock_ids_page(); | |
175 | } | |
176 | } | |
177 | ||
82979dec | 178 | 1; |