rule.h 6.63 KB
Newer Older
1
2
3
4
5
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2011 Craig Drummond <craig.p.drummond@gmail.com>
// SPDX-FileCopyrightText: 2018 Alexis Lopes Zubeta <contact@azubieta.net>
// SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@kde.org>

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
6
7
8
#ifndef UFW_RULE_H
#define UFW_RULE_H

9
#include <kcm_firewall_core_export.h>
10
#include <sys/socket.h>
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
11
#include <QObject>
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
12
#include <QString>
13
#include <QDebug>
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
14

Lucas Biaggi's avatar
Lucas Biaggi committed
15
16
#include "types.h"

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
17
class KCM_FIREWALL_CORE_EXPORT Rule : public QObject
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
18
{
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
19
20
21
22
23
24
25
26
27
28
    Q_OBJECT
    Q_PROPERTY(QString policy READ policy WRITE setPolicy NOTIFY policyChanged)
    Q_PROPERTY(bool incoming READ incoming WRITE setIncoming NOTIFY incomingChanged)
    Q_PROPERTY(QString sourceAddress READ sourceAddress WRITE setSourceAddress NOTIFY sourceAddressChanged)
    Q_PROPERTY(QString sourcePort READ sourcePort WRITE setSourcePort NOTIFY sourcePortChanged)
    Q_PROPERTY(QString destinationAddress READ destinationAddress WRITE setDestinationAddress NOTIFY destinationAddressChanged)
    Q_PROPERTY(QString destinationPort READ destinationPort WRITE setDestinationPort NOTIFY destinationPortChanged)
    Q_PROPERTY(bool ipv6 READ ipv6 WRITE setIpv6 NOTIFY ipv6Changed)
    Q_PROPERTY(int protocol READ protocol WRITE setProtocol NOTIFY protocolChanged)
    Q_PROPERTY(int interface READ interface WRITE setInterface NOTIFY interfaceChanged)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
29
    Q_PROPERTY(QString logging READ loggingStr WRITE setLogging NOTIFY loggingChanged)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
30
31
    Q_PROPERTY(int position READ position WRITE setPosition NOTIFY positionChanged)

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
32
public:
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
33

34
    static int servicePort(const QString &name);
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
35
36
    static QString protocolSuffix(int prot, const QString &sep = QString("/"));
    static QString modify(const QString &address, const QString &port, const QString &application, const QString &iface, int protocol, bool matchPortNoProto = false);
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
37
38

    Rule();
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
39
40
41
    Rule(Types::Policy pol,
         bool incomming,
         Types::Logging log,
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
42
         int protocolIdx,
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
43
44
45
46
47
48
49
50
51
52
         const QString &srcHost = QString(),
         const QString &srcPort = QString(),
         const QString &destHost = QString(),
         const QString &destPort = QString(),
         const QString &ifaceIn = QString(),
         const QString &ifaceOut = QString(),
         const QString &srcApp = QString(),
         const QString &destApp = QString(),
         unsigned int i = 0,
         bool ipv6 = false)
53
54
55
56
        : m_position(i)
        , m_action(pol)
        , m_incoming(incomming)
        , m_ipv6(ipv6)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
57
        , m_protocol(protocolIdx)
58
59
60
61
62
63
64
65
        , m_logtype(log)
        , m_destApplication(destApp)
        , m_sourceApplication(srcApp)
        , m_destAddress(destHost)
        , m_sourceAddress(srcHost)
        , m_destPort(destPort)
        , m_sourcePort(srcPort)
        , m_interfaceIn(ifaceIn)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
66
        , m_interfaceOut(ifaceOut)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
67
        , m_interface(0)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
68
69
    {
    }
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
70
71
72
73
74
75
    Rule(const Rule& rhs, QObject *parent) : QObject(parent), m_position(rhs.m_position), m_action(rhs.m_action),
    m_incoming(rhs.m_incoming), m_ipv6(rhs.m_ipv6), m_protocol(rhs.m_protocol),
    m_logtype(rhs.m_logtype), m_destApplication(rhs.m_destApplication),
    m_sourceApplication(rhs.m_sourceApplication), m_destAddress(rhs.m_destAddress),
    m_sourceAddress(rhs.m_sourceAddress), m_destPort(rhs.m_destPort),
    m_sourcePort(rhs.m_sourcePort), m_interfaceIn(rhs.m_interfaceIn),
76
    m_interfaceOut(rhs.m_interfaceIn), m_interface(rhs.m_interface){};
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
77

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
78

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
79
80
81
    QString toStr() const;
    QString fromStr() const;
    QString actionStr() const;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
82
83
84
85
86
87
88
89
90
91
92
93
94
    QString loggingStr() const;
    QString policy() const;
    QString destinationAddress() const;
    QString destinationPort() const;
    QString sourceApplication() const;
    QString sourceAddress() const;
    QString sourcePort() const;
    QString interfaceIn() const;
    QString interfaceOut() const;
    QString destinationApplication() const;
    Types::Logging logging() const;

    // TODO: are they really necessary?
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
95
    QString ipV6Str() const;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
96
    QString interfaceStr() const;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
97

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
98
99
100
101
102
103
    bool incoming() const;
    bool ipv6() const;
    int interface() const;
    int position() const;
    int protocol() const;
    Types::Policy action() const;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
104
105
106
107

    // 'different' is used in the EditRule dialog to know whether the rule has actually changed...
    bool different(const Rule &o) const
    {
108
        return m_logtype != o.m_logtype /*|| description!=o.description*/ || !(*this == o);
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
109
110
111
112
    }

    bool operator==(const Rule &o) const
    {
113
114
115
116
117
118
119
120
121
122
123
124
            return m_action == o.m_action
                && m_incoming == o.m_incoming
                && m_ipv6 == o.m_ipv6
                && m_protocol == o.m_protocol
                && m_destApplication == o.m_destApplication
                && m_sourceApplication == o.m_sourceApplication
                && m_destAddress == o.m_destAddress
                && m_sourceAddress == o.m_sourceAddress
                && (m_destApplication.isEmpty() && o.m_destApplication.isEmpty() ? m_destPort == o.m_destPort : true)
                && (m_sourceApplication.isEmpty() && o.m_sourceApplication.isEmpty() ? m_sourcePort == o.m_sourcePort : true)
                && m_interfaceIn == o.m_interfaceIn
                && m_interfaceOut == o.m_interfaceOut;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
125
126
    }

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
public slots:
    void setPolicy(const QString &policy);
    void setIncoming(bool incoming);
    void setSourceAddress(const QString &sourceAddress);
    void setSourcePort(const QString &sourcePort);
    void setDestinationAddress(const QString &destinationAddress);
    void setDestinationPort(const QString &destinationPort);
    void setIpv6(bool ipv6);
    void setProtocol(int protocol);
    void setInterface(int interface);
    void setLogging(const QString &logging);
    void setV6(const bool v);
    void setPosition(int position);

signals:
    void policyChanged(const QString &policy);
    void directionChanged(const QString &direction);
    void sourceAddressChanged(const QString &sourceAddress);
    void sourcePortChanged(const QString &sourcePort);
    void destinationAddressChanged(const QString &destinationAddress);
    void destinationPortChanged(const QString &destinationPort);
    void ipv6Changed(bool ipv6);
    void protocolChanged(int protocol);
    void interfaceChanged(int interface);
    void loggingChanged(const QString &logging);
    void incomingChanged(bool incoming);
    void positionChanged(int position);


Tomaz  Canabrava's avatar
Tomaz Canabrava committed
156
private:
157
158
159
    int m_position;
    Types::Policy m_action;
    bool m_incoming, m_ipv6;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
160
    int m_protocol;
161
162
163
164
165
166
167
168
169
    Types::Logging m_logtype;
    QString m_destApplication;
    QString m_sourceApplication;
    QString m_destAddress;
    QString m_sourceAddress;
    QString m_destPort;
    QString m_sourcePort;
    QString m_interfaceIn;
    QString m_interfaceOut;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
170
171
    QString m_interfaceStr;
    int m_interface;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
172
173
174
};

#endif