00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "kwalletd.h"
00025
00026 #include "kbetterthankdialog.h"
00027 #include "kwalletwizard.h"
00028 #include "ktimeout.h"
00029
00030 #include <kuniqueapplication.h>
00031 #include <ktoolinvocation.h>
00032 #include <kconfig.h>
00033 #include <kconfiggroup.h>
00034 #include <kdebug.h>
00035 #include <kdirwatch.h>
00036 #include <kglobal.h>
00037 #include <klocale.h>
00038 #include <kmessagebox.h>
00039 #include <kpassworddialog.h>
00040 #include <knewpassworddialog.h>
00041 #include <kstandarddirs.h>
00042 #include <kwalletentry.h>
00043 #include <kwindowsystem.h>
00044 #include <kpluginfactory.h>
00045 #include <kpluginloader.h>
00046
00047 #include <QtCore/QDir>
00048 #include <QtGui/QTextDocument>
00049 #include <QtCore/QRegExp>
00050 #include <QtCore/QTimer>
00051
00052 #include <assert.h>
00053
00054 #include "kwalletdadaptor.h"
00055 #include "kwalletsynctimer.h"
00056
00057 class KWalletTransaction {
00058 public:
00059 KWalletTransaction() {
00060 tType = Unknown;
00061 }
00062
00063 ~KWalletTransaction() {
00064 }
00065
00066 enum Type { Unknown, Open, ChangePassword, OpenFail };
00067 QDBusMessage msg;
00068 Type tType;
00069 QString appid;
00070 qlonglong wId;
00071 QString wallet;
00072 bool modal;
00073 };
00074
00075 KWalletD::KWalletD()
00076 : QObject(0), _failed(0) {
00077 srand(time(0));
00078 _showingFailureNotify = false;
00079 _timeouts = new KTimeout();
00080 _closeIdle = false;
00081 _idleTime = 0;
00082 connect(_timeouts, SIGNAL(timedOut(int)), this, SLOT(timedOut(int)));
00083
00084 (void)new KWalletDAdaptor(this);
00085
00086 QDBusConnection::sessionBus().registerService(QLatin1String("org.kde.kwalletd"));
00087 QDBusConnection::sessionBus().registerObject(QLatin1String("/modules/kwalletd"), this);
00088
00089 #ifdef Q_WS_X11
00090 screensaver = new QDBusInterface("org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver");
00091 #endif
00092
00093 reconfigure();
00094 KGlobal::dirs()->addResourceType("kwallet", 0, "share/apps/kwallet");
00095 connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceUnregistered(QString)),
00096 SLOT(slotServiceUnregistered(QString)));
00097 _dw = new KDirWatch(this );
00098 _dw->setObjectName( "KWallet Directory Watcher" );
00099 _dw->addDir(KGlobal::dirs()->saveLocation("kwallet"));
00100 _dw->startScan(true);
00101 connect(_dw, SIGNAL(dirty(const QString&)), this, SLOT(emitWalletListDirty()));
00102 }
00103
00104
00105 KWalletD::~KWalletD() {
00106 delete _timeouts;
00107 _timeouts = 0;
00108 #ifdef Q_WS_X11
00109 delete screensaver;
00110 screensaver = 0;
00111 #endif
00112 closeAllWallets();
00113 qDeleteAll(_synctimers);
00114 qDeleteAll(_transactions);
00115 }
00116
00117
00118 int KWalletD::generateHandle() {
00119 int rc;
00120
00121
00122 do {
00123 rc = rand();
00124 } while (_wallets.contains(rc) || rc == 0);
00125
00126 return rc;
00127 }
00128
00129 QPair<int, KWallet::Backend*> KWalletD::findWallet(const QString& walletName) const
00130 {
00131 Wallets::const_iterator it = _wallets.begin();
00132 const Wallets::const_iterator end = _wallets.end();
00133 for (; it != end; ++it) {
00134 if (it.value()->walletName() == walletName) {
00135 return qMakePair(it.key(), it.value());
00136 }
00137 }
00138 return qMakePair(-1, static_cast<KWallet::Backend*>(0));
00139 }
00140
00141 void KWalletD::processTransactions() {
00142 static bool processing = false;
00143
00144 if (processing) {
00145 return;
00146 }
00147
00148 processing = true;
00149
00150
00151 KWalletTransaction *xact;
00152 while (!_transactions.isEmpty()) {
00153 xact = _transactions.first();
00154 int res;
00155
00156 assert(xact->tType != KWalletTransaction::Unknown);
00157
00158 switch (xact->tType) {
00159 case KWalletTransaction::Open:
00160 res = doTransactionOpen(xact->appid, xact->wallet, xact->wId, xact->modal);
00161
00162
00163
00164
00165 if (res < 0) {
00166 QList<KWalletTransaction *>::iterator it = _transactions.begin();
00167 Q_ASSERT(*it == xact);
00168 ++it;
00169 for (; it != _transactions.end(); ++it) {
00170 KWalletTransaction *x = *it;
00171 if (xact->appid == x->appid && x->tType == KWalletTransaction::Open
00172 && x->wallet == xact->wallet && x->wId == xact->wId) {
00173 x->tType = KWalletTransaction::OpenFail;
00174 }
00175 }
00176 }
00177 break;
00178 case KWalletTransaction::OpenFail:
00179 res = -1;
00180 break;
00181 case KWalletTransaction::ChangePassword:
00182 doTransactionChangePassword(xact->appid, xact->wallet, xact->wId);
00183
00184 default:
00185 _transactions.removeAll(xact);
00186 continue;
00187 }
00188
00189 if (xact->tType != KWalletTransaction::ChangePassword) {
00190 QDBusConnection::sessionBus().send(xact->msg.createReply(res));
00191 }
00192 _transactions.removeAll(xact);
00193 }
00194
00195 processing = false;
00196 }
00197
00198 #if 0
00199 void KWalletD::openAsynchronous(const QString& wallet, const QByteArray& returnObject, uint wId, const QString& appid) {
00200 DCOPClient *dc = callingDcopClient();
00201 if (!dc) {
00202 return;
00203 }
00204
00205 if (!_enabled ||
00206 !QRegExp("^[A-Za-z0-9]+[A-Za-z0-9\\s\\-_]*$").exactMatch(wallet)) {
00207 DCOPRef(appid, returnObject).send("walletOpenResult", -1);
00208 return;
00209 }
00210
00211 KWalletTransaction *xact = new KWalletTransaction;
00212
00213 xact->appid = appid;
00214 xact->wallet = wallet;
00215 xact->wId = wId;
00216 xact->modal = false;
00217 xact->tType = KWalletTransaction::Open;
00218 xact->returnObject = returnObject;
00219 _transactions.append(xact);
00220
00221 DCOPRef(appid, returnObject).send("walletOpenResult", 0);
00222
00223 QTimer::singleShot(0, this, SLOT(processTransactions()));
00224 }
00225 #endif
00226
00227 int KWalletD::openPath(const QString& path, qlonglong wId, const QString& appid) {
00228 if (!_enabled) {
00229 return -1;
00230 }
00231
00232
00233 int rc = internalOpen(appid, path, true, (WId)wId, false);
00234 return rc;
00235 }
00236
00237
00238 int KWalletD::open(const QString& wallet, qlonglong wId, const QString& appid, const QDBusMessage &msg) {
00239 if (!_enabled) {
00240 return -1;
00241 }
00242
00243 if (!QRegExp("^[A-Za-z0-9]+[A-Za-z0-9\\s\\-_]*$").exactMatch(wallet)) {
00244 return -1;
00245 }
00246
00247 KWalletTransaction *xact = new KWalletTransaction;
00248 _transactions.append(xact);
00249
00250 msg.setDelayedReply(true);
00251 xact->msg = msg;
00252 xact->appid = appid;
00253 xact->wallet = wallet;
00254 xact->wId = wId;
00255 xact->modal = true;
00256 xact->tType = KWalletTransaction::Open;
00257 QTimer::singleShot(0, this, SLOT(processTransactions()));
00258 checkActiveDialog();
00259 return 0;
00260 }
00261
00262
00263 void KWalletD::setupDialog( QWidget* dialog, WId wId, const QString& appid, bool modal ) {
00264 #ifdef Q_WS_X11
00265 if( wId != 0 )
00266 KWindowSystem::setMainWindow( dialog, wId );
00267 else {
00268 #endif
00269 if( appid.isEmpty())
00270 kWarning() << "Using kwallet without parent window!";
00271 else
00272 kWarning() << "Application '" << appid << "' using kwallet without parent window!";
00273
00274
00275 kapp->updateUserTimestamp();
00276 #ifdef Q_WS_X11
00277 }
00278 if( modal )
00279 KWindowSystem::setState( dialog->winId(), NET::Modal );
00280 else
00281 KWindowSystem::clearState( dialog->winId(), NET::Modal );
00282 #endif
00283 activeDialog = dialog;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 void KWalletD::checkActiveDialog() {
00293 if( !activeDialog || activeDialog->isHidden())
00294 return;
00295 kapp->updateUserTimestamp();
00296 #ifdef Q_WS_X11
00297 KWindowSystem::setState( activeDialog->winId(), NET::KeepAbove );
00298 KWindowSystem::setOnAllDesktops( activeDialog->winId(), true );
00299 KWindowSystem::forceActiveWindow( activeDialog->winId());
00300 #endif
00301 }
00302
00303
00304 int KWalletD::doTransactionOpen(const QString& appid, const QString& wallet, qlonglong wId, bool modal) {
00305 if (_firstUse && !wallets().contains(KWallet::Wallet::LocalWallet())) {
00306
00307 KWalletWizard *wiz = new KWalletWizard(0);
00308 wiz->setWindowTitle(i18n("KDE Wallet Service"));
00309 setupDialog( wiz, (WId)wId, appid, modal );
00310 int rc = wiz->exec();
00311 if (rc == QDialog::Accepted) {
00312 bool useWallet = wiz->field("useWallet").toBool();
00313 KConfig kwalletrc("kwalletrc");
00314 KConfigGroup cfg(&kwalletrc, "Wallet");
00315 cfg.writeEntry("First Use", false);
00316 cfg.writeEntry("Enabled", useWallet);
00317 cfg.writeEntry("Close When Idle", wiz->field("closeWhenIdle").toBool());
00318 cfg.writeEntry("Use One Wallet", !wiz->field("networkWallet").toBool());
00319 cfg.sync();
00320 reconfigure();
00321
00322 if (!useWallet) {
00323 delete wiz;
00324 return -1;
00325 }
00326
00327
00328 KWallet::Backend *b = new KWallet::Backend(KWallet::Wallet::LocalWallet());
00329 QString pass = wiz->field("pass1").toString();
00330 QByteArray p(pass.toUtf8(), pass.length());
00331 b->open(p);
00332 b->createFolder(KWallet::Wallet::PasswordFolder());
00333 b->createFolder(KWallet::Wallet::FormDataFolder());
00334 b->close(p);
00335 p.fill(0);
00336 delete b;
00337 delete wiz;
00338 } else {
00339 delete wiz;
00340 return -1;
00341 }
00342 } else if (_firstUse) {
00343 KConfig kwalletrc("kwalletrc");
00344 KConfigGroup cfg(&kwalletrc, "Wallet");
00345 _firstUse = false;
00346 cfg.writeEntry("First Use", false);
00347 }
00348
00349 int rc = internalOpen(appid, wallet, false, WId(wId), modal);
00350 return rc;
00351 }
00352
00353
00354 int KWalletD::internalOpen(const QString& appid, const QString& wallet, bool isPath, WId w, bool modal) {
00355 bool brandNew = false;
00356
00357 QString thisApp;
00358 if (appid.isEmpty()) {
00359 thisApp = "KDE System";
00360 } else {
00361 thisApp = appid;
00362 }
00363
00364 if (implicitDeny(wallet, thisApp)) {
00365 return -1;
00366 }
00367
00368 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
00369 int rc = walletInfo.first;
00370 if (rc == -1) {
00371 if (_wallets.count() > 20) {
00372 kDebug() << "Too many wallets open.";
00373 return -1;
00374 }
00375
00376 KWallet::Backend *b = new KWallet::Backend(wallet, isPath);
00377 QString password;
00378 bool emptyPass = false;
00379 if ((isPath && QFile::exists(wallet)) || (!isPath && KWallet::Backend::exists(wallet))) {
00380 int pwless = b->open(QByteArray());
00381 if (0 != pwless || !b->isOpen()) {
00382 if (pwless == 0) {
00383
00384 delete b;
00385 b = new KWallet::Backend(wallet, isPath);
00386 }
00387 KPasswordDialog *kpd = new KPasswordDialog();
00388 if (appid.isEmpty()) {
00389 kpd->setPrompt(i18n("<qt>KDE has requested to open the wallet '<b>%1</b>'. Please enter the password for this wallet below.</qt>", Qt::escape(wallet)));
00390 } else {
00391 kpd->setPrompt(i18n("<qt>The application '<b>%1</b>' has requested to open the wallet '<b>%2</b>'. Please enter the password for this wallet below.</qt>", Qt::escape(appid), Qt::escape(wallet)));
00392 }
00393 brandNew = false;
00394
00395 kpd->setButtonGuiItem(KDialog::Ok,KGuiItem( i18n( "&Open" ), "document-open"));
00396 kpd->setCaption(i18n("KDE Wallet Service"));
00397 while (!b->isOpen()) {
00398 setupDialog( kpd, w, appid, modal );
00399 if (kpd->exec() == KDialog::Accepted) {
00400 password = kpd->password();
00401 int rc = b->open(password.toUtf8());
00402 if (!b->isOpen()) {
00403 kpd->setPrompt(i18n("<qt>Error opening the wallet '<b>%1</b>'. Please try again.<br />(Error code %2: %3)</qt>", Qt::escape(wallet), rc, KWallet::Backend::openRCToString(rc)));
00404 }
00405 } else {
00406 break;
00407 }
00408 }
00409 delete kpd;
00410 } else {
00411 emptyPass = true;
00412 }
00413 } else {
00414 KNewPasswordDialog *kpd = new KNewPasswordDialog();
00415 if (wallet == KWallet::Wallet::LocalWallet() ||
00416 wallet == KWallet::Wallet::NetworkWallet())
00417 {
00418
00419 if (appid.isEmpty()) {
00420 kpd->setPrompt(i18n("KDE has requested to open the wallet. This is used to store sensitive data in a secure fashion. Please enter a password to use with this wallet or click cancel to deny the application's request."));
00421 } else {
00422 kpd->setPrompt(i18n("<qt>The application '<b>%1</b>' has requested to open the KDE wallet. This is used to store sensitive data in a secure fashion. Please enter a password to use with this wallet or click cancel to deny the application's request.</qt>", Qt::escape(appid)));
00423 }
00424 } else {
00425 if (appid.length() == 0) {
00426 kpd->setPrompt(i18n("<qt>KDE has requested to create a new wallet named '<b>%1</b>'. Please choose a password for this wallet, or cancel to deny the application's request.</qt>", Qt::escape(wallet)));
00427 } else {
00428 kpd->setPrompt(i18n("<qt>The application '<b>%1</b>' has requested to create a new wallet named '<b>%2</b>'. Please choose a password for this wallet, or cancel to deny the application's request.</qt>", Qt::escape(appid), Qt::escape(wallet)));
00429 }
00430 }
00431 brandNew = true;
00432 kpd->setCaption(i18n("KDE Wallet Service"));
00433 kpd->setButtonGuiItem(KDialog::Ok,KGuiItem(i18n("C&reate"),"document-new"));
00434 while (!b->isOpen()) {
00435 setupDialog( kpd, w, appid, modal );
00436 if (kpd->exec() == KDialog::Accepted) {
00437 password = kpd->password();
00438 int rc = b->open(password.toUtf8());
00439 if (!b->isOpen()) {
00440 kpd->setPrompt(i18n("<qt>Error opening the wallet '<b>%1</b>'. Please try again.<br />(Error code %2: %3)</qt>", Qt::escape(wallet), rc, KWallet::Backend::openRCToString(rc)));
00441 }
00442 } else {
00443 break;
00444 }
00445 }
00446 delete kpd;
00447 }
00448
00449
00450
00451 if (!emptyPass && (password.isNull() || !b->isOpen())) {
00452 delete b;
00453 return -1;
00454 }
00455
00456 if (emptyPass && _openPrompt && !isAuthorizedApp(appid, wallet, w)) {
00457 delete b;
00458 return -1;
00459 }
00460
00461 _wallets.insert(rc = generateHandle(), b);
00462 if (emptyPass) {
00463 _passwords[wallet] = "";
00464 } else {
00465 _passwords[wallet] = password.toUtf8();
00466 }
00467 _handles[appid].append(rc);
00468 _synctimers[wallet] = new KWalletSyncTimer(this, wallet);
00469 connect(_synctimers[wallet], SIGNAL(timeoutSync(const QString&)), this, SLOT(doTransactionSync(const QString&)));
00470
00471 if (brandNew) {
00472 createFolder(rc, KWallet::Wallet::PasswordFolder(), appid);
00473 createFolder(rc, KWallet::Wallet::FormDataFolder(), appid);
00474 }
00475
00476 b->ref();
00477 if (_closeIdle && _timeouts) {
00478 _timeouts->addTimer(rc, _idleTime);
00479 }
00480 if (brandNew)
00481 emit walletCreated(wallet);
00482 emit walletOpened(wallet);
00483 if (_wallets.count() == 1 && _launchManager) {
00484 KToolInvocation::startServiceByDesktopName("kwalletmanager-kwalletd");
00485 }
00486 } else {
00487 if (!_handles[appid].contains(rc) && _openPrompt && !isAuthorizedApp(appid, wallet, w)) {
00488 return -1;
00489 }
00490 _handles[appid].append(rc);
00491 _wallets.value(rc)->ref();
00492 }
00493
00494 return rc;
00495 }
00496
00497
00498 bool KWalletD::isAuthorizedApp(const QString& appid, const QString& wallet, WId w) {
00499 int response = 0;
00500
00501 QString thisApp;
00502 if (appid.isEmpty()) {
00503 thisApp = "KDE System";
00504 } else {
00505 thisApp = appid;
00506 }
00507
00508 if (!implicitAllow(wallet, thisApp)) {
00509 KConfigGroup cfg = KSharedConfig::openConfig("kwalletrc")->group("Auto Allow");
00510 if (!cfg.isEntryImmutable(wallet)) {
00511 KBetterThanKDialog *dialog = new KBetterThanKDialog;
00512 if (appid.isEmpty()) {
00513 dialog->setLabel(i18n("<qt>KDE has requested access to the open wallet '<b>%1</b>'.</qt>", Qt::escape(wallet)));
00514 } else {
00515 dialog->setLabel(i18n("<qt>The application '<b>%1</b>' has requested access to the open wallet '<b>%2</b>'.</qt>", Qt::escape(QString(appid)), Qt::escape(wallet)));
00516 }
00517 setupDialog( dialog, w, appid, false );
00518 response = dialog->exec();
00519 delete dialog;
00520 }
00521 }
00522
00523 if (response == 0 || response == 1) {
00524 if (response == 1) {
00525 KConfigGroup cfg = KSharedConfig::openConfig("kwalletrc")->group("Auto Allow");
00526 QStringList apps = cfg.readEntry(wallet, QStringList());
00527 if (!apps.contains(thisApp)) {
00528 if (cfg.isEntryImmutable(wallet)) {
00529 return false;
00530 }
00531 apps += thisApp;
00532 _implicitAllowMap[wallet] += thisApp;
00533 cfg.writeEntry(wallet, apps);
00534 cfg.sync();
00535 }
00536 }
00537 } else if (response == 3) {
00538 KConfigGroup cfg = KSharedConfig::openConfig("kwalletrc")->group("Auto Deny");
00539 QStringList apps = cfg.readEntry(wallet, QStringList());
00540 if (!apps.contains(thisApp)) {
00541 apps += thisApp;
00542 _implicitDenyMap[wallet] += thisApp;
00543 cfg.writeEntry(wallet, apps);
00544 cfg.sync();
00545 }
00546 return false;
00547 } else {
00548 return false;
00549 }
00550 return true;
00551 }
00552
00553
00554 int KWalletD::deleteWallet(const QString& wallet) {
00555 QString path = KGlobal::dirs()->saveLocation("kwallet") + QDir::separator() + wallet + ".kwl";
00556
00557 if (QFile::exists(path)) {
00558 close(wallet, true);
00559 QFile::remove(path);
00560 emit walletDeleted(wallet);
00561 return 0;
00562 }
00563
00564 return -1;
00565 }
00566
00567
00568 void KWalletD::changePassword(const QString& wallet, qlonglong wId, const QString& appid, const QDBusMessage& msg) {
00569 KWalletTransaction *xact = new KWalletTransaction;
00570
00571
00572 xact->msg = msg;
00573 xact->appid = appid;
00574 xact->wallet = wallet;
00575 xact->wId = wId;
00576 xact->modal = false;
00577 xact->tType = KWalletTransaction::ChangePassword;
00578
00579 _transactions.append(xact);
00580
00581 QTimer::singleShot(0, this, SLOT(processTransactions()));
00582 checkActiveDialog();
00583 checkActiveDialog();
00584 }
00585
00586 void KWalletD::initiateSync(const QString& wallet) {
00587 assert(_synctimers.contains(wallet));
00588
00589 _synctimers[wallet]->start();
00590 }
00591
00592 void KWalletD::doTransactionChangePassword(const QString& appid, const QString& wallet, qlonglong wId) {
00593
00594 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
00595 int handle = walletInfo.first;
00596 KWallet::Backend* w = walletInfo.second;
00597
00598 bool reclose = false;
00599 if (!w) {
00600 handle = doTransactionOpen(appid, wallet, wId, false);
00601 if (-1 == handle) {
00602 KMessageBox::sorryWId((WId)wId, i18n("Unable to open wallet. The wallet must be opened in order to change the password."), i18n("KDE Wallet Service"));
00603 return;
00604 }
00605
00606 w = _wallets.value(handle);
00607 reclose = true;
00608 }
00609
00610 assert(w);
00611
00612 KNewPasswordDialog *kpd = new KNewPasswordDialog();
00613 kpd->setPrompt(i18n("<qt>Please choose a new password for the wallet '<b>%1</b>'.</qt>", Qt::escape(wallet)));
00614 kpd->setCaption(i18n("KDE Wallet Service"));
00615 kpd->setAllowEmptyPasswords(true);
00616 setupDialog( kpd, (WId)wId, appid, false );
00617 if (kpd->exec() == KDialog::Accepted) {
00618 QString p = kpd->password();
00619 if (!p.isNull()) {
00620 _passwords[wallet] = p.toUtf8();
00621 int rc = w->close(p.toUtf8());
00622 if (rc < 0) {
00623 KMessageBox::sorryWId((WId)wId, i18n("Error re-encrypting the wallet. Password was not changed."), i18n("KDE Wallet Service"));
00624 reclose = true;
00625 } else {
00626 rc = w->open(p.toUtf8());
00627 if (rc < 0) {
00628 KMessageBox::sorryWId((WId)wId, i18n("Error reopening the wallet. Data may be lost."), i18n("KDE Wallet Service"));
00629 reclose = true;
00630 }
00631 }
00632 }
00633 }
00634
00635 delete kpd;
00636
00637 if (reclose) {
00638 close(handle, true, appid);
00639 }
00640 }
00641
00642
00643 int KWalletD::close(const QString& wallet, bool force) {
00644 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
00645 int handle = walletInfo.first;
00646 KWallet::Backend* w = walletInfo.second;
00647
00648 return closeWallet(w, handle, force);
00649 }
00650
00651
00652 int KWalletD::closeWallet(KWallet::Backend *w, int handle, bool force) {
00653 if (w) {
00654 const QString& wallet = w->walletName();
00655 assert(_passwords.contains(wallet));
00656 assert(_synctimers.contains(wallet));
00657 if (w->refCount() == 0 || force) {
00658 invalidateHandle(handle);
00659 if (_closeIdle && _timeouts) {
00660 _timeouts->removeTimer(handle);
00661 }
00662 _wallets.remove(handle);
00663 delete _synctimers.take(wallet);
00664 if (_passwords.contains(wallet)) {
00665 w->close(QByteArray(_passwords[wallet].data(), _passwords[wallet].length()));
00666 _passwords[wallet].fill(0);
00667 _passwords.remove(wallet);
00668 }
00669 doCloseSignals(handle, wallet);
00670 delete w;
00671 return 0;
00672 }
00673 return 1;
00674 }
00675
00676 return -1;
00677 }
00678
00679
00680 int KWalletD::close(int handle, bool force, const QString& appid) {
00681 KWallet::Backend *w = _wallets.value(handle);
00682 bool contains = false;
00683
00684 if (w) {
00685 if (_handles.contains(appid)) {
00686 if (_handles[appid].contains(handle)) {
00687
00688 _handles[appid].removeAt(_handles[appid].indexOf(handle));
00689 contains = true;
00690 if (_handles[appid].isEmpty()) {
00691 _handles.remove(appid);
00692 }
00693 }
00694 }
00695
00696
00697 if ((contains && w->deref() == 0 && !_leaveOpen) || force) {
00698 if (_closeIdle && _timeouts) {
00699 _timeouts->removeTimer(handle);
00700 }
00701 _wallets.remove(handle);
00702 if (_synctimers.contains(w->walletName())) {
00703 delete _synctimers.take(w->walletName());
00704 }
00705 if (force) {
00706 invalidateHandle(handle);
00707 }
00708 if (_passwords.contains(w->walletName())) {
00709 w->close(QByteArray(_passwords[w->walletName()].data(), _passwords[w->walletName()].length()));
00710 _passwords[w->walletName()].fill(0);
00711 _passwords.remove(w->walletName());
00712 }
00713 doCloseSignals(handle, w->walletName());
00714 delete w;
00715 return 0;
00716 }
00717 return 1;
00718 }
00719
00720 return -1;
00721 }
00722
00723
00724 bool KWalletD::isOpen(const QString& wallet) {
00725 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
00726 return walletInfo.second != 0;
00727 }
00728
00729
00730 bool KWalletD::isOpen(int handle) {
00731 if (handle == 0) {
00732 return false;
00733 }
00734
00735 KWallet::Backend *rc = _wallets.value(handle);
00736
00737 if (rc == 0 && ++_failed > 5) {
00738 _failed = 0;
00739 QTimer::singleShot(0, this, SLOT(notifyFailures()));
00740 } else if (rc != 0) {
00741 _failed = 0;
00742 }
00743
00744 return rc != 0;
00745 }
00746
00747
00748 QStringList KWalletD::wallets() const {
00749 QString path = KGlobal::dirs()->saveLocation("kwallet");
00750 QDir dir(path, "*.kwl");
00751 QStringList rc;
00752
00753 dir.setFilter(QDir::Files | QDir::NoSymLinks);
00754
00755 foreach (const QFileInfo &fi, dir.entryInfoList()) {
00756 QString fn = fi.fileName();
00757 if (fn.endsWith(".kwl")) {
00758 fn.truncate(fn.length()-4);
00759 }
00760 rc += fn;
00761 }
00762 return rc;
00763 }
00764
00765
00766 void KWalletD::sync(int handle, const QString& appid) {
00767 KWallet::Backend *b;
00768
00769 if ((b = getWallet(appid, handle))) {
00770 QByteArray p;
00771 QString wallet = b->walletName();
00772 p = QByteArray(_passwords[wallet].data(), _passwords[wallet].length());
00773 b->sync(p);
00774 p.fill(0);
00775 }
00776 }
00777
00778 void KWalletD::doTransactionSync(const QString& wallet) {
00779 if (_synctimers.contains(wallet)) {
00780 _synctimers[wallet]->stop();
00781 }
00782 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
00783
00784 if (walletInfo.second) {
00785 QByteArray p = QByteArray(_passwords[wallet].data(), _passwords[wallet].length());
00786 walletInfo.second->sync(p);
00787 p.fill(0);
00788 }
00789 }
00790
00791
00792 QStringList KWalletD::folderList(int handle, const QString& appid) {
00793 KWallet::Backend *b;
00794
00795 if ((b = getWallet(appid, handle))) {
00796 return b->folderList();
00797 }
00798
00799 return QStringList();
00800 }
00801
00802
00803 bool KWalletD::hasFolder(int handle, const QString& f, const QString& appid) {
00804 KWallet::Backend *b;
00805
00806 if ((b = getWallet(appid, handle))) {
00807 return b->hasFolder(f);
00808 }
00809
00810 return false;
00811 }
00812
00813
00814 bool KWalletD::removeFolder(int handle, const QString& f, const QString& appid) {
00815 KWallet::Backend *b;
00816
00817 if ((b = getWallet(appid, handle))) {
00818 bool rc = b->removeFolder(f);
00819 initiateSync(b->walletName());
00820 emit folderListUpdated(b->walletName());
00821 return rc;
00822 }
00823
00824 return false;
00825 }
00826
00827
00828 bool KWalletD::createFolder(int handle, const QString& f, const QString& appid) {
00829 KWallet::Backend *b;
00830
00831 if ((b = getWallet(appid, handle))) {
00832 bool rc = b->createFolder(f);
00833 initiateSync(b->walletName());
00834 emit folderListUpdated(b->walletName());
00835 return rc;
00836 }
00837
00838 return false;
00839 }
00840
00841
00842 QByteArray KWalletD::readMap(int handle, const QString& folder, const QString& key, const QString& appid) {
00843 KWallet::Backend *b;
00844
00845 if ((b = getWallet(appid, handle))) {
00846 b->setFolder(folder);
00847 KWallet::Entry *e = b->readEntry(key);
00848 if (e && e->type() == KWallet::Wallet::Map) {
00849 return e->map();
00850 }
00851 }
00852
00853 return QByteArray();
00854 }
00855
00856
00857 QVariantMap KWalletD::readMapList(int handle, const QString& folder, const QString& key, const QString& appid) {
00858 KWallet::Backend *b;
00859
00860 if ((b = getWallet(appid, handle))) {
00861 b->setFolder(folder);
00862 QVariantMap rc;
00863 foreach (KWallet::Entry *entry, b->readEntryList(key)) {
00864 if (entry->type() == KWallet::Wallet::Map) {
00865 rc.insert(entry->key(), entry->map());
00866 }
00867 }
00868 return rc;
00869 }
00870
00871 return QVariantMap();
00872 }
00873
00874
00875 QByteArray KWalletD::readEntry(int handle, const QString& folder, const QString& key, const QString& appid) {
00876 KWallet::Backend *b;
00877
00878 if ((b = getWallet(appid, handle))) {
00879 b->setFolder(folder);
00880 KWallet::Entry *e = b->readEntry(key);
00881 if (e) {
00882 return e->value();
00883 }
00884 }
00885
00886 return QByteArray();
00887 }
00888
00889
00890 QVariantMap KWalletD::readEntryList(int handle, const QString& folder, const QString& key, const QString& appid) {
00891 KWallet::Backend *b;
00892
00893 if ((b = getWallet(appid, handle))) {
00894 b->setFolder(folder);
00895 QVariantMap rc;
00896 foreach (KWallet::Entry *entry, b->readEntryList(key)) {
00897 rc.insert(entry->key(), entry->value());
00898 }
00899 return rc;
00900 }
00901
00902 return QVariantMap();
00903 }
00904
00905
00906 QStringList KWalletD::entryList(int handle, const QString& folder, const QString& appid) {
00907 KWallet::Backend *b;
00908
00909 if ((b = getWallet(appid, handle))) {
00910 b->setFolder(folder);
00911 return b->entryList();
00912 }
00913
00914 return QStringList();
00915 }
00916
00917
00918 QString KWalletD::readPassword(int handle, const QString& folder, const QString& key, const QString& appid) {
00919 KWallet::Backend *b;
00920
00921 if ((b = getWallet(appid, handle))) {
00922 b->setFolder(folder);
00923 KWallet::Entry *e = b->readEntry(key);
00924 if (e && e->type() == KWallet::Wallet::Password) {
00925 return e->password();
00926 }
00927 }
00928
00929 return QString();
00930 }
00931
00932
00933 QVariantMap KWalletD::readPasswordList(int handle, const QString& folder, const QString& key, const QString& appid) {
00934 KWallet::Backend *b;
00935
00936 if ((b = getWallet(appid, handle))) {
00937 b->setFolder(folder);
00938 QVariantMap rc;
00939 foreach (KWallet::Entry *entry, b->readEntryList(key)) {
00940 if (entry->type() == KWallet::Wallet::Password) {
00941 rc.insert(entry->key(), entry->password());
00942 }
00943 }
00944 return rc;
00945 }
00946
00947 return QVariantMap();
00948 }
00949
00950
00951 int KWalletD::writeMap(int handle, const QString& folder, const QString& key, const QByteArray& value, const QString& appid) {
00952 KWallet::Backend *b;
00953
00954 if ((b = getWallet(appid, handle))) {
00955 b->setFolder(folder);
00956 KWallet::Entry e;
00957 e.setKey(key);
00958 e.setValue(value);
00959 e.setType(KWallet::Wallet::Map);
00960 b->writeEntry(&e);
00961 initiateSync(b->walletName());
00962 emitFolderUpdated(b->walletName(), folder);
00963 return 0;
00964 }
00965
00966 return -1;
00967 }
00968
00969
00970 int KWalletD::writeEntry(int handle, const QString& folder, const QString& key, const QByteArray& value, int entryType, const QString& appid) {
00971 KWallet::Backend *b;
00972
00973 if ((b = getWallet(appid, handle))) {
00974 b->setFolder(folder);
00975 KWallet::Entry e;
00976 e.setKey(key);
00977 e.setValue(value);
00978 e.setType(KWallet::Wallet::EntryType(entryType));
00979 b->writeEntry(&e);
00980 initiateSync(b->walletName());
00981 emitFolderUpdated(b->walletName(), folder);
00982 return 0;
00983 }
00984
00985 return -1;
00986 }
00987
00988
00989 int KWalletD::writeEntry(int handle, const QString& folder, const QString& key, const QByteArray& value, const QString& appid) {
00990 KWallet::Backend *b;
00991
00992 if ((b = getWallet(appid, handle))) {
00993 b->setFolder(folder);
00994 KWallet::Entry e;
00995 e.setKey(key);
00996 e.setValue(value);
00997 e.setType(KWallet::Wallet::Stream);
00998 b->writeEntry(&e);
00999 initiateSync(b->walletName());
01000 emitFolderUpdated(b->walletName(), folder);
01001 return 0;
01002 }
01003
01004 return -1;
01005 }
01006
01007
01008 int KWalletD::writePassword(int handle, const QString& folder, const QString& key, const QString& value, const QString& appid) {
01009 KWallet::Backend *b;
01010
01011 if ((b = getWallet(appid, handle))) {
01012 b->setFolder(folder);
01013 KWallet::Entry e;
01014 e.setKey(key);
01015 e.setValue(value);
01016 e.setType(KWallet::Wallet::Password);
01017 b->writeEntry(&e);
01018 initiateSync(b->walletName());
01019 emitFolderUpdated(b->walletName(), folder);
01020 return 0;
01021 }
01022
01023 return -1;
01024 }
01025
01026
01027 int KWalletD::entryType(int handle, const QString& folder, const QString& key, const QString& appid) {
01028 KWallet::Backend *b;
01029
01030 if ((b = getWallet(appid, handle))) {
01031 if (!b->hasFolder(folder)) {
01032 return KWallet::Wallet::Unknown;
01033 }
01034 b->setFolder(folder);
01035 if (b->hasEntry(key)) {
01036 return b->readEntry(key)->type();
01037 }
01038 }
01039
01040 return KWallet::Wallet::Unknown;
01041 }
01042
01043
01044 bool KWalletD::hasEntry(int handle, const QString& folder, const QString& key, const QString& appid) {
01045 KWallet::Backend *b;
01046
01047 if ((b = getWallet(appid, handle))) {
01048 if (!b->hasFolder(folder)) {
01049 return false;
01050 }
01051 b->setFolder(folder);
01052 return b->hasEntry(key);
01053 }
01054
01055 return false;
01056 }
01057
01058
01059 int KWalletD::removeEntry(int handle, const QString& folder, const QString& key, const QString& appid) {
01060 KWallet::Backend *b;
01061
01062 if ((b = getWallet(appid, handle))) {
01063 if (!b->hasFolder(folder)) {
01064 return 0;
01065 }
01066 b->setFolder(folder);
01067 bool rc = b->removeEntry(key);
01068 initiateSync(b->walletName());
01069 emitFolderUpdated(b->walletName(), folder);
01070 return rc ? 0 : -3;
01071 }
01072
01073 return -1;
01074 }
01075
01076
01077 void KWalletD::slotServiceUnregistered(const QString& app) {
01078 if (_handles.contains(app)) {
01079 QList<int> l = _handles[app];
01080 for (QList<int>::Iterator i = l.begin(); i != l.end(); ++i) {
01081 _handles[app].removeAll(*i);
01082 KWallet::Backend *w = _wallets.value(*i);
01083 if (w && !_leaveOpen && 0 == w->deref()) {
01084 close(w->walletName(), true);
01085 }
01086 }
01087 _handles.remove(app);
01088 }
01089 }
01090
01091
01092 void KWalletD::invalidateHandle(int handle) {
01093 for (QHash<QString,QList<int> >::Iterator i = _handles.begin();
01094 i != _handles.end();
01095 ++i) {
01096 i.value().removeAll(handle);
01097 }
01098 }
01099
01100
01101 KWallet::Backend *KWalletD::getWallet(const QString& appid, int handle) {
01102 if (handle == 0) {
01103 return 0L;
01104 }
01105
01106 KWallet::Backend *w = _wallets.value(handle);
01107
01108 if (w) {
01109 if (_handles.contains(appid)) {
01110 if (_handles[appid].contains(handle)) {
01111
01112 _failed = 0;
01113 if (_closeIdle && _timeouts) {
01114 _timeouts->resetTimer(handle, _idleTime);
01115 }
01116 return w;
01117 }
01118 }
01119 }
01120
01121 if (++_failed > 5) {
01122 _failed = 0;
01123 QTimer::singleShot(0, this, SLOT(notifyFailures()));
01124 }
01125
01126 return 0L;
01127 }
01128
01129
01130 void KWalletD::notifyFailures() {
01131 if (!_showingFailureNotify) {
01132 _showingFailureNotify = true;
01133 KMessageBox::information(0, i18n("There have been repeated failed attempts to gain access to a wallet. An application may be misbehaving."), i18n("KDE Wallet Service"));
01134 _showingFailureNotify = false;
01135 }
01136 }
01137
01138
01139 void KWalletD::doCloseSignals(int handle, const QString& wallet) {
01140 emit walletClosed(handle);
01141 emit walletClosed(wallet);
01142 if (_wallets.isEmpty()) {
01143 emit allWalletsClosed();
01144 }
01145 }
01146
01147
01148 int KWalletD::renameEntry(int handle, const QString& folder, const QString& oldName, const QString& newName, const QString& appid) {
01149 KWallet::Backend *b;
01150
01151 if ((b = getWallet(appid, handle))) {
01152 b->setFolder(folder);
01153 int rc = b->renameEntry(oldName, newName);
01154 initiateSync(b->walletName());
01155 emitFolderUpdated(b->walletName(), folder);
01156 return rc;
01157 }
01158
01159 return -1;
01160 }
01161
01162
01163 QStringList KWalletD::users(const QString& wallet) const {
01164 QStringList rc;
01165
01166 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
01167 int handle = walletInfo.first;
01168 KWallet::Backend* w = walletInfo.second;
01169
01170 if (w) {
01171 for (QHash<QString,QList<int> >::ConstIterator hit = _handles.begin(); hit != _handles.end(); ++hit) {
01172 if (hit.value().contains(handle)) {
01173 rc.append(hit.key());
01174 }
01175 }
01176 }
01177
01178 return rc;
01179 }
01180
01181
01182 bool KWalletD::disconnectApplication(const QString& wallet, const QString& application) {
01183 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
01184 int handle = walletInfo.first;
01185 KWallet::Backend* backend = walletInfo.second;
01186
01187 if (handle != -1 && _handles[application].contains(handle)) {
01188 _handles[application].removeAll(handle);
01189
01190 if (_handles[application].isEmpty()) {
01191 _handles.remove(application);
01192 }
01193
01194 if (backend->deref() == 0) {
01195 close(backend->walletName(), true);
01196 }
01197
01198 emit applicationDisconnected(wallet, application);
01199 return true;
01200 }
01201
01202 return false;
01203 }
01204
01205
01206 void KWalletD::emitFolderUpdated(const QString& wallet, const QString& folder) {
01207 emit folderUpdated(wallet, folder);
01208 }
01209
01210
01211 void KWalletD::emitWalletListDirty() {
01212 emit walletListDirty();
01213 }
01214
01215
01216 void KWalletD::reconfigure() {
01217 KConfig cfg("kwalletrc");
01218 KConfigGroup walletGroup(&cfg, "Wallet");
01219 _firstUse = walletGroup.readEntry("First Use", true);
01220 _enabled = walletGroup.readEntry("Enabled", true);
01221 _launchManager = walletGroup.readEntry("Launch Manager", true);
01222 _leaveOpen = walletGroup.readEntry("Leave Open", false);
01223 bool idleSave = _closeIdle;
01224 _closeIdle = walletGroup.readEntry("Close When Idle", false);
01225 _openPrompt = walletGroup.readEntry("Prompt on Open", true);
01226 int timeSave = _idleTime;
01227
01228 _idleTime = walletGroup.readEntry("Idle Timeout", 10) * 60 * 1000;
01229 #ifdef Q_WS_X11
01230 if ( screensaver->isValid() ) {
01231 if (walletGroup.readEntry("Close on Screensaver", false)) {
01232 connect(screensaver, SIGNAL(ActiveChanged(bool)), SLOT(screenSaverChanged(bool)));
01233 } else {
01234 screensaver->disconnect(SIGNAL(ActiveChanged(bool)), this, SLOT(screenSaverChanged(bool)));
01235 }
01236 }
01237 #endif
01238
01239 if (_closeIdle) {
01240 if (_idleTime != timeSave) {
01241 Wallets::const_iterator it = _wallets.begin();
01242 const Wallets::const_iterator end = _wallets.end();
01243 for (; it != end; ++it) {
01244 _timeouts->resetTimer(it.key(), _idleTime);
01245 }
01246 }
01247
01248 if (!idleSave) {
01249 Wallets::const_iterator it = _wallets.begin();
01250 const Wallets::const_iterator end = _wallets.end();
01251 for (; it != end; ++it) {
01252 _timeouts->addTimer(it.key(), _idleTime);
01253 }
01254 }
01255 } else {
01256 _timeouts->clear();
01257 }
01258
01259
01260 _implicitAllowMap.clear();
01261 const KConfigGroup autoAllowGroup(&cfg, "Auto Allow");
01262 QStringList entries = autoAllowGroup.entryMap().keys();
01263 for (QStringList::Iterator i = entries.begin(); i != entries.end(); ++i) {
01264 _implicitAllowMap[*i] = autoAllowGroup.readEntry(*i, QStringList());
01265 }
01266
01267
01268 _implicitDenyMap.clear();
01269 const KConfigGroup autoDenyGroup(&cfg, "Auto Deny");
01270 entries = autoDenyGroup.entryMap().keys();
01271 for (QStringList::Iterator i = entries.begin(); i != entries.end(); ++i) {
01272 _implicitDenyMap[*i] = autoDenyGroup.readEntry(*i, QStringList());
01273 }
01274
01275
01276 if (!_enabled) {
01277 while (!_wallets.isEmpty()) {
01278 Wallets::const_iterator it = _wallets.begin();
01279 closeWallet(it.value(), it.key(), true);
01280 }
01281 KUniqueApplication::exit(0);
01282 }
01283 }
01284
01285
01286 bool KWalletD::isEnabled() const {
01287 return _enabled;
01288 }
01289
01290
01291 bool KWalletD::folderDoesNotExist(const QString& wallet, const QString& folder) {
01292 if (!wallets().contains(wallet)) {
01293 return true;
01294 }
01295
01296 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
01297 if (walletInfo.second) {
01298 return walletInfo.second->folderDoesNotExist(folder);
01299 }
01300
01301 KWallet::Backend *b = new KWallet::Backend(wallet);
01302 b->open(QByteArray());
01303 bool rc = b->folderDoesNotExist(folder);
01304 delete b;
01305 return rc;
01306 }
01307
01308
01309 bool KWalletD::keyDoesNotExist(const QString& wallet, const QString& folder, const QString& key) {
01310 if (!wallets().contains(wallet)) {
01311 return true;
01312 }
01313
01314 const QPair<int, KWallet::Backend*> walletInfo = findWallet(wallet);
01315 if (walletInfo.second) {
01316 return walletInfo.second->entryDoesNotExist(folder, key);
01317 }
01318
01319 KWallet::Backend *b = new KWallet::Backend(wallet);
01320 b->open(QByteArray());
01321 bool rc = b->entryDoesNotExist(folder, key);
01322 delete b;
01323 return rc;
01324 }
01325
01326
01327 bool KWalletD::implicitAllow(const QString& wallet, const QString& app) {
01328 return _implicitAllowMap[wallet].contains(app);
01329 }
01330
01331
01332 bool KWalletD::implicitDeny(const QString& wallet, const QString& app) {
01333 return _implicitDenyMap[wallet].contains(app);
01334 }
01335
01336
01337 void KWalletD::timedOut(int id) {
01338 KWallet::Backend *w = _wallets.value(id);
01339 if (w) {
01340 closeWallet(w, id, true);
01341 }
01342 }
01343
01344
01345 void KWalletD::closeAllWallets() {
01346 Wallets walletsCopy = _wallets;
01347
01348 Wallets::const_iterator it = walletsCopy.begin();
01349 const Wallets::const_iterator end = walletsCopy.end();
01350 for (; it != end; ++it) {
01351 closeWallet(it.value(), it.key(), true);
01352 }
01353
01354 walletsCopy.clear();
01355
01356
01357 _wallets.clear();
01358
01359 for (QMap<QString,QByteArray>::Iterator it = _passwords.begin();
01360 it != _passwords.end();
01361 ++it) {
01362 it.value().fill(0);
01363 }
01364 _passwords.clear();
01365 }
01366
01367
01368 QString KWalletD::networkWallet() {
01369 return KWallet::Wallet::NetworkWallet();
01370 }
01371
01372
01373 QString KWalletD::localWallet() {
01374 return KWallet::Wallet::LocalWallet();
01375 }
01376
01377 void KWalletD::screenSaverChanged(bool s)
01378 {
01379 if (s)
01380 closeAllWallets();
01381 }
01382
01383 #include "kwalletd.moc"
01384 #include "kwalletdadaptor.moc"