-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile.cpp
More file actions
174 lines (127 loc) · 4.5 KB
/
Copy pathfile.cpp
File metadata and controls
174 lines (127 loc) · 4.5 KB
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
#include "file.hpp"
#include <QProcess>
#include <sstream>
#include <cstdlib>
#include <string>
#define GET_VALUE(param, key_text) \
do { \
const std::string key = std::string(key_text) + "="; \
auto pos = data.find(key); \
if (pos == std::string::npos) break; \
pos += key.size(); \
std::istringstream iss(data.substr(pos)); \
std::getline(iss, param); \
} while (0)
constexpr auto root_exec = "pkexec";
bool save_as_root(const QString &target, const QByteArray &data) {
QStringList args;
args << "sh" << "-c" << QString("cat > %1").arg(target);
QProcess proc;
proc.start(root_exec, args);
if (!proc.waitForStarted()) {
return false;
}
proc.write(data);
proc.closeWriteChannel();
if (!proc.waitForFinished()) {
return false;
}
if (proc.exitStatus() != QProcess::NormalExit || proc.exitCode() != 0) {
return false;
}
return true;
}
std::string read_as_root(const QString &target) {
auto nil = std::string{};
QStringList args;
args << "sh" << "-c" << QString("cat %1").arg(target);
QProcess proc;
proc.start(root_exec, args);
if (!proc.waitForStarted()){
return nil;
}
if (!proc.waitForFinished()){
return nil;
}
if (proc.exitStatus() != QProcess::NormalExit || proc.exitCode() != 0){
return nil;
}
return proc.readAllStandardOutput().toStdString();
}
bool open_config::save(){
QByteArray data = QString("[Settings]\nAutoConnect=%1\n")
.arg(this->autoconnect ? "True" : "False")
.toUtf8();
auto path = QString("%1%2.%3").arg(iwd_networks_folder).arg(this->ssid).arg("open");
return save_as_root(path, data);
}
void open_config::read(){
auto path = QString("%1%2.%3").arg(iwd_networks_folder)
.arg(this->ssid)
.arg("open");
std::string autoconnect;
auto data = read_as_root(path);
GET_VALUE(autoconnect, "AutoConnect");
if(autoconnect.empty()){
this->autoconnect = true;
} else{
this->autoconnect = autoconnect == "True";
}
}
bool psk_config::save(){
QByteArray data = QString("[Security]\nPassphrase=%1\n[Settings]\nAutoConnect=%2\n")
.arg(this->psk)
.arg(this->autoconnect ? "True" : "False")
.toUtf8();
auto path = QString("%1%2.%3").arg(iwd_networks_folder).arg(this->ssid).arg("psk");
return save_as_root(path, data);
}
bool eap_config::save(){
auto prefix = this->eap_method;
QByteArray data = QString("[Security]\nEAP-Method=%1\nEAP-Identity=%2\nEAP-%1-Phase2-Method=%3\nEAP-%1-Phase2-Identity=%4\nEAP-%1-Phase2-Password=%5\n[Settings]\nAutoConnect=%6\n")
.arg(prefix)
.arg(this->eap_identity)
.arg(this->phase2_method)
.arg(this->phase2_identity)
.arg(this->phase2_password)
.arg(this->autoconnect ? "True" : "False")
.toUtf8();
auto path = QString("%1%2.%3").arg(iwd_networks_folder).arg(this->ssid).arg("8021x");
return save_as_root(path, data);
}
void psk_config::read(){
auto path = QString("%1%2.%3").arg(iwd_networks_folder)
.arg(this->ssid)
.arg("psk");
auto data = read_as_root(path);
std::string autoconnect;
GET_VALUE(autoconnect, "AutoConnect");
if(autoconnect.empty()){
this->autoconnect = true;
} else{
this->autoconnect = autoconnect == "True";
}
GET_VALUE(this->psk, "Passphrase");
}
void eap_config::read(){
auto path = QString("%1%2.%3").arg(iwd_networks_folder)
.arg(this->ssid)
.arg("8021x");
auto data = read_as_root(path);
std::string autoconnect;
GET_VALUE(autoconnect, "AutoConnect");
if(autoconnect.empty()){
this->autoconnect = true;
} else{
this->autoconnect = autoconnect == "True";
}
GET_VALUE(this->eap_method, "EAP-Method");
GET_VALUE(this->eap_identity, "EAP-Identity");
if(this->eap_method.empty()){
return;
}
std::string prefix = "EAP-";
GET_VALUE(this->phase2_method, prefix + this->eap_method + "-Phase2-Method");
GET_VALUE(this->phase2_identity, prefix + this->eap_method + "-Phase2-Identity");
GET_VALUE(this->phase2_password, prefix + this->eap_method + "-Phase2-Password");
}