diff --git a/logdoctor/CMakeLists.txt b/logdoctor/CMakeLists.txt index d666ecbb..73dcc731 100644 --- a/logdoctor/CMakeLists.txt +++ b/logdoctor/CMakeLists.txt @@ -98,6 +98,22 @@ set(PROJECT_SOURCES modules/dialogs/ida_dialog.h modules/dialogs/ida_dialog.cpp + modules/blacklists/blacklists.h + modules/blacklists/blacklists.cpp + modules/blacklists/modules/lib.h + modules/blacklists/modules/blacklist.h + modules/blacklists/modules/blacklist.cpp + modules/blacklists/modules/blacklist_item.h + modules/blacklists/modules/blacklist_item.cpp + + modules/warnlists/warnlists.h + modules/warnlists/warnlists.cpp + modules/warnlists/modules/lib.h + modules/warnlists/modules/warnlist.h + modules/warnlists/modules/warnlist.cpp + modules/warnlists/modules/warnlist_item.h + modules/warnlists/modules/warnlist_item.cpp + modules/craplog/craplog.h modules/craplog/craplog.cpp modules/craplog/modules/lib.h diff --git a/logdoctor/modules/blacklists/blacklists.cpp b/logdoctor/modules/blacklists/blacklists.cpp new file mode 100644 index 00000000..f8e48f0c --- /dev/null +++ b/logdoctor/modules/blacklists/blacklists.cpp @@ -0,0 +1,35 @@ + +#include "blacklists.h" + +#include "modules/exceptions.h" + + +const Blacklist& Blacklists::getConst( const WebServer ws ) const +{ + switch (ws) { + case WebServer::Apache: + return this->apache; + case WebServer::Nginx: + return this->nginx; + case WebServer::IIS: + return this->iis; + default: + // should be unreachable + throw DoNotCatchException( "Unexpected WebServer" ); + } +} + +Blacklist& Blacklists::get( const WebServer ws ) +{ + switch (ws) { + case WebServer::Apache: + return this->apache; + case WebServer::Nginx: + return this->nginx; + case WebServer::IIS: + return this->iis; + default: + // should be unreachable + throw DoNotCatchException( "Unexpected WebServer" ); + } +} diff --git a/logdoctor/modules/blacklists/blacklists.h b/logdoctor/modules/blacklists/blacklists.h new file mode 100644 index 00000000..6aa98e99 --- /dev/null +++ b/logdoctor/modules/blacklists/blacklists.h @@ -0,0 +1,74 @@ +#ifndef LOGDOCTOR__BLACKLISTS__H +#define LOGDOCTOR__BLACKLISTS__H + + +#include "main_lib.h" + +#include "modules/blacklists/modules/blacklist.h" + +#include + + +struct Blacklists final +{ + Blacklist apache; + Blacklist nginx; + Blacklist iis; + + explicit Blacklists() noexcept = default; + + //! \throw DoNotCatchException + inline bool isUsed( const WebServer ws, const BlacklistField fld ) + { return get( ws ).isUsed( fld ); } + + //! \throw DoNotCatchException + inline void setUsed( const WebServer ws, const BlacklistField fld, const bool used ) + { get( ws ).setUsed( fld, used ); } + + //! \throw DoNotCatchException + const Blacklist& getConst( const WebServer ws ) const; + + //! \throw DoNotCatchException + Blacklist& get( const WebServer ws ); + + //! \throw DoNotCatchException + inline BlacklistItem& get( const WebServer ws, const BlacklistField fld ) + { return get( ws ).get( fld ); } + + //! \throw DoNotCatchException + inline std::vector& getList( const WebServer ws, const BlacklistField fld ) + { return get( ws, fld ).list; } + + //! \throw DoNotCatchException + inline const std::vector& getListConst( const WebServer ws, const BlacklistField fld ) + { return get( ws, fld ).list; } + + //! \throw BWlistException, DoNotCatchException + inline void setList( const WebServer ws, const BlacklistField fld, const std::vector& list ) + { get( ws, fld ).set( list ); } + + //! \throw BWlistException, DoNotCatchException + inline void clearList( const WebServer ws, const BlacklistField fld ) + { getList( ws, fld ).clear(); } + + //! \throw BWlistException, DoNotCatchException + inline void addItem( const WebServer ws, const BlacklistField fld, const std::string& item ) + { get( ws, fld ).add( item ); } + + //! \throw BWlistException + inline void removeItem( const WebServer ws, const BlacklistField fld, const std::string& item ) + { get( ws, fld ).remove( item ); } + + //! \throw BWlistException + inline int moveUpItem( const WebServer ws, const BlacklistField fld, const std::string& item ) + { return get( ws, fld ).moveUp( item ); } + + //! \throw BWlistException + inline int moveDownItem( const WebServer ws, const BlacklistField fld, const std::string& item ) + { return get( ws, fld ).moveDown( item ); } +}; + +Q_DECLARE_METATYPE(Blacklists) + + +#endif // LOGDOCTOR__BLACKLISTS__H diff --git a/logdoctor/modules/blacklists/modules/blacklist.cpp b/logdoctor/modules/blacklists/modules/blacklist.cpp new file mode 100644 index 00000000..bfdb6e85 --- /dev/null +++ b/logdoctor/modules/blacklists/modules/blacklist.cpp @@ -0,0 +1,21 @@ + +#include "blacklist.h" + +#include "modules/exceptions.h" + + +Blacklist::Blacklist() noexcept + : client{ BlacklistField::Client } +{ + +} + +BlacklistItem& Blacklist::get( const BlacklistField field ) +{ + switch (field) { + case BlacklistField::Client: + return this->client; + default: + throw DoNotCatchException( "Unexpected BlacklistField" ); + } +} diff --git a/logdoctor/modules/blacklists/modules/blacklist.h b/logdoctor/modules/blacklists/modules/blacklist.h new file mode 100644 index 00000000..4dd23788 --- /dev/null +++ b/logdoctor/modules/blacklists/modules/blacklist.h @@ -0,0 +1,27 @@ +#ifndef LOGDOCTOR__BLACKLISTS__BLACKLIST__H +#define LOGDOCTOR__BLACKLISTS__BLACKLIST__H + + +#include "modules/blacklists/modules/blacklist_item.h" + + +struct Blacklist final +{ + BlacklistItem client; + + explicit Blacklist() noexcept; + + //! \throw DoNotCatchException + BlacklistItem& get( const BlacklistField field ); + + //! \throw DoNotCatchException + inline bool isUsed( const BlacklistField field ) + { return get( field ).used; } + + //! \throw DoNotCatchException + inline void setUsed( const BlacklistField field, const bool used ) + { get( field ).used = used; } +}; + + +#endif // LOGDOCTOR__BLACKLISTS__BLACKLIST__H diff --git a/logdoctor/modules/blacklists/modules/blacklist_item.cpp b/logdoctor/modules/blacklists/modules/blacklist_item.cpp new file mode 100644 index 00000000..f3d50f98 --- /dev/null +++ b/logdoctor/modules/blacklists/modules/blacklist_item.cpp @@ -0,0 +1,76 @@ + +#include "blacklist_item.h" + +#include "modules/shared.h" + +#include "modules/exceptions.h" + +#include "utilities/bwlists.h" + + +const char* BlacklistItem::fieldName() const +{ + switch (this->field) { + case BlacklistField::Client: + return FIELDS__CLIENT.c_str(); + default: + // should be unreachable + throw DoNotCatchException( "Unexpected BlacklistField" ); + } +} + +void BlacklistItem::set( const std::vector& new_list ) +{ + this->list.clear(); + this->list.reserve( new_list.size() ); + for ( const std::string& item : new_list ) { + this->add( item ); + } +} + +void BlacklistItem::add( const std::string& item ) +{ + this->list.push_back( this->sanitized( item ) ); +} + +void BlacklistItem::remove( const std::string& item ) +{ + if ( const auto it{ std::find( this->list.cbegin(), this->list.cend(), item ) }; it != this->list.cend() ) { + this->list.erase( it ); + } else { + throw BWlistException( "Failed to remove the item: "+item ); + } +} + +int BlacklistItem::moveUp( const std::string& item ) +{ + if ( auto it{ std::find( std::next(this->list.begin()), this->list.end(), item ) }; it != this->list.end() ) { + const int pos{ static_cast( std::distance(this->list.begin(), it) ) - 1 }; + std::swap( *it, *std::prev(it) ); + return pos; + } else { + throw BWlistException( "Failed to move up the item: "+item ); + } +} + +int BlacklistItem::moveDown( const std::string& item ) +{ + if ( auto it{ std::find( this->list.begin(), std::prev(this->list.end()), item ) }; it != this->list.end() ) { + const int pos{ static_cast( std::distance(this->list.begin(), it) ) + 1 }; + std::swap( *it, *std::next(it) ); + return pos; + } else { + throw BWlistException( "Failed to move down the item: "+item ); + } +} + +std::string BlacklistItem::sanitized(const std::string& item ) const +{ + switch (this->field) { + case BlacklistField::Client: + return BWutils::sanitizedClient( item ); + default: + // should be unreachable + throw DoNotCatchException( "Unexpected BlacklistField" ); + } +} diff --git a/logdoctor/modules/blacklists/modules/blacklist_item.h b/logdoctor/modules/blacklists/modules/blacklist_item.h new file mode 100644 index 00000000..d28e41e7 --- /dev/null +++ b/logdoctor/modules/blacklists/modules/blacklist_item.h @@ -0,0 +1,46 @@ +#ifndef LOGDOCTOR__BLACKLISTS__BLACKLISTITEM__H +#define LOGDOCTOR__BLACKLISTS__BLACKLISTITEM__H + + +#include "modules/blacklists/modules/lib.h" + +#include +#include + + +struct BlacklistItem final +{ + bool used; + BlacklistField field; + std::vector list; + + BlacklistItem( const BlacklistField fld ) noexcept + : used{false}, field{fld} {} + BlacklistItem( const bool use, const BlacklistField fld ) noexcept + : used{use}, field{fld} {} + + //! \throw DoNotCatchException + const char* fieldName() const; + + //! \throw BWlistException, DoNotCatchException + void set( const std::vector& new_list ); + + //! \throw BWlistException, DoNotCatchException + void add( const std::string& item ); + + //! \throw BWlistException + void remove( const std::string& item ); + + //! \throw BWlistException + int moveUp( const std::string& item ); + + //! \throw BWlistException + int moveDown( const std::string& item ); + +private: + //! \throw BWlistException, DoNotCatchException + std::string sanitized( const std::string& item ) const; +}; + + +#endif // LOGDOCTOR__BLACKLISTS__BLACKLISTITEM__H diff --git a/logdoctor/modules/blacklists/modules/lib.h b/logdoctor/modules/blacklists/modules/lib.h new file mode 100644 index 00000000..1ac95591 --- /dev/null +++ b/logdoctor/modules/blacklists/modules/lib.h @@ -0,0 +1,11 @@ +#ifndef LOGDOCTOR__BLACKLISTS__LIB__H +#define LOGDOCTOR__BLACKLISTS__LIB__H + + +enum class BlacklistField +{ + Client +}; + + +#endif // LOGDOCTOR__BLACKLISTS__LIB__H diff --git a/logdoctor/modules/warnlists/modules/lib.h b/logdoctor/modules/warnlists/modules/lib.h new file mode 100644 index 00000000..56416235 --- /dev/null +++ b/logdoctor/modules/warnlists/modules/lib.h @@ -0,0 +1,14 @@ +#ifndef LOGDOCTOR__WARNLISTS__LIB__H +#define LOGDOCTOR__WARNLISTS__LIB__H + + +enum class WarnlistField +{ + Method, + Uri, + Client, + UserAgent +}; + + +#endif // LOGDOCTOR__WARNLISTS__LIB__H diff --git a/logdoctor/modules/warnlists/modules/warnlist.cpp b/logdoctor/modules/warnlists/modules/warnlist.cpp new file mode 100644 index 00000000..58b3f161 --- /dev/null +++ b/logdoctor/modules/warnlists/modules/warnlist.cpp @@ -0,0 +1,30 @@ + +#include "warnlist.h" + +#include "modules/exceptions.h" + + +Warnlist::Warnlist() noexcept + : method{ WarnlistField::Method } + , uri{ WarnlistField::Uri } + , client{ WarnlistField::Client } + , user_agent{ WarnlistField::UserAgent } +{ + +} + +WarnlistItem& Warnlist::get( const WarnlistField field ) +{ + switch (field) { + case WarnlistField::Method: + return this->method; + case WarnlistField::Uri: + return this->uri; + case WarnlistField::Client: + return this->client; + case WarnlistField::UserAgent: + return this->user_agent; + default: + throw DoNotCatchException( "Unexpected WarnlistField" ); + } +} diff --git a/logdoctor/modules/warnlists/modules/warnlist.h b/logdoctor/modules/warnlists/modules/warnlist.h new file mode 100644 index 00000000..2d29cda6 --- /dev/null +++ b/logdoctor/modules/warnlists/modules/warnlist.h @@ -0,0 +1,29 @@ +#ifndef LOGDOCTOR__WARNLISTS__WARNLIST__H +#define LOGDOCTOR__WARNLISTS__WARNLIST__H + + +#include "modules/warnlists/modules/warnlist_item.h" + + +struct Warnlist final +{ + WarnlistItem method; + WarnlistItem uri; + WarnlistItem client; + WarnlistItem user_agent; + + explicit Warnlist() noexcept; + + //! \throw DoNotCatchException + WarnlistItem& get( const WarnlistField field ); + + //! \throw DoNotCatchException + inline bool isUsed( const WarnlistField field ) + { return get( field ).used; } + + //! \throw DoNotCatchException + inline void setUsed( const WarnlistField field, const bool used ) + { get( field ).used = used; } +}; + +#endif // LOGDOCTOR__WARNLISTS__WARNLIST__H diff --git a/logdoctor/modules/warnlists/modules/warnlist_item.cpp b/logdoctor/modules/warnlists/modules/warnlist_item.cpp new file mode 100644 index 00000000..683cbd9c --- /dev/null +++ b/logdoctor/modules/warnlists/modules/warnlist_item.cpp @@ -0,0 +1,88 @@ + +#include "warnlist_item.h" + +#include "modules/shared.h" + +#include "modules/exceptions.h" + +#include "utilities/bwlists.h" + + +const char* WarnlistItem::fieldName() const +{ + switch (this->field) { + case WarnlistField::Method: + return FIELDS__METHOD.c_str(); + case WarnlistField::Uri: + return FIELDS__URI.c_str(); + case WarnlistField::Client: + return FIELDS__CLIENT.c_str(); + case WarnlistField::UserAgent: + return FIELDS__USER_AGENT.c_str(); + default: + // should be unreachable + throw DoNotCatchException( "Unexpected WarnlistField" ); + } +} + +void WarnlistItem::set( const std::vector& new_list ) +{ + this->list.clear(); + this->list.reserve( new_list.size() ); + for ( const std::string& item : new_list ) { + this->add( item ); + } +} + +void WarnlistItem::add( const std::string& item ) +{ + this->list.push_back( this->sanitized( item ) ); +} + +void WarnlistItem::remove( const std::string& item ) +{ + if ( const auto it{ std::find( this->list.cbegin(), this->list.cend(), item ) }; it != this->list.cend() ) { + this->list.erase( it ); + } else { + throw BWlistException( "Failed to remove the item: "+item ); + } +} + +int WarnlistItem::moveUp( const std::string& item ) +{ + if ( auto it{ std::find( std::next(this->list.begin()), this->list.end(), item ) }; it != this->list.end() ) { + const int pos{ static_cast( std::distance(this->list.begin(), it) ) - 1 }; + std::swap( *it, *std::prev(it) ); + return pos; + } else { + throw BWlistException( "Failed to move up the item: "+item ); + } +} + +int WarnlistItem::moveDown( const std::string& item ) +{ + if ( auto it{ std::find( this->list.begin(), std::prev(this->list.end()), item ) }; it != this->list.end() ) { + const int pos{ static_cast( std::distance(this->list.begin(), it) ) + 1 }; + std::swap( *it, *std::next(it) ); + return pos; + } else { + throw BWlistException( "Failed to move down the item: "+item ); + } +} + +std::string WarnlistItem::sanitized( const std::string& item ) const +{ + switch (this->field) { + case WarnlistField::Method: + return BWutils::sanitizedMethod( item ); + case WarnlistField::Uri: + return BWutils::sanitizedUri( item ); + case WarnlistField::Client: + return BWutils::sanitizedClient( item ); + case WarnlistField::UserAgent: + return BWutils::sanitizedUserAgent( item ); + default: + // should be unreachable + throw DoNotCatchException( "Unexpected WarnlistField" ); + } +} diff --git a/logdoctor/modules/warnlists/modules/warnlist_item.h b/logdoctor/modules/warnlists/modules/warnlist_item.h new file mode 100644 index 00000000..a28ef9b8 --- /dev/null +++ b/logdoctor/modules/warnlists/modules/warnlist_item.h @@ -0,0 +1,45 @@ +#ifndef LOGDOCTOR__WARNLISTS__WARNLISTITEM__H +#define LOGDOCTOR__WARNLISTS__WARNLISTITEM__H + + +#include "modules/warnlists/modules/lib.h" + +#include +#include + + +struct WarnlistItem final +{ + bool used; + WarnlistField field; + std::vector list; + + WarnlistItem( const WarnlistField fld ) noexcept + : used{false}, field{fld} {} + WarnlistItem( const bool use, const WarnlistField fld ) noexcept + : used{use}, field{fld} {} + + //! \throw DoNotCatchException + const char* fieldName() const; + + //! \throw BWlistException, DoNotCatchException + void set( const std::vector& new_list ); + + //! \throw BWlistException, DoNotCatchException + void add( const std::string& item ); + + //! \throw BWlistException + void remove( const std::string& item ); + + //! \throw BWlistException + int moveUp( const std::string& item ); + + //! \throw BWlistException + int moveDown( const std::string& item ); + +private: + //! \throw BWlistException, DoNotCatchException + std::string sanitized( const std::string& item ) const; +}; + +#endif // LOGDOCTOR__WARNLISTS__WARNLISTITEM__H diff --git a/logdoctor/modules/warnlists/warnlists.cpp b/logdoctor/modules/warnlists/warnlists.cpp new file mode 100644 index 00000000..a58810a0 --- /dev/null +++ b/logdoctor/modules/warnlists/warnlists.cpp @@ -0,0 +1,20 @@ + +#include "warnlists.h" + +#include "modules/exceptions.h" + + +Warnlist& Warnlists::get( const WebServer ws ) +{ + switch (ws) { + case WebServer::Apache: + return this->apache; + case WebServer::Nginx: + return this->nginx; + case WebServer::IIS: + return this->iis; + default: + // should be unreachable + throw DoNotCatchException( "Unexpected WebServer" ); + } +} diff --git a/logdoctor/modules/warnlists/warnlists.h b/logdoctor/modules/warnlists/warnlists.h new file mode 100644 index 00000000..febee83b --- /dev/null +++ b/logdoctor/modules/warnlists/warnlists.h @@ -0,0 +1,67 @@ +#ifndef LOGDOCTOR__WARNLISTS__H +#define LOGDOCTOR__WARNLISTS__H + + +#include "main_lib.h" + +#include "modules/warnlists/modules/warnlist.h" + + +struct Warnlists final +{ + Warnlist apache; + Warnlist nginx; + Warnlist iis; + + explicit Warnlists() noexcept = default; + + //! \throw DoNotCatchException + inline bool isUsed( const WebServer ws, const WarnlistField fld ) + { return get( ws ).isUsed( fld ); } + + //! \throw DoNotCatchException + inline void setUsed( const WebServer ws, const WarnlistField fld, const bool used ) + { get( ws ).setUsed( fld, used ); } + + //! \throw DoNotCatchException + Warnlist& get( const WebServer ws ); + + //! \throw DoNotCatchException + inline WarnlistItem& get( const WebServer ws, const WarnlistField fld ) + { return get( ws ).get( fld ); } + + //! \throw DoNotCatchException + inline std::vector& getList( const WebServer ws, const WarnlistField fld ) + { return get( ws, fld ).list; } + + //! \throw DoNotCatchException + inline const std::vector& getListConst( const WebServer ws, const WarnlistField fld ) + { return get( ws, fld ).list; } + + //! \throw BWlistException, DoNotCatchException + inline void setList( const WebServer ws, const WarnlistField fld, const std::vector& list ) + { get( ws, fld ).set( list ); } + + //! \throw BWlistException, DoNotCatchException + inline void clearList( const WebServer ws, const WarnlistField fld ) + { getList( ws, fld ).clear(); } + + //! \throw BWlistException, DoNotCatchException + inline void addItem( const WebServer ws, const WarnlistField fld, const std::string& item ) + { get( ws, fld ).add( item ); } + + //! \throw BWlistException + inline void removeItem( const WebServer ws, const WarnlistField fld, const std::string& item ) + { get( ws, fld ).remove( item ); } + + //! \throw BWlistException + inline int moveUpItem( const WebServer ws, const WarnlistField fld, const std::string& item ) + { return get( ws, fld ).moveUp( item ); } + + //! \throw BWlistException + inline int moveDownItem( const WebServer ws, const WarnlistField fld, const std::string& item ) + { return get( ws, fld ).moveDown( item ); } +}; + + +#endif // LOGDOCTOR__WARNLISTS__H