diff --git a/core/DapBaseDataLocal.cpp b/core/DapBaseDataLocal.cpp index d069e154da1731e904e944d7a94bf0da012cca64..b29ada73367c48c8ec12d704f954e8f41772f180 100755 --- a/core/DapBaseDataLocal.cpp +++ b/core/DapBaseDataLocal.cpp @@ -275,10 +275,18 @@ void DapBaseDataLocal::saveSerialKeyData() void DapBaseDataLocal::resetSerialKeyData() { + qDebug() << "Resetting serial key data..."; + if (m_serialKeyData) { + qDebug() << "Serial key data exists, performing reset."; m_serialKeyData->reset(); this->saveToSettings(TEXT_SERIAL_KEY, *m_serialKeyData); + qDebug() << "Serial key data has been reset and saved to settings."; + } + else + { + qDebug() << "No serial key data to reset."; } } diff --git a/core/DapSession.cpp b/core/DapSession.cpp index efb2b4b7b0950324e80947af9479499ac078f46e..d8f99b6b2d231cf7f516558c66446e07931c6b52 100644 --- a/core/DapSession.cpp +++ b/core/DapSession.cpp @@ -820,57 +820,44 @@ void DapSession::answerBugReport() void DapSession::errorResetSerialKey(const QString& error) { - qDebug() << "Reset serial key error: network error"; - emit sigResetSerialKeyError (2, tr ("Reset error: ") + error); + qDebug() << "Reset serial key error: " << error; + int errorCode = (error == "Serial key expired") ? 1 : 2; + emit sigResetSerialKeyError(errorCode, tr("Reset error: ") + error); } +// At some point, expired keys started being identified as an error, +// which led to the invocation of the errorResetSerialKey callback. +// Therefore, duplication ("Serial key expired") is kept in both errorResetSerialKey and onResetSerialKey. void DapSession::onResetSerialKey() { - if(m_netKeyActivateReply->getReplyData().size() <= 0 ) - { - emit errorResetSerialKey (tr ("Wrong answer from server")); + if (m_netKeyActivateReply->getReplyData().isEmpty()) { + emit sigResetSerialKeyError(2, tr("Wrong answer from server")); return; } QByteArray replyArr; m_dapCryptCDB->decode(m_netKeyActivateReply->getReplyData(), replyArr, KeyRoleSession); - qDebug() << "Serial key reset reply: " << QString::fromUtf8(replyArr); + qDebug() << "Processing serial key reset, reply: " << QString::fromUtf8(replyArr); QString op_code = QString::fromUtf8(replyArr).left(4); - if (op_code == OP_CODE_GENERAL_ERR) - { - emit sigResetSerialKeyError (1, tr ("Unknown authorization error")); - return; - } - else if (op_code == OP_CODE_ALREADY_ACTIVATED) - { - emit sigResetSerialKeyError (1, tr ("Serial key already activated on another device")); - return; - } - else if (op_code == OP_CODE_NOT_FOUND_LOGIN_IN_DB) - { - emit sigResetSerialKeyError (1, isSerial ? tr("Serial key not found in database") : tr ("Login not found in database")); - return; - } - else if (op_code == OP_CODE_SUBSCRIBE_EXPIRED) - { - emit sigResetSerialKeyError (1, tr ("Serial key expired")); - return; - } - else if (op_code == OP_CODE_CANT_CONNECTION_TO_DB) - { - emit sigResetSerialKeyError (1, tr ("Can't connect to database")); - return; - } - else if (op_code == OP_CODE_INCORRECT_SYM) - { - emit sigResetSerialKeyError(1, tr ("Incorrect symbols in request")); + static const QMap<QString, QString> errorMessages = { + {OP_CODE_GENERAL_ERR, tr("Unknown authorization error")}, + {OP_CODE_ALREADY_ACTIVATED, tr("Serial key already activated on another device")}, + {OP_CODE_NOT_FOUND_LOGIN_IN_DB, isSerial ? tr("Serial key not found in database") : tr("Login not found in database")}, + {OP_CODE_SUBSCRIBE_EXPIRED, tr("Serial key expired")}, + {OP_CODE_CANT_CONNECTION_TO_DB, tr("Can't connect to database")}, + {OP_CODE_INCORRECT_SYM, tr("Incorrect symbols in request")} + }; + + if (errorMessages.contains(op_code)) { + emit sigResetSerialKeyError(1, errorMessages.value(op_code)); return; } - emit sigSerialKeyReseted (tr ("Serial key reset successfully")); + emit sigSerialKeyReseted(tr("Serial key reset successfully")); } + void DapSession::answerBugReportsStatus() { if (!m_netBugReportsStatusReply) diff --git a/core/UserConfigManager.cpp b/core/UserConfigManager.cpp index 29c67be6f2fb552d6e47bd2902f51bacb458d698..db5a2a69bbf320a99fb8d559a3f2645f621f9c33 100644 --- a/core/UserConfigManager.cpp +++ b/core/UserConfigManager.cpp @@ -38,17 +38,16 @@ bool UserConfigManager::configure() { } qDebug() << "User config path:" << userConfigPath; - if (!checkFolderExists(userConfigPath)) { + if (!checkFileExists(userConfigPath)) { qDebug() << "Config path does not exist:" << userConfigPath; return false; } #ifndef Q_OS_WIN - if (!changeOwnershipRecursively(userConfigPath, guiUser)) { + if (!changeOwnership(userConfigPath, guiUser)) { qDebug() << "Failed to change ownership for:" << userConfigPath; - return false; } - if (!setReadWritePermissionsRecursively(userConfigPath)) { + if (!setReadWritePermissions(userConfigPath)) { qDebug() << "Failed to change permissions for:" << userConfigPath; return false; } @@ -60,19 +59,51 @@ bool UserConfigManager::configure() { QString UserConfigManager::getGuiUser() const { QProcess process; + qDebug() << "Starting process to determine GUI user..."; + process.start(processName, QStringList()); - process.waitForFinished(); + if (!process.waitForFinished()) { + qWarning() << "Process failed to finish in a timely manner."; + return QString(); + } + QString output = process.readAllStandardOutput(); + qDebug() << "Process output:" << output; QStringList lines = output.split('\n'); + + // Regular expressions to match session identifiers + QRegExp guiSessionRegex(":\\d+"); // Matches GUI sessions like :0, :1, etc. + QRegExp ttyRegex("tty\\d+"); // Matches tty sessions like tty1, tty2, etc. + + // Priority check for GUI session identifiers + for (const QString& line : lines) { + qDebug() << "Processing line:" << line; + + if (line.contains(guiSessionRegex) || line.contains("wayland") || line.contains("seat0")) { + qDebug() << "Found high-priority GUI session line:" << line; + QStringList parts = line.split(QRegExp("\\s+"), Qt::SkipEmptyParts); + if (!parts.isEmpty()) { + qDebug() << "Returning GUI user (high-priority):" << parts[0]; + return parts[0]; + } + } + } + + // Secondary check for tty and pts sessions for (const QString& line : lines) { - if (line.contains(":0")) { // Check if the session is GUI-based + qDebug() << "Processing line (secondary):" << line; + if (line.contains(ttyRegex) || line.contains("pts")) { + qDebug() << "Found secondary session line:" << line; QStringList parts = line.split(QRegExp("\\s+"), Qt::SkipEmptyParts); if (!parts.isEmpty()) { + qDebug() << "Returning user (secondary):" << parts[0]; return parts[0]; } } } + + qWarning() << "No GUI user found."; return QString(); } @@ -85,62 +116,47 @@ QString UserConfigManager::getUserConfigPath(const QString& user) const { qDebug() << "Failed to retrieve home directory for user:" << user; return QString(); } - return QString::fromUtf8(pw->pw_dir) + "/.config/" + DAP_BRAND; + return QString::fromUtf8(pw->pw_dir) + "/.config/" + DAP_BRAND + "/" + DAP_BRAND + ".conf"; #endif } -bool UserConfigManager::checkFolderExists(const QString& folderPath) const { - QDir dir(folderPath); - return dir.exists(); +bool UserConfigManager::checkFileExists(const QString& filePath) const { + QFile file(filePath); + return file.exists(); } #ifndef Q_OS_WIN -bool UserConfigManager::changeOwnershipRecursively(const QString& folderPath, const QString& user) const { +bool UserConfigManager::changeOwnership(const QString& targetPath, const QString& user) const { struct passwd* pw = getpwnam(user.toUtf8().constData()); if (!pw) { - qDebug() << "Failed to retrieve user information for:" << user; + qDebug() << "Error: Failed to retrieve user information for:" << user; return false; } - if (chown(folderPath.toUtf8().constData(), pw->pw_uid, pw->pw_gid) != 0) { - perror(("Failed to change ownership for folder: " + folderPath).toUtf8().constData()); + if (chown(targetPath.toUtf8().constData(), pw->pw_uid, pw->pw_gid) != 0) { + qDebug() << "Error: Failed to change ownership for:" << targetPath + << "Error:" << strerror(errno); return false; } - QDirIterator it(folderPath, QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); - while (it.hasNext()) { - QString path = it.next(); - if (chown(path.toUtf8().constData(), pw->pw_uid, pw->pw_gid) != 0) { - perror(("Failed to change ownership for: " + path).toUtf8().constData()); - return false; - } - } - + qDebug() << "Ownership successfully changed for:" << targetPath; return true; } -bool UserConfigManager::setReadWritePermissionsRecursively(const QString& folderPath) const { +bool UserConfigManager::setReadWritePermissions(const QString& targetPath) const { mode_t mode_dir = S_IRUSR | S_IWUSR | S_IXUSR; mode_t mode_file = S_IRUSR | S_IWUSR; - if (chmod(folderPath.toUtf8().constData(), mode_dir) != 0) { - perror(("Failed to change permissions for folder: " + folderPath).toUtf8().constData()); - return false; - } - - QDirIterator it(folderPath, QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); - while (it.hasNext()) { - QString path = it.next(); + QFileInfo fileInfo(targetPath); + mode_t mode = fileInfo.isDir() ? mode_dir : mode_file; - QFileInfo fileInfo(path); - mode_t mode = fileInfo.isDir() ? mode_dir : mode_file; - - if (chmod(path.toUtf8().constData(), mode) != 0) { - perror(("Failed to change permissions for: " + path).toUtf8().constData()); - return false; - } + if (chmod(targetPath.toUtf8().constData(), mode) != 0) { + qDebug() << "Error: Failed to change permissions for:" << targetPath + << "Error:" << strerror(errno); + return false; } + qDebug() << "Permissions successfully changed for:" << targetPath; return true; } diff --git a/core/UserConfigManager.h b/core/UserConfigManager.h index f9ce0f114644afe1af9fa34299c79465f3020cf7..b556741f4bf06c83f358490da85b9fea174327ea 100644 --- a/core/UserConfigManager.h +++ b/core/UserConfigManager.h @@ -16,12 +16,12 @@ private: QString getUserConfigPath(const QString& user) const; - bool checkFolderExists(const QString& folderPath) const; + bool checkFileExists(const QString& folderPath) const; #ifndef Q_OS_WIN - bool changeOwnershipRecursively(const QString& folderPath, const QString& user) const; + bool changeOwnership(const QString& folderPath, const QString& user) const; - bool setReadWritePermissionsRecursively(const QString& folderPath) const; + bool setReadWritePermissions(const QString& folderPath) const; #endif }; diff --git a/vpn/client/DapCmdHandlers/DapCmdConnect.cpp b/vpn/client/DapCmdHandlers/DapCmdConnect.cpp index b23d46a06972e3e2fae6d12c072efa8e75b186cf..ed292a324194e803b23fde6519ecefaf1fcbd450 100644 --- a/vpn/client/DapCmdHandlers/DapCmdConnect.cpp +++ b/vpn/client/DapCmdHandlers/DapCmdConnect.cpp @@ -54,26 +54,17 @@ void DapCmdConnect::handle(const QJsonObject* params) { DapCmdServiceAbstract::handle(params); - if (params->contains(ACTION_KEY)) - { - //this is disconnect request - QString req = params->value(ACTION_KEY).toString(); - if (req == "Disconnect") - { + if (params->contains(ACTION_KEY)) { + QString req = params->value(ACTION_KEY).toString(""); + if (req == "Disconnect") { qDebug() << "DapCmdConnect::Disconnect signal"; emit sigDisconnect(); return; - } - - if (req == "RestartService") - { + } else if (req == "RestartService") { qDebug() << "DapCmdConnect::RestartService signal"; emit sigRestartService(false); return; - } - - if (req == "RestartServiceIfRunning") - { + } else if (req == "RestartServiceIfRunning") { qDebug() << "DapCmdConnect::RestartServiceIfRunning signal"; emit sigRestartService(true); return; @@ -86,27 +77,34 @@ void DapCmdConnect::handle(const QJsonObject* params) {UPDATE_ROUTE_TABLE, params->value(UPDATE_ROUTE_TABLE)} }; + if (!mandatoryConnParams[ADDRESS_KEY].isString() || !mandatoryConnParams[PORT_KEY].isDouble()) { + qWarning() << "Missing or invalid mandatory connection parameters"; + return; + } + bool updateRouteTable = mandatoryConnParams[UPDATE_ROUTE_TABLE].toBool(true); QString serialKey; - if(params->contains("serial")) - { - serialKey = params->value("serial").toString(); - } - else - { - serialKey = QString(DapServiceDataLocal::instance()->serialKeyData()->serialKey()).remove('-'); + if (params->contains("serial")) { + serialKey = params->value("serial").toString().remove('-'); + if (serialKey.isEmpty()) { + serialKey = DapServiceDataLocal::instance()->serialKeyData()->serialKey().remove('-'); + } + } else { + serialKey = DapServiceDataLocal::instance()->serialKeyData()->serialKey().remove('-'); } uint16_t port = uint16_t(mandatoryConnParams[PORT_KEY].toInt()); QString address = mandatoryConnParams[ADDRESS_KEY].toString(); - if (!serialKey.isEmpty()) - { + qDebug() << "Address:" << address << ", Port:" << port << ", UpdateRouteTable:" << updateRouteTable; + qDebug() << "SerialKey:" << (serialKey.isEmpty() ? "No serial key provided" : "Serial key provided"); + + if (!serialKey.isEmpty()) { emit sigConnect(serialKey, "", "", address, port, updateRouteTable); - } - else - { + } else { emit sigConnectNoAuth(address, port); } } + + diff --git a/vpn/client/DapCmdHandlers/DapCmdResetSerialKey.cpp b/vpn/client/DapCmdHandlers/DapCmdResetSerialKey.cpp index 85a75975fb2e8b14380d70fe0e57c2ef629ebff5..973e4a20495352c4d331c93247ea46666a5b55dd 100644 --- a/vpn/client/DapCmdHandlers/DapCmdResetSerialKey.cpp +++ b/vpn/client/DapCmdHandlers/DapCmdResetSerialKey.cpp @@ -3,22 +3,25 @@ DapCmdResetSerialKey::DapCmdResetSerialKey(QObject *parent) : DapCmdServiceAbstract (DapJsonCmdType::SEND_RESET_SERIAL_KEY_REQUEST, parent) { +} + +void DapCmdResetSerialKey::handle(const QJsonObject *params) +{ + DapCmdServiceAbstract::handle(params); + connect(this, &DapCmdResetSerialKey::sigResetSerialKeyReplied, [&] (const QString& reply) { QJsonObject l_obj; l_obj["reset_reply"] = reply; sendCmd(&l_obj); }); -} -void DapCmdResetSerialKey::handle(const QJsonObject *params) -{ - DapCmdServiceAbstract::handle(params); emit sigResetRequestSent(); } void DapCmdResetSerialKey::sendResetSerialError(const int& a_errorCode, const QString& error) { - qWarning() <<"Error message: "<< error; + qWarning() << "Error code: " << a_errorCode << ", message: " << error; + QJsonObject response; QJsonObject errorObj; @@ -28,3 +31,4 @@ void DapCmdResetSerialKey::sendResetSerialError(const int& a_errorCode, const QS sendCmd(&response); } + diff --git a/vpn/qml/handlers/DapCmdResetSerialKey.cpp b/vpn/qml/handlers/DapCmdResetSerialKey.cpp index 3e5650b0b7323acef307115d3dccebddc8605503..c57a6a597d973bb7298cca282c60f609a0558506 100644 --- a/vpn/qml/handlers/DapCmdResetSerialKey.cpp +++ b/vpn/qml/handlers/DapCmdResetSerialKey.cpp @@ -22,16 +22,21 @@ void DapCmdResetSerialKey::handleResult(const QJsonObject& result) void DapCmdResetSerialKey::handleError(int code, const QString& message) { - Q_UNUSED(code); Q_UNUSED(message); + qWarning() << "Error code:" << code << ", message:" << message; qWarning() << *m_errorObject; switch (code) { case 1: + // Code 1 indicates a non-critical reset error that allows clearing the local key storage. + // In this case, the user will not lose the key if it is still valid. emit sigResetSerialKeyError(message); break; case 2: emit sigResetSerialKeyErrorSetOnlyMessage(message); break; + default: + qWarning() << "Unknown error code received."; + break; } }