#!/usr/bin/perl
#
#
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################
#
#
#
# This code is based on the dshield section of the Logsend addon for IPCop
# and modified to run on IPFire.
#
# New code has been added to allow the user to modify the time when
# reports are sent to Dshield and now uses IPFire's dragonfly mail agent
# to sent the report instead of the original sendEmail.
# Rob Brewer    29 July 2021
#
# iptables.pl written by dshield.org
#
#


require '/var/ipfire/general-functions.pl';
require '/var/ipfire/lang.pl';
require '/var/ipfire/header.pl';

use warnings;

my %dshieldsettings;

&Header::showhttpheaders();

$dshieldsettings{'DSHIELDRECIPIENT'} = '';
$dshieldsettings{'DSHIELDENABLE'} = 'off';
$dshieldsettings{'DSHIELDSENDER'} = '';
$dshieldsettings{'USERID'} = '0';
$dshieldsettings{'TIMEZONE'} = 'GMT';
$dshieldsettings{'INTERVAL'} = '0';

&Header::getcgihash(\%dshieldsettings);


if ($dshieldsettings{'ACTION'} eq $Lang::tr{'save'})
 { 
	{	 
        &General::writehash("${General::swroot}/dshield/settings", \%dshieldsettings);        
	}

    	    {

# Check for DShield Sender name
	    
		if ($dshieldsettings{'DSHIELDSENDER'} eq '')
	    	{
		$errormessage = $Lang::tr{'dshieldsender blank'};
		}		

# Check for DShield Recipient name

                if ($dshieldsettings{'DSHIELDRECIPIENT'} eq '')
                {
                $errormessage = $Lang::tr{'dshieldrecipient blank'};
                }
            }


	if ($dshieldsettings{'DSHIELDENABLE'} eq 'on') 
 
	{	

# Write Dshield.mail file
	
	open(FILE, ">/${General::swroot}/dshield/dshield.mail") or die "Unable to write dshield.mail file";
	flock(FILE, 2);
	print FILE <<EOF;
#!/bin/bash

if [ -f /var/ipfire/red/active ]; then

if [ -f /var/tmp/dshield.report ]; then

now=\$(/bin/date -R)
random=\$(/usr/bin/tr -dc 'a-zA-Z0-9' < /dev/urandom | /usr/bin/fold -w 12 | /bin/head -n 1)
messageid=\$(/bin/date --utc +%Y%m%d%H%M%S.\$random\@\$HOSTNAME)
email=\$(/bin/cat /var/tmp/dshield.report)
recipient=$dshieldsettings{'DSHIELDRECIPIENT'}

function err_exit() { echo -e 1>&2; exit 1; }

function mail_input {
echo -ne "From: <$dshieldsettings{'DSHIELDSENDER'}>\\r\\n"
echo -ne "To: <\$recipient>\\r\\n"
echo -ne "Subject: FORMAT IPTABLES USERID $dshieldsettings{'USERID'} TZ $dshieldsettings{'TIMEZONE'}\\r\\n"
echo -ne "Message-Id: <\$messageid>\\r\\n"
echo -ne "Date: \$now\\r\\n"
echo -ne "\\r\\n"
echo -ne "\$email\\r\\n"
echo -ne ".\\r\\n"
		}

mail_input | /usr/sbin/sendmail \$recipient 

cat /var/tmp/dshield.report >> /var/log/dshield/report.log && rm /var/tmp/dshield.report

fi

else
    exit 0
fi

EOF
    ;
	close FILE;

	
# Write Dshield.conf Enabled  file	


	open(FILE, ">/${General::swroot}/dshield/dshield.conf") or die "Unable to write dshield.conf file";
	flock(FILE, 2);
 	print FILE <<END
#!/bin/bash

# pre-process message log
fgrep -a "\$(/bin/date -d '1 minute ago' "+%b %d %H:")" /var/log/messages > /var/tmp/dshield.preprocess  && \\

# Runs the Dshield script and creates the report daily
#
/var/ipfire/dshield/iptables.pl -config=/var/ipfire/dshield/dshield.cnf && \\
rm /var/tmp/dshield.preprocess

END
    ;
	close FILE;


# Generate Enabled dshield.cron file


	open(FILE, ">/${General::swroot}/dshield/dshield.cron") or die "Unable to write dshield.cron file";
	flock(FILE, 2);
 	print FILE <<EOF; 
#!/bin/bash

CRONTAB="01 $dshieldsettings{'INTERVAL'} * * * root /var/ipfire/dshield/dshield.mail > /dev/null 2>&1"
ORIG=/var/spool/cron/root.orig

if fgrep -q "\$CRONTAB" \$ORIG ; then
        exit 0
else
        if fgrep -q  dshield.mail \$ORIG ; then
        sed -i "/dshield.mail/c \$CRONTAB" \$ORIG
else
        cat <<END >> \$ORIG
# added for dshield
\$CRONTAB

END
        fi

        /usr/bin/fcrontab -z

fi

EOF
;
	close FILE;

}		
		
	if ($dshieldsettings{'DSHIELDENABLE'} eq 'off')

	{

# write Disabled dshield.cron file

	open(FILE, ">/${General::swroot}/dshield/dshield.cron") or die "Unable to write dshield.cron file";
	flock(FILE, 2);print FILE <<END
#!/bin/bash

ORIG=/var/spool/cron/root.orig

if grep -q dshield \$ORIG; then
	sed -i '/dshield.mail/d' \$ORIG
	sed -i '/added for dshield/d' \$ORIG
	fi
	
fcrontab -z

END
;
	close FILE;

# write disabled dshield.conf file

        open(FILE, ">/${General::swroot}/dshield/dshield.conf") or die "Unable to write dshield file";
        flock(FILE, 2);
        print FILE <<END
#!/bin/bash


# Dshield is disabled

END
    ;
        close FILE;

	}
}
&General::readhash("${General::swroot}/dshield/settings", \%dshieldsettings);

my (%checked,%selected);

$checked{'DSHIELDENABLE'}{'off'} = '';
$checked{'DSHIELDENABLE'}{'on'} = '';
$checked{'DSHIELDENABLE'}{$dshieldsettings{'DSHIELDENABLE'}} = 'CHECKED';

$selected{'TIMEZONE'}{'GMT'} = '';
$selected{'TIMEZONE'}{'-0100'} = '';
$selected{'TIMEZONE'}{'-0200'} = '';
$selected{'TIMEZONE'}{'-0300'} = '';
$selected{'TIMEZONE'}{'-0400'} = '';
$selected{'TIMEZONE'}{'-0500'} = '';
$selected{'TIMEZONE'}{'-0600'} = '';
$selected{'TIMEZONE'}{'-0700'} = '';
$selected{'TIMEZONE'}{'-0800'} = '';
$selected{'TIMEZONE'}{'-0900'} = '';
$selected{'TIMEZONE'}{'-1000'} = '';
$selected{'TIMEZONE'}{'-1100'} = '';
$selected{'TIMEZONE'}{'-1200'} = '';
$selected{'TIMEZONE'}{'+0100'} = '';
$selected{'TIMEZONE'}{'+0200'} = '';
$selected{'TIMEZONE'}{'+0300'} = '';
$selected{'TIMEZONE'}{'+0400'} = '';
$selected{'TIMEZONE'}{'+0500'} = '';
$selected{'TIMEZONE'}{'+0600'} = '';
$selected{'TIMEZONE'}{'+0700'} = '';
$selected{'TIMEZONE'}{'+0800'} = '';
$selected{'TIMEZONE'}{'+0900'} = '';
$selected{'TIMEZONE'}{'+1000'} = '';
$selected{'TIMEZONE'}{'+1100'} = '';
$selected{'TIMEZONE'}{'+1200'} = '';
$selected{'TIMEZONE'}{$dshieldsettings{'TIMEZONE'}} = 'selected';

$selected{'INTERVAL'}{'1'} = '';
$selected{'INTERVAL'}{'2'} = '';
$selected{'INTERVAL'}{'3'} = '';
$selected{'INTERVAL'}{'4'} = '';
$selected{'INTERVAL'}{'6'} = '';
$selected{'INTERVAL'}{'8'} = '';
$selected{'INTERVAL'}{'12'} = '';
$selected{'INTERVAL'}{'24'} = '';
$selected{'INTERVAL'}{$dshieldsettings{'INTERVAL'}} = 'selected';


&Header::openpage($Lang::tr{'dshield'}, 1, '');

&Header::openbigbox('100%', 'LEFT');

if ($errormessage) {
	&Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
	print "<FONT CLASS='base'>$errormessage&nbsp;</FONT>\n";
	&Header::closebox();
}

print "<FORM METHOD='POST'>\n";

&Header::openbox('100%', 'LEFT', $Lang::tr{'dshield conf'});
print <<END


<form method='post'>
<style type="text/css">
        td {
        padding-top: 10px;
        padding-bottom: 10px;
        width:24em;
        font-size:12px;
        }
</style>

<table style='width:100%' border='0'>

<tr>
        <td> $Lang::tr{'dshieldenabled'}</td>
        <td><label><input type='checkbox' NAME='DSHIELDENABLE' $checked{'DSHIELDENABLE'}{'on'}></label></td>
        <td></td>
</tr>

<tr>
        <td> $Lang::tr{'dshieldsender'}</td>
        <td> <input type='text' name='DSHIELDSENDER' value='$dshieldsettings{'DSHIELDSENDER'}'style='width:12em;'></td>
</tr>

<tr>

	<td align='left'>$Lang::tr{'timezone'}</td>
	<td><select name='TIMEZONE'>
		<option value='GMT' $selected{'TIMEZONE'}{'GMT'}>GMT</option>
		<option value='-0100' $selected{'TIMEZONE'}{'-0100'}>-0100</option>
		<option value='-0200' $selected{'TIMEZONE'}{'-0200'}>-0200</option>
		<option value='-0300' $selected{'TIMEZONE'}{'-0300'}>-0300</option>
		<option value='-0400' $selected{'TIMEZONE'}{'-0400'}>-0400</option>
		<option value='-0500' $selected{'TIMEZONE'}{'-0500'}>-0500</option>
		<option value='-0600' $selected{'TIMEZONE'}{'-0600'}>-0600</option>
		<option value='-0700' $selected{'TIMEZONE'}{'-0700'}>-0700</option>
		<option value='-0800' $selected{'TIMEZONE'}{'-0800'}>-0800</option>
		<option value='-0900' $selected{'TIMEZONE'}{'-0900'}>-0900</option>
		<option value='-1000' $selected{'TIMEZONE'}{'-1000'}>-1000</option>
		<option value='-1100' $selected{'TIMEZONE'}{'-1100'}>-1100</option>
		<option value='-1200' $selected{'TIMEZONE'}{'-1200'}>-1200</option>
		<option value='+0100' $selected{'TIMEZONE'}{'+0100'}>+0100</option>
		<option value='+0200' $selected{'TIMEZONE'}{'+0200'}>+0200</option>
		<option value='+0300' $selected{'TIMEZONE'}{'+0300'}>+0300</option>
		<option value='+0400' $selected{'TIMEZONE'}{'+0400'}>+0400</option>
		<option value='+0500' $selected{'TIMEZONE'}{'+0500'}>+0500</option>
		<option value='+0600' $selected{'TIMEZONE'}{'+0600'}>+0600</option>
		<option value='+0700' $selected{'TIMEZONE'}{'+0700'}>+0700</option>
		<option value='+0800' $selected{'TIMEZONE'}{'+0800'}>+0800</option>
		<option value='+0900' $selected{'TIMEZONE'}{'+0900'}>+0900</option>
		<option value='+1000' $selected{'TIMEZONE'}{'+1000'}>+1000</option>
		<option value='+1100' $selected{'TIMEZONE'}{'+1100'}>+1100</option>
		<option value='+1200' $selected{'TIMEZONE'}{'+1200'}>+1200</option>
	</select></td>
</tr>
<tr>
        <td> $Lang::tr{'userid'}&nbsp;</td>
        <td><input type='text' name='USERID' VALUE='$dshieldsettings{'USERID'}'style='width:12em;'><IMG SRC='/blob.gif'></td>
</tr>

<tr>

	<td>$Lang::tr{'interval'}&nbsp;</td>
	<td><select name='INTERVAL'>
		<option value='0' $selected{'INTERVAL'}{'0'}>24</option>
		<option value='*/12' $selected{'INTERVAL'}{'*/12'}>12</option>
		<option value='*/8' $selected{'INTERVAL'}{'*/8'}>8</option>
		<option value='*/6' $selected{'INTERVAL'}{'*/6'}>6</option>
		<option value='*/4' $selected{'INTERVAL'}{'*/4'}>4</option>
		<option value='*/3' $selected{'INTERVAL'}{'*/3'}>3</option>
		<option value='*/2' $selected{'INTERVAL'}{'*/2'}>2</option>
		<option value='*' $selected{'INTERVAL'}{'*'}>1</option>
	
	</select></td>
</tr>
<tr>
	<td>$Lang::tr{'dshieldrecipient'}&nbsp;</td>
	<td><input type='text' name='DSHIELDRECIPIENT' VALUE='$dshieldsettings{'DSHIELDRECIPIENT'}'style='width:12em;'><IMG SRC='/blob.gif'><IMG SRC='/blob.gif'></td>
</tr>
<tr> 
        <td></td>
        <td></td>
        <td ALIGN='RIGHT'><INPUT TYPE='submit' NAME='ACTION' VALUE='$Lang::tr{'save'}'></td>
</tr>
</table><br>
END
;
&Header::closebox();

print <<END
<br>
<IMG SRC='/blob.gif' VALIGN='top'>&nbsp;$Lang::tr{'field can be blank'}&nbsp;To obtain ID register at https://www.dshield.org/register.html<br><br>
<IMG SRC='/blob.gif'><IMG SRC='/blob.gif'>&nbsp;Reports should be sent to 'report\@dshield.org'.<br><br>
<IMG SRC='/blob.gif'><IMG SRC='/blob.gif'><IMG SRC='/blob.gif'>&nbsp;To send reports the Mail Server must be configured from the 'Mail Service' menu.<br>
<br>
END
;

print "</FORM>\n";

&Header::closebigbox();

&Header::closepage();
