File: main.cpp

package info (click to toggle)
plasma-firewall 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,980 kB
  • sloc: cpp: 4,661; xml: 237; makefile: 7; sh: 1
file content (248 lines) | stat: -rw-r--r-- 8,048 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@kde.org>

#include <QCoreApplication>
#include <QDebug>

#include "firewallclient.h"
#include "rule.h"
#include "rulelistmodel.h"

#include <KJob>

// Start
void testDisableClient(FirewallClient *client);
void testDisableClientResult(FirewallClient *client);

// Second Method.
void testEnableClient(FirewallClient *client);
void testEnableClientResult(FirewallClient *client);

// Third Method
void testCurrentRulesEmpty(FirewallClient *client);
void testCurrentRulesEmptyResult(FirewallClient *client);

// Fourth Method
void testAddRules(FirewallClient *client);
void testAddRulesResult(FirewallClient *client);

// Fifth Method
void testRemoveRules(FirewallClient *client);
void testRemoveRulesResult(FirewallClient *client);

void printHelp()
{
    qDebug() << "Usage Information:";
    qDebug() << "$"
             << "firewall_test backend_name";
    qDebug() << "Where backend name is ether ufw or firewalld.";
    qDebug() << "Make sure you have no firewall running";
    qDebug() << "And make sure you have no profile configured on either firewall.";
}

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    qDebug() << "argc" << argc;
    if (argc != 2) {
        qDebug() << "Invalid number of arguments, please use:";
        printHelp();
        exit(1);
    }

    QString firewallBackendName = argv[1];
    if (firewallBackendName != "ufw" && firewallBackendName != "firewalld") {
        printHelp();
        exit(1);
    }
    qDebug() << firewallBackendName << "test starting";

    auto *client = new FirewallClient();
    client->setBackend({firewallBackendName});
    Q_ASSERT(client->backend() == firewallBackendName);

    // Start trying to disable, the order of calls is the same as the definition order.
    // Please don't change as this is really annoying to test.
    testDisableClient(client);

    return app.exec();
}

void testDisableClient(FirewallClient *client)
{
    qDebug() << "Test Disable the Firewall";
    KJob *enableJob = client->setEnabled(false);

    // Ignore, we are already disabled.
    if (enableJob == nullptr) {
        testEnableClient(client);
        return;
    }

    // Nice hack: One time connections.
    QMetaObject::Connection *const connection = new QMetaObject::Connection;
    *connection = QObject::connect(client, &FirewallClient::enabledChanged, [client, connection] {
        QObject::disconnect(*connection);
        delete connection;
        testDisableClientResult(client);
    });

    enableJob->start();
}

void testDisableClientResult(FirewallClient *client)
{
    Q_ASSERT(client->enabled() == false);
    testEnableClient(client);
}

void testEnableClient(FirewallClient *client)
{
    qDebug() << "Test Enable the Firewall";

    // From here on, We will jump thru the usage via connects.
    KJob *enableJob = client->setEnabled(true);

    // Ignore, we are already enabled.
    if (enableJob == nullptr) {
        testCurrentRulesEmpty(client);
        return;
    }

    // Nice hack: One time connections.
    QMetaObject::Connection *const connection = new QMetaObject::Connection;
    *connection = QObject::connect(client, &FirewallClient::enabledChanged, [client, connection] {
        qDebug() << "Enabled Changed";
        QObject::disconnect(*connection);
        delete connection;
        testEnableClientResult(client);
    });

    enableJob->start();
}

void testEnableClientResult(FirewallClient *client)
{
    qDebug() << "Client Enabled" << client->enabled();
    testCurrentRulesEmpty(client);
}

void testCurrentRulesEmpty(FirewallClient *client)
{
    if (client->rulesModel()->rowCount() != 0) {
        qDebug() << "We need a clean firewall rules to do the testing, please backup your rules and try again.";
        exit(1);
    }

    Q_ASSERT(client->rulesModel()->rowCount() == 0);
    testCurrentRulesEmptyResult(client);
}

void testCurrentRulesEmptyResult(FirewallClient *client)
{
    testAddRules(client);
}

// Fourth Method
void testAddRules(FirewallClient *client)
{
    QString interface = client->knownInterfaces()[0];

    // Expected output for firewalld
    // firewalld.client: ("-j", "DROP", "-p", "tcp", "-d", "127.0.0.1", "--dport=21", "-s", "127.0.0.1", "--sport=12")
    // firewalld.job: firewalld  "addRule" (
    //     QVariant(QString, "ipv4"),
    //     QVariant(QString, "filter"),
    //     QVariant(QString, "INPUT"),
    //     QVariant(int, 0),
    //     QVariant(QStringList, ("-j", "DROP", "-p", "tcp", "-d", "127.0.0.1", "--dport=21", "-s", "127.0.0.1", "--sport=12"))
    //  )

    // Current Output for firewalld:
    // firewalld.client: ("-j", "DROP", "-p", "tcp", "-d", "127.0.0.1", "--dport=21", "-s", "127.0.0.1", "--sport=12")
    //
    // firewalld.job: firewalld  "addRule" (
    //     QVariant(QString, "ipv4"),
    //     QVariant(QString, "filter"),
    //     QVariant(QString, "INPUT"),
    //     QVariant(int, 0),
    //     QVariant(QStringList, ("-j", "DROP", "-p", "tcp", "-d", "127.0.0.1", "--dport=21", "-s", "127.0.0.1", "--sport=12")))

    // Expected output for ufw
    // Debug message from helper: Cmd args passed to ufw: (
    // "--add=<rule action=\"deny\"
    //         direction=\"in\"
    //         dport=\"21\"
    //         sport=\"12\"
    //         protocol=\"TCP\"
    //         dst=\"127.0.0.1\"
    //         src=\"127.0.0.1\"
    //         logtype=\"\"
    //         v6=\"False\"/>")

    // Current Output for ufw
    // Debug message from helper: Cmd args passed to ufw: (
    // "--add=<rule action=\"deny\"
    //    direction=\"in\"
    //    dport=\"21\"
    //    sport=\"12\"
    //    dst=\"127.0.0.1\"
    //    src=\"127.0.0.1\"
    //    logtype=\"\"
    //    v6=\"False\"/>")

    auto *rule = new Rule(Types::Policy::POLICY_DENY, // Policy
                          true, // Incomming?
                          Types::Logging::LOGGING_OFF, // Logging
                          0, // Protocol Id on knownProtocols
                          "127.0.0.1", // Source Host
                          "12", // Source Port
                          "127.0.0.1", // Destination Port
                          "21", // Destination Port
                          QString(), // Interface In
                          QString(), // Interface Out
                          QString(), // Source App // Only used in UFW - Remove?
                          QString(), // Destination App // Only used in UFW - Remove?
                          0, // Index (TODO: Remove This)
                          false); // IPV6?

    // This call should perhaps have an client::addRuleFinished(), but currently we need
    // to rely on the model refresh
    client->addRule(rule);

    // Nice hack: One time connections.
    QMetaObject::Connection *const connection = new QMetaObject::Connection;
    *connection = QObject::connect(client->rulesModel(), &RuleListModel::modelReset, [client, connection] {
        qDebug() << "Add rule finished.";
        QObject::disconnect(*connection);
        delete connection;
        testAddRulesResult(client);
    });

    // TODO:
    // This job is started inside of the addRule, currently we have an inconsistency on what's
    // started by default and what's started by Qml. We need to fix this.
    // job->start();
}

void testAddRulesResult(FirewallClient *client)
{
    Q_ASSERT(client->rulesModel()->rowCount() == 1);
    client->removeRule(0);

    // Nice hack: One time connections.
    QMetaObject::Connection *const connection = new QMetaObject::Connection;
    *connection = QObject::connect(client->rulesModel(), &RuleListModel::modelReset, [client, connection] {
        qDebug() << "Remove Rule Finished.";
        QObject::disconnect(*connection);
        delete connection;
        testRemoveRulesResult(client);
    });
}

void testRemoveRulesResult(FirewallClient *client)
{
    Q_ASSERT(client->rulesModel()->rowCount() == 0);
    qDebug() << "Test Finished";
}