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 
QGpgMEWKSPublishJob(Context * context)53 QGpgMEWKSPublishJob::QGpgMEWKSPublishJob(Context *context)
54     : mixin_type(context)
55 {
56     lateInitialization();
57 }
58 
~QGpgMEWKSPublishJob()59 QGpgMEWKSPublishJob::~QGpgMEWKSPublishJob() {}
60 
getWKSClient()61 static QString getWKSClient()
62 {
63     auto libexecdir = QString::fromLocal8Bit(dirInfo("libexecdir"));
64     if (libexecdir.isEmpty()) {
65         return QString();
66     }
67 
68     const QFileInfo fi(QDir(libexecdir).absoluteFilePath(QStringLiteral("gpg-wks-client")));
69     if (fi.exists() && fi.isExecutable()) {
70         return fi.absoluteFilePath();
71     }
72     return QString();
73 }
74 
check_worker(const QString & mail)75 static QGpgMEWKSPublishJob::result_type check_worker(const QString &mail)
76 {
77     if (mail.isEmpty()) {
78         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
79                                 QByteArray(), QByteArray(), QString(), Error());
80     }
81 
82     const auto wksPath = getWKSClient();
83     if (wksPath.isEmpty()) {
84         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
85                                 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     QProcess proc;
91     proc.setProgram(wksPath);
92     proc.setArguments(QStringList() << QStringLiteral("--supported") << mail);
93     proc.start();
94     if (!proc.waitForStarted()) {
95         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
96                                 QByteArray(), QByteArray(), QString(), Error());
97     }
98     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
99         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
100                                 QByteArray(), QByteArray(), QString(), Error());
101     }
102     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
103         return std::make_tuple (Error(), QByteArray(), QByteArray(), QString(), Error());
104     }
105     return std::make_tuple (Error(make_error(GPG_ERR_NOT_ENABLED)),
106                             QByteArray(), QByteArray(), QString(), Error());
107 }
108 
create_worker(const char * fpr,const QString & mail)109 static QGpgMEWKSPublishJob::result_type create_worker(const char *fpr, const QString &mail)
110 {
111     if (mail.isEmpty() || !fpr) {
112         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
113                                 QByteArray(), QByteArray(), QString(), Error());
114     }
115 
116     const auto wksPath = getWKSClient();
117     if (wksPath.isEmpty()) {
118         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
119                                 QByteArray(), QByteArray(), QString(), Error());
120     }
121 
122     QProcess proc;
123     proc.setProgram(wksPath);
124     proc.setArguments(QStringList() << QStringLiteral("--create")
125                                     << QLatin1String(fpr)
126                                     << mail);
127     proc.start();
128     if (!proc.waitForStarted()) {
129         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
130                                 QByteArray(), QByteArray(), QString(), Error());
131     }
132 
133     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
134         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
135                                 QByteArray(), QByteArray(), QString(), Error());
136     }
137     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
138         return std::make_tuple (Error(), proc.readAllStandardOutput(),
139                                 proc.readAllStandardError(), QString(), Error());
140     }
141     return std::make_tuple (Error(make_error(GPG_ERR_GENERAL)),
142                             proc.readAllStandardOutput(), proc.readAllStandardError(), QString(), Error());
143 }
144 
receive_worker(const QByteArray & response)145 static QGpgMEWKSPublishJob::result_type receive_worker(const QByteArray &response)
146 {
147     if (response.isEmpty()) {
148         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
149                                 QByteArray(), QByteArray(), QString(), Error());
150     }
151 
152     const auto wksPath = getWKSClient();
153     if (wksPath.isEmpty()) {
154         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
155                                 QByteArray(), QByteArray(), QString(), Error());
156     }
157 
158     QProcess proc;
159     proc.setProgram(wksPath);
160     proc.setArguments(QStringList() << QStringLiteral("--receive"));
161     proc.start();
162     if (!proc.waitForStarted()) {
163         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
164                                 QByteArray(), QByteArray(), QString(), Error());
165     }
166     proc.write(response);
167     proc.closeWriteChannel();
168     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
169         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
170                                 QByteArray(), QByteArray(), QString(), Error());
171     }
172     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
173         return std::make_tuple (Error(), proc.readAllStandardOutput(),
174                                 proc.readAllStandardError(), QString(), Error());
175     }
176     return std::make_tuple (Error(make_error(GPG_ERR_GENERAL)),
177                             proc.readAllStandardOutput(), proc.readAllStandardError(), QString(), Error());
178 }
179 
startCheck(const QString & mailbox)180 void QGpgMEWKSPublishJob::startCheck(const QString &mailbox)
181 {
182     run(std::bind(&check_worker, mailbox));
183 }
184 
startCreate(const char * fpr,const QString & mailbox)185 void QGpgMEWKSPublishJob::startCreate(const char *fpr, const QString &mailbox) {
186     run(std::bind(&create_worker, fpr, mailbox));
187 }
188 
startReceive(const QByteArray & response)189 void QGpgMEWKSPublishJob::startReceive(const QByteArray &response)
190 {
191     run(std::bind(&receive_worker, response));
192 }
193 
194 #include "qgpgmewkspublishjob.moc"
195