2021-09-15 08:14:09 +02:00
|
|
|
|
// swad_firewall_database.c: firewall to mitigate denial of service attacks, operations with database
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
SWAD (Shared Workspace At a Distance),
|
|
|
|
|
is a web platform developed at the University of Granada (Spain),
|
|
|
|
|
and used to support university teaching.
|
|
|
|
|
|
|
|
|
|
This file is part of SWAD core.
|
2024-02-07 00:40:28 +01:00
|
|
|
|
Copyright (C) 1999-2024 Antonio Ca<EFBFBD>as Vargas
|
2021-09-15 08:14:09 +02:00
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************************* Headers ***********************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h> // For exit
|
|
|
|
|
|
|
|
|
|
#include "swad_database.h"
|
|
|
|
|
#include "swad_firewall.h"
|
|
|
|
|
#include "swad_global.h"
|
2022-10-27 22:09:44 +02:00
|
|
|
|
#include "swad_parameter.h"
|
2021-09-15 08:14:09 +02:00
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************** External global variables from others modules ****************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
extern struct Globals Gbl;
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************* Log access into firewall recent log *******************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Fir_DB_LogAccess (void)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryINSERT ("can not log access into firewall_log",
|
|
|
|
|
"INSERT INTO fir_log"
|
2023-02-10 10:02:21 +01:00
|
|
|
|
" (ClickTime,IP,UsrCod)"
|
2021-09-15 08:14:09 +02:00
|
|
|
|
" VALUES"
|
2023-02-10 10:02:21 +01:00
|
|
|
|
" (NOW(),'%s',%ld)",
|
|
|
|
|
Par_GetIP (),
|
|
|
|
|
Gbl.Usrs.Me.UsrDat.UsrCod);
|
2021-09-15 08:14:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************* Get number of clicks from database ********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
unsigned Fir_DB_GetNumClicksFromLog (void)
|
|
|
|
|
{
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QueryCOUNT ("can not check firewall log",
|
|
|
|
|
"SELECT COUNT(*)"
|
|
|
|
|
" FROM fir_log"
|
|
|
|
|
" WHERE IP='%s'"
|
2023-02-10 10:02:21 +01:00
|
|
|
|
" AND UsrCod=%ld"
|
2021-09-15 08:14:09 +02:00
|
|
|
|
" AND ClickTime>FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
|
2022-10-27 22:09:44 +02:00
|
|
|
|
Par_GetIP (),
|
2023-02-10 10:02:21 +01:00
|
|
|
|
Gbl.Usrs.Me.UsrDat.UsrCod,
|
2021-09-15 08:14:09 +02:00
|
|
|
|
Fw_CHECK_INTERVAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************** Remove old clicks from firewall **********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Fir_DB_PurgeFirewallLog (void)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not purge firewall log",
|
|
|
|
|
"DELETE LOW_PRIORITY FROM fir_log"
|
|
|
|
|
" WHERE ClickTime<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
|
|
|
|
|
(unsigned long) Fw_TIME_TO_DELETE_OLD_CLICKS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************************* Ban an IP *********************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Fir_DB_BanIP (void)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryINSERT ("can not ban IP",
|
|
|
|
|
"INSERT INTO fir_banned"
|
|
|
|
|
" (IP,BanTime,UnbanTime)"
|
|
|
|
|
" VALUES"
|
|
|
|
|
" ('%s',NOW(),FROM_UNIXTIME(UNIX_TIMESTAMP()+%lu))",
|
2022-10-27 22:09:44 +02:00
|
|
|
|
Par_GetIP (),
|
2021-09-15 08:14:09 +02:00
|
|
|
|
(unsigned long) Fw_TIME_BANNED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/***************** Get number of current bans from database ******************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
unsigned Fir_DB_GetNumBansIP (void)
|
|
|
|
|
{
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QueryCOUNT ("can not check firewall log",
|
|
|
|
|
"SELECT COUNT(*)"
|
|
|
|
|
" FROM fir_banned"
|
|
|
|
|
" WHERE IP='%s'"
|
|
|
|
|
" AND UnbanTime>NOW()",
|
2022-10-27 22:09:44 +02:00
|
|
|
|
Par_GetIP ());
|
2021-09-15 08:14:09 +02:00
|
|
|
|
}
|