| 1 | /* |
| 2 | * Copyright (C) 2003-2005 Justin Karneges <justin@affinix.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "gpgop.h" |
| 23 | #include "gpgproc.h" |
| 24 | #include "lineconverter.h" |
| 25 | #include "qca_safetimer.h" |
| 26 | #include <QByteArray> |
| 27 | #include <QObject> |
| 28 | #include <QStringList> |
| 29 | |
| 30 | #ifdef GPG_PROFILE |
| 31 | #include <QTime> |
| 32 | #endif |
| 33 | |
| 34 | namespace gpgQCAPlugin { |
| 35 | |
| 36 | class GpgAction : public QObject |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | public: |
| 40 | struct Input |
| 41 | { |
| 42 | QString bin; |
| 43 | GpgOp::Type op; |
| 44 | bool opt_ascii, opt_noagent, opt_alwaystrust; |
| 45 | QString opt_pubfile, opt_secfile; |
| 46 | QStringList recip_ids; |
| 47 | QString signer_id; |
| 48 | QByteArray sig; |
| 49 | QByteArray inkey; |
| 50 | QString export_key_id; |
| 51 | QString delete_key_fingerprint; |
| 52 | |
| 53 | Input() |
| 54 | : opt_ascii(false) |
| 55 | , opt_noagent(false) |
| 56 | , opt_alwaystrust(false) |
| 57 | { |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | struct Output |
| 62 | { |
| 63 | bool success; |
| 64 | GpgOp::Error errorCode; |
| 65 | GpgOp::KeyList keys; |
| 66 | QString keyringFile; |
| 67 | QString encryptedToId; |
| 68 | bool wasSigned; |
| 69 | QString signerId; |
| 70 | QDateTime timestamp; |
| 71 | GpgOp::VerifyResult verifyResult; |
| 72 | QString homeDir; |
| 73 | |
| 74 | Output() |
| 75 | : success(false) |
| 76 | , errorCode(GpgOp::ErrorUnknown) |
| 77 | , wasSigned(false) |
| 78 | { |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | Input input; |
| 83 | Output output; |
| 84 | |
| 85 | GpgAction(QObject *parent = nullptr); |
| 86 | ~GpgAction() override; |
| 87 | void reset(); |
| 88 | void start(); |
| 89 | #ifdef QPIPE_SECURE |
| 90 | void submitPassphrase(const QCA::SecureArray &a); |
| 91 | #else |
| 92 | void submitPassphrase(const QByteArray &a); |
| 93 | #endif |
| 94 | |
| 95 | public Q_SLOTS: |
| 96 | QByteArray read(); |
| 97 | void write(const QByteArray &in); |
| 98 | void endWrite(); |
| 99 | void cardOkay(); |
| 100 | QString readDiagnosticText(); |
| 101 | |
| 102 | Q_SIGNALS: |
| 103 | void readyRead(); |
| 104 | void bytesWritten(int bytes); |
| 105 | void finished(); |
| 106 | void needPassphrase(const QString &keyId); |
| 107 | void needCard(); |
| 108 | void readyReadDiagnosticText(); |
| 109 | |
| 110 | private: |
| 111 | void submitCommand(const QByteArray &a); |
| 112 | |
| 113 | // since str is taken as a value, it is ok to use the same variable for 'rest' |
| 114 | QString nextArg(QString str, QString *rest = nullptr); |
| 115 | void processStatusLine(const QString &line); |
| 116 | void processResult(int code); |
| 117 | void ensureDTextEmit(); |
| 118 | |
| 119 | GPGProc proc; |
| 120 | bool collectOutput, allowInput; |
| 121 | LineConverter readConv, writeConv; |
| 122 | bool readText, writeText; |
| 123 | QByteArray buf_stdout, buf_stderr; |
| 124 | bool useAux; |
| 125 | QString passphraseKeyId; |
| 126 | bool signing, decryptGood, signGood; |
| 127 | GpgOp::Error curError; |
| 128 | bool badPassphrase; |
| 129 | bool need_submitPassphrase, need_cardOkay; |
| 130 | QString diagnosticText; |
| 131 | QCA::SafeTimer dtextTimer; |
| 132 | bool utf8Output; |
| 133 | |
| 134 | #ifdef GPG_PROFILE |
| 135 | QTime timer; |
| 136 | #endif |
| 137 | |
| 138 | private Q_SLOTS: |
| 139 | void t_dtext(); |
| 140 | void proc_error(gpgQCAPlugin::GPGProc::Error e); |
| 141 | void proc_finished(int exitCode); |
| 142 | void proc_readyReadStdout(); |
| 143 | void proc_readyReadStderr(); |
| 144 | void proc_readyReadStatusLines(); |
| 145 | void proc_bytesWrittenStdin(int bytes); |
| 146 | void proc_bytesWrittenAux(int bytes); |
| 147 | void proc_bytesWrittenCommand(int); |
| 148 | void proc_debug(const QString &str); |
| 149 | void appendDiagnosticText(const QString &line); |
| 150 | }; |
| 151 | |
| 152 | } // end namespace gpgQCAPlugin |
| 153 | |