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
|
/*
qgpgmejob.cpp
This file is part of libkleopatra, the KDE keymanagement library
Copyright (c) 2004 Klarälvdalens Datakonsult AB
Libkleopatra 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 2 of the
License, or (at your option) any later version.
Libkleopatra 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the TQt library by Trolltech AS, Norway (or with modified versions
of TQt that use the same license as TQt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
TQt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "qgpgmejob.h"
#include "qgpgmeprogresstokenmapper.h"
#include <kleo/job.h>
#include <ui/passphrasedialog.h>
#include <qgpgme/eventloopinteractor.h>
#include <qgpgme/dataprovider.h>
#include <gpgmepp/context.h>
#include <gpgmepp/data.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <algorithm>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
namespace {
class InvarianceChecker {
public:
#ifdef NDEBUG
InvarianceChecker( const Kleo::QGpgMEJob * ) {}
#else
InvarianceChecker( const Kleo::QGpgMEJob * job )
: _this( job )
{
assert( _this );
_this->checkInvariants();
}
~InvarianceChecker() {
_this->checkInvariants();
}
private:
const Kleo::QGpgMEJob * _this;
#endif
};
}
Kleo::QGpgMEJob::QGpgMEJob( Kleo::Job * _this, GpgME::Context * context )
: GpgME::ProgressProvider(),
GpgME::PassphraseProvider(),
mThis( _this ),
mCtx( context ),
mInData( 0 ),
mInDataDataProvider( 0 ),
mOutData( 0 ),
mOutDataDataProvider( 0 ),
mPatterns( 0 ),
mReplacedPattern( 0 ),
mNumPatterns( 0 ),
mChunkSize( 1024 ),
mPatternStartIndex( 0 ), mPatternEndIndex( 0 )
{
InvarianceChecker check( this );
assert( context );
TQObject::connect( QGpgME::EventLoopInteractor::instance(), TQ_SIGNAL(aboutToDestroy()),
_this, TQ_SLOT(slotCancel()) );
context->setProgressProvider( this );
// (mmutz) work around a gpgme bug in versions at least <= 0.9.0.
// These versions will return GPG_ERR_NOT_IMPLEMENTED from
// a CMS sign operation when a passphrase callback is set.
if ( context->protocol() == GpgME::Context::OpenPGP )
context->setPassphraseProvider( this );
}
void Kleo::QGpgMEJob::checkInvariants() const {
#ifndef NDEBUG
if ( mPatterns ) {
assert( mPatterns[mNumPatterns] == 0 );
if ( mPatternEndIndex > 0 ) {
assert( mPatternEndIndex > mPatternStartIndex );
assert( mPatternEndIndex - mPatternStartIndex == mChunkSize );
} else {
assert( mPatternEndIndex == mPatternStartIndex );
}
if ( mPatternEndIndex < mNumPatterns ) {
assert( mPatterns[mPatternEndIndex] == 0 );
assert( mReplacedPattern != 0 );
} else {
assert( mReplacedPattern == 0 );
}
} else {
assert( mNumPatterns == 0 );
assert( mPatternStartIndex == 0 );
assert( mPatternEndIndex == 0 );
assert( mReplacedPattern == 0 );
}
#endif
}
Kleo::QGpgMEJob::~QGpgMEJob() {
InvarianceChecker check( this );
delete mCtx; mCtx = 0;
delete mInData; mInData = 0;
delete mInDataDataProvider; mInDataDataProvider = 0;
delete mOutData; mOutData = 0;
delete mOutDataDataProvider; mOutDataDataProvider = 0;
deleteAllPatterns();
}
void Kleo::QGpgMEJob::deleteAllPatterns() {
if ( mPatterns )
for ( unsigned int i = 0 ; i < mNumPatterns ; ++i )
free( (void*)mPatterns[i] );
free( (void*)mReplacedPattern ); mReplacedPattern = 0;
delete[] mPatterns; mPatterns = 0;
mPatternEndIndex = mPatternStartIndex = mNumPatterns = 0;
}
void Kleo::QGpgMEJob::hookupContextToEventLoopInteractor() {
mCtx->setManagedByEventLoopInteractor( true );
TQObject::connect( QGpgME::EventLoopInteractor::instance(),
TQ_SIGNAL(operationDoneEventSignal(GpgME::Context*,const GpgME::Error&)),
mThis, TQ_SLOT(slotOperationDoneEvent(GpgME::Context*,const GpgME::Error&)) );
}
void Kleo::QGpgMEJob::setPatterns( const TQStringList & sl, bool allowEmpty ) {
InvarianceChecker check( this );
deleteAllPatterns();
// create a new null-terminated C array of char* from patterns:
mPatterns = new const char*[ sl.size() + 1 ];
const char* * pat_it = mPatterns;
mNumPatterns = 0;
for ( TQStringList::const_iterator it = sl.begin() ; it != sl.end() ; ++it ) {
if ( (*it).isNull() )
continue;
if ( (*it).isEmpty() && !allowEmpty )
continue;
*pat_it++ = strdup( (*it).utf8().data() );
++mNumPatterns;
}
*pat_it++ = 0;
mReplacedPattern = 0;
mPatternEndIndex = mChunkSize = mNumPatterns;
}
void Kleo::QGpgMEJob::setChunkSize( unsigned int chunksize ) {
InvarianceChecker check( this );
if ( mReplacedPattern ) {
mPatterns[mPatternEndIndex] = mReplacedPattern;
mReplacedPattern = 0;
}
mChunkSize = std::min( chunksize, mNumPatterns );
mPatternStartIndex = 0;
mPatternEndIndex = mChunkSize;
mReplacedPattern = mPatterns[mPatternEndIndex];
mPatterns[mPatternEndIndex] = 0;
}
const char* * Kleo::QGpgMEJob::nextChunk() {
InvarianceChecker check( this );
if ( mReplacedPattern ) {
mPatterns[mPatternEndIndex] = mReplacedPattern;
mReplacedPattern = 0;
}
mPatternStartIndex += mChunkSize;
mPatternEndIndex += mChunkSize;
if ( mPatternEndIndex < mNumPatterns ) { // could safely be <=, but the last entry is NULL anyway
mReplacedPattern = mPatterns[mPatternEndIndex];
mPatterns[mPatternEndIndex] = 0;
}
return patterns();
}
const char* * Kleo::QGpgMEJob::patterns() const {
InvarianceChecker check( this );
if ( mPatternStartIndex < mNumPatterns )
return mPatterns + mPatternStartIndex;
return 0;
}
GpgME::Error Kleo::QGpgMEJob::setSigningKeys( const std::vector<GpgME::Key> & signers ) {
mCtx->clearSigningKeys();
for ( std::vector<GpgME::Key>::const_iterator it = signers.begin() ; it != signers.end() ; ++it ) {
if ( (*it).isNull() )
continue;
if ( const GpgME::Error err = mCtx->addSigningKey( *it ) )
return err;
}
return 0;
}
void Kleo::QGpgMEJob::createInData( const TQByteArray & in ) {
mInDataDataProvider = new QGpgME::TQByteArrayDataProvider( in );
mInData = new GpgME::Data( mInDataDataProvider );
assert( !mInData->isNull() );
}
void Kleo::QGpgMEJob::createOutData() {
mOutDataDataProvider = new QGpgME::TQByteArrayDataProvider();
mOutData = new GpgME::Data( mOutDataDataProvider );
assert( !mOutData->isNull() );
}
static const unsigned int GetAuditLogFlags = GpgME::Context::AuditLogWithHelp|GpgME::Context::HtmlAuditLog;
static TQString audit_log_as_html( GpgME::Context * ctx, GpgME::Error & err ) {
assert( ctx );
QGpgME::TQByteArrayDataProvider dp;
GpgME::Data data( &dp );
assert( !data.isNull() );
if ( ( err = ctx->getAuditLog( data, GetAuditLogFlags ) ) )
return TQString();
const TQByteArray ba = dp.data();
return TQString::fromUtf8( ba.data(), ba.size() );
}
void Kleo::QGpgMEJob::doSlotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) {
if ( context == mCtx ) {
doEmitDoneSignal();
doOperationDoneEvent( e );
mThis->deleteLater();
}
}
void Kleo::QGpgMEJob::getAuditLog() {
if ( !mCtx )
return;
mAuditLogAsHtml = audit_log_as_html( mCtx, mAuditLogError );
}
void Kleo::QGpgMEJob::doSlotCancel() {
mCtx->cancelPendingOperation();
}
void Kleo::QGpgMEJob::showProgress( const char * what, int type, int current, int total ) {
doEmitProgressSignal( QGpgMEProgressTokenMapper::instance()->map( what, type, current, total ), current, total );
}
char * Kleo::QGpgMEJob::getPassphrase( const char * useridHint, const char * /*description*/,
bool previousWasBad, bool & canceled ) {
// DF: here, description is the key fingerprint, twice, then "17 0". Not really descriptive.
// So I'm ignoring TQString::fromLocal8Bit( description ) )
TQString msg = previousWasBad ?
i18n( "You need a passphrase to unlock the secret key for user:<br/> %1 (retry)" ) :
i18n( "You need a passphrase to unlock the secret key for user:<br/> %1" );
msg = msg.arg( TQString::fromUtf8( useridHint ) ) + "<br/><br/>";
msg.prepend( "<qt>" );
msg += i18n( "This dialog will reappear every time the passphrase is needed. For a more secure solution that also allows caching the passphrase, use gpg-agent." ) + "<br/>";
const TQString gpgAgent = TDEStandardDirs::findExe( "gpg-agent" );
if ( !gpgAgent.isEmpty() ) {
msg += i18n( "gpg-agent was found in %1, but does not appear to be running." )
.arg( gpgAgent );
} else {
msg += i18n( "gpg-agent is part of gnupg-%1, which you can download from %2" )
.arg( "1.9" )
.arg( "http://www.gnupg.org/download" ); // add #gnupg2 if you can make this a real link
}
msg += "<br/>";
msg += i18n( "For information on how to set up gpg-agent, see %1" )
.arg( "http://userbase.kde.org/KMail/PGP_MIME" );
msg += "<br/><br/>";
msg += i18n( "Enter passphrase:" );
Kleo::PassphraseDialog dlg( msg, i18n("Passphrase Dialog") );
if ( dlg.exec() != TQDialog::Accepted ) {
canceled = true;
return 0;
}
canceled = false;
// gpgme++ free()s it, and we need to copy as long as dlg isn't deleted :o
return strdup( dlg.passphrase().utf8() );
}
|