mirror of
https://github.com/acanas/swad-core.git
synced 2024-09-22 00:00:40 +02:00
123 lines
4.7 KiB
C
123 lines
4.7 KiB
C
|
// 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.
|
|||
|
Copyright (C) 1999-2021 Antonio Ca<EFBFBD>as Vargas
|
|||
|
|
|||
|
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"
|
|||
|
|
|||
|
/*****************************************************************************/
|
|||
|
/************** External global variables from others modules ****************/
|
|||
|
/*****************************************************************************/
|
|||
|
|
|||
|
extern struct Globals Gbl;
|
|||
|
|
|||
|
/*****************************************************************************/
|
|||
|
/***************************** Private constants *****************************/
|
|||
|
/*****************************************************************************/
|
|||
|
|
|||
|
/*****************************************************************************/
|
|||
|
/******************************* Private types *******************************/
|
|||
|
/*****************************************************************************/
|
|||
|
|
|||
|
/*****************************************************************************/
|
|||
|
/****************************** Private prototypes ***************************/
|
|||
|
/*****************************************************************************/
|
|||
|
|
|||
|
/*****************************************************************************/
|
|||
|
/********************* Log access into firewall recent log *******************/
|
|||
|
/*****************************************************************************/
|
|||
|
|
|||
|
void Fir_DB_LogAccess (void)
|
|||
|
{
|
|||
|
DB_QueryINSERT ("can not log access into firewall_log",
|
|||
|
"INSERT INTO fir_log"
|
|||
|
" (ClickTime,IP)"
|
|||
|
" VALUES"
|
|||
|
" (NOW(),'%s')",
|
|||
|
Gbl.IP);
|
|||
|
}
|
|||
|
|
|||
|
/*****************************************************************************/
|
|||
|
/********************* 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'"
|
|||
|
" AND ClickTime>FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
|
|||
|
Gbl.IP,
|
|||
|
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))",
|
|||
|
Gbl.IP,
|
|||
|
(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()",
|
|||
|
Gbl.IP);
|
|||
|
}
|