Line data Source code
1 : /* wkspublishjob.cpp
2 :
3 : Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
4 : Software engineering by Intevation GmbH
5 :
6 : QGpgME is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : QGpgME is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License along
17 : with this program; if not, write to the Free Software Foundation, Inc.,
18 : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : In addition, as a special exception, the copyright holders give
21 : permission to link the code of this program with any edition of
22 : the Qt library by Trolltech AS, Norway (or with modified versions
23 : of Qt that use the same license as Qt), and distribute linked
24 : combinations including the two. You must obey the GNU General
25 : Public License in all respects for all of the code used other than
26 : Qt. If you modify this file, you may extend this exception to
27 : your version of the file, but you are not obligated to do so. If
28 : you do not wish to do so, delete this exception statement from
29 : your version.
30 : */
31 :
32 : #ifdef HAVE_CONFIG_H
33 : #include "config.h"
34 : #endif
35 :
36 : #include "qgpgmewkspublishjob.h"
37 :
38 : #include "context.h"
39 : #include "key.h"
40 : #include "util.h"
41 :
42 : #include <QFileInfo>
43 : #include <QDir>
44 : #include <QProcess>
45 :
46 : /* Timeout for the WKS Processes will be 5 Minutes as
47 : * they can involve pinentry questions. */
48 : #define TIMEOUT_VALUE (5*60*1000)
49 :
50 : using namespace QGpgME;
51 : using namespace GpgME;
52 :
53 0 : QGpgMEWKSPublishJob::QGpgMEWKSPublishJob(Context *context)
54 0 : : mixin_type(context)
55 : {
56 0 : lateInitialization();
57 0 : }
58 :
59 0 : QGpgMEWKSPublishJob::~QGpgMEWKSPublishJob() {}
60 :
61 0 : static QString getWKSClient()
62 : {
63 0 : auto libexecdir = QString::fromLocal8Bit(dirInfo("libexecdir"));
64 0 : if (libexecdir.isEmpty()) {
65 0 : return QString();
66 : }
67 :
68 0 : const QFileInfo fi(QDir(libexecdir).absoluteFilePath(QStringLiteral("gpg-wks-client")));
69 0 : if (fi.exists() && fi.isExecutable()) {
70 0 : return fi.absoluteFilePath();
71 : }
72 0 : return QString();
73 : }
74 :
75 0 : static QGpgMEWKSPublishJob::result_type check_worker(const QString &mail)
76 : {
77 0 : if (mail.isEmpty()) {
78 0 : return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
79 0 : QByteArray(), QByteArray(), QString(), Error());
80 : }
81 :
82 0 : const auto wksPath = getWKSClient();
83 0 : if (wksPath.isEmpty()) {
84 0 : return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
85 0 : QByteArray(), QByteArray(), QString(), Error());
86 : }
87 :
88 : /* QProcess instead of engine_spawn because engine_spawn does not communicate
89 : * the return value of the process and we are in qt anyway. */
90 0 : QProcess proc;
91 0 : proc.setProgram(wksPath);
92 0 : proc.setArguments(QStringList() << QStringLiteral("--supported") << mail);
93 0 : proc.start();
94 0 : if (!proc.waitForStarted()) {
95 0 : return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
96 0 : QByteArray(), QByteArray(), QString(), Error());
97 : }
98 0 : if (!proc.waitForFinished(TIMEOUT_VALUE)) {
99 0 : return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
100 0 : QByteArray(), QByteArray(), QString(), Error());
101 : }
102 0 : if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
103 0 : return std::make_tuple (Error(), QByteArray(), QByteArray(), QString(), Error());
104 : }
105 0 : return std::make_tuple (Error(make_error(GPG_ERR_NOT_ENABLED)),
106 0 : QByteArray(), QByteArray(), QString(), Error());
107 : }
108 :
109 0 : static QGpgMEWKSPublishJob::result_type create_worker(const char *fpr, const QString &mail)
110 : {
111 0 : if (mail.isEmpty() || !fpr) {
112 0 : return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
113 0 : QByteArray(), QByteArray(), QString(), Error());
114 : }
115 :
116 0 : const auto wksPath = getWKSClient();
117 0 : if (wksPath.isEmpty()) {
118 0 : return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
119 0 : QByteArray(), QByteArray(), QString(), Error());
120 : }
121 :
122 0 : QProcess proc;
123 0 : proc.setProgram(wksPath);
124 0 : proc.setArguments(QStringList() << QStringLiteral("--create")
125 0 : << QLatin1String(fpr)
126 0 : << mail);
127 0 : proc.start();
128 0 : if (!proc.waitForStarted()) {
129 0 : return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
130 0 : QByteArray(), QByteArray(), QString(), Error());
131 : }
132 :
133 0 : if (!proc.waitForFinished(TIMEOUT_VALUE)) {
134 0 : return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
135 0 : QByteArray(), QByteArray(), QString(), Error());
136 : }
137 0 : if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
138 0 : return std::make_tuple (Error(), proc.readAllStandardOutput(),
139 0 : proc.readAllStandardError(), QString(), Error());
140 : }
141 0 : return std::make_tuple (Error(make_error(GPG_ERR_GENERAL)),
142 0 : proc.readAllStandardOutput(), proc.readAllStandardError(), QString(), Error());
143 : }
144 :
145 0 : static QGpgMEWKSPublishJob::result_type receive_worker(const QByteArray &response)
146 : {
147 0 : if (response.isEmpty()) {
148 0 : return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
149 0 : QByteArray(), QByteArray(), QString(), Error());
150 : }
151 :
152 0 : const auto wksPath = getWKSClient();
153 0 : if (wksPath.isEmpty()) {
154 0 : return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
155 0 : QByteArray(), QByteArray(), QString(), Error());
156 : }
157 :
158 0 : QProcess proc;
159 0 : proc.setProgram(wksPath);
160 0 : proc.setArguments(QStringList() << QStringLiteral("--receive"));
161 0 : proc.start();
162 0 : if (!proc.waitForStarted()) {
163 0 : return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
164 0 : QByteArray(), QByteArray(), QString(), Error());
165 : }
166 0 : proc.write(response);
167 0 : proc.closeWriteChannel();
168 0 : if (!proc.waitForFinished(TIMEOUT_VALUE)) {
169 0 : return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
170 0 : QByteArray(), QByteArray(), QString(), Error());
171 : }
172 0 : if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
173 0 : return std::make_tuple (Error(), proc.readAllStandardOutput(),
174 0 : proc.readAllStandardError(), QString(), Error());
175 : }
176 0 : return std::make_tuple (Error(make_error(GPG_ERR_GENERAL)),
177 0 : proc.readAllStandardOutput(), proc.readAllStandardError(), QString(), Error());
178 : }
179 :
180 0 : void QGpgMEWKSPublishJob::startCheck(const QString &mailbox)
181 : {
182 0 : run(std::bind(&check_worker, mailbox));
183 0 : }
184 :
185 0 : void QGpgMEWKSPublishJob::startCreate(const char *fpr, const QString &mailbox) {
186 0 : run(std::bind(&create_worker, fpr, mailbox));
187 0 : }
188 :
189 0 : void QGpgMEWKSPublishJob::startReceive(const QByteArray &response)
190 : {
191 0 : run(std::bind(&receive_worker, response));
192 0 : }
193 :
194 : #include "qgpgmewkspublishjob.moc"
|