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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
/*
This file is part of the KDE Password Server
Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
Copyright (C) 2005 David Faure (faure@kde.org)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This software 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 library; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
//----------------------------------------------------------------------------
//
// KDE Password Server
// $Id$
#include "kpasswdserver.h"
#include <time.h>
#include <tqtimer.h>
#include <kapplication.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <kio/passdlg.h>
#include <kwallet.h>
#include "config.h"
#ifdef Q_WS_X11
#include <X11/X.h>
#include <X11/Xlib.h>
#endif
extern "C" {
KDE_EXPORT KDEDModule *create_kpasswdserver(const TQCString &name)
{
return new KPasswdServer(name);
}
}
int
KPasswdServer::AuthInfoList::compareItems(TQPtrCollection::Item n1, TQPtrCollection::Item n2)
{
if (!n1 || !n2)
return 0;
AuthInfo *i1 = (AuthInfo *) n1;
AuthInfo *i2 = (AuthInfo *) n2;
int l1 = i1->directory.length();
int l2 = i2->directory.length();
if (l1 > l2)
return -1;
if (l1 < l2)
return 1;
return 0;
}
KPasswdServer::KPasswdServer(const TQCString &name)
: KDEDModule(name)
{
m_authDict.setAutoDelete(true);
m_authPending.setAutoDelete(true);
m_seqNr = 0;
m_wallet = 0;
connect(this, TQT_SIGNAL(windowUnregistered(long)),
this, TQT_SLOT(removeAuthForWindowId(long)));
}
KPasswdServer::~KPasswdServer()
{
delete m_wallet;
}
// Helper - returns the wallet key to use for read/store/checking for existence.
static TQString makeWalletKey( const TQString& key, const TQString& realm )
{
return realm.isEmpty() ? key : key + '-' + realm;
}
// Helper for storeInWallet/readFromWallet
static TQString makeMapKey( const char* key, int entryNumber )
{
TQString str = TQString::fromLatin1( key );
if ( entryNumber > 1 )
str += "-" + TQString::number( entryNumber );
return str;
}
static bool storeInWallet( KWallet::Wallet* wallet, const TQString& key, const KIO::AuthInfo &info )
{
if ( !wallet->hasFolder( KWallet::Wallet::PasswordFolder() ) )
if ( !wallet->createFolder( KWallet::Wallet::PasswordFolder() ) )
return false;
wallet->setFolder( KWallet::Wallet::PasswordFolder() );
// Before saving, check if there's already an entry with this login.
// If so, replace it (with the new password). Otherwise, add a new entry.
typedef TQMap<TQString,TQString> Map;
int entryNumber = 1;
Map map;
TQString walletKey = makeWalletKey( key, info.realmValue );
kdDebug(130) << "storeInWallet: walletKey=" << walletKey << " reading existing map" << endl;
if ( wallet->readMap( walletKey, map ) == 0 ) {
Map::ConstIterator end = map.end();
Map::ConstIterator it = map.find( "login" );
while ( it != end ) {
if ( it.data() == info.username ) {
break; // OK, overwrite this entry
}
it = map.find( TQString( "login-" ) + TQString::number( ++entryNumber ) );
}
// If no entry was found, create a new entry - entryNumber is set already.
}
const TQString loginKey = makeMapKey( "login", entryNumber );
const TQString passwordKey = makeMapKey( "password", entryNumber );
kdDebug(130) << "storeInWallet: writing to " << loginKey << "," << passwordKey << endl;
// note the overwrite=true by default
map.insert( loginKey, info.username );
map.insert( passwordKey, info.password );
wallet->writeMap( walletKey, map );
return true;
}
static bool readFromWallet( KWallet::Wallet* wallet, const TQString& key, const TQString& realm, TQString& username, TQString& password, bool userReadOnly, TQMap<TQString,TQString>& knownLogins )
{
//kdDebug(130) << "readFromWallet: key=" << key << " username=" << username << " password=" /*<< password*/ << " userReadOnly=" << userReadOnly << " realm=" << realm << endl;
if ( wallet->hasFolder( KWallet::Wallet::PasswordFolder() ) )
{
wallet->setFolder( KWallet::Wallet::PasswordFolder() );
TQMap<TQString,TQString> map;
if ( wallet->readMap( makeWalletKey( key, realm ), map ) == 0 )
{
typedef TQMap<TQString,TQString> Map;
int entryNumber = 1;
Map::ConstIterator end = map.end();
Map::ConstIterator it = map.find( "login" );
while ( it != end ) {
//kdDebug(130) << "readFromWallet: found " << it.key() << "=" << it.data() << endl;
Map::ConstIterator pwdIter = map.find( makeMapKey( "password", entryNumber ) );
if ( pwdIter != end ) {
if ( it.data() == username )
password = pwdIter.data();
knownLogins.insert( it.data(), pwdIter.data() );
}
it = map.find( TQString( "login-" ) + TQString::number( ++entryNumber ) );
}
//kdDebug(130) << knownLogins.count() << " known logins" << endl;
if ( !userReadOnly && !knownLogins.isEmpty() && username.isEmpty() ) {
// Pick one, any one...
username = knownLogins.begin().key();
password = knownLogins.begin().data();
//kdDebug(130) << "readFromWallet: picked the first one : " << username << endl;
}
return true;
}
}
return false;
}
KIO::AuthInfo
KPasswdServer::checkAuthInfo(KIO::AuthInfo info, long windowId)
{
return checkAuthInfo(info, windowId, 0);
}
KIO::AuthInfo
KPasswdServer::checkAuthInfo(KIO::AuthInfo info, long windowId, unsigned long usertime)
{
kdDebug(130) << "KPasswdServer::checkAuthInfo: User= " << info.username
<< ", WindowId = " << windowId << endl;
if( usertime != 0 )
kapp->updateUserTimestamp( usertime );
TQString key = createCacheKey(info);
Request *request = m_authPending.first();
TQString path2 = info.url.directory(false, false);
for(; request; request = m_authPending.next())
{
if (request->key != key)
continue;
if (info.verifyPath)
{
TQString path1 = request->info.url.directory(false, false);
if (!path2.startsWith(path1))
continue;
}
request = new Request;
request->client = callingDcopClient();
request->transaction = request->client->beginTransaction();
request->key = key;
request->info = info;
m_authWait.append(request);
return info;
}
const AuthInfo *result = findAuthInfoItem(key, info);
if (!result || result->isCanceled)
{
if (!result &&
(info.username.isEmpty() || info.password.isEmpty()) &&
!KWallet::Wallet::keyDoesNotExist(KWallet::Wallet::NetworkWallet(),
KWallet::Wallet::PasswordFolder(), makeWalletKey(key, info.realmValue)))
{
TQMap<TQString, TQString> knownLogins;
if (openWallet(windowId)) {
if (readFromWallet(m_wallet, key, info.realmValue, info.username, info.password,
info.readOnly, knownLogins))
{
info.setModified(true);
return info;
}
}
}
info.setModified(false);
return info;
}
updateAuthExpire(key, result, windowId, false);
return copyAuthInfo(result);
}
KIO::AuthInfo
KPasswdServer::queryAuthInfo(KIO::AuthInfo info, TQString errorMsg, long windowId, long seqNr)
{
return queryAuthInfo(info, errorMsg, windowId, seqNr, 0 );
}
KIO::AuthInfo
KPasswdServer::queryAuthInfo(KIO::AuthInfo info, TQString errorMsg, long windowId, long seqNr, unsigned long usertime)
{
kdDebug(130) << "KPasswdServer::queryAuthInfo: User= " << info.username
<< ", Message= " << info.prompt << ", WindowId = " << windowId << endl;
if ( !info.password.isEmpty() ) // should we really allow the caller to pre-fill the password?
kdDebug(130) << "password was set by caller" << endl;
if( usertime != 0 )
kapp->updateUserTimestamp( usertime );
TQString key = createCacheKey(info);
Request *request = new Request;
request->client = callingDcopClient();
request->transaction = request->client->beginTransaction();
request->key = key;
request->info = info;
request->windowId = windowId;
request->seqNr = seqNr;
if (errorMsg == "<NoAuthPrompt>")
{
request->errorMsg = TQString::null;
request->prompt = false;
}
else
{
request->errorMsg = errorMsg;
request->prompt = true;
}
m_authPending.append(request);
if (m_authPending.count() == 1)
TQTimer::singleShot(0, this, TQT_SLOT(processRequest()));
return info;
}
void
KPasswdServer::addAuthInfo(KIO::AuthInfo info, long windowId)
{
kdDebug(130) << "KPasswdServer::addAuthInfo: User= " << info.username
<< ", RealmValue= " << info.realmValue << ", WindowId = " << windowId << endl;
TQString key = createCacheKey(info);
m_seqNr++;
addAuthInfoItem(key, info, windowId, m_seqNr, false);
}
bool
KPasswdServer::openWallet( WId windowId )
{
if ( m_wallet && !m_wallet->isOpen() ) { // forced closed
delete m_wallet;
m_wallet = 0;
}
if ( !m_wallet )
m_wallet = KWallet::Wallet::openWallet(
KWallet::Wallet::NetworkWallet(), windowId );
return m_wallet != 0;
}
void
KPasswdServer::processRequest()
{
Request *request = m_authPending.first();
if (!request)
return;
KIO::AuthInfo &info = request->info;
kdDebug(130) << "KPasswdServer::processRequest: User= " << info.username
<< ", Message= " << info.prompt << endl;
const AuthInfo *result = findAuthInfoItem(request->key, request->info);
if (result && (request->seqNr < result->seqNr))
{
kdDebug(130) << "KPasswdServer::processRequest: auto retry!" << endl;
if (result->isCanceled)
{
info.setModified(false);
}
else
{
updateAuthExpire(request->key, result, request->windowId, false);
info = copyAuthInfo(result);
}
}
else
{
m_seqNr++;
bool askPw = request->prompt;
if (result && !info.username.isEmpty() &&
!request->errorMsg.isEmpty())
{
TQString prompt = request->errorMsg;
prompt += i18n(" Do you want to retry?");
int dlgResult = KMessageBox::warningContinueCancelWId(request->windowId, prompt,
i18n("Authentication"), i18n("Retry"));
if (dlgResult != KMessageBox::Continue)
askPw = false;
}
int dlgResult = TQDialog::Rejected;
if (askPw)
{
TQString username = info.username;
TQString password = info.password;
bool hasWalletData = false;
TQMap<TQString, TQString> knownLogins;
if ( ( username.isEmpty() || password.isEmpty() )
&& !KWallet::Wallet::keyDoesNotExist(KWallet::Wallet::NetworkWallet(), KWallet::Wallet::PasswordFolder(), makeWalletKey( request->key, info.realmValue )) )
{
// no login+pass provided, check if kwallet has one
if ( openWallet( request->windowId ) )
hasWalletData = readFromWallet( m_wallet, request->key, info.realmValue, username, password, info.readOnly, knownLogins );
}
KIO::PasswordDialog dlg( info.prompt, username, info.keepPassword );
if (info.caption.isEmpty())
dlg.setPlainCaption( i18n("Authorization Dialog") );
else
dlg.setPlainCaption( info.caption );
if ( !info.comment.isEmpty() )
dlg.addCommentLine( info.commentLabel, info.comment );
if ( !password.isEmpty() )
dlg.setPassword( password );
if (info.readOnly)
dlg.setUserReadOnly( true );
else
dlg.setKnownLogins( knownLogins );
if (hasWalletData)
dlg.setKeepPassword( true );
#ifdef Q_WS_X11
XSetTransientForHint( qt_xdisplay(), dlg.winId(), request->windowId);
#endif
dlgResult = dlg.exec();
if (dlgResult == TQDialog::Accepted)
{
info.username = dlg.username();
info.password = dlg.password();
info.keepPassword = dlg.keepPassword();
// When the user checks "keep password", that means:
// * if the wallet is enabled, store it there for long-term, and in kpasswdserver
// only for the duration of the window (#92928)
// * otherwise store in kpasswdserver for the duration of the KDE session.
if ( info.keepPassword ) {
if ( openWallet( request->windowId ) ) {
if ( storeInWallet( m_wallet, request->key, info ) )
// password is in wallet, don't keep it in memory after window is closed
info.keepPassword = false;
}
}
}
}
if ( dlgResult != TQDialog::Accepted )
{
addAuthInfoItem(request->key, info, 0, m_seqNr, true);
info.setModified( false );
}
else
{
addAuthInfoItem(request->key, info, request->windowId, m_seqNr, false);
info.setModified( true );
}
}
TQCString replyType;
TQByteArray replyData;
TQDataStream stream2(replyData, IO_WriteOnly);
stream2 << info << m_seqNr;
replyType = "KIO::AuthInfo";
request->client->endTransaction( request->transaction,
replyType, replyData);
m_authPending.remove((unsigned int) 0);
// Check all requests in the wait queue.
for(Request *waitRequest = m_authWait.first();
waitRequest; )
{
bool keepQueued = false;
TQString key = waitRequest->key;
request = m_authPending.first();
TQString path2 = waitRequest->info.url.directory(false, false);
for(; request; request = m_authPending.next())
{
if (request->key != key)
continue;
if (info.verifyPath)
{
TQString path1 = request->info.url.directory(false, false);
if (!path2.startsWith(path1))
continue;
}
keepQueued = true;
break;
}
if (keepQueued)
{
waitRequest = m_authWait.next();
}
else
{
const AuthInfo *result = findAuthInfoItem(waitRequest->key, waitRequest->info);
TQCString replyType;
TQByteArray replyData;
TQDataStream stream2(replyData, IO_WriteOnly);
if (!result || result->isCanceled)
{
waitRequest->info.setModified(false);
stream2 << waitRequest->info;
}
else
{
updateAuthExpire(waitRequest->key, result, waitRequest->windowId, false);
KIO::AuthInfo info = copyAuthInfo(result);
stream2 << info;
}
replyType = "KIO::AuthInfo";
waitRequest->client->endTransaction( waitRequest->transaction,
replyType, replyData);
m_authWait.remove();
waitRequest = m_authWait.current();
}
}
if (m_authPending.count())
TQTimer::singleShot(0, this, TQT_SLOT(processRequest()));
}
TQString KPasswdServer::createCacheKey( const KIO::AuthInfo &info )
{
if( !info.url.isValid() ) {
// Note that a null key will break findAuthInfoItem later on...
kdWarning(130) << "createCacheKey: invalid URL " << info.url << endl;
return TQString::null;
}
// Generate the basic key sequence.
TQString key = info.url.protocol();
key += '-';
if (!info.url.user().isEmpty())
{
key += info.url.user();
key += "@";
}
key += info.url.host();
int port = info.url.port();
if( port )
{
key += ':';
key += TQString::number(port);
}
return key;
}
KIO::AuthInfo
KPasswdServer::copyAuthInfo(const AuthInfo *i)
{
KIO::AuthInfo result;
result.url = i->url;
result.username = i->username;
result.password = i->password;
result.realmValue = i->realmValue;
result.digestInfo = i->digestInfo;
result.setModified(true);
return result;
}
const KPasswdServer::AuthInfo *
KPasswdServer::findAuthInfoItem(const TQString &key, const KIO::AuthInfo &info)
{
AuthInfoList *authList = m_authDict.find(key);
if (!authList)
return 0;
TQString path2 = info.url.directory(false, false);
for(AuthInfo *current = authList->first();
current; )
{
if ((current->expire == AuthInfo::expTime) &&
(difftime(time(0), current->expireTime) > 0))
{
authList->remove();
current = authList->current();
continue;
}
if (info.verifyPath)
{
TQString path1 = current->directory;
if (path2.startsWith(path1) &&
(info.username.isEmpty() || info.username == current->username))
return current;
}
else
{
if (current->realmValue == info.realmValue &&
(info.username.isEmpty() || info.username == current->username))
return current; // TODO: Update directory info,
}
current = authList->next();
}
return 0;
}
void
KPasswdServer::removeAuthInfoItem(const TQString &key, const KIO::AuthInfo &info)
{
AuthInfoList *authList = m_authDict.find(key);
if (!authList)
return;
for(AuthInfo *current = authList->first();
current; )
{
if (current->realmValue == info.realmValue)
{
authList->remove();
current = authList->current();
}
else
{
current = authList->next();
}
}
if (authList->isEmpty())
{
m_authDict.remove(key);
}
}
void
KPasswdServer::addAuthInfoItem(const TQString &key, const KIO::AuthInfo &info, long windowId, long seqNr, bool canceled)
{
AuthInfoList *authList = m_authDict.find(key);
if (!authList)
{
authList = new AuthInfoList;
m_authDict.insert(key, authList);
}
AuthInfo *current = authList->first();
for(; current; current = authList->next())
{
if (current->realmValue == info.realmValue)
{
authList->take();
break;
}
}
if (!current)
{
current = new AuthInfo;
current->expire = AuthInfo::expTime;
kdDebug(130) << "Creating AuthInfo" << endl;
}
else
{
kdDebug(130) << "Updating AuthInfo" << endl;
}
current->url = info.url;
current->directory = info.url.directory(false, false);
current->username = info.username;
current->password = info.password;
current->realmValue = info.realmValue;
current->digestInfo = info.digestInfo;
current->seqNr = seqNr;
current->isCanceled = canceled;
updateAuthExpire(key, current, windowId, info.keepPassword && !canceled);
// Insert into list, keep the list sorted "longest path" first.
authList->inSort(current);
}
void
KPasswdServer::updateAuthExpire(const TQString &key, const AuthInfo *auth, long windowId, bool keep)
{
AuthInfo *current = const_cast<AuthInfo *>(auth);
if (keep)
{
current->expire = AuthInfo::expNever;
}
else if (windowId && (current->expire != AuthInfo::expNever))
{
current->expire = AuthInfo::expWindowClose;
if (!current->windowList.contains(windowId))
current->windowList.append(windowId);
}
else if (current->expire == AuthInfo::expTime)
{
current->expireTime = time(0)+10;
}
// Update mWindowIdList
if (windowId)
{
TQStringList *keysChanged = mWindowIdList.find(windowId);
if (!keysChanged)
{
keysChanged = new QStringList;
mWindowIdList.insert(windowId, keysChanged);
}
if (!keysChanged->contains(key))
keysChanged->append(key);
}
}
void
KPasswdServer::removeAuthForWindowId(long windowId)
{
TQStringList *keysChanged = mWindowIdList.find(windowId);
if (!keysChanged) return;
for(TQStringList::ConstIterator it = keysChanged->begin();
it != keysChanged->end(); ++it)
{
TQString key = *it;
AuthInfoList *authList = m_authDict.find(key);
if (!authList)
continue;
AuthInfo *current = authList->first();
for(; current; )
{
if (current->expire == AuthInfo::expWindowClose)
{
if (current->windowList.remove(windowId) && current->windowList.isEmpty())
{
authList->remove();
current = authList->current();
continue;
}
}
current = authList->next();
}
}
}
#include "kpasswdserver.moc"
|