3 |
4 | #include "window.h"
5 | #include "initdbus.h"
6 |
7 | int main(int argc, char *argv[])
8 | {
9 | Q_INIT_RESOURCE(systray);
10 |
11 | QApplication app(argc, argv);
12 | QApplication::setQuitOnLastWindowClosed(false);
13 |
14 | Window window;
15 | window.setWindowIcon(QIcon(":/icons/laf_icon.svg"));
16 | window.setWindowTitle("qLAF - Linux Application Firewall");
17 |
18 | initDBus dbus;
19 | dbus.setup();
20 |
21 | QObject::connect(&dbus, SIGNAL(recvEvent(QString)), &window, SLOT(addEvent(QString)));
22 |
23 | return app.exec();
24 | }
25 |
--------------------------------------------------------------------------------
/qlaf/qlaf.pro:
--------------------------------------------------------------------------------
1 | QT += core gui dbus
2 |
3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4 |
5 | TARGET = qlaf
6 | TEMPLATE = app
7 |
8 |
9 | SOURCES += \
10 | main.cpp \
11 | window.cpp \
12 | initdbus.cpp
13 |
14 | HEADERS += \
15 | window.h \
16 | initdbus.h \
17 | common.h
18 |
19 | RESOURCES += \
20 | systray.qrc
21 |
22 | FORMS +=
23 |
--------------------------------------------------------------------------------
/qlaf/systray.qrc:
--------------------------------------------------------------------------------
1 |
2 |
3 | icons/laf_mute_green.svg
4 | icons/laf_mute_amber.svg
5 | icons/laf_mute_red.svg
6 | icons/laf_green.svg
7 | icons/laf_amber.svg
8 | icons/laf_red.svg
9 | icons/laf_icon.svg
10 |
11 |
12 |
--------------------------------------------------------------------------------
/qlaf/window.cpp:
--------------------------------------------------------------------------------
1 | #include "window.h"
2 |
3 | Window::Window(QWidget *parent) :
4 | QWidget(parent)
5 | {
6 | // Tray unmuted by default
7 | trayMuted = 0;
8 |
9 | createActions();
10 | createTrayIcon();
11 |
12 | connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
13 | connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
14 | this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
15 |
16 | updateIcon();
17 | trayIcon->show();
18 |
19 | QVBoxLayout *layout = new QVBoxLayout(this);
20 | QToolBar *toolbar = new QToolBar;
21 | QLabel *label = new QLabel;
22 |
23 | table = new QTableWidget(0,8);
24 | table->setColumnWidth(0,160);
25 | table->setColumnWidth(1,60);
26 | table->setColumnWidth(2,60);
27 | table->setColumnWidth(3,180);
28 | table->setColumnWidth(4,60);
29 | table->setColumnWidth(5,60);
30 | table->setColumnWidth(6,180);
31 | table->setColumnWidth(7,60);
32 |
33 | label->setText(tr("Event Log"));
34 | this->resize(900,500);
35 |
36 | layout->addWidget(toolbar);
37 | layout->addWidget(label);
38 | layout->addWidget(table);
39 |
40 | QStringList header;
41 | header << tr("Date") << tr("Family") << tr("Protocol") << tr("Command") << "PID" << "TID" << tr("Parent") << "PPID";
42 | table->setHorizontalHeaderLabels(header);
43 |
44 | qDebug() << "Program started!";
45 | }
46 |
47 | Window::~Window()
48 | {
49 | }
50 |
51 | void Window::addEvent(QString event)
52 | {
53 | QString fam = event.split('/')[1];
54 | QString proto = event.split('/')[2];
55 | QString cmd = event.split('/')[3];
56 | QString pid = event.split('/')[4];
57 | QString tid = event.split('/')[5];
58 | QString pcmd = event.split('/')[6];
59 | QString ppid = event.split('/')[7];
60 |
61 | QString text;
62 | text.append("FAM: ");
63 | text.append(fam);
64 | text.append(" PROTO: ");
65 | text.append(proto);
66 | text.append(" CMD: ");
67 | text.append(cmd);
68 | text.append(" (");
69 | text.append(pid);
70 | text.append(") PCMD: ");
71 | text.append(pcmd);
72 | text.append(" (");
73 | text.append(ppid);
74 | text.append(")");
75 |
76 | trayIcon->setObjectName(pid + "/" + tid + "/" + cmd);
77 | if (!trayMuted)
78 | trayIcon->showMessage("Application networking blocked", text, QSystemTrayIcon::Warning, LAF_MSG_TIMEOUT * 1000);
79 |
80 | // Amber icon until timeout
81 | setIcon(2);
82 | QTimer::singleShot(LAF_MSG_TIMEOUT * 1000, this, SLOT(updateIcon()));
83 |
84 | table->insertRow(0);
85 | for (int rc = 0; rc < 8; rc++) {
86 | table->setItem(0,rc,new QTableWidgetItem());
87 | table->item(0,rc)->setFlags(Qt::ItemIsEnabled);
88 | }
89 |
90 | table->item(0,0)->setText(QTime::currentTime().toString() + " " + QDate::currentDate().toString(Qt::ISODate));
91 | table->item(0,1)->setText(fam);
92 | table->item(0,2)->setText(proto);
93 | table->item(0,3)->setText(cmd);
94 | table->item(0,4)->setText(pid);
95 | table->item(0,5)->setText(tid);
96 | table->item(0,6)->setText(pcmd);
97 | table->item(0,7)->setText(ppid);
98 |
99 | connect(table, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(addItemWhitelist(int, int)),Qt::UniqueConnection);
100 | }
101 |
102 | void Window::setStatus_on() {
103 | setStatus(1);
104 | }
105 |
106 | void Window::setStatus_off() {
107 | setStatus(0);
108 | }
109 |
110 | void Window::setMute_on() {
111 | // trayIcon->showMessage("LAF", "Notifications muted", QSystemTrayIcon::Information, LAF_MSG_TIMEOUT * 100);
112 | trayMuted = 1;
113 | updateIcon();
114 | muteAction->setEnabled(0);
115 | unmuteAction->setEnabled(1);
116 | }
117 |
118 | void Window::setMute_off() {
119 | // trayIcon->showMessage("LAF", "Notifications unmuted", QSystemTrayIcon::Information, LAF_MSG_TIMEOUT * 100);
120 | trayMuted = 0;
121 | updateIcon();
122 | muteAction->setEnabled(1);
123 | unmuteAction->setEnabled(0);
124 | }
125 |
126 | void Window::setStatus(int status) {
127 |
128 | QString program = "pkexec";
129 | QStringList arguments;
130 | QProcess *myProcess = new QProcess(this);
131 |
132 | arguments << "lafctl";
133 | if (status)
134 | arguments << "-d";
135 | else
136 | arguments << "-e";
137 |
138 | myProcess->start(program, arguments);
139 | connect(myProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(updateIcon()));
140 | }
141 |
142 | void Window::showAbout()
143 | {
144 | QMessageBox::about(this, "qLAF", "qLAF " VERSION "
LAF (Linux Application Firewall) is a kernel driver that blocks network sockets, allowing only whitelisted process to connect to the LAN and the Internet. This project is licensed under the GPLv3 license.
More information in the project page.
2015-2016 (c) @sha0coder and @capi_x
");
145 | }
146 |
147 | void Window::updateIcon()
148 | {
149 | setIcon(getStatus());
150 | }
151 |
152 | void Window::setIcon(int iconNum)
153 | {
154 | switch (iconNum)
155 | {
156 | case 0:
157 | if (trayMuted)
158 | trayIcon->setIcon(QPixmap(":/icons/laf_mute_red.svg"));
159 | else
160 | trayIcon->setIcon(QPixmap(":/icons/laf_red.svg"));
161 |
162 | enableAction->setEnabled(1);
163 | disableAction->setEnabled(0);
164 |
165 | break;
166 | case 2:
167 | if (trayMuted)
168 | trayIcon->setIcon(QPixmap(":/icons/laf_mute_amber.svg"));
169 | else
170 | trayIcon->setIcon(QPixmap(":/icons/laf_amber.svg"));
171 | break;
172 | case 1:
173 | default:
174 | if (trayMuted)
175 | trayIcon->setIcon(QPixmap(":/icons/laf_mute_green.svg"));
176 | else
177 | trayIcon->setIcon(QPixmap(":/icons/laf_green.svg"));
178 |
179 | enableAction->setEnabled(0);
180 | disableAction->setEnabled(1);
181 | break;
182 | }
183 | }
184 |
185 | int Window::getStatus()
186 | {
187 | int ret = 0;
188 |
189 | QFile file("/proc/sys/kernel/laf/enabled");
190 | if(!file.open(QIODevice::ReadOnly)) {
191 | QMessageBox::information(0, "error", file.errorString());
192 | }
193 | QTextStream in(&file);
194 |
195 | ret = in.read(1).toInt();
196 |
197 | file.close();
198 |
199 | return ret;
200 | }
201 |
202 | void Window::updateWhitelist()
203 | {
204 | QString program = "pkexec";
205 | QStringList arguments;
206 | QProcess *myProcess = new QProcess(this);
207 |
208 | arguments << "lafctl" << "-u";
209 | myProcess->start(program, arguments);
210 |
211 | connect(myProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(updateWhitelist_slot()));
212 | }
213 |
214 | void Window::createActions()
215 | {
216 | enableAction = new QAction(tr("&Enable LAF"), this);
217 | connect(enableAction, SIGNAL(triggered()), this, SLOT(setStatus_off()));
218 |
219 | disableAction = new QAction(tr("&Disable LAF"), this);
220 | connect(disableAction, SIGNAL(triggered()), this, SLOT(setStatus_on()));
221 |
222 | muteAction = new QAction(tr("&Mute LAF"), this);
223 | connect(muteAction, SIGNAL(triggered()), this, SLOT(setMute_on()));
224 |
225 | unmuteAction = new QAction(tr("U&nmute LAF"), this);
226 | connect(unmuteAction, SIGNAL(triggered()), this, SLOT(setMute_off()));
227 |
228 | updateAction = new QAction(tr("&Update whitelist"), this);
229 | connect(updateAction, SIGNAL(triggered()), this, SLOT(updateWhitelist()));
230 |
231 | aboutAction = new QAction(tr("&About"), this);
232 | connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
233 |
234 | quitAction = new QAction(tr("&Quit"), this);
235 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
236 | }
237 |
238 | void Window::createTrayIcon()
239 | {
240 | trayIconMenu = new QMenu(this);
241 |
242 | trayIconMenu->addAction(enableAction);
243 | trayIconMenu->addAction(disableAction);
244 | trayIconMenu->addSeparator();
245 |
246 | trayIconMenu->addAction(muteAction);
247 | trayIconMenu->addAction(unmuteAction);
248 | unmuteAction->setEnabled(0);
249 | trayIconMenu->addSeparator();
250 |
251 | trayIconMenu->addAction(updateAction);
252 | trayIconMenu->addSeparator();
253 |
254 | trayIconMenu->addAction(aboutAction);
255 | trayIconMenu->addAction(quitAction);
256 |
257 | trayIcon = new QSystemTrayIcon(this);
258 | trayIcon->setContextMenu(trayIconMenu);
259 | }
260 |
261 | void Window::messageClicked()
262 | {
263 | if (trayIcon->objectName().length() == 0)
264 | return;
265 |
266 | int pid = trayIcon->objectName().split('/')[0].toInt();
267 | int tid = trayIcon->objectName().split('/')[1].toInt();
268 | QString cmd = trayIcon->objectName().split('/')[2];
269 |
270 | QString text = QString(tr("Do you want add \"%1\" to the whitelist?").arg(cmd));
271 | int ret = QMessageBox::question(0, tr("Add program to the whitelist"), text, QMessageBox::Yes, QMessageBox::No);
272 |
273 | if(ret == QMessageBox::Yes) {
274 | if (pid == tid)
275 | addWhitelist(0,cmd);
276 | else
277 | addWhitelist(1,cmd);
278 | }
279 | }
280 |
281 | void Window::addItemWhitelist(int x, int y)
282 | {
283 | int pid = table->item(x, 4)->text().toInt();
284 | int tid = table->item(x, 5)->text().toInt();
285 | QString cmd = table->item(x, 3)->text();
286 | UNUSED(y);
287 |
288 | QString text = QString(tr("Do you want add \"%1\" to the whitelist?").arg(cmd));
289 | int ret = QMessageBox::question(0, tr("Add program to the whitelist"), text, QMessageBox::Yes, QMessageBox::No);
290 |
291 | if(ret == QMessageBox::Yes) {
292 | if (pid == tid)
293 | addWhitelist(0,cmd);
294 | else
295 | addWhitelist(1,cmd);
296 | }
297 | }
298 |
299 | void Window::addWhitelist(int similar, QString cmd)
300 | {
301 | QString program = "pkexec";
302 | QStringList arguments;
303 | QProcess *myProcess = new QProcess(this);
304 |
305 | if (similar)
306 | arguments << "lafctl" << "-a" << "1" << cmd << "-u";
307 | else
308 | arguments << "lafctl" << "-a" << "0" << cmd << "-u";
309 |
310 | myProcess->start(program, arguments);
311 | }
312 |
313 | void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
314 | {
315 |
316 | switch (reason) {
317 | case QSystemTrayIcon::Trigger:
318 | // case QSystemTrayIcon::DoubleClick:
319 | if (this->isVisible())
320 | this->hide();
321 | else
322 | this->show();
323 |
324 | break;
325 | case QSystemTrayIcon::MiddleClick:
326 | if (trayMuted)
327 | setMute_off();
328 | else
329 | setMute_on();
330 | break;
331 | default:
332 | break;
333 | }
334 |
335 | }
336 |
--------------------------------------------------------------------------------
/qlaf/window.h:
--------------------------------------------------------------------------------
1 | #ifndef WINDOW_H
2 | #define WINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 |
26 | #include "common.h"
27 |
28 | class Window : public QWidget
29 | {
30 | Q_OBJECT
31 | public:
32 | explicit Window(QWidget *parent = 0);
33 | ~Window();
34 | QSystemTrayIcon *trayIcon;
35 | QTableWidget *table;
36 | bool trayMuted;
37 |
38 | signals:
39 |
40 | public slots:
41 | void addEvent(QString);
42 |
43 | private slots:
44 | void iconActivated(QSystemTrayIcon::ActivationReason);
45 | void messageClicked();
46 | void setStatus_on();
47 | void setStatus_off();
48 | void setStatus(int);
49 | void setMute_on();
50 | void setMute_off();
51 | void updateWhitelist();
52 | void showAbout();
53 | void updateIcon();
54 | void setIcon(int);
55 | void addWhitelist(int, QString);
56 | void addItemWhitelist(int, int);
57 | int getStatus();
58 |
59 | private:
60 | void createTrayIcon();
61 | void createActions();
62 |
63 | QAction *disableAction;
64 | QAction *enableAction;
65 | QAction *muteAction;
66 | QAction *unmuteAction;
67 | QAction *aboutAction;
68 | QAction *updateAction;
69 | QAction *quitAction;
70 | QMenu *trayIconMenu;
71 | };
72 |
73 | #endif // WINDOW_H
74 |
--------------------------------------------------------------------------------
/scripts/premake.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | SYSADDR=$(grep ia32_sys_call_table /boot/System.map-`uname -r` | cut -d ' ' -f 1)
4 | echo "#define SYSCALL_IA32_ADDR 0x$SYSADDR" > ia32_addr.h
5 |
--------------------------------------------------------------------------------