Added new module CheckSec
Performs integrity checks on variables and files/folders
This commit is contained in:
parent
2ad2231e81
commit
47f83c75ae
4 changed files with 184 additions and 0 deletions
|
@ -39,6 +39,8 @@ set(PROJECT_SOURCES
|
|||
utilities/vectors.cpp
|
||||
|
||||
modules.h
|
||||
modules/checks.h
|
||||
modules/checks.cpp
|
||||
modules/dialogs.h
|
||||
modules/dialogs.cpp
|
||||
modules/tb.h
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef MODULES_H
|
||||
#define MODULES_H
|
||||
|
||||
#include "modules/checks.h"
|
||||
#include "modules/dialogs.h"
|
||||
#include "modules/tb.h"
|
||||
|
||||
#endif // MODULES_H
|
||||
|
|
162
craplog/modules/checks.cpp
Normal file
162
craplog/modules/checks.cpp
Normal file
|
@ -0,0 +1,162 @@
|
|||
|
||||
#include "checks.h"
|
||||
|
||||
#include "utilities/io.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
|
||||
CheckSec::CheckSec()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool CheckSec::checkStatsDatabase( std::string path )
|
||||
{
|
||||
bool ok = true;
|
||||
if ( IOutils::exists( path ) ) {
|
||||
// database file exists
|
||||
if ( IOutils::checkFile( path, true ) == false ) {
|
||||
// database not readable, abort
|
||||
DialogSec::errDatabaseNotReadable( nullptr, QString("statistics") );
|
||||
ok = false;
|
||||
} else if ( IOutils::checkFile( path, false, true ) == false ) {
|
||||
// database not writable, abort
|
||||
DialogSec::errDatabaseNotWritable( nullptr, QString("statistics") );
|
||||
ok = false;
|
||||
|
||||
} else {
|
||||
// database file has read and write permissions, now check the tables
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
// database does not exist, yet ask to create a new one
|
||||
if ( DialogSec::choiceDatabaseNotFound( nullptr, QString("statistics") ) ) {
|
||||
// choosed to create it
|
||||
int rc;
|
||||
char *errMsg = 0;
|
||||
sqlite3 *db;
|
||||
|
||||
rc = sqlite3_open( path.c_str(), &db);
|
||||
if ( rc != SQLITE_OK ) {
|
||||
// error opening database
|
||||
ok = false;
|
||||
DialogSec::errDatabaseFailedOpening( nullptr, "statistics", QString(sqlite3_errmsg(db)) );
|
||||
|
||||
} else {
|
||||
// succesfully creted database file, now create the tables
|
||||
std::vector<std::string> ws_names = { "apache", "ngin", "iis" };
|
||||
std::string statement;
|
||||
int tries;
|
||||
for ( const std::string& ws_name : ws_names ) {
|
||||
// compose the statement with the table name for the access logs
|
||||
statement = "\
|
||||
CREATE TABLE "+ws_name+"_access (\
|
||||
warning BOOLEAN,\
|
||||
year SMALLINT,\
|
||||
month TINYINT,\
|
||||
day TINYINT,\
|
||||
hour TINYINT,\
|
||||
minute TINYINT,\
|
||||
second TINYINT,\
|
||||
protocol TEXT,\
|
||||
method TEXT,\
|
||||
request TEXT,\
|
||||
query TEXT,\
|
||||
response SMALLINT,\
|
||||
time_taken INTEGER,\
|
||||
bytes_sent INTEGER,\
|
||||
bytes_received INTEGER,\
|
||||
client TEXT,\
|
||||
user_agent TEXT,\
|
||||
cookie TEXT,\
|
||||
referrer TEXT\
|
||||
);";
|
||||
tries = 0;
|
||||
while (true) {
|
||||
tries ++;
|
||||
if ( tries > 3 ) {
|
||||
break;
|
||||
}
|
||||
rc = sqlite3_exec(db, statement.c_str(), nullptr, nullptr, &errMsg);
|
||||
if ( rc == SQLITE_BUSY ) {
|
||||
// database busy, wait 1 sec and retry
|
||||
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( rc != SQLITE_OK ) {
|
||||
// error creating table
|
||||
ok = false;
|
||||
DialogSec::errDatabaseFailedExecuting( nullptr,
|
||||
QString("statistics"),
|
||||
QString("CREATE TABLE %1_access (...)").arg( ws_name.c_str() ),
|
||||
QString(errMsg) );
|
||||
sqlite3_free( errMsg );
|
||||
}
|
||||
|
||||
// compose the statement with the table name for the error logs
|
||||
statement = "\
|
||||
CREATE TABLE "+ws_name+"_error (\
|
||||
warning BOOLEAN,\
|
||||
year SMALLINT,\
|
||||
month TINYINT,\
|
||||
day TINYINT,\
|
||||
hour TINYINT,\
|
||||
minute TINYINT,\
|
||||
second TINYINT,\
|
||||
level TEXT,\
|
||||
message TEXT,\
|
||||
source_file TEXT,\
|
||||
client TEXT,\
|
||||
port SMALLINT\
|
||||
);";
|
||||
tries = 0;
|
||||
while (true) {
|
||||
tries ++;
|
||||
if ( tries > 3 ) {
|
||||
break;
|
||||
}
|
||||
rc = sqlite3_exec(db, statement.c_str(), nullptr, nullptr, &errMsg);
|
||||
if ( rc == SQLITE_BUSY ) {
|
||||
// database busy, wait 1 sec and retry
|
||||
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( rc != SQLITE_OK ) {
|
||||
// error creating table
|
||||
ok = false;
|
||||
DialogSec::errDatabaseFailedExecuting( nullptr,
|
||||
QString("statistics"),
|
||||
QString("CREATE TABLE %1_error (...)").arg( ws_name.c_str() ),
|
||||
QString(errMsg) );
|
||||
sqlite3_free( errMsg );
|
||||
}
|
||||
}
|
||||
|
||||
// finally close the database
|
||||
sqlite3_close( db );
|
||||
}
|
||||
|
||||
} else {
|
||||
// refused to create it, abort
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
bool CheckSec::checkCraplog( Craplog craplog )
|
||||
{
|
||||
bool ok = true;
|
||||
|
||||
|
||||
return ok;
|
||||
}
|
18
craplog/modules/checks.h
Normal file
18
craplog/modules/checks.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifndef CHECKS_H
|
||||
#define CHECKS_H
|
||||
|
||||
#include "tools/craplog/craplog.h"
|
||||
|
||||
|
||||
class CheckSec
|
||||
{
|
||||
public:
|
||||
CheckSec();
|
||||
|
||||
static bool checkStatsDatabase( std::string path );
|
||||
static bool checkHashesDatabase( std::string path );
|
||||
|
||||
static bool checkCraplog( Craplog craplog );
|
||||
};
|
||||
|
||||
#endif // CHECKS_H
|
Loading…
Reference in a new issue