mirror of
https://bitbucket.org/smil3y/kde-playground.git
synced 2025-02-23 18:32:51 +00:00
kfirewall: fix and adjust
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
5a7c5dfb30
commit
134d60c30c
6 changed files with 50 additions and 61 deletions
|
@ -1,5 +1,14 @@
|
||||||
project(kfirewall)
|
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)
|
find_program(IPTABLES_EXECUTABLE iptables-restore)
|
||||||
add_feature_info(iptables
|
add_feature_info(iptables
|
||||||
IPTABLES_EXECUTABLE
|
IPTABLES_EXECUTABLE
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
#include <kuser.h>
|
#include <kuser.h>
|
||||||
#include <klocale.h>
|
#include <klocale.h>
|
||||||
#include <kmessagebox.h>
|
#include <kmessagebox.h>
|
||||||
#include <kauthaction.h>
|
#include <kauthorization.h>
|
||||||
#include <kaboutdata.h>
|
#include <kaboutdata.h>
|
||||||
#include <kpluginfactory.h>
|
#include <kpluginfactory.h>
|
||||||
#include <kpluginloader.h>
|
#include <kpluginloader.h>
|
||||||
|
@ -69,7 +69,8 @@ KCMFirewall::KCMFirewall(QWidget* parent, const QVariantList& args)
|
||||||
about->addAuthor(ki18n("Ivailo Monev"), KLocalizedString(), "xakepa10@gmail.com");
|
about->addAuthor(ki18n("Ivailo Monev"), KLocalizedString(), "xakepa10@gmail.com");
|
||||||
setAboutData(about);
|
setAboutData(about);
|
||||||
|
|
||||||
setNeedsAuthorization(true);
|
// TODO:
|
||||||
|
// setNeedsAuthorization(true);
|
||||||
|
|
||||||
load();
|
load();
|
||||||
|
|
||||||
|
|
|
@ -116,6 +116,5 @@
|
||||||
<container>1</container>
|
<container>1</container>
|
||||||
</customwidget>
|
</customwidget>
|
||||||
</customwidgets>
|
</customwidgets>
|
||||||
<resources/>
|
|
||||||
<connections/>
|
<connections/>
|
||||||
</ui>
|
</ui>
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
|
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <kstandarddirs.h>
|
#include <kstandarddirs.h>
|
||||||
#include <kauthhelpersupport.h>
|
|
||||||
#include <kdebug.h>
|
#include <kdebug.h>
|
||||||
|
|
||||||
static QByteArray ruleForSettings(const QByteArray &uservalue, const QByteArray &trafficvalue,
|
static QByteArray ruleForSettings(const QByteArray &uservalue, const QByteArray &trafficvalue,
|
||||||
|
@ -102,22 +101,18 @@ static QByteArray rulesForParameters(const QVariantMap ¶meters, const bool a
|
||||||
return iptablesruledata;
|
return iptablesruledata;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ActionReply applyRules(KFirewallHelper *helper, const QString &iptablesexe,
|
static int applyRules(KFirewallHelper *helper, const QString &iptablesexe,
|
||||||
const QByteArray &iptablesruledata)
|
const QByteArray &iptablesruledata)
|
||||||
{
|
{
|
||||||
QProcess iptablesproc(helper);
|
QProcess iptablesproc(helper);
|
||||||
iptablesproc.start(iptablesexe);
|
iptablesproc.start(iptablesexe);
|
||||||
if (!iptablesproc.waitForStarted()) {
|
if (!iptablesproc.waitForStarted()) {
|
||||||
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
|
kWarning() << "Could not start iptables-restore";
|
||||||
errorreply.setErrorDescription("Could not start iptables-restore");
|
return KAuthorization::HelperError;
|
||||||
errorreply.setErrorCode(3);
|
|
||||||
return errorreply;
|
|
||||||
}
|
}
|
||||||
if (iptablesproc.write(iptablesruledata) != iptablesruledata.size()) {
|
if (iptablesproc.write(iptablesruledata) != iptablesruledata.size()) {
|
||||||
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
|
kWarning() << "Could not write rules";
|
||||||
errorreply.setErrorDescription("Could not write rules");
|
return KAuthorization::HelperError;
|
||||||
errorreply.setErrorCode(4);
|
|
||||||
return errorreply;
|
|
||||||
}
|
}
|
||||||
iptablesproc.closeWriteChannel();
|
iptablesproc.closeWriteChannel();
|
||||||
iptablesproc.waitForFinished();
|
iptablesproc.waitForFinished();
|
||||||
|
@ -126,55 +121,45 @@ static ActionReply applyRules(KFirewallHelper *helper, const QString &iptablesex
|
||||||
if (errorstring.isEmpty()) {
|
if (errorstring.isEmpty()) {
|
||||||
errorstring = QString::fromLatin1("Could not apply rules");
|
errorstring = QString::fromLatin1("Could not apply rules");
|
||||||
}
|
}
|
||||||
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
|
kWarning() << errorstring;
|
||||||
errorreply.setErrorDescription(errorstring);
|
return KAuthorization::HelperError;
|
||||||
errorreply.setErrorCode(5);
|
|
||||||
return errorreply;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return KAuth::ActionReply::SuccessReply;
|
return KAuthorization::NoError;
|
||||||
}
|
}
|
||||||
|
|
||||||
ActionReply KFirewallHelper::apply(const QVariantMap ¶meters)
|
int KFirewallHelper::apply(const QVariantMap ¶meters)
|
||||||
{
|
{
|
||||||
if (parameters.isEmpty()) {
|
if (parameters.isEmpty()) {
|
||||||
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
|
kWarning() << "Empty rules";
|
||||||
errorreply.setErrorDescription("Empty rules");
|
return KAuthorization::HelperError;
|
||||||
errorreply.setErrorCode(1);
|
|
||||||
return errorreply;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString iptablesexe = KStandardDirs::findRootExe("iptables-restore");
|
const QString iptablesexe = KStandardDirs::findRootExe("iptables-restore");
|
||||||
if (iptablesexe.isEmpty()) {
|
if (iptablesexe.isEmpty()) {
|
||||||
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
|
kWarning() << "Could not find iptables-restore";
|
||||||
errorreply.setErrorDescription("Could not find iptables-restore");
|
return KAuthorization::HelperError;
|
||||||
errorreply.setErrorCode(2);
|
|
||||||
return errorreply;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return applyRules(this, iptablesexe, rulesForParameters(parameters, true));
|
return applyRules(this, iptablesexe, rulesForParameters(parameters, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
ActionReply KFirewallHelper::revert(const QVariantMap ¶meters)
|
int KFirewallHelper::revert(const QVariantMap ¶meters)
|
||||||
{
|
{
|
||||||
//qDebug() << Q_FUNC_INFO << parameters;
|
//qDebug() << Q_FUNC_INFO << parameters;
|
||||||
|
|
||||||
if (parameters.isEmpty()) {
|
if (parameters.isEmpty()) {
|
||||||
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
|
kWarning() << "Empty rules";
|
||||||
errorreply.setErrorDescription("Empty rules");
|
return KAuthorization::HelperError;
|
||||||
errorreply.setErrorCode(1);
|
|
||||||
return errorreply;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString iptablesexe = KStandardDirs::findRootExe("iptables-restore");
|
const QString iptablesexe = KStandardDirs::findRootExe("iptables-restore");
|
||||||
if (iptablesexe.isEmpty()) {
|
if (iptablesexe.isEmpty()) {
|
||||||
KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError);
|
kWarning() << "Could not find iptables-restore";
|
||||||
errorreply.setErrorDescription("Could not find iptables-restore");
|
return KAuthorization::HelperError;
|
||||||
errorreply.setErrorCode(2);
|
|
||||||
return errorreply;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return applyRules(this, iptablesexe, rulesForParameters(parameters, false));
|
return applyRules(this, iptablesexe, rulesForParameters(parameters, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
KDE4_AUTH_HELPER_MAIN("org.kde.kcontrol.kcmkfirewall", KFirewallHelper)
|
K_AUTH_MAIN("org.kde.kcontrol.kcmkfirewall", KFirewallHelper)
|
||||||
|
|
|
@ -19,17 +19,14 @@
|
||||||
#ifndef KFIREWALLHELPER_H
|
#ifndef KFIREWALLHELPER_H
|
||||||
#define KFIREWALLHELPER_H
|
#define KFIREWALLHELPER_H
|
||||||
|
|
||||||
#include <kauthactionreply.h>
|
#include <kauthorization.h>
|
||||||
|
|
||||||
// methods return type must be ActionReply otherwise QMetaObject::invokeMethod() fails
|
class KFirewallHelper : public KAuthorization
|
||||||
using namespace KAuth;
|
|
||||||
|
|
||||||
class KFirewallHelper : public QObject
|
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public slots:
|
public slots:
|
||||||
ActionReply apply(const QVariantMap ¶meters);
|
int apply(const QVariantMap ¶meters);
|
||||||
ActionReply revert(const QVariantMap ¶meters);
|
int revert(const QVariantMap ¶meters);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // KFIREWALLHELPER_H
|
#endif // KFIREWALLHELPER_H
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <kstandarddirs.h>
|
#include <kstandarddirs.h>
|
||||||
#include <kauthaction.h>
|
#include <kauthorization.h>
|
||||||
#include <kpluginfactory.h>
|
#include <kpluginfactory.h>
|
||||||
#include <kdebug.h>
|
#include <kdebug.h>
|
||||||
|
|
||||||
|
@ -69,14 +69,13 @@ bool KFirewallModule::enable()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
KAuth::Action kfirewallaction("org.kde.kcontrol.kcmkfirewall.apply");
|
const int kfirewallreply = KAuthorization::execute(
|
||||||
kfirewallaction.setHelperID("org.kde.kcontrol.kcmkfirewall");
|
"org.kde.kcontrol.kcmkfirewall",
|
||||||
kfirewallaction.setArguments(m_kfirewallsettingsmap);
|
"apply",
|
||||||
KAuth::ActionReply kfirewallreply = kfirewallaction.execute();
|
m_kfirewallsettingsmap
|
||||||
// qDebug() << Q_FUNC_INFO << kfirewallreply.errorCode() << kfirewallreply.errorDescription();
|
);
|
||||||
|
if (kfirewallreply != KAuthorization::NoError) {
|
||||||
if (kfirewallreply != KAuth::ActionReply::SuccessReply) {
|
kWarning() << kfirewallreply;
|
||||||
kWarning() << kfirewallreply.errorCode() << kfirewallreply.errorDescription();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,14 +89,13 @@ bool KFirewallModule::disable()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
KAuth::Action kfirewallaction("org.kde.kcontrol.kcmkfirewall.revert");
|
const int kfirewallreply = KAuthorization::execute(
|
||||||
kfirewallaction.setHelperID("org.kde.kcontrol.kcmkfirewall");
|
"org.kde.kcontrol.kcmkfirewall",
|
||||||
kfirewallaction.setArguments(m_kfirewallsettingsmap);
|
"revert",
|
||||||
KAuth::ActionReply kfirewallreply = kfirewallaction.execute();
|
m_kfirewallsettingsmap
|
||||||
// qDebug() << Q_FUNC_INFO << kfirewallreply.errorCode() << kfirewallreply.errorDescription();
|
);
|
||||||
|
if (kfirewallreply != KAuthorization::NoError) {
|
||||||
if (kfirewallreply != KAuth::ActionReply::SuccessReply) {
|
kWarning() << kfirewallreply;
|
||||||
kWarning() << kfirewallreply.errorCode() << kfirewallreply.errorDescription();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue