File indexing completed on 2025-01-05 05:07:02

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2020 Lucas Biaggi <lbjanuario@gmail.com>
0003 /*
0004  * Firewalld backend for plasma firewall
0005  */
0006 
0007 #ifndef FIREWALLDCLIENT_H
0008 #define FIREWALLDCLIENT_H
0009 
0010 #include <QLoggingCategory>
0011 #include <QString>
0012 #include <QTimer>
0013 
0014 #include <ifirewallclientbackend.h>
0015 #include <profile.h>
0016 
0017 class RuleListModel;
0018 class LogListModel;
0019 struct firewalld_reply;
0020 
0021 Q_DECLARE_LOGGING_CATEGORY(FirewallDClientDebug)
0022 
0023 class Q_DECL_EXPORT FirewalldClient : public IFirewallClientBackend
0024 {
0025     Q_OBJECT
0026 public:
0027     explicit FirewalldClient(QObject *parent, const QVariantList &args);
0028 
0029     void refresh() override;
0030     RuleListModel *rules() const override;
0031     Rule *ruleAt(int index) override;
0032     KJob *addRule(Rule *rule) override;
0033     KJob *removeRule(int index) override;
0034     KJob *updateRule(Rule *rule) override;
0035     KJob *moveRule(int from, int to) override;
0036     KJob *queryStatus(FirewallClient::DefaultDataBehavior defaultsBehavior, FirewallClient::ProfilesBehavior profilesBehavior) override;
0037     KJob *setDefaultIncomingPolicy(QString defaultIncomingPolicy) override;
0038     KJob *setDefaultOutgoingPolicy(QString defaultOutgoingPolicy) override;
0039     KJob *setEnabled(const bool enabled) override;
0040     KJob *save() override;
0041     bool isTcpAndUdp(int protocolIdx) override;
0042 
0043     /* Creates a new Rule and returns it to the Qml side, passing arguments based
0044      * on the Connection Table. */
0045     Rule *createRuleFromConnection(const QString &protocol, const QString &localAddress, const QString &foreignAddres, const QString &status) override;
0046 
0047     Rule *createRuleFromLog(const QString &protocol,
0048                             const QString &sourceAddress,
0049                             const QString &sourcePort,
0050                             const QString &destinationAddress,
0051                             const QString &destinationPort,
0052                             const QString &inn) override;
0053 
0054     bool enabled() const override;
0055     QString defaultIncomingPolicy() const override;
0056     QString defaultOutgoingPolicy() const override;
0057     QString name() const override;
0058 
0059     FirewallClient::Capabilities capabilities() const override;
0060     LogListModel *logs() override;
0061     bool logsAutoRefresh() const override;
0062     void setLogsAutoRefresh(bool logsAutoRefresh) override;
0063     static IFirewallClientBackend *createMethod(FirewallClient *parent);
0064     void refreshProfiles() override;
0065     QStringList knownProtocols() override;
0066     bool isCurrentlyLoaded() const override;
0067     bool supportsRuleUpdate() const override;
0068     QString version() const override;
0069     QStringList knownApplications() override;
0070 protected slots:
0071     void refreshLogs();
0072 
0073 protected:
0074     QList<Rule *> extractRulesFromResponse(const QList<firewalld_reply> &reply) const;
0075     QList<Rule *> extractRulesFromResponse(const QStringList &reply) const;
0076     QVariantList buildRule(const Rule *r) const;
0077     void setProfile(Profile profile);
0078     void queryKnownApplications();
0079     void getDefaultIncomingPolicyFromDbus();
0080 
0081 private:
0082     QString m_status;
0083     QStringList m_rawLogs;
0084     Profile m_currentProfile;
0085     RuleListModel *const m_rulesModel;
0086     LogListModel *m_logs = nullptr;
0087     QTimer m_logsRefreshTimer;
0088     bool m_logsAutoRefresh;
0089     QStringList m_knownApplications;
0090 };
0091 
0092 #endif // FIREWALLDCLIENT_H