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
|
/**
* Copyright (C) 2021 Saturneric <[email protected]>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <[email protected]> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "GnuPGInfoGatheringModule.h"
#include <vector>
#include "GpgInfo.h"
#include "Log.h"
#include "core/function/gpg/GpgCommandExecutor.h"
#include "core/module/ModuleManager.h"
namespace GpgFrontend::Module::Integrated::GnuPGInfoGatheringModule {
auto CheckBinaryChacksum(QString path) -> std::optional<QString> {
// check file info and access rights
QFileInfo info(path);
if (!info.exists() || !info.isFile() || !info.isReadable()) {
MODULE_LOG_ERROR("get info for file {} error, exists: {}", info.filePath(),
info.exists());
return {};
}
// open and read file
QFile f(info.filePath());
if (!f.open(QIODevice::ReadOnly)) {
MODULE_LOG_ERROR("open {} to calculate check sum error: {}", path,
f.errorString());
return {};
}
// read all data from file
auto buffer = f.readAll();
f.close();
auto hash_sha = QCryptographicHash(QCryptographicHash::Sha256);
// md5
hash_sha.addData(buffer);
return QString(hash_sha.result().toHex()).left(6);
}
GnuPGInfoGatheringModule::GnuPGInfoGatheringModule()
: Module(
"com.bktus.gpgfrontend.module.integrated.gnupg-info-gathering",
"1.0.0",
ModuleMetaData{{"description", "try to gathering gnupg informations"},
{"author", "saturneric"}}) {}
GnuPGInfoGatheringModule::~GnuPGInfoGatheringModule() = default;
auto GnuPGInfoGatheringModule::Register() -> bool {
MODULE_LOG_DEBUG("gnupg info gathering module registering");
listenEvent("GPGFRONTEND_CORE_INITLIZED");
return true;
}
auto GnuPGInfoGatheringModule::Active() -> bool {
MODULE_LOG_DEBUG("gnupg info gathering module activating");
return true;
}
auto GnuPGInfoGatheringModule::Exec(EventRefrernce event) -> int {
MODULE_LOG_DEBUG("gnupg info gathering module executing, event id: {}",
event->GetIdentifier());
const auto gpgme_version = RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.version", QString{"0.0.0"});
MODULE_LOG_DEBUG("got gpgme version from rt: {}", gpgme_version);
const auto gpgconf_path = RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.ctx.gpgconf_path", QString{});
MODULE_LOG_DEBUG("got gpgconf path from rt: {}", gpgconf_path);
MODULE_LOG_DEBUG("start to load extra info at module gnupginfogathering...");
// get all components
GpgCommandExecutor::ExecuteSync(
{gpgconf_path, QStringList{"--list-components"},
[this, gpgme_version, gpgconf_path](int exit_code, const QString &p_out,
const QString &p_err) {
MODULE_LOG_DEBUG(
"gpgconf components exit_code: {} process stdout size: {}",
exit_code, p_out.size());
if (exit_code != 0) {
MODULE_LOG_ERROR(
"gpgconf execute error, process stderr: {}, "
"process stdout: {}",
p_err, p_out);
return;
}
std::vector<GpgComponentInfo> component_infos;
GpgComponentInfo c_i_gpgme;
c_i_gpgme.name = "gpgme";
c_i_gpgme.desc = "GPG Made Easy";
c_i_gpgme.version = gpgme_version;
c_i_gpgme.path = tr("Embedded In");
c_i_gpgme.binary_checksum = "/";
GpgComponentInfo c_i_gpgconf;
c_i_gpgconf.name = "gpgconf";
c_i_gpgconf.desc = "GPG Configure";
c_i_gpgconf.version = "/";
c_i_gpgconf.path = gpgconf_path;
auto gpgconf_binary_checksum = CheckBinaryChacksum(gpgconf_path);
c_i_gpgconf.binary_checksum = (gpgconf_binary_checksum.has_value()
? gpgconf_binary_checksum.value()
: QString("/"));
component_infos.push_back(c_i_gpgme);
component_infos.push_back(c_i_gpgconf);
auto const jsonlized_gpgme_component_info = c_i_gpgme.Json();
auto const jsonlized_gpgconf_component_info = c_i_gpgconf.Json();
UpsertRTValue(GetModuleIdentifier(), "gnupg.components.gpgme",
QJsonDocument(jsonlized_gpgme_component_info).toJson());
UpsertRTValue(
GetModuleIdentifier(), "gnupg.components.gpgconf",
QJsonDocument(jsonlized_gpgconf_component_info).toJson());
auto line_split_list = p_out.split("\n");
for (const auto &line : line_split_list) {
auto info_split_list = line.split(":");
if (info_split_list.size() != 3) continue;
auto component_name = info_split_list[0].trimmed();
auto component_desc = info_split_list[1].trimmed();
auto component_path = info_split_list[2].trimmed();
#ifdef WINDOWS
// replace some special substrings on windows platform
component_path.replace("%3a", ":");
#endif
auto binary_checksum = CheckBinaryChacksum(component_path);
MODULE_LOG_DEBUG(
"gnupg component name: {} desc: {} checksum: {} path: {} ",
component_name, component_desc,
binary_checksum.has_value() ? binary_checksum.value() : "/",
component_path);
QString version = "/";
if (component_name == "gpg") {
version = RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.ctx.gnupg_version", QString{"2.0.0"});
}
if (component_name == "gpg-agent") {
UpsertRTValue(GetModuleIdentifier(), "gnupg.gpg_agent_path",
QString(component_path));
}
if (component_name == "dirmngr") {
UpsertRTValue(GetModuleIdentifier(), "gnupg.dirmngr_path",
QString(component_path));
}
if (component_name == "keyboxd") {
UpsertRTValue(GetModuleIdentifier(), "gnupg.keyboxd_path",
QString(component_path));
}
{
GpgComponentInfo c_i;
c_i.name = component_name;
c_i.desc = component_desc;
c_i.version = version;
c_i.path = component_path;
c_i.binary_checksum =
(binary_checksum.has_value() ? binary_checksum.value()
: QString("/"));
auto const jsonlized_component_info = c_i.Json();
UpsertRTValue(GetModuleIdentifier(),
QString("gnupg.components.%1").arg(component_name),
QJsonDocument(jsonlized_component_info).toJson());
component_infos.push_back(c_i);
}
}
},
getTaskRunner()});
GpgCommandExecutor::ExecuteContexts exec_contexts;
exec_contexts.emplace_back(GpgCommandExecutor::ExecuteContext{
gpgconf_path, QStringList{"--list-dirs"},
[this](int exit_code, const QString &p_out, const QString &p_err) {
MODULE_LOG_DEBUG(
"gpgconf configurations exit_code: {} process stdout size: {}",
exit_code, p_out.size());
if (exit_code != 0) {
MODULE_LOG_ERROR(
"gpgconf execute error, process stderr: {} process stdout: "
"{}",
p_err, p_out);
return;
}
auto line_split_list = p_out.split("\n");
for (const auto &line : line_split_list) {
auto info_split_list = line.split(":");
MODULE_LOG_DEBUG("gpgconf info line: {} info size: {}", line,
info_split_list.size());
if (info_split_list.size() != 2) continue;
auto configuration_name = info_split_list[0].trimmed();
auto configuration_value = info_split_list[1].trimmed();
#ifdef WINDOWS
// replace some special substrings on windows platform
configuration_value.replace("%3a", ":");
#endif
// record gnupg home path
if (configuration_name == "homedir") {
UpsertRTValue(GetModuleIdentifier(), "gnupg.home_path",
configuration_value);
}
UpsertRTValue(GetModuleIdentifier(),
QString("gnupg.dirs.%1").arg(configuration_name),
configuration_value);
}
},
getTaskRunner()});
auto components = ListRTChildKeys(GetModuleIdentifier(), "gnupg.components");
for (const auto &component : components) {
auto component_info_json = RetrieveRTValueTypedOrDefault(
GetModuleIdentifier(), QString("gnupg.components.%1").arg(component),
QByteArray{});
auto jsonlized_component_info =
QJsonDocument::fromJson(component_info_json);
assert(jsonlized_component_info.isObject());
auto component_info = GpgComponentInfo(jsonlized_component_info.object());
MODULE_LOG_DEBUG("gpgconf check options ready, component: {}",
component_info.name);
if (component_info.name == "gpgme" || component_info.name == "gpgconf") {
continue;
}
exec_contexts.emplace_back(GpgCommandExecutor::ExecuteContext{
gpgconf_path, QStringList{"--list-options", component_info.name},
[this, component_info](int exit_code, const QString &p_out,
const QString &p_err) {
MODULE_LOG_DEBUG(
"gpgconf {} avaliable options exit_code: {} process stdout "
"size: {} ",
component_info.name, exit_code, p_out.size());
if (exit_code != 0) {
MODULE_LOG_ERROR(
"gpgconf {} avaliable options execute error, process stderr: "
"{} , process stdout:",
component_info.name, p_err, p_out);
return;
}
std::vector<GpgOptionsInfo> options_infos;
auto line_split_list = p_out.split("\n");
for (const auto &line : line_split_list) {
auto info_split_list = line.split(":");
MODULE_LOG_DEBUG(
"component {} avaliable options line: {} info size: {}",
component_info.name, line, info_split_list.size());
if (info_split_list.size() < 10) continue;
// The format of each line is:
// name:flags:level:description:type:alt-type:argname:default:argdef:value
auto option_name = info_split_list[0].trimmed();
auto option_flags = info_split_list[1].trimmed();
auto option_level = info_split_list[2].trimmed();
auto option_desc = info_split_list[3].trimmed();
auto option_type = info_split_list[4].trimmed();
auto option_alt_type = info_split_list[5].trimmed();
auto option_argname = info_split_list[6].trimmed();
auto option_default = info_split_list[7].trimmed();
auto option_argdef = info_split_list[8].trimmed();
auto option_value = info_split_list[9].trimmed();
GpgOptionsInfo info;
info.name = option_name;
info.flags = option_flags;
info.level = option_level;
info.description = option_desc;
info.type = option_type;
info.alt_type = option_alt_type;
info.argname = option_argname;
info.default_value = option_default;
info.argdef = option_argdef;
info.value = option_value;
auto const jsonlized_option_info = info.Json();
UpsertRTValue(GetModuleIdentifier(),
QString("gnupg.components.%1.options.%2")
.arg(component_info.name)
.arg(option_name),
QJsonDocument(jsonlized_option_info).toJson());
options_infos.push_back(info);
}
},
getTaskRunner()});
}
GpgCommandExecutor::ExecuteConcurrentlySync(exec_contexts);
UpsertRTValue(GetModuleIdentifier(), "gnupg.gathering_done", true);
event->ExecuteCallback(GetModuleIdentifier(), TransferParams(true));
MODULE_LOG_DEBUG("gnupg external info gathering done");
return 0;
}
auto GnuPGInfoGatheringModule::Deactive() -> bool { return true; }
} // namespace GpgFrontend::Module::Integrated::GnuPGInfoGatheringModule
|