kfirewall: fix and adjust

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-12-02 04:28:48 +02:00
parent 5a7c5dfb30
commit 134d60c30c
6 changed files with 50 additions and 61 deletions

View file

@ -1,5 +1,14 @@
project(kfirewall)
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
include(FeatureSummary)
find_package(KDE4 4.21.0 REQUIRED)
include(KDE4Defaults)
include_directories(${KDE4_INCLUDES})
add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
endif()
find_program(IPTABLES_EXECUTABLE iptables-restore)
add_feature_info(iptables
IPTABLES_EXECUTABLE

View file

@ -26,7 +26,7 @@
#include <kuser.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kauthaction.h>
#include <kauthorization.h>
#include <kaboutdata.h>
#include <kpluginfactory.h>
#include <kpluginloader.h>
@ -69,7 +69,8 @@ KCMFirewall::KCMFirewall(QWidget* parent, const QVariantList& args)
about->addAuthor(ki18n("Ivailo Monev"), KLocalizedString(), "xakepa10@gmail.com");
setAboutData(about);
setNeedsAuthorization(true);
// TODO:
// setNeedsAuthorization(true);
load();

View file

@ -116,6 +116,5 @@
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View file

@ -20,7 +20,6 @@
#include <QProcess>
#include <kstandarddirs.h>
#include <kauthhelpersupport.h>
#include <kdebug.h>
static QByteArray ruleForSettings(const QByteArray &uservalue, const QByteArray &trafficvalue,
@ -102,22 +101,18 @@ static QByteArray rulesForParameters(const QVariantMap &parameters, const bool a
return iptablesruledata;
}
static ActionReply applyRules(KFirewallHelper *helper, const QString &iptablesexe,
static int applyRules(KFirewallHelper *helper, const QString &iptablesexe,
const QByteArray &iptablesruledata)
{
QProcess iptablesproc(helper);
iptablesproc.start(iptablesexe);
if (!iptablesproc.waitForStarted()) {
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
errorreply.setErrorDescription("Could not start iptables-restore");
errorreply.setErrorCode(3);
return errorreply;
kWarning() << "Could not start iptables-restore";
return KAuthorization::HelperError;
}
if (iptablesproc.write(iptablesruledata) != iptablesruledata.size()) {
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
errorreply.setErrorDescription("Could not write rules");
errorreply.setErrorCode(4);
return errorreply;
kWarning() << "Could not write rules";
return KAuthorization::HelperError;
}
iptablesproc.closeWriteChannel();
iptablesproc.waitForFinished();
@ -126,55 +121,45 @@ static ActionReply applyRules(KFirewallHelper *helper, const QString &iptablesex
if (errorstring.isEmpty()) {
errorstring = QString::fromLatin1("Could not apply rules");
}
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
errorreply.setErrorDescription(errorstring);
errorreply.setErrorCode(5);
return errorreply;
kWarning() << errorstring;
return KAuthorization::HelperError;
}
return KAuth::ActionReply::SuccessReply;
return KAuthorization::NoError;
}
ActionReply KFirewallHelper::apply(const QVariantMap &parameters)
int KFirewallHelper::apply(const QVariantMap &parameters)
{
if (parameters.isEmpty()) {
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
errorreply.setErrorDescription("Empty rules");
errorreply.setErrorCode(1);
return errorreply;
kWarning() << "Empty rules";
return KAuthorization::HelperError;
}
const QString iptablesexe = KStandardDirs::findRootExe("iptables-restore");
if (iptablesexe.isEmpty()) {
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
errorreply.setErrorDescription("Could not find iptables-restore");
errorreply.setErrorCode(2);
return errorreply;
kWarning() << "Could not find iptables-restore";
return KAuthorization::HelperError;
}
return applyRules(this, iptablesexe, rulesForParameters(parameters, true));
}
ActionReply KFirewallHelper::revert(const QVariantMap &parameters)
int KFirewallHelper::revert(const QVariantMap &parameters)
{
//qDebug() << Q_FUNC_INFO << parameters;
if (parameters.isEmpty()) {
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
errorreply.setErrorDescription("Empty rules");
errorreply.setErrorCode(1);
return errorreply;
kWarning() << "Empty rules";
return KAuthorization::HelperError;
}
const QString iptablesexe = KStandardDirs::findRootExe("iptables-restore");
if (iptablesexe.isEmpty()) {
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
errorreply.setErrorDescription("Could not find iptables-restore");
errorreply.setErrorCode(2);
return errorreply;
kWarning() << "Could not find iptables-restore";
return KAuthorization::HelperError;
}
return applyRules(this, iptablesexe, rulesForParameters(parameters, false));
}
KDE4_AUTH_HELPER_MAIN("org.kde.kcontrol.kcmkfirewall", KFirewallHelper)
K_AUTH_MAIN("org.kde.kcontrol.kcmkfirewall", KFirewallHelper)

View file

@ -19,17 +19,14 @@
#ifndef KFIREWALLHELPER_H
#define KFIREWALLHELPER_H
#include <kauthactionreply.h>
#include <kauthorization.h>
// methods return type must be ActionReply otherwise QMetaObject::invokeMethod() fails
using namespace KAuth;
class KFirewallHelper : public QObject
class KFirewallHelper : public KAuthorization
{
Q_OBJECT
public slots:
ActionReply apply(const QVariantMap &parameters);
ActionReply revert(const QVariantMap &parameters);
int apply(const QVariantMap &parameters);
int revert(const QVariantMap &parameters);
};
#endif // KFIREWALLHELPER_H

View file

@ -21,7 +21,7 @@
#include <QJsonDocument>
#include <QFile>
#include <kstandarddirs.h>
#include <kauthaction.h>
#include <kauthorization.h>
#include <kpluginfactory.h>
#include <kdebug.h>
@ -69,14 +69,13 @@ bool KFirewallModule::enable()
return true;
}
KAuth::Action kfirewallaction("org.kde.kcontrol.kcmkfirewall.apply");
kfirewallaction.setHelperID("org.kde.kcontrol.kcmkfirewall");
kfirewallaction.setArguments(m_kfirewallsettingsmap);
KAuth::ActionReply kfirewallreply = kfirewallaction.execute();
// qDebug() << Q_FUNC_INFO << kfirewallreply.errorCode() << kfirewallreply.errorDescription();
if (kfirewallreply != KAuth::ActionReply::SuccessReply) {
kWarning() << kfirewallreply.errorCode() << kfirewallreply.errorDescription();
const int kfirewallreply = KAuthorization::execute(
"org.kde.kcontrol.kcmkfirewall",
"apply",
m_kfirewallsettingsmap
);
if (kfirewallreply != KAuthorization::NoError) {
kWarning() << kfirewallreply;
return false;
}
@ -90,14 +89,13 @@ bool KFirewallModule::disable()
return true;
}
KAuth::Action kfirewallaction("org.kde.kcontrol.kcmkfirewall.revert");
kfirewallaction.setHelperID("org.kde.kcontrol.kcmkfirewall");
kfirewallaction.setArguments(m_kfirewallsettingsmap);
KAuth::ActionReply kfirewallreply = kfirewallaction.execute();
// qDebug() << Q_FUNC_INFO << kfirewallreply.errorCode() << kfirewallreply.errorDescription();
if (kfirewallreply != KAuth::ActionReply::SuccessReply) {
kWarning() << kfirewallreply.errorCode() << kfirewallreply.errorDescription();
const int kfirewallreply = KAuthorization::execute(
"org.kde.kcontrol.kcmkfirewall",
"revert",
m_kfirewallsettingsmap
);
if (kfirewallreply != KAuthorization::NoError) {
kWarning() << kfirewallreply;
return false;
}