aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/gpg/GpgAssuanHelper.cpp
blob: b9ee63f116706a9216246116efed6b9bd845575a (plain)
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
/**
 * Copyright (C) 2021-2024 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 "GpgAssuanHelper.h"

#include "core/module/ModuleManager.h"
#include "core/utils/GpgUtils.h"

namespace GpgFrontend {

GpgAssuanHelper::GpgAssuanHelper(int channel)
    : GpgFrontend::SingletonFunctionObject<GpgAssuanHelper>(channel),
      gpgconf_path_(Module::RetrieveRTValueTypedOrDefault<>(
          "core", "gpgme.ctx.gpgconf_path", QString{})) {}

GpgAssuanHelper::~GpgAssuanHelper() = default;

auto GpgAssuanHelper::ConnectToSocket(GpgComponentType type) -> GpgError {
  if (assuan_ctx_.contains(type)) return GPG_ERR_NO_ERROR;

  auto socket_path = ctx_.ComponentDirectory(type);
  if (socket_path.isEmpty()) {
    LOG_W() << "socket path of component: " << component_type_to_q_string(type)
            << " is empty";
    return GPG_ERR_ENOPKG;
  }

  QFileInfo info(socket_path);
  if (!info.exists()) {
    LOG_W() << "socket path is not exists: " << socket_path;

    LOG_W() << "launching component: " << component_type_to_q_string(type)
            << " by gpgconf, sockets: " << socket_path;
    launch_component(type);

    if (!info.exists()) {
      LOG_W() << "socket path is still not exists: " << socket_path
              << "abort...";
      return GPG_ERR_ENOTSOCK;
    }
  }

  assuan_context_t a_ctx;
  auto err = assuan_new(&a_ctx);
  if (err != GPG_ERR_NO_ERROR) {
    LOG_E() << "create assuan context failed, err:" << CheckGpgError(err);
    return err;
  }

  auto pa_ctx = QSharedPointer<struct assuan_context_s>(
      a_ctx, [](assuan_context_t p) { assuan_release(p); });

  err = assuan_socket_connect(pa_ctx.get(), info.absoluteFilePath().toUtf8(),
                              ASSUAN_INVALID_PID, 0);
  if (err != GPG_ERR_NO_ERROR) {
    LOG_W() << "failed to connect to socket:" << info.absoluteFilePath()
            << "err:" << CheckGpgError(err);
    return err;
  }

  LOG_D() << "connected to socket by assuan protocol: "
          << info.absoluteFilePath() << "channel:" << GetChannel();

  err = assuan_transact(pa_ctx.get(), "GETINFO pid", simple_data_callback,
                        nullptr, nullptr, nullptr, nullptr, nullptr);
  if (err != GPG_ERR_NO_ERROR) {
    LOG_W() << "failed to test assuan connection:" << CheckGpgError(err);
    return err;
  }

  assuan_ctx_[type] = pa_ctx;
  return err;
}

auto GpgAssuanHelper::SendCommand(GpgComponentType type, const QString& command,
                                  DataCallback data_cb,
                                  InqueryCallback inquery_cb,
                                  StatusCallback status_cb) -> GpgError {
  if (!assuan_ctx_.contains(type)) {
    LOG_W() << "haven't connect to: " << component_type_to_q_string(type)
            << ", trying to make a connection";

    auto err = CheckGpgError(ConnectToSocket(type));
    if (err != GPG_ERR_NO_ERROR) return err;
  }

  auto context = QSharedPointer<AssuanCallbackContext>::create();
  context->self = this;
  context->data_cb = std::move(data_cb);
  context->status_cb = std::move(status_cb);
  context->inquery_cb = std::move(inquery_cb);

  LOG_D() << "sending assuan command: " << command;

  auto err =
      assuan_transact(assuan_ctx_[type].get(), command.toUtf8(),
                      default_data_callback, &context, default_inquery_callback,
                      &context, default_status_callback, &context);

  if (err != GPG_ERR_NO_ERROR) {
    LOG_W() << "failed to send assuan command:" << CheckGpgError(err);

    // broken pipe error, try reconnect next time
    if (CheckGpgError(err) == 32877) {
      assuan_ctx_.remove(type);
      return SendCommand(type, command, data_cb, inquery_cb, status_cb);
    }
    return err;
  }

  return err;
}

auto GpgAssuanHelper::SendStatusCommand(GpgComponentType type,
                                        const QString& command)
    -> std::tuple<GpgError, QStringList> {
  GpgAssuanHelper::DataCallback d_cb =
      [&](const QSharedPointer<GpgAssuanHelper::AssuanCallbackContext>& ctx)
      -> gpg_error_t {
    LOG_D() << "data callback of command " << command << ": " << ctx->buffer;

    return 0;
  };

  GpgAssuanHelper::InqueryCallback i_cb =
      [=](const QSharedPointer<GpgAssuanHelper::AssuanCallbackContext>& ctx)
      -> gpg_error_t {
    LOG_D() << "inquery callback of command: " << command << ": "
            << ctx->inquery;
    return 0;
  };

  QStringList lines;
  GpgAssuanHelper::StatusCallback s_cb =
      [&](const QSharedPointer<GpgAssuanHelper::AssuanCallbackContext>& ctx)
      -> gpg_error_t {
    LOG_D() << "status callback of command: " << command << ":  "
            << ctx->status;
    lines.append(ctx->status);
    return 0;
  };

  auto ret = SendCommand(type, command, d_cb, i_cb, s_cb);
  return {ret, lines};
}

auto GpgAssuanHelper::SendDataCommand(GpgComponentType type,
                                      const QString& command)
    -> std::tuple<GpgError, QStringList> {
  QStringList lines;
  GpgAssuanHelper::DataCallback d_cb =
      [&](const QSharedPointer<GpgAssuanHelper::AssuanCallbackContext>& ctx)
      -> gpg_error_t {
    LOG_D() << "data callback of command " << command << ": " << ctx->buffer;
    lines.push_back(QString::fromUtf8(ctx->buffer));
    return 0;
  };

  GpgAssuanHelper::InqueryCallback i_cb =
      [=](const QSharedPointer<GpgAssuanHelper::AssuanCallbackContext>& ctx)
      -> gpg_error_t {
    LOG_D() << "inquery callback of command: " << command << ": "
            << ctx->inquery;

    return 0;
  };

  GpgAssuanHelper::StatusCallback s_cb =
      [&](const QSharedPointer<GpgAssuanHelper::AssuanCallbackContext>& ctx)
      -> gpg_error_t {
    LOG_D() << "status callback of command: " << command << ":  "
            << ctx->status;

    return 0;
  };

  auto ret = SendCommand(type, command, d_cb, i_cb, s_cb);
  return {ret, lines};
}

auto GpgAssuanHelper::default_data_callback(void* opaque, const void* buffer,
                                            size_t length) -> gpgme_error_t {
  auto ctx = *static_cast<QSharedPointer<AssuanCallbackContext>*>(opaque);
  ctx->buffer.clear();
  ctx->buffer.append(static_cast<const char*>(buffer),
                     static_cast<qsizetype>(length));
  if (ctx->data_cb) ctx->data_cb(ctx);
  return GPG_ERR_NO_ERROR;
}

auto GpgAssuanHelper::default_status_callback(void* opaque, const char* status)
    -> gpgme_error_t {
  auto ctx = *static_cast<QSharedPointer<AssuanCallbackContext>*>(opaque);
  ctx->status = QString::fromUtf8(status);
  if (ctx->status_cb) ctx->status_cb(ctx);
  return GPG_ERR_NO_ERROR;
}

auto GpgAssuanHelper::default_inquery_callback(
    void* opaque, const char* inquery) -> gpgme_error_t {
  auto ctx = *static_cast<QSharedPointer<AssuanCallbackContext>*>(opaque);
  ctx->inquery = QString::fromUtf8(inquery);
  if (ctx->status_cb) ctx->inquery_cb(ctx);
  return GPG_ERR_NO_ERROR;
}

void GpgAssuanHelper::launch_component(GpgComponentType type) {
  if (gpgconf_path_.isEmpty()) {
    LOG_W() << "gpgconf_path is not collected by initializing";
    return;
  }

  auto gpgconf_path = QFileInfo(gpgconf_path_).absoluteFilePath();
  LOG_D() << "assuan helper channel: " << GetChannel()
          << "gpgconf path: " << gpgconf_path;

  QProcess process;
  process.setProgram(gpgconf_path);
  process.setArguments({"--launch", component_type_to_q_string(type)});
  process.start();

  if (!process.waitForFinished()) {
    LOG_E() << "failed to execute gpgconf" << process.arguments();
    return;
  }
}

auto GpgAssuanHelper::component_type_to_q_string(GpgComponentType type)
    -> QString {
  switch (type) {
    case GpgComponentType::kGPG_AGENT:
    case GpgComponentType::kGPG_AGENT_SSH:
      return "gpg-agent";
    case GpgComponentType::kDIRMNGR:
      return "dirmngr";
    case GpgComponentType::kKEYBOXD:
      return "keyboxd";
    default:
      return "all";
  }
}
auto GpgAssuanHelper::simple_data_callback(void* opaque, const void* buffer,
                                           size_t length) -> gpgme_error_t {
  LOG_D() << "assuan callback data: "
          << QByteArray::fromRawData(static_cast<const char*>(buffer), length);
  return 0;
}

auto GpgAssuanHelper::AssuanCallbackContext::SendData(const QByteArray& b) const
    -> gpg_error_t {
  return assuan_send_data(ctx, b.constData(), b.size());
}

void GpgAssuanHelper::ResetAllConnections() { assuan_ctx_.clear(); }
}  // namespace GpgFrontend