Hello,
I'm trying to change a PHP script that writes entries in the MySQL database (fail2ban) to a PostgreSQL database. I'm trying to change two PHP files (config.php and fail2ban.php). I need your help here. Everything marked with --- should be deleted. Everything marked with +++ should be added. But I'm still missing something.
Can you help me?
Greetings from Stefan Harbich
I'm trying to change a PHP script that writes entries in the MySQL database (fail2ban) to a PostgreSQL database. I'm trying to change two PHP files (config.php and fail2ban.php). I need your help here. Everything marked with --- should be deleted. Everything marked with +++ should be added. But I'm still missing something.
Code:
root@dsme01:/etc/fail2ban# cat config.php
#!/usr/bin/php
<?php
// jail to be used
$jail = "pf-*";
// database configuration, use only one central mysql server
$dbserver="localhost";
$dbuser="fail2ban";
$dbpass="########";
$dbname="fail2ban";
$tablename="fail2ban";
--- // connect to mysql database
--- $link = mysqli_connect($dbserver, $dbuser, $dbpass, $dbname) or die('Could not connect: ' . mysqli_error());
--- mysqli_select_db($link,$dbname) or die('Could not select database');
+++ // connect to postgresql database
+++ $link = new PDO("pgsql:host=$dbserver;dbname=$dbname", $dbuser, $dbpass);
????
?>
----------------------------------------------------------------------------------------------------------------
root@dsme01:/etc/fail2ban# cat fail2ban.php
#!/usr/bin/php
<?php
//REQUIREMENTS:
--- // mysql
--- //sudo apt-get install php7.0 php-mysql
+++ // postgresql
+++ // sudo apt-get install php-pgsql
//MANUAL COMMANDS:
//
//UNBAN: sudo fail2ban-client set domoticz unbanip Ban 5.90.201.166
/*
Open the "jail.local" file and find the "banaction" used by the rule
It's necessary to add the following line to the "banaction" rule used.
php /home/domoticz/fail2ban-central/fail2ban.php <name> <protocol> <port> <ip>
EXAMPLE: if you use "iptables-multiport.conf" replace:
---------------------------------------------------------
actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
---------------------------------------------------------
with:
---------------------------------------------------------
actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
php /home/domoticz/fail2ban-central/fail2ban.php <name> <protocol> <port> <ip>
---------------------------------------------------------
*/
require_once((dirname(__FILE__))."/config.php");
$name = $_SERVER["argv"][1];
$protocol = $_SERVER["argv"][2];
$port = $_SERVER["argv"][3];
if (!preg_match('/^\d{1,5}$/', $port))
$port = getservbyname($_SERVER["argv"][3], $protocol);
$ip = $_SERVER["argv"][4];
$hostname = gethostname();
--- // query mysql
--- $query = "INSERT INTO
--- `".$tablename."`
--- (`hostname`,
--- `created`,
--- `name`,
--- `protocol`,
--- `port`,
--- `ip`)
--- VALUES
--- ('".addslashes($hostname)."',
--- NOW(),
--- '".addslashes($name)."',
--- '".addslashes($protocol)."',
--- '".addslashes($port)."',
--- '".addslashes($ip)."')
--- ON DUPLICATE KEY UPDATE
--- `hostname` = '$hostname',
--- `created` = NOW(),
--- `name` = '$name',
--- `protocol` = '$protocol',
--- `port` = '$port'";
--- if (mysqli_query($link, $query)) {
--- echo "Ip to BAN added to DATABASE";
--- } else {
--- echo "Error: " . $query . "<br>" . mysqli_error($link);
--- }
--- mysqli_close($link);
+++ // query postgresql
????
exit;
?>
Greetings from Stefan Harbich