summaryrefslogtreecommitdiff
path: root/lang/cpp/src/editinteractor.cpp
blob: 31591fa390b8b455ff61014eb5f471976c9344c3 (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
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
/*
  editinteractor.cpp - Interface for edit interactors
  Copyright (C) 2007 Klarälvdalens Datakonsult AB

  This file is part of GPGME++.

  GPGME++ is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  GPGME++ 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 Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with GPGME++; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#ifdef HAVE_CONFIG_H
 #include "config.h"
#endif

#include "editinteractor.h"
#include "callbacks.h"
#include "error.h"

#include <gpgme.h>

#ifdef _WIN32
# include <io.h>
#include <windows.h>
#else
# include <unistd.h>
#endif

#include <cerrno>
#include <cstring>

#ifndef GPG_ERR_ALREADY_SIGNED
# define GPG_ERR_ALREADY_SIGNED GPG_ERR_USER_1
#endif

using namespace GpgME;

static const char *status_to_string(unsigned int status);
static Error status_to_error(unsigned int status);

class EditInteractor::Private
{
    friend class ::GpgME::EditInteractor;
    friend class ::GpgME::CallbackHelper;
    EditInteractor *const q;
public:
    explicit Private(EditInteractor *qq);
    ~Private();

private:
    unsigned int state;
    Error error;
    std::FILE *debug;
};

class GpgME::CallbackHelper
{
private:
    static int writeAll(int fd, const void *buf, size_t count)
    {
        size_t toWrite = count;
        while (toWrite > 0) {
            const int n = gpgme_io_write(fd, buf, toWrite);
            if (n < 0) {
                return n;
            }
            toWrite -= n;
        }
        return count;
    }

public:
    static int edit_interactor_callback_impl(void *opaque, gpgme_status_code_t status, const char *args, int fd)
    {
        EditInteractor::Private *ei = (EditInteractor::Private *)opaque;

        Error err = status_to_error(status);

        if (!err) {

            // advance to next state based on input:
            const unsigned int oldState = ei->state;
            ei->state = ei->q->nextState(status, args, err);
            if (ei->debug) {
                std::fprintf(ei->debug, "EditInteractor: %u -> nextState( %s, %s ) -> %u\n",
                             oldState, status_to_string(status), args ? args : "<null>", ei->state);
            }
            if (err) {
                ei->state = oldState;
                goto error;
            }

            if (ei->state != oldState &&
                    // if there was an error from before, we stop here (### this looks weird, can this happen at all?)
                    ei->error.code() == GPG_ERR_NO_ERROR) {

                // successful state change -> call action
                if (const char *const result = ei->q->action(err)) {
                    if (err) {
                        goto error;
                    }
                    if (ei->debug) {
                        std::fprintf(ei->debug, "EditInteractor: action result \"%s\"\n", result);
                    }
                    // if there's a result, write it:
                    if (*result) {
                        gpgme_err_set_errno(0);
                        const ssize_t len = std::strlen(result);
                        if (writeAll(fd, result, len) != len) {
                            err = Error::fromSystemError();
                            if (ei->debug) {
                                std::fprintf(ei->debug, "EditInteractor: Could not write to fd %d (%s)\n", fd, err.asString());
                            }
                            goto error;
                        }
                    }
                    gpgme_err_set_errno(0);
                    if (writeAll(fd, "\n", 1) != 1) {
                        err = Error::fromSystemError();
                        if (ei->debug) {
                            std::fprintf(ei->debug, "EditInteractor: Could not write to fd %d (%s)\n", fd, err.asString());
                        }
                        goto error;
                    }
                } else {
                    if (err) {
                        goto error;
                    }
                    if (ei->debug) {
                        std::fprintf(ei->debug, "EditInteractor: no action result\n");
                    }
                }
            } else {
                if (ei->debug) {
                    std::fprintf(ei->debug, "EditInteractor: no action executed\n");
                }
            }
        }

    error:
        if (err) {
            ei->error = err;
            ei->state = EditInteractor::ErrorState;
        }

        if (ei->debug) {
            std::fprintf(ei->debug, "EditInteractor: error now %u (%s)\n",
                         ei->error.encodedError(), gpgme_strerror(ei->error.encodedError()));
        }

        return ei->error.encodedError();
    }
};

static gpgme_error_t edit_interactor_callback(void *opaque, gpgme_status_code_t status, const char *args, int fd)
{
    return CallbackHelper::edit_interactor_callback_impl(opaque, status, args, fd);
}

const gpgme_edit_cb_t GpgME::edit_interactor_callback = ::edit_interactor_callback;

EditInteractor::Private::Private(EditInteractor *qq)
    : q(qq),
      state(StartState),
      error(),
      debug(0)
{

}

EditInteractor::Private::~Private() {}

EditInteractor::EditInteractor()
    : d(new Private(this))
{

}

EditInteractor::~EditInteractor()
{
    delete d;
}

unsigned int EditInteractor::state() const
{
    return d->state;
}

Error EditInteractor::lastError() const
{
    return d->error;
}

bool EditInteractor::needsNoResponse(unsigned int status) const
{
    switch (status) {
    case GPGME_STATUS_ALREADY_SIGNED:
    case GPGME_STATUS_ERROR:
    case GPGME_STATUS_GET_BOOL:
    case GPGME_STATUS_GET_LINE:
    case GPGME_STATUS_KEY_CREATED:
    case GPGME_STATUS_NEED_PASSPHRASE_SYM:
    case GPGME_STATUS_SC_OP_FAILURE:
        return false;
    default:
        return true;
    }
}

// static
Error status_to_error(unsigned int status)
{
    switch (status) {
    case GPGME_STATUS_MISSING_PASSPHRASE:
        return Error::fromCode(GPG_ERR_NO_PASSPHRASE);
    case GPGME_STATUS_ALREADY_SIGNED:
        return Error::fromCode(GPG_ERR_ALREADY_SIGNED);
    case GPGME_STATUS_SIGEXPIRED:
        return Error::fromCode(GPG_ERR_SIG_EXPIRED);
    }
    return Error();
}

void EditInteractor::setDebugChannel(std::FILE *debug)
{
    d->debug = debug;
}

static const char *const status_strings[] = {
    "EOF",
    /* mkstatus processing starts here */
    "ENTER",
    "LEAVE",
    "ABORT",

    "GOODSIG",
    "BADSIG",
    "ERRSIG",

    "BADARMOR",

    "RSA_OR_IDEA",
    "KEYEXPIRED",
    "KEYREVOKED",

    "TRUST_UNDEFINED",
    "TRUST_NEVER",
    "TRUST_MARGINAL",
    "TRUST_FULLY",
    "TRUST_ULTIMATE",

    "SHM_INFO",
    "SHM_GET",
    "SHM_GET_BOOL",
    "SHM_GET_HIDDEN",

    "NEED_PASSPHRASE",
    "VALIDSIG",
    "SIG_ID",
    "ENC_TO",
    "NODATA",
    "BAD_PASSPHRASE",
    "NO_PUBKEY",
    "NO_SECKEY",
    "NEED_PASSPHRASE_SYM",
    "DECRYPTION_FAILED",
    "DECRYPTION_OKAY",
    "MISSING_PASSPHRASE",
    "GOOD_PASSPHRASE",
    "GOODMDC",
    "BADMDC",
    "ERRMDC",
    "IMPORTED",
    "IMPORT_OK",
    "IMPORT_PROBLEM",
    "IMPORT_RES",
    "FILE_START",
    "FILE_DONE",
    "FILE_ERROR",

    "BEGIN_DECRYPTION",
    "END_DECRYPTION",
    "BEGIN_ENCRYPTION",
    "END_ENCRYPTION",

    "DELETE_PROBLEM",
    "GET_BOOL",
    "GET_LINE",
    "GET_HIDDEN",
    "GOT_IT",
    "PROGRESS",
    "SIG_CREATED",
    "SESSION_KEY",
    "NOTATION_NAME",
    "NOTATION_DATA",
    "POLICY_URL",
    "BEGIN_STREAM",
    "END_STREAM",
    "KEY_CREATED",
    "USERID_HINT",
    "UNEXPECTED",
    "INV_RECP",
    "NO_RECP",
    "ALREADY_SIGNED",
    "SIGEXPIRED",
    "EXPSIG",
    "EXPKEYSIG",
    "TRUNCATED",
    "ERROR",
    "NEWSIG",
    "REVKEYSIG",
    "SIG_SUBPACKET",
    "NEED_PASSPHRASE_PIN",
    "SC_OP_FAILURE",
    "SC_OP_SUCCESS",
    "CARDCTRL",
    "BACKUP_KEY_CREATED",
    "PKA_TRUST_BAD",
    "PKA_TRUST_GOOD",

    "PLAINTEXT",
};
static const unsigned int num_status_strings = sizeof status_strings / sizeof * status_strings ;

const char *status_to_string(unsigned int idx)
{
    if (idx < num_status_strings) {
        return status_strings[idx];
    } else {
        return "(unknown)";
    }
}