]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/aliases.cgi
suricata: Change midstream policy to "pass-flow"
[ipfire-2.x.git] / html / cgi-bin / aliases.cgi
CommitLineData
28b7d78a
CS
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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# this cgi is base on IPCop CGI - aliases.cgi
23#
24
25# to fully troubleshot your code, uncomment diagnostics, Carp and cluck lines
26#use diagnostics; # need to add the file /usr/lib/perl5/5.8.x/pods/perldiag.pod before to work
27# next look at /var/log/httpd/error_log , http://www.perl.com/pub/a/2002/05/07/mod_perl.html may help
28#use warnings;
29use strict;
30#use Carp ();
31#local $SIG{__WARN__} = \&Carp::cluck;
32
33require '/var/ipfire/general-functions.pl'; # replace /var/ipcop with /var/ipcop in case of manual install
34require "${General::swroot}/lang.pl";
35require "${General::swroot}/header.pl";
f6eb1a40 36require "${General::swroot}/ids-functions.pl";
6395bed8 37require "${General::swroot}/network-functions.pl";
28b7d78a 38
747d030a
AM
39my $configfwdfw = "${General::swroot}/firewall/config";
40my $configinput = "${General::swroot}/firewall/input";
41my $configoutgoing = "${General::swroot}/firewall/outgoing";
42my %input=();
43my %forward=();
44my %output=();
45
28b7d78a
CS
46#workaround to suppress a warning when a variable is used only once
47my @dummy = ( ${Header::colouryellow} );
48 @dummy = ( ${Header::table1colour} );
49 @dummy = ( ${Header::table2colour} );
50undef (@dummy);
51
52# Files used
53my $setting = "${General::swroot}/ethernet/settings";
54our $datafile = "${General::swroot}/ethernet/aliases";
55
6395bed8
MT
56# Fetch the name of the main RED interface
57my $RED_INTERFACE = &General::get_red_interface();
58
59# Fetch all RED interfaces
60my @RED_INTERFACES = &Network::get_red_interfaces();
28b7d78a
CS
61
62our %settings=();
63#Settings1
64
65#Settings2 for editing the multi-line list
66#Must not be saved !
67$settings{'IP'} = '';
68$settings{'ENABLED'} = 'off'; # Every check box must be set to off
69$settings{'NAME'} = '';
6395bed8
MT
70$settings{'INTERFACE'} = '';
71my @nosaved=('IP','ENABLED','NAME','INTERFACE'); # List here ALL setting2 fields. Mandatory
66c36198 72
28b7d78a
CS
73$settings{'ACTION'} = ''; # add/edit/remove
74$settings{'KEY1'} = ''; # point record for ACTION
75
76#Define each field that can be used to sort columns
77my $sortstring='^IP|^NAME';
78my $errormessage = '';
79my $warnmessage = '';
80
81&Header::showhttpheaders();
82
83# Read needed Ipcop netsettings
84my %netsettings=();
85$netsettings{'SORT_ALIASES'} = 'NAME'; # default sort
86&General::readhash($setting, \%netsettings);
87
88#Get GUI values
89&Header::getcgihash(\%settings);
90
91# Load multiline data
92our @current = ();
93if (open(FILE, "$datafile")) {
94 @current = <FILE>;
95 close (FILE);
96}
97
98#
99# Check Settings1 first because they are needed before working on @current
100#
101# Remove if no Setting1 needed
102#
103if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
66c36198 104
28b7d78a
CS
105 #
106 #Validate static Settings1 here
107 #
28b7d78a 108 unless ($errormessage) { # Everything is ok, save settings
66c36198 109 #map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
28b7d78a
CS
110 #&General::writehash($setting, \%settings); # Save good settings
111 #$settings{'ACTION'} = $Lang::tr{'save'}; # Recreate 'ACTION'
112 #map ($settings{$_}= '',(@nosaved,'KEY1')); # and reinit var to empty
66c36198 113
28b7d78a
CS
114 # Rebuild configuration file if needed
115 &BuildConfiguration;
f6eb1a40
SS
116
117 # Handle suricata related actions.
118 &HandleSuricata();
28b7d78a
CS
119 }
120
121 ERROR: # Leave the faulty field untouched
122} else {
123 #&General::readhash($setting, \%settings); # Get saved settings and reset to good if needed
124}
125
126## Now manipulate the multi-line list with Settings2
127# Basic actions are:
128# toggle the check box
129# add/update a new line
130# begin editing a line
131# remove a line
132
133
134# Toggle enable/disable field. Field is in second position
135if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
136 #move out new line
137 chomp(@current[$settings{'KEY1'}]);
138 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
139 $temp[1] = $temp[1] eq 'on' ? 'off' : 'on'; # Toggle the field
140 $temp[2] = '' if ( $temp[2] eq '' );
141 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
142 $settings{'KEY1'} = ''; # End edit mode
66c36198 143
28b7d78a 144 &General::log($Lang::tr{'ip alias changed'});
66c36198 145
28b7d78a
CS
146 #Save current
147 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
148 print FILE @current;
149 close(FILE);
66c36198 150
28b7d78a
CS
151 # Rebuild configuration file
152 &BuildConfiguration;
f6eb1a40
SS
153
154 # Handle Suricata related actions.
155 &HandleSuricata();
28b7d78a
CS
156}
157
158if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
159 # Validate inputs
160 if (! &General::validip($settings{'IP'})) {$errormessage = "invalid ip"};
161 $settings{'NAME'} = &Header::cleanhtml($settings{'NAME'});
162
163 # Make sure we haven't duplicated an alias or RED
164 my $spacer='';
165 if ($settings{'IP'} eq $netsettings{'RED_ADDRESS'}) {
166 $errormessage = $Lang::tr{'duplicate ip'} . ' (RED)';
167 $spacer=" & ";
168 }
747d030a
AM
169 #Check if we have an emtpy name
170 if (!$settings{'NAME'}){
171 $errormessage=$Lang::tr{'fwhost err name1'};
172 }elsif(! &General::validfqdn($settings{'NAME'}) && ! &General::validhostname($settings{'NAME'})){
173 $errormessage=$Lang::tr{'invalid hostname'};
174 }
28b7d78a
CS
175 my $idx=0;
176 foreach my $line (@current) {
177 chomp ($line);
178 my @temp = split (/\,/, $line);
179 if ( ($settings{'KEY1'} eq '')||(($settings{'KEY1'} ne '') && ($settings{'KEY1'} != $idx))) { # update
180 if ($temp[0] eq $settings{'IP'}) {
181 $errormessage .= $spacer.$Lang::tr{'duplicate ip'};
182 $spacer=" & ";
183 }
184 if ($temp[2] eq $settings{'NAME'} && $temp[2] ne '') {
185 $errormessage .= $spacer.$Lang::tr{'duplicate name'};
186 $spacer=" & ";
187 }
188 }
189 $idx++;
190 }
747d030a
AM
191 #Update firewallrules if aliasname is changed
192 if ($settings{'OLDNAME'} ne $settings {'NAME'}){
193 &General::readhasharray("$configfwdfw", \%forward);
194 &General::readhasharray("$configinput", \%input);
195 &General::readhasharray("$configoutgoing", \%output);
196 #Check FORWARD
197 foreach my $forwardkey (sort keys %forward){
198 if ($forward{$forwardkey}[29] eq $settings{'OLDNAME'}){
199 $forward{$forwardkey}[29] = $settings {'NAME'};
200 }
201 }
202 &General::writehasharray($configfwdfw, \%forward);
203 #Check INPUT
204 foreach my $inputkey (sort keys %input){
205 if ($input{$inputkey}[6] eq $settings{'OLDNAME'}){
206 $input{$inputkey}[6] = $settings {'NAME'};
207 }
208 }
209 &General::writehasharray($configinput, \%input);
210 #Check OUTPUT
211 foreach my $outputkey (sort keys %output){
212 if ($output{$outputkey}[4] eq $settings{'OLDNAME'}){
213 $output{$outputkey}[4] = $settings {'NAME'};
214 }
215 }
216 &General::writehasharray($configoutgoing, \%output);
217 &General::firewall_config_changed;
218 }
219 #If Alias IP has changed, set firewall_config_changed
220 if($settings{'OLDIP'} ne $settings{'IP'} && $settings{'OLDIP'}){
221 &General::firewall_config_changed;
222 }
28b7d78a
CS
223 unless ($errormessage) {
224 if ($settings{'KEY1'} eq '') { #add or edit ?
6395bed8 225 unshift (@current, "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'},$settings{'INTERFACE'}\n");
28b7d78a
CS
226 &General::log($Lang::tr{'ip alias added'});
227 } else {
6395bed8 228 @current[$settings{'KEY1'}] = "$settings{'IP'},$settings{'ENABLED'},$settings{'NAME'},$settings{'INTERFACE'}\n";
28b7d78a
CS
229 $settings{'KEY1'} = ''; # End edit mode
230 &General::log($Lang::tr{'ip alias changed'});
231 }
232
233 # Write changes to config file.
234 &SortDataFile; # sort newly added/modified entry
235
236 &BuildConfiguration; # then re-build conf which use new data
f6eb1a40
SS
237
238 # Handle Suricata related actions.
239 &HandleSuricata();
66c36198 240
28b7d78a
CS
241##
242## if entering data line is repetitive, choose here to not erase fields between each addition
243##
244 map ($settings{$_}='' ,@nosaved); # Clear fields
245 }
246}
247
248if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
249 #move out new line
250 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
251 chomp($line);
252 my @temp = split(/\,/, $line);
253
254##
255## move data fields to Setting2 for edition
256##
257 $settings{'IP'}=$temp[0]; # Prepare the screen for editing
258 $settings{'ENABLED'}=$temp[1];
259 $settings{'NAME'}=$temp[2];
6395bed8 260 $settings{'INTERFACE'}=$temp[3];
28b7d78a
CS
261}
262
263if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
66c36198 264 splice (@current,$settings{'KEY1'},1); # Delete line
28b7d78a
CS
265 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
266 print FILE @current;
267 close(FILE);
268 $settings{'KEY1'} = ''; # End remove mode
269 &General::log($Lang::tr{'ip alias removed'});
270
271 &BuildConfiguration; # then re-build conf which use new data
f6eb1a40
SS
272
273 # Handle Suricata related actions.
274 &HandleSuricata();
28b7d78a
CS
275}
276
277
278
279## Check if sorting is asked
280# If same column clicked, reverse the sort.
281if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
282 my $newsort=$ENV{'QUERY_STRING'};
283 my $actual=$netsettings{'SORT_ALIASES'};
284 #Reverse actual sort ?
285 if ($actual =~ $newsort) {
286 my $Rev='';
287 if ($actual !~ 'Rev') {
288 $Rev='Rev';
289 }
290 $newsort.=$Rev;
291 }
292 $netsettings{'SORT_ALIASES'}=$newsort;
293 &General::writehash($setting, \%netsettings);
294 &SortDataFile;
295 $settings{'ACTION'} = 'SORT'; # Recreate 'ACTION'
296}
297
298# Default initial value
299if ($settings{'ACTION'} eq '' ) { # First launch from GUI
300 $settings{'ENABLED'} ='on';
301}
66c36198 302
28b7d78a
CS
303&Header::openpage($Lang::tr{'external aliases configuration'}, 1, '');
304&Header::openbigbox('100%', 'left', '', $errormessage);
305my %checked =(); # Checkbox manipulations
6395bed8 306my %selected = ();
28b7d78a
CS
307
308if ($errormessage) {
309 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
d7fe5d79 310 print "$errormessage&nbsp;";
28b7d78a
CS
311 &Header::closebox();
312}
313unless (( $netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ ) && ($netsettings{'RED_TYPE'} eq 'STATIC'))
314{
315 &Header::openbox('100%', 'left', $Lang::tr{'capswarning'});
316 print <<END
d7fe5d79 317 <table style='width:100%;'>
28b7d78a 318 <tr>
d7fe5d79 319 <td class='boldbase' style='color:${Header::colourred};'><b>$Lang::tr{'aliases not active'}</b></td>
28b7d78a
CS
320 </tr>
321 </table>
322END
323;
324 &Header::closebox();
325}
66c36198 326
28b7d78a
CS
327#
328# Second check box is for editing the list
329#
330$checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq 'on') ? "checked='checked'" : '' ;
331
6395bed8
MT
332$selected{'INTERFACE'} = ();
333foreach my $intf (@RED_INTERFACES) {
334 $selected{'INTERFACE'}{$intf} = ($settings{'INTERFACE'} eq $intf) ? "selected" : "";
335}
336
28b7d78a
CS
337my $buttontext = $Lang::tr{'add'};
338if ($settings{'KEY1'} ne '') {
339 $buttontext = $Lang::tr{'update'};
340 &Header::openbox('100%', 'left', $Lang::tr{'edit an existing alias'});
341} else {
342 &Header::openbox('100%', 'left', $Lang::tr{'add new alias'});
343}
344
345#Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
6395bed8 346print <<END;
28b7d78a
CS
347<form method='post' action='$ENV{'SCRIPT_NAME'}'>
348<input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
747d030a
AM
349<input type='hidden' name='OLDNAME' value='$settings{'NAME'}' />
350<input type='hidden' name='OLDIP' value='$settings{'IP'}' />
d7fe5d79 351<table style='width:100%;'>
28b7d78a 352<tr>
48fde0b6 353<td class='base' style='color:${Header::colourred};'>$Lang::tr{'name'}:</td>
28b7d78a 354<td><input type='text' name='NAME' value='$settings{'NAME'}' size='32' /></td>
d7fe5d79 355<td class='base' style='text-align:right; color:${Header::colourred};'>$Lang::tr{'alias ip'}:&nbsp;</td>
28b7d78a 356<td><input type='text' name='IP' value='$settings{'IP'}' size='16' /></td>
6395bed8
MT
357END
358
359if (scalar @RED_INTERFACES >= 2) {
360 print <<END;
361 <td class='base' style='color:${Header::colourred};'>$Lang::tr{'interface'}:</td>
362 <td>
363 <select name="INTERFACE">
364 <option value="">$Lang::tr{'aliases default interface'}</option>
365END
366
367 # Print an option for each RED interface
368 foreach my $intf (@RED_INTERFACES) {
369 # Skip the default one
370 next if ($RED_INTERFACE eq $intf);
371
372 print <<END;
373 <option value="$intf" $selected{'INTERFACE'}{$intf}>$intf</option>
374END
375 }
376
377 print <<END;
378 </select>
379 </td>
380END
381}
382
383print <<END;
d7fe5d79 384<td class='base' style='text-align:right;'>$Lang::tr{'enabled'}&nbsp;</td>
28b7d78a
CS
385<td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
386</tr>
387</table>
bfe4320c 388<br>
28b7d78a 389<hr />
d7fe5d79 390<table style='width:100%;'>
28b7d78a 391<tr>
d7fe5d79 392 <td style='text-align:right;'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
28b7d78a
CS
393</tr>
394</table>
395</form>
396END
6395bed8 397
28b7d78a
CS
398&Header::closebox();
399
400# Add visual indicators to column headings to show sort order - EO
401my $sortarrow1 = '';
402my $sortarrow2 = '';
403
404if ($netsettings{'SORT_ALIASES'} eq 'NAMERev') {
405 $sortarrow1 = $Header::sortdn;
406} elsif ($netsettings{'SORT_ALIASES'} eq 'NAME') {
407 $sortarrow1 = $Header::sortup;
408} elsif ($netsettings{'SORT_ALIASES'} eq 'IPRev') {
409 $sortarrow2 = $Header::sortdn;
410} else {
411 $sortarrow2 = $Header::sortup;
412}
413
414#
415# Third box shows the list, in columns
416#
417# Columns headers may content a link. In this case it must be named in $sortstring
418#
419&Header::openbox('100%', 'left', $Lang::tr{'current aliases'});
420print <<END
d7fe5d79 421<table class='tbl' style='width:100%;'>
28b7d78a 422<tr>
d7fe5d79
AH
423 <th style='width:55%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?NAME'><b>$Lang::tr{'name'}</b></a> $sortarrow1</th>
424 <th style='width:45%; text-align:center;'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'alias ip'}</b></a> $sortarrow2</th>
425 <th colspan='3' class='boldbase' style='width:5%; text-align:center;'><b>$Lang::tr{'action'}</b></th>
28b7d78a
CS
426</tr>
427END
428;
429
430#
431# Print each line of @current list
432#
433# each data line is splitted into @temp.
434#
435
436my $key = 0;
c7edc1c7 437my $col="";
28b7d78a
CS
438foreach my $line (@current) {
439 chomp($line);
440 my @temp = split(/\,/,$line);
441
442 #Choose icon for checkbox
443 my $gif = '';
444 my $gdesc = '';
445 if ($temp[1] eq "on") {
446 $gif = 'on.gif';
447 $gdesc = $Lang::tr{'click to disable'};
448 } else {
449 $gif = 'off.gif';
66c36198 450 $gdesc = $Lang::tr{'click to enable'};
28b7d78a
CS
451 }
452
453 #Colorize each line
454 if ($settings{'KEY1'} eq $key) {
d7fe5d79 455 $col="background-color:${Header::colouryellow};";
28b7d78a 456 } elsif ($key % 2) {
d7fe5d79 457 $col="background-color:${Header::table2colour};";
28b7d78a 458 } else {
d7fe5d79 459 $col="background-color:${Header::table1colour};";
28b7d78a 460 }
d7fe5d79 461 print "<tr style='$col'>";
28b7d78a 462
6395bed8
MT
463 my $address = $temp[0];
464
465 if ($temp[3] ne "") {
466 $address .= " @ $temp[3]";
467 }
468
28b7d78a 469 print <<END
d7fe5d79 470<td style='text-align:center; $col'>$temp[2]</td>
6395bed8 471<td style='text-align:center; $col'>$address</td>
28b7d78a 472
d7fe5d79 473<td style='text-align:center; $col'>
28b7d78a
CS
474<form method='post' action='$ENV{'SCRIPT_NAME'}'>
475<input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
476<input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
477<input type='hidden' name='KEY1' value='$key' />
478</form>
479</td>
480
d7fe5d79 481<td style='text-align:center; $col'>
28b7d78a
CS
482<form method='post' action='$ENV{'SCRIPT_NAME'}'>
483<input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
484<input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
485<input type='hidden' name='KEY1' value='$key' />
486</form>
487</td>
488
d7fe5d79 489<td style='text-align:center; $col'>
28b7d78a
CS
490<form method='post' action='$ENV{'SCRIPT_NAME'}'>
491<input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
492<input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
493<input type='hidden' name='KEY1' value='$key' />
494</form>
495</td>
496</tr>
497END
498;
499 $key++;
500}
501print "</table>";
502
503# If table contains entries, print 'Key to action icons'
504if ($key) {
505print <<END
506<table>
507<tr>
508 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
509 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
510 <td class='base'>$Lang::tr{'click to disable'}</td>
511 <td>&nbsp;&nbsp;</td>
512 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
513 <td class='base'>$Lang::tr{'click to enable'}</td>
514 <td>&nbsp;&nbsp;</td>
515 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
516 <td class='base'>$Lang::tr{'edit'}</td>
517 <td>&nbsp;&nbsp;</td>
518 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
519 <td class='base'>$Lang::tr{'remove'}</td>
520</tr>
521</table>
522END
523;
524}
525
526&Header::closebox();
527&Header::closebigbox();
528&Header::closepage();
529
530## Ouf it's the end !
531
532
533
534# Sort the "current" array according to choices
535sub SortDataFile
536{
537 our %entries = ();
66c36198 538
28b7d78a
CS
539 # Sort pair of record received in $a $b special vars.
540 # When IP is specified use numeric sort else alpha.
541 # If sortname ends with 'Rev', do reverse sort.
542 #
543 sub fixedleasesort {
544 my $qs=''; # The sort field specified minus 'Rev'
545 if (rindex ($netsettings{'SORT_ALIASES'},'Rev') != -1) {
546 $qs=substr ($netsettings{'SORT_ALIASES'},0,length($netsettings{'SORT_ALIASES'})-3);
547 if ($qs eq 'IP') {
548 my @a = split(/\./,$entries{$a}->{$qs});
549 my @b = split(/\./,$entries{$b}->{$qs});
550 ($b[0]<=>$a[0]) ||
551 ($b[1]<=>$a[1]) ||
552 ($b[2]<=>$a[2]) ||
553 ($b[3]<=>$a[3]);
554 } else {
555 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
556 }
557 } else { #not reverse
558 $qs=$netsettings{'SORT_ALIASES'};
559 if ($qs eq 'IP') {
560 my @a = split(/\./,$entries{$a}->{$qs});
561 my @b = split(/\./,$entries{$b}->{$qs});
562 ($a[0]<=>$b[0]) ||
563 ($a[1]<=>$b[1]) ||
564 ($a[2]<=>$b[2]) ||
565 ($a[3]<=>$b[3]);
566 } else {
567 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
568 }
569 }
570 }
571
572 #Use an associative array (%entries)
573 my $key = 0;
574 foreach my $line (@current) {
575 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
576 my @temp = split (',',$line);
66c36198 577
28b7d78a
CS
578 # Build a pair 'Field Name',value for each of the data dataline.
579 # Each SORTABLE field must have is pair.
580 # Other data fields (non sortable) can be grouped in one
66c36198 581
28b7d78a
CS
582 # Exemple
583 # F1,F2,F3,F4,F5 only F1 F2 for sorting
584 # my @record = ('KEY',$key++,
585 # 'F1',$temp[0],
586 # 'F2',$temp[1],
587 # 'DATA',join(',',@temp[2..4]) ); #group remainning values, with separator (,)
66c36198 588
28b7d78a 589 # The KEY,key record permits doublons. If removed, then F1 becomes the key without doublon permitted.
66c36198
PM
590
591
6395bed8 592 my @record = ('KEY',$key++,'IP',$temp[0],'ENABLED',$temp[1],'NAME',$temp[2],'INTERFACE',$temp[3]);
28b7d78a
CS
593 my $record = {}; # create a reference to empty hash
594 %{$record} = @record; # populate that hash with @record
595 $entries{$record->{KEY}} = $record; # add this to a hash of hashes
596 }
66c36198 597
28b7d78a
CS
598 open(FILE, ">$datafile") or die 'Unable to open aliases file.';
599
600 # Each field value is printed , with the newline ! Don't forget separator and order of them.
601 foreach my $entry (sort fixedleasesort keys %entries) {
6395bed8 602 print FILE "$entries{$entry}->{IP},$entries{$entry}->{ENABLED},$entries{$entry}->{NAME},$entries{$entry}->{INTERFACE}\n";
28b7d78a
CS
603 }
604
605 close(FILE);
606 # Reload sorted @current
607 open (FILE, "$datafile");
608 @current = <FILE>;
609 close (FILE);
610}
611
66c36198 612#
28b7d78a
CS
613# Build the configuration file for application aliases
614#
615sub BuildConfiguration {
616 # Restart service associated with this
c4391a01 617 &General::system('/usr/local/bin/setaliases');
28b7d78a 618}
d7fe5d79 619
f6eb1a40
SS
620#
621## Handle Suricata related actions.
622#
623sub HandleSuricata() {
624 # Check if suricata is running.
625 if(&IDS::ids_is_running()) {
626 # Re-generate file which contains the HOME_NET declaration.
627 &IDS::generate_home_net_file();
628
629 # Call suricatactrl to perform a restart of suricata.
630 &IDS::call_suricatactrl("restart");
631 }
632}