From 6fcbdeb461133a4befafbb67cf37ddf769f0bcff Mon Sep 17 00:00:00 2001 From: TD-er Date: Fri, 3 Dec 2021 16:13:20 +0100 Subject: [PATCH] [Password] Add clearPassword command As suggested here: https://github.com/letscontrolit/ESPEasy/issues/3860 --- docs/source/Plugin/P000_commands.repl | 11 ++++++++++- src/src/Commands/InternalCommands.cpp | 1 + src/src/Commands/Settings.cpp | 14 ++++++++++++++ src/src/Commands/Settings.h | 1 + src/src/DataStructs/SecurityStruct.cpp | 10 ++++++++++ src/src/DataStructs/SecurityStruct.h | 2 ++ 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/docs/source/Plugin/P000_commands.repl b/docs/source/Plugin/P000_commands.repl index cf850b393..18ea544b5 100644 --- a/docs/source/Plugin/P000_commands.repl +++ b/docs/source/Plugin/P000_commands.repl @@ -50,6 +50,14 @@ See also ``AccessInfo``. ``ClearAccessBlock``" + + " + ClearPassword"," + :red:`Internal`"," + Clear the password of the unit. + See also ``Password``. + + ``ClearPassword,``" " ClearRTCram"," :red:`Internal`"," @@ -287,7 +295,8 @@ " Password"," :red:`Internal`"," - Set the password of the unit + Set the password of the unit. + See also ``ClearPassword``. ``Password,``" " diff --git a/src/src/Commands/InternalCommands.cpp b/src/src/Commands/InternalCommands.cpp index a1584ca58..6a1a1fe85 100644 --- a/src/src/Commands/InternalCommands.cpp +++ b/src/src/Commands/InternalCommands.cpp @@ -268,6 +268,7 @@ bool executeInternalCommand(command_case_data & data) } case 'c': { COMMAND_CASE_R( "clearaccessblock", Command_AccessInfo_Clear, 0); // Network Command + COMMAND_CASE_R( "clearpasword", Command_Settings_Password_Clear, 1); // Settings.h COMMAND_CASE_R( "clearrtcram", Command_RTC_Clear, 0); // RTC.h COMMAND_CASE_R( "config", Command_Task_RemoteConfig, -1); // Tasks.h COMMAND_CASE_R("controllerdisable", Command_Controller_Disable, 1); // Controller.h diff --git a/src/src/Commands/Settings.cpp b/src/src/Commands/Settings.cpp index 61b2bf293..0e16e99b8 100644 --- a/src/src/Commands/Settings.cpp +++ b/src/src/Commands/Settings.cpp @@ -62,6 +62,20 @@ String Command_Settings_Password(struct EventStruct *event, const char* Line) ); } +String Command_Settings_Password_Clear(struct EventStruct *event, const char* Line) +{ + const String storedPassword = SecuritySettings.getPassword(); + if (storedPassword.length() > 0) { + // There is a password set, so we must check it. + const String password = parseStringKeepCase(Line, 2); + if (!storedPassword.equals(password)) { + return return_command_failed(); + } + ZERO_FILL(SecuritySettings.Password); + } + return return_command_success(); +} + const __FlashStringHelper * Command_Settings_Save(struct EventStruct *event, const char* Line) { SaveSettings(); diff --git a/src/src/Commands/Settings.h b/src/src/Commands/Settings.h index b6dc63a0c..bf39baf2a 100644 --- a/src/src/Commands/Settings.h +++ b/src/src/Commands/Settings.h @@ -7,6 +7,7 @@ String Command_Settings_Build(struct EventStruct *event, const char* Line); String Command_Settings_Unit(struct EventStruct *event, const char* Line); String Command_Settings_Name(struct EventStruct *event, const char* Line); String Command_Settings_Password(struct EventStruct *event, const char* Line); +String Command_Settings_Password_Clear(struct EventStruct *event, const char* Line); const __FlashStringHelper * Command_Settings_Save(struct EventStruct *event, const char* Line); const __FlashStringHelper * Command_Settings_Load(struct EventStruct *event, const char* Line); const __FlashStringHelper * Command_Settings_Print(struct EventStruct *event, const char* Line); diff --git a/src/src/DataStructs/SecurityStruct.cpp b/src/src/DataStructs/SecurityStruct.cpp index c9caba1d8..2a81ea4be 100644 --- a/src/src/DataStructs/SecurityStruct.cpp +++ b/src/src/DataStructs/SecurityStruct.cpp @@ -68,3 +68,13 @@ bool SecurityStruct::hasWiFiCredentials(SecurityStruct::WiFiCredentialsSlot slot } return false; } + +String SecurityStruct::getPassword() const { + String res; + const size_t passLength = strnlen(Password, sizeof(Password)); + res.reserve(passLength); + for (size_t i = 0; i < passLength; ++i) { + res += Password[i]; + } + return res; +} \ No newline at end of file diff --git a/src/src/DataStructs/SecurityStruct.h b/src/src/DataStructs/SecurityStruct.h index 49c66acc1..fca718e48 100644 --- a/src/src/DataStructs/SecurityStruct.h +++ b/src/src/DataStructs/SecurityStruct.h @@ -27,6 +27,8 @@ struct SecurityStruct bool hasWiFiCredentials(WiFiCredentialsSlot slot) const; + String getPassword() const; + char WifiSSID[32]; char WifiKey[64]; char WifiSSID2[32];