From 0de55d84c9d32a6905448285f5d4bf8b4889c9ea Mon Sep 17 00:00:00 2001 From: lunardunno <126363523+lunardunno@users.noreply.github.com> Date: Sat, 16 Nov 2024 09:50:37 +0400 Subject: [PATCH] Checking requirements in script (#1210) * Checking requirements in script Checking requirements for sudo users in script * Adding error handling Adding error handling in the server controller for: Sudo package is not pre-installed for sudo users. Server user or associated group is not listed in the sudoers file. Server user password required * adding error codes * added extended error descriptions --- client/core/controllers/serverController.cpp | 6 ++++++ client/core/defs.h | 3 +++ client/core/errorstrings.cpp | 3 +++ client/server_scripts/check_user_in_sudo.sh | 3 ++- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/client/core/controllers/serverController.cpp b/client/core/controllers/serverController.cpp index ec4c1f8d7..be659fb32 100644 --- a/client/core/controllers/serverController.cpp +++ b/client/core/controllers/serverController.cpp @@ -766,6 +766,12 @@ ErrorCode ServerController::isUserInSudo(const ServerCredentials &credentials, D if (!stdOut.contains("root :") && !stdOut.contains(" sudo") && !stdOut.contains(" wheel")) return ErrorCode::ServerUserNotInSudo; + if (stdOut.contains("command not found")) + return ErrorCode::SudoPackageIsNotPreinstalled; + if (stdOut.contains("sudoers")) + return ErrorCode::ServerUserNotListedInSudoers; + if (stdOut.contains("password is required")) + return ErrorCode::ServerUserPasswordRequired; return error; } diff --git a/client/core/defs.h b/client/core/defs.h index d00d347b1..13f881b72 100644 --- a/client/core/defs.h +++ b/client/core/defs.h @@ -56,6 +56,9 @@ namespace amnezia ServerCancelInstallation = 204, ServerUserNotInSudo = 205, ServerPacketManagerError = 206, + SudoPackageIsNotPreinstalled = 207, + ServerUserNotListedInSudoers = 208, + ServerUserPasswordRequired = 209, // Ssh connection errors SshRequestDeniedError = 300, diff --git a/client/core/errorstrings.cpp b/client/core/errorstrings.cpp index 495346063..7867ac5af 100644 --- a/client/core/errorstrings.cpp +++ b/client/core/errorstrings.cpp @@ -21,6 +21,9 @@ QString errorString(ErrorCode code) { case(ErrorCode::ServerCancelInstallation): errorMessage = QObject::tr("Installation canceled by user"); break; case(ErrorCode::ServerUserNotInSudo): errorMessage = QObject::tr("The user does not have permission to use sudo"); break; case(ErrorCode::ServerPacketManagerError): errorMessage = QObject::tr("Server error: Packet manager error"); break; + case(ErrorCode::SudoPackageIsNotPreinstalled): errorMessage = QObject::tr("The sudo package is not pre-installed"); break; + case(ErrorCode::ServerUserNotListedInSudoers): errorMessage = QObject::tr("The user is not listed in sudoers"); break; + case(ErrorCode::ServerUserPasswordRequired): errorMessage = QObject::tr("The user's password is required"); break; // Libssh errors case(ErrorCode::SshRequestDeniedError): errorMessage = QObject::tr("SSH request was denied"); break; diff --git a/client/server_scripts/check_user_in_sudo.sh b/client/server_scripts/check_user_in_sudo.sh index e7ee953cd..a5922c850 100644 --- a/client/server_scripts/check_user_in_sudo.sh +++ b/client/server_scripts/check_user_in_sudo.sh @@ -1,2 +1,3 @@ +echo $LC_MESSAGES | grep -qE "en_US.UTF-8|C.UTF-8" || export LC_MESSAGES=C.UTF-8;\ CUR_USER=$(whoami);\ -groups $CUR_USER \ No newline at end of file +groups $CUR_USER | grep sudo && sudo -nu $CUR_USER sudo -n uname > /dev/null