// 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ņ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 .
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
#include // For exit
#include "swad_database.h"
#include "swad_firewall.h"
#include "swad_global.h"
/*****************************************************************************/
/************** 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"
" (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 ClickTimeNOW()",
Gbl.IP);
}