-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.cpp
910 lines (835 loc) · 26.3 KB
/
manager.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
#include "manager.h"
#include "ui_manager.h"
#include "netinfo.h"
Manager::Manager(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Manager)
{
ui->setupUi(this);
init();
}
Manager::~Manager()
{
delete ui;
}
QString Manager::getPassword() const
{
return password;
}
void Manager::setPassword(const QString &value)
{
password = value;
}
QString Manager::getUsername() const
{
return username;
}
void Manager::setUsername(const QString &value)
{
username = value;
}
// A few things to be initialized correctly
void Manager::init()
{
setUsername("");
setPassword("");
// Username can be visible
ui->username_lineEdit->setEchoMode(QLineEdit::Normal);
// Password should not be visible by default
ui->password_lineEdit->setEchoMode(QLineEdit::Password);
ui->retype_password_lineEdit->setEchoMode(QLineEdit::Password);
// Also the password for the new user who will be created should not
// be visible by default
ui->new_user_password_lineEdit->setEchoMode(QLineEdit::Password);
}
/* ============================================================================================================ */
/* SECTION I : Credentials */
/* ============================================================================================================ */
// Show or hide the password when user checks or unchecks this box
void Manager::on_show_password_checkBox_clicked(bool checked)
{
if(checked)
{
// If user clicks this checkbox the password is shown
ui->password_lineEdit->setEchoMode(QLineEdit::Normal);
ui->retype_password_lineEdit->setEchoMode(QLineEdit::Normal);
}
else {
// If this checkbox is un-checked, please hide the password
ui->password_lineEdit->setEchoMode(QLineEdit::Password);
ui->retype_password_lineEdit->setEchoMode(QLineEdit::Password);
}
}
// Compare the entered username with the username that is returned by the Qt
bool Manager::compare_usernames()
{
QString current_user = getenv("USER");
if(getUsername()!=current_user)
{
return false;
}
return true;
}
// Find out if password is correct
bool Manager::compare_passwords()
{
if(getPassword() != ui->retype_password_lineEdit->text())
{
return false;
}
return true;
}
// When the SUBMIT button is pushed ::
void Manager::on_submit_button_clicked()
{
// Grab the username & password at first
setUsername(ui->username_lineEdit->text());
setPassword(ui->password_lineEdit->text());
// If the username or password is empty prompt to enter them
if(getUsername().isEmpty() || getPassword().isEmpty() || ui->retype_password_lineEdit->text().isEmpty())
{
QMessageBox::warning(this, "WARNING", "Username and password should be both specified!");
return;
}
// Username should be the currently logged user's
if(!compare_usernames())
{
QMessageBox::critical(this, "ERROR", "Please provide correct username and try again!");
return;
}
// Next check if the provided password is correct
// Just execute a trivial 'ls' command using sudo. If it succeeds, password is ok. Otherwise, password is false!
if(!compare_passwords())
{
QMessageBox::critical(this, "ERROR", "Passwords do not match. Try again!");
return;
}
// Checks are ok by now.
submit_validation = true;
QMessageBox::information(this, "INFO", "Credentials entered. Continue procedure ...");
}
/* ============================================================================================================ */
/* SECTION II : Information Section */
/* ============================================================================================================ */
// List human users
void Manager::cat_users()
{
QProcess users_proc;
QString real_users;
users_proc.start("bash", QStringList() << "-c" << "cut -d: -f1,3 /etc/passwd | egrep ':[0-9]{4}$' | cut -d: -f1");
if(!users_proc.waitForFinished(3000) || users_proc.exitCode()!=0)
{
QMessageBox::critical(this, "ERROR", "Something went wrong. Please try again!");
return;
}
real_users = users_proc.readAllStandardOutput();
QMessageBox::information(this, "Real System Users", real_users);
return;
}
void Manager::on_human_users_checkBox_clicked(bool checked)
{
if(checked)
{
cat_users();
}
}
// List human system groups
void Manager::cat_groups()
{
QProcess groups_proc;
QString real_users;
groups_proc.start("bash", QStringList() << "-c" << "cut -d: -f1,3 /etc/group | egrep ':[0-9]{4}$' | cut -d: -f1");
if(!groups_proc.waitForFinished(3000) || groups_proc.exitCode()!=0)
{
QMessageBox::critical(this, "ERROR", "Something went wrong. Please try again!");
return;
}
real_users = groups_proc.readAllStandardOutput();
QMessageBox::information(this, "Real System Groups", real_users);
return;
}
void Manager::on_human_groups_checkBox_clicked(bool checked)
{
if(checked)
{
cat_groups();
}
}
// Clear the Credentials Section Fields
void Manager::on_clear_credentials_checkBox_clicked(bool checked)
{
if(checked)
{
ui->username_lineEdit->setText("");
ui->password_lineEdit->setText("");
ui->retype_password_lineEdit->setText("");
}
}
// Clear User Management fields
void Manager::on_clear_user_fields_checkBox_clicked(bool checked)
{
if(checked)
{
ui->new_username_lineEdit->setText("");
ui->new_user_real_name_lineEdit->setText("");
ui->new_user_group_lineEdit->setText("");
ui->new_user_id_lineEdit->setText("");
ui->new_user_shell_lineEdit->setText("");
ui->new_user_password_lineEdit->setText("");
}
}
// Clear Group Management fields
void Manager::on_clear_group_fields_checkBox_clicked(bool checked)
{
if(checked)
{
ui->group_name_lineEdit->setText("");
ui->group_id_lineEdit->setText("");
ui->new_group_name_lineEdit->setText("");
}
}
// Exit the application helper function to call whenever I wish!
void Manager::exit_app()
{
int checked_action = QMessageBox::question(this, "Quit and close Application ?",
"You are about to exit the Application. Are you sure you want to continue?", QMessageBox::No | QMessageBox::Yes);
if(checked_action==QMessageBox::Yes) {
close();
} else if(checked_action==QMessageBox::No) {
return;
}
}
void Manager::on_exit_manager_checkBox_clicked(bool checked)
{
if(checked)
{
exit_app();
}
}
// List the available system shells [ aka bash, tcsh, ksh, etc... ]
void Manager::on_shells_checkBox_clicked(bool checked)
{
if(checked)
{
QProcess proc;
QString shells;
proc.start("cat /etc/shells");
proc.waitForFinished(-1);
if(proc.exitCode()!=0)
{
QMessageBox::warning(this, "WARNING", "Could not find available shells in path /ec/shells !");
return;
}
shells = proc.readAllStandardOutput();
QMessageBox::information(this, "Available shells", shells);
}
}
/* ============================================================================================================ */
/* TOOLBAR ACTIONS SECTION */
/* ============================================================================================================ */
void Manager::on_actionExit_Manager_triggered()
{
exit_app();
}
void Manager::on_actionAbout_Author_triggered()
{
QString author("Author: Dimos Katsimardos\nManager Version: v1.0\nDate: August 18, 2019");
QMessageBox::information(this, "Author Information", author);
}
void Manager::on_actionAbout_Manager_triggered()
{
QString manager("Manager for Linux systems regarding Users, Groups and Networking activities.");
QMessageBox::information(this, "Manager Information", manager);
}
/* ============================================================================================================ */
/* SECTION III : Group Management */
/* ============================================================================================================ */
QString Manager::getGid() const
{
return gid;
}
void Manager::setGid(const QString &value)
{
gid = value;
}
QString Manager::getNew_groupname() const
{
return new_groupname;
}
void Manager::setNew_groupname(const QString &value)
{
new_groupname = value;
}
QString Manager::getGroupname() const
{
return groupname;
}
void Manager::setGroupname(const QString &value)
{
groupname = value;
}
bool Manager::group_exists()
{
QProcess proc;
proc.start("getent group " + getGroupname());
proc.waitForFinished(-1);
if(proc.exitCode()!=0)
{
return false;
}
return true;
}
bool Manager::groupadd()
{
QProcess pass, add;
pass.setStandardOutputProcess(&add);
pass.start("echo " + getPassword());
if(gid.isEmpty())
{
add.start("sudo -S groupadd " + getGroupname());
}
else {
add.start("sudo -S groupadd -g " + getGid() + " " + getGroupname());
}
pass.waitForFinished(-1);
add.waitForFinished(-1);
if(add.exitCode()!=0)
{
return false;
}
return true;
}
bool Manager::groupmod()
{
QProcess pass, mod;
pass.setStandardOutputProcess(&mod);
pass.start("echo " + getPassword());
mod.start("sudo -S groupmod -n " + getNew_groupname() + " " + getGroupname());
pass.waitForFinished(-1);
mod.waitForFinished(-1);
if(mod.exitCode()!=0)
{
return false;
}
return true;
}
bool Manager::groupdel()
{
QProcess pass, del;
pass.setStandardOutputProcess(&del);
pass.start("echo " + getPassword());
del.start("sudo -S groupdel " + getGroupname());
pass.waitForFinished(-1);
del.waitForFinished(-1);
if(del.exitCode()!=0)
{
return false;
}
return true;
}
void Manager::on_confirm_group_stuff_checkBox_clicked(bool checked)
{
if(checked)
{
// If this checkbox is 'checked' then grab group related info
setGroupname(ui->group_name_lineEdit->text());
setNew_groupname(ui->new_group_name_lineEdit->text());
setGid(ui->group_id_lineEdit->text());
if(getGroupname().isEmpty())
{
QMessageBox::warning(this, "WARNING", "Please provide compulsory option groupname and try again!");
}
}
}
// Create the new group | Only if it does not already exist
void Manager::on_create_group_button_clicked()
{
if(!submit_validation)
{
QMessageBox::critical(this, "Warning", "Please provide username & password and try again!");
return;
}
if(group_exists()==true)
{
QMessageBox::warning(this, "WARNING", "This group already exists in the system. Please try with another one!");
return;
}
// Else create the new group called 'groupname'
if(groupadd())
{
QMessageBox::information(this, "SUCCESS", "The new group: " + getGroupname() + " successfully created!");
return;
}
else {
QMessageBox::critical(this, "FAILURE", "New group: " + getGroupname() + " failed to be created!");
return;
}
}
// Remove the group | Only if it exists in the system
void Manager::on_remove_group_button_clicked()
{
if(!submit_validation)
{
QMessageBox::critical(this, "Warning", "Please provide username & password and try again!");
return;
}
if(!group_exists())
{
QMessageBox::warning(this, "WARNING", "This group already does not exist in the system. No need to remove it!");
return;
}
// Else remove it
if(groupdel())
{
QMessageBox::information(this, "SUCCESS", "The group: " + getGroupname() + " successfully removed from the system!");
return;
}
else {
QMessageBox::critical(this, "FAILURE", "The group: " + getGroupname() + " failed to be removed from the system!");
return;
}
}
// Rename a group | If it exists of course
void Manager::on_rename_group_button_clicked()
{
if(getNew_groupname().isEmpty())
{
QMessageBox::warning(this, "ERROR", "Cannot rename group: " + getGroupname() + " to: []! Enter new groupname and try again!");
return;
}
if(!submit_validation)
{
QMessageBox::critical(this, "Warning", "Please provide username & password and try again!");
return;
}
if(!group_exists())
{
QMessageBox::warning(this, "WARNING", "This group already does not exist in the system!");
return;
}
// Else try to rename this group named getGroupname()
if(groupmod())
{
QMessageBox::information(this, "SUCCESS", "The group: " + getGroupname() + " successfully renamed to: " + getNew_groupname() + " !");
return;
}
else {
QMessageBox::critical(this, "FAILURE", "The group: " + getGroupname() + " failed to be renamed to: " + getNew_groupname() + " !");
return;
}
}
/* ============================================================================================================ */
/* SECTION III : User Management */
/* ============================================================================================================ */
QString Manager::getNew_user_shell() const
{
return new_user_shell;
}
void Manager::setNew_user_shell(const QString &value)
{
new_user_shell = value;
}
QString Manager::getNew_user_id() const
{
return new_user_id;
}
void Manager::setNew_user_id(const QString &value)
{
new_user_id = value;
}
QString Manager::getNew_user_group() const
{
return new_user_group;
}
void Manager::setNew_user_group(const QString &value)
{
new_user_group = value;
}
QString Manager::getNew_user_realname() const
{
return new_user_realname;
}
void Manager::setNew_user_realname(const QString &value)
{
new_user_realname = value;
}
QString Manager::getNew_username() const
{
return new_username;
}
void Manager::setNew_username(const QString &value)
{
new_username = value;
}
QString Manager::getNew_user_encr_password() const
{
return new_user_encr_password;
}
void Manager::setNew_user_encr_password(const QString &value)
{
new_user_encr_password = value;
}
void Manager::create_enc_password()
{
// opnssl passwd <plain_password> --> creates the encrypted password hash
// to be used with the useradd -p <password_hash> option !
QProcess openssl;
openssl.start("openssl passwd " + ui->new_user_password_lineEdit->text());
openssl.waitForFinished();
QString hold(openssl.readAllStandardOutput());
hold.remove("\n");
setNew_user_encr_password(hold);
if(openssl.exitCode()!=0)
{
new_user_encr_password = "";
}
}
void Manager::on_show_new_user_password_checkBox_clicked(bool checked)
{
if(checked)
{
// If user clicks this checkbox the password is shown
ui->new_user_password_lineEdit->setEchoMode(QLineEdit::Normal);
}
else {
// If this checkbox is un-checked, please hide the password
ui->new_user_password_lineEdit->setEchoMode(QLineEdit::Password);
}
}
// Add a new user in the system
//useradd command example :: useradd -c "Real User Name" -m -u <UID> -g <GROUP> -d /home/$username -s <shell> $username
bool Manager::is_username_valid()
{
// Name of the new user should not start with a digit
// Also a username in Linux should be lower case name
if(getNew_username() == getenv("USER") || getNew_username().at(0).isDigit() || getNew_username().at(0).isUpper())
{
return false;
}
QVector<QString> invalid_characters = {"`", "~", "@", "!", "#", "$", "%", "^", "&", "*", "(", ")", "-", "+", "<", ">", ",", ".", "=", "_", "/", ";", ":", "?"};
QVector<QString>::iterator start = invalid_characters.begin();
if(getNew_username()=="root")
{
return false;
}
while(start != invalid_characters.end())
{
if(getNew_username().at(0) == *start)
{
return false;
}
start++;
}
return true;
}
// Confirm box is checked | Set new user information fields / string variables
void Manager::on_confirm_user_stuff_checkBox_clicked(bool checked)
{
if(checked)
{
// If this box is checked, then set all entered stuff from the user regarding the new user to be created
setNew_username(ui->new_username_lineEdit->text());
setNew_user_realname(ui->new_user_real_name_lineEdit->text());
setNew_user_group(ui->new_user_group_lineEdit->text());
setNew_user_id(ui->new_user_id_lineEdit->text());
setNew_user_shell(ui->new_user_shell_lineEdit->text());
setNew_groupname(ui->new_user_group_lineEdit->text());
// This functions takes the password as plain text and sets the new_user_enc_password as an encrypted hash
// of the entered plain text password using openssl
if(!ui->new_user_password_lineEdit->text().isEmpty())
{
// If this check is missing, openssl process is running ... at the background ! Problem !
create_enc_password();
}
// Next check is new_username || new_user_password are empty
// The other fields can be empty | Doesn't matter so much
if(getNew_username().isEmpty() || getNew_user_encr_password().isEmpty())
{
QMessageBox::information(this, "ERROR", "The username and password for the new user should not be empty!");
return;
}
if(!is_username_valid())
{
QMessageBox::warning(this, "ERROR", "Please provide a valid username and try again!");
return;
}
// Show info about the new user that is about to be created. Maybe the operator wants to change some stuff
QString info("User: " + getNew_username());
info += !getNew_user_realname().isEmpty() ? "\nReal Name: " + getNew_user_realname() : "";
info += !getNew_groupname().isEmpty() ? "\nGroup: " + getNew_groupname() : "";
info += !getNew_user_id().isEmpty() ? "\nID: " + getNew_user_id() : "";
info += !getNew_user_shell().isEmpty() ? "\nDefault shell: " + getNew_user_shell() : "";
QMessageBox::information(this, "New created user information", info);
}
}
// Ownership & Permissions for the home directory of the new user
bool Manager::set_chown()
{
QProcess passwd, chown;
passwd.setStandardOutputProcess(&chown);
passwd.start("echo " + getPassword());
chown.start("sudo -S chown " + getNew_username() + " /home/" + getNew_username());
chown.waitForFinished(6000);
passwd.waitForFinished(6000);
if(chown.exitCode()!=0)
{
return false;
}
return true;
}
bool Manager::set_chmod()
{
QProcess pass, chmod;
pass.setStandardOutputProcess(&chmod);
pass.start("echo " + getPassword());
chmod.start("sudo -S chmod -R u=rwx,g=rw,o=--- /home/" + getNew_username());
chmod.waitForFinished(6000);
pass.waitForFinished(6000);
if(chmod.exitCode()!=0)
{
return false;
}
return true;
}
// Create tthe new user using a QProcess
bool Manager::adduser()
{
QString options;
QProcess pass, add;
pass.setStandardOutputProcess(&add);
pass.start("echo " + getPassword());
if(!getNew_user_realname().isEmpty())
{
options += " -c \"" + getNew_user_realname() + "\"";
}
if(!getNew_user_group().isEmpty())
{
options += " -g " + getNew_groupname();
}
if(!getNew_user_id().isEmpty())
{
options += " -u " + getNew_user_id();
}
options += " -m -d /home/" + getNew_username();
if(!getNew_user_shell().isEmpty())
{
options += " -s /bin/" + getNew_user_shell();
}
options += " -p " + getNew_user_encr_password();
options += " " + getNew_username();
add.start("sudo -S useradd " + options);
add.waitForFinished(-1);
pass.waitForFinished(6000);
if(add.exitCode()!=0)
{
return false;
}
set_chmod();
set_chown();
return true;
}
void Manager::on_create_new_user_button_clicked()
{
if(!submit_validation)
{
QMessageBox::critical(this, "Warning", "Please provide username & password and try again!");
return;
}
// Check if the user exists in the system
if(user_exists())
{
QMessageBox::information(this, "INFO", "The user: " + getNew_username() + " already exists in the system. Please try with another name!");
return;
}
// Remember to check here if the entered group for the new user who will be created
// exists in the system, or the user should create this group from the GROUP Management Section
//
if(adduser())
{
QMessageBox::information(this, "SUCCESS", "The new user: " + getNew_username() + " created successfully!");
return;
}
else {
QMessageBox::critical(this, "FAILURE", "The user: " + getNew_username() + " Failed to be created!");
return;
}
}
// REMOVE A USER
bool Manager::deluser()
{
QProcess pass, del;
pass.setStandardOutputProcess(&del);
pass.start("echo " + getPassword());
del.start("sudo -S userdel " + getNew_username());
pass.waitForFinished(6000);
del.waitForFinished(-1);
if(del.exitCode()!=0)
{
return false;
}
return true;
}
// CHeck if a user exists in the system
bool Manager::user_exists()
{
QProcess proc;
proc.start("id " + getNew_username());
proc.waitForFinished();
if(proc.exitCode()!=0)
{
return false;
}
return true;
}
// Delete the user's home directory
bool Manager::del_user_home()
{
QProcess pass, rm_dir;
pass.setStandardOutputProcess(&rm_dir);
pass.start("echo " + getPassword());
rm_dir.start("sudo -S rm -r /home/" + getNew_username());
pass.waitForFinished(6000);
rm_dir.waitForFinished(6000);
if(rm_dir.exitCode()!=0)
{
return false;
}
return true;
}
void Manager::on_remove_user_button_clicked()
{
if(!submit_validation)
{
QMessageBox::critical(this, "Warning", "Please provide username & password and try again!");
return;
}
if(!user_exists())
{
QMessageBox::information(this, "WARNING", "The user: " + getNew_username() + " does not exist in the system!");
return;
}
if(deluser() && del_user_home())
{
QMessageBox::information(this, "SUCCESS", "The user: " + getNew_username() + " successfully removed from the system!");
return;
}
else {
QMessageBox::critical(this, "FAILURE", "The user: " + getNew_username() + " failed to be removed from the system! Please try again!");
return;
}
}
/* ============================================================================================================ */
/* SECTION IV : Networking */
/* ============================================================================================================ */
QString Manager::netstat()
{
QProcess net;
// r -> display routing table argument
net.start("netstat -r");
net.waitForFinished(6000);
QString hold(net.readAllStandardOutput());
if(net.exitCode()!=0)
{
return "[]";
}
return hold;
}
QString Manager::ifconfig()
{
QProcess ifconf;
ifconf.start("ifconfig");
ifconf.waitForFinished(6000);
QString hold(ifconf.readAllStandardOutput());
if(ifconf.exitCode()!=0)
{
return "[]";
}
return hold;
}
QString Manager::iptables()
{
// Let the user choose the Table to check the Firewall
// Tables : [ Filter | RAW | Security | Mangle ]
bool table_entered;
Table = QInputDialog::getText(nullptr, "Please choose the table to print its Firewall Set-Up",
"OPTIONS: [ mangle | filter | raw | security ]",
QLineEdit::Normal, "", &table_entered);
if(!table_entered || Table.isEmpty())
{
return "[]";
}
QProcess pass, ip_proc;
pass.setStandardOutputProcess(&ip_proc);
pass.start("echo " + getPassword());
ip_proc.start("sudo -S iptables -t " + Table + " -nL --line-numbers");
ip_proc.waitForFinished(-1);
pass.waitForFinished(-1);
QString hold(ip_proc.readAllStandardOutput());
if(ip_proc.exitCode()!=0)
{
return "[]";
}
return hold;
}
QString Manager::getTable() const
{
return Table;
}
void Manager::on_interfaces_checkBox_clicked(bool checked)
{
if(checked)
{
if(!submit_validation)
{
QMessageBox::critical(this, "Warning", "Please provide username & password and try again!");
return;
}
// Show the NetInfo Dialog
NetInfo *NetInfoDialog = new NetInfo(this);
NetInfoDialog->setWindowTitle("NETWORK INTERFACES");
QString info = ifconfig();
NetInfoDialog->catchText(info);
NetInfoDialog->exec();
}
}
void Manager::on_routing_table_checkBox_clicked(bool checked)
{
if(checked)
{
if(!submit_validation)
{
QMessageBox::critical(this, "Warning", "Please provide username & password and try again!");
return;
}
// Show the NetInfo Dialog
NetInfo *NetInfoDialog = new NetInfo(this);
NetInfoDialog->setWindowTitle("ROUTING TABLE");
QString info = netstat();
NetInfoDialog->catchText(info);
NetInfoDialog->exec();
}
}
void Manager::on_firewall_checkBox_clicked(bool checked)
{
if(checked)
{
if(!submit_validation)
{
QMessageBox::critical(this, "Warning", "Please provide username & password and try again!");
return;
}
if(iptables()=="[]")
{
QMessageBox::critical(this, "ERROR", "Please provide a valid table!");
return;
}
else {
// Show the NetInfo Dialog
NetInfo *NetInfoDialog = new NetInfo(this);
NetInfoDialog->setWindowTitle(getTable().toUpper() + " FIREWALL SET-UP");
QString info = iptables();
NetInfoDialog->catchText(info);
NetInfoDialog->exec();
}
}
}