From 2c97b90ca303a8dcf5992aacee08ba2762957b4a Mon Sep 17 00:00:00 2001 From: Valentino Orlandi Date: Tue, 28 Nov 2023 19:46:23 +0100 Subject: [PATCH] Improvements Removed predefined logs formats. Moved logs format check inside Craplog. --- logdoctor/mainwindow.cpp | 23 +++---------- logdoctor/modules/craplog/craplog.cpp | 34 ++++++++++++++----- logdoctor/modules/craplog/craplog.h | 3 ++ logdoctor/modules/craplog/modules/formats.cpp | 12 +++++++ 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/logdoctor/mainwindow.cpp b/logdoctor/mainwindow.cpp index 57a8f618..284cf2f3 100644 --- a/logdoctor/mainwindow.cpp +++ b/logdoctor/mainwindow.cpp @@ -2694,21 +2694,8 @@ void MainWindow::on_button_MakeStats_Start_clicked() { if ( this->dbUsable() ) { bool proceed{ true }; - // check that the format has been set - const LogsFormat& lf{ this->craplog.getLogsFormat( this->craplog.getCurrentWSID() ) }; - if ( lf.string.empty() ) { - // format string not set - proceed &= false; - DialogSec::errLogFormatNotSet( nullptr ); - } else if ( lf.fields.empty() ) { - // no field, useless to parse - proceed &= false; - DialogSec::errLogFormatNoFields( nullptr ); - } else if ( lf.separators.size() < lf.fields.size()-1 ) { - // missing at least a separator between two (or more) fields - proceed &= false; - DialogSec::errLogFormatNoSeparators( nullptr ); - } + // check that the format has been set and is consistent + proceed = craplog.checkCurrentLogsFormat(); if ( proceed ) { // take actions on Craplog's start @@ -4677,7 +4664,7 @@ void MainWindow::on_inLine_ConfApache_Format_String_returnPressed() void MainWindow::on_button_ConfApache_Format_Save_clicked() { const bool success{ this->craplog.setApacheLogFormat( - this->ui->inLine_ConfApache_Format_String->text().toStdString() ) }; + this->ui->inLine_ConfApache_Format_String->text().trimmed().toStdString() ) }; if ( success ) { this->ui->button_ConfApache_Format_Save->setEnabled( false ); if ( this->craplog.getCurrentWSID() == APACHE_ID ) { @@ -5048,7 +5035,7 @@ void MainWindow::on_inLine_ConfNginx_Format_String_returnPressed() void MainWindow::on_button_ConfNginx_Format_Save_clicked() { const bool success{ this->craplog.setNginxLogFormat( - this->ui->inLine_ConfNginx_Format_String->text().toStdString() ) }; + this->ui->inLine_ConfNginx_Format_String->text().trimmed().toStdString() ) }; if ( success ) { this->ui->button_ConfNginx_Format_Save->setEnabled( false ); if ( this->craplog.getCurrentWSID() == NGINX_ID ) { @@ -5481,7 +5468,7 @@ void MainWindow::on_inLine_ConfIis_Format_String_returnPressed() void MainWindow::on_button_ConfIis_Format_Save_clicked() { const bool success{ this->craplog.setIisLogFormat( - StringOps::strip( this->ui->inLine_ConfIis_Format_String->text().toStdString() ), + this->ui->inLine_ConfIis_Format_String->text().trimmed().toStdString(), this->getIisLogsModule() ) }; if ( success ) { this->ui->button_ConfIis_Format_Save->setEnabled( false ); diff --git a/logdoctor/modules/craplog/craplog.cpp b/logdoctor/modules/craplog/craplog.cpp index 53bb8523..a63904b2 100644 --- a/logdoctor/modules/craplog/craplog.cpp +++ b/logdoctor/modules/craplog/craplog.cpp @@ -35,23 +35,23 @@ Craplog::Craplog() //////////////////////// // blacklists / whitelists for ( unsigned i{APACHE_ID}; i<=IIS_ID; i++ ) { - this->warnlists.emplace( i, std::unordered_map{} ); - this->blacklists.emplace( i, std::unordered_map{} ); + this->warnlists.emplace( i, std::unordered_map(4) ); + this->blacklists.emplace( i, std::unordered_map(1) ); // default data - this->warnlists.at( i ).emplace( 11, BWlist{ .used=false, .list={"DELETE","HEAD","OPTIONS","PUT","PATCH"} } ); - this->warnlists.at( i ).emplace( 12, BWlist{ .used=true, .list={"/robots.txt","/../","/./","/.env","/.htaccess","/phpmyadmin","/wp-admin","/wp-content","/wp-config.php","/config.py","/views.py","/routes.py","/setup.cgi","/cgi-bin"} } ); + this->warnlists.at( i ).emplace( 11, BWlist{ .used=false, .list={} } ); + this->warnlists.at( i ).emplace( 12, BWlist{ .used=false, .list={} } ); this->warnlists.at( i ).emplace( 20, BWlist{ .used=false, .list={} } ); this->warnlists.at( i ).emplace( 21, BWlist{ .used=false, .list={} } ); - this->blacklists.at( i ).emplace( 20, BWlist{ .used=true, .list={} } ); + this->blacklists.at( i ).emplace( 20, BWlist{ .used=false, .list={} } ); } // default format strings this->logs_format_strings.emplace( - APACHE_ID, "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" ); + APACHE_ID, "" ); this->logs_format_strings.emplace( - NGINX_ID, "$remote_addr - $remote_user [$time_local] \"$request\" $status $bytes_sent \"$http_referer\" \"$http_user_agent\"" ); + NGINX_ID, "" ); this->logs_format_strings.emplace( - IIS_ID, "date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken" ); + IIS_ID, "" ); // initialize formats this->logs_formats.emplace( @@ -393,6 +393,24 @@ QString Craplog::getLogsFormatSample( const unsigned& web_server_id ) const } } +bool Craplog::checkCurrentLogsFormat() const +{ + if ( this->current_LF.string.empty() ) { + // format string not set + DialogSec::errLogFormatNotSet( nullptr ); + return false; + } else if ( this->current_LF.fields.empty() ) { + // no field, useless to parse + DialogSec::errLogFormatNoFields( nullptr ); + return false; + } else if ( this->current_LF.separators.size() < this->current_LF.fields.size()-1 ) { + // at least one separator is missing between two (or more) fields + DialogSec::errLogFormatNoSeparators( nullptr ); + return false; + } + return true; +} + // set the current Web Server void Craplog::setCurrentWSID( const unsigned web_server_id ) diff --git a/logdoctor/modules/craplog/craplog.h b/logdoctor/modules/craplog/craplog.h index b4c3ee2d..52c6dfe1 100644 --- a/logdoctor/modules/craplog/craplog.h +++ b/logdoctor/modules/craplog/craplog.h @@ -214,6 +214,9 @@ public: */ QString getLogsFormatSample( const unsigned& web_server_id ) const; + //! Checks whether the current Logs Format is valid or not + bool checkCurrentLogsFormat() const; + ////////////////////// diff --git a/logdoctor/modules/craplog/modules/formats.cpp b/logdoctor/modules/craplog/modules/formats.cpp index 92aabfa7..a6aa1448 100644 --- a/logdoctor/modules/craplog/modules/formats.cpp +++ b/logdoctor/modules/craplog/modules/formats.cpp @@ -251,6 +251,10 @@ void checkIisString( std::string_view string ) LogsFormat FormatOps::processApacheFormatString( const std::string& f_str ) const { + if ( f_str.empty() ) { + return LogsFormat(); + } + const auto& f_map { this->APACHE_ALF }; const auto& f_map_v { this->APACHE_ALF_V }; @@ -582,6 +586,10 @@ QString FormatOps::getApacheLogSample( const LogsFormat& log_format ) const LogsFormat FormatOps::processNginxFormatString( const std::string& f_str ) const { + if ( f_str.empty() ) { + return LogsFormat(); + } + const auto& f_map{ this->NGINX_ALF }; std::string initial, final; @@ -678,6 +686,10 @@ QString FormatOps::getNginxLogSample( const LogsFormat& log_format ) const LogsFormat FormatOps::processIisFormatString( const std::string& f_str, const int& l_mod ) const { + if ( f_str.empty() ) { + return LogsFormat(); + } + checkIisString( f_str ); std::string initial, final; std::vector separators, fields;