Commit 7204275a authored by Volker Krause's avatar Volker Krause
Browse files

Store password in config file if wallet is not available and move passwords...

Store password in config file if wallet is not available and move passwords from config file to wallet if it is available.

svn path=/trunk/KDE/kdepim/mailtransport/; revision=619841
parent 481420b5
......@@ -4,8 +4,8 @@
@section Overview
The mailtransport module provides the following functionality:
- Shared mail transport settings
- GUI elements to configure mail transport settings (implementation incomplete)
- Job classes for mail sending (not yet implemented)
- GUI elements to configure mail transport settings
- Job classes for mail sending
The mailtransport module consists of the following components:
- a shared library containing the above listed functionality
......
......@@ -19,6 +19,7 @@ TransportManager
(create a copy for edit?)
- load passwords if another instance opens the wallet
- add job scheduler (see knode) for async wallet loading during sending
- migrate passwords to wallet only in one instance
TransportComboBox
-----------------
......@@ -32,8 +33,8 @@ TransportConfigDialog
Transport
---------
- fallback to config file password storage
- use int for port number instead of QString
- password is not reloaded from wallet on remote change
TransportJob
------------
......
/*
Copyright (c) 2006 Volker Krause <vkrause@kde.org>
Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
......@@ -24,6 +24,7 @@
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstringhandler.h>
#include <kwallet.h>
using namespace KPIM;
......@@ -32,14 +33,17 @@ using namespace KWallet;
Transport::Transport( const QString &cfgGroup ) :
TransportBase( cfgGroup ),
mPasswordLoaded( false ),
mPasswordDirty( false )
mPasswordDirty( false ),
mStorePasswordInFile( false ),
mNeedsWalletMigration( false )
{
kDebug() << k_funcinfo << cfgGroup << endl;
readConfig();
}
bool Transport::isNull() const
bool Transport::isValid() const
{
return id() <= 0;
return id() > 0 && !host().isEmpty();
}
QString Transport::password()
......@@ -51,6 +55,7 @@ QString Transport::password()
void Transport::setPassword(const QString & passwd)
{
mPasswordLoaded = true;
if ( mPassword == passwd )
return;
mPasswordDirty = true;
......@@ -80,20 +85,21 @@ void Transport::usrReadConfig()
{
TransportBase::usrReadConfig();
// TODO legacy password reading
// we have everything we need
if ( !storePassword() || mPasswordLoaded )
return;
// try to find a password in the config file otherwise
KConfigGroup group( config(), currentGroup() );
mPassword = KStringHandler::obscure( group.readEntry( "password" ) );
// TODO legacy password migration from KMail/KNode
if ( !mPassword.isEmpty() ) {
// migration to kwallet if available
mPasswordLoaded = true;
if ( Wallet::isEnabled() ) {
// config.deleteEntry( "pass" );
mPasswordDirty = true;
// mStorePasswdInConfig = false;
// writeConfig( id );
mNeedsWalletMigration = true;
} else {
// mStorePasswdInConfig = true;
mStorePasswordInFile = true;
}
} else {
// read password if wallet is open, defer otherwise
......@@ -104,14 +110,11 @@ void Transport::usrReadConfig()
void Transport::usrWriteConfig()
{
// TODO
kDebug() << k_funcinfo << endl;
if ( requiresAuthentication() && mPasswordDirty ) {
if ( requiresAuthentication() && storePassword() && mPasswordDirty ) {
Wallet *wallet = TransportManager::self()->wallet();
if ( !wallet || wallet->writePassword(QString::number(id()), mPassword) ) {
if ( !wallet || wallet->writePassword(QString::number(id()), mPassword) != 0 ) {
// wallet saving failed, ask if we should store in the config file instead
if ( KMessageBox::warningYesNo( 0,
if ( mStorePasswordInFile || KMessageBox::warningYesNo( 0,
i18n("KWallet is not available. It is strongly recommended to use "
"KWallet for managing your passwords.\n"
"However, the password can be stored in the configuration "
......@@ -125,7 +128,9 @@ void Transport::usrWriteConfig()
KGuiItem( i18n("Do Not Store Password") ) )
== KMessageBox::Yes ) {
// write to config file
// confif()->writeEntry( "password", KNHelper::encryptStr( p_ass ) );
KConfigGroup group( config(), currentGroup() );
group.writeEntry( "password", KStringHandler::obscure( mPassword ) );
mStorePasswordInFile = true;
}
}
mPasswordDirty = false;
......@@ -168,3 +173,19 @@ void Transport::readPassword()
if ( wallet )
wallet->readPassword( QString::number(id()), mPassword );
}
bool Transport::needsWalletMigration() const
{
return mNeedsWalletMigration;
}
void Transport::migrateToWallet()
{
kDebug() << k_funcinfo << "migrating " << id() << " to wallet" << endl;
mNeedsWalletMigration = false;
KConfigGroup group( config(), currentGroup() );
group.deleteEntry( "password" );
mPasswordDirty = true;
mStorePasswordInFile = false;
writeConfig();
}
/*
Copyright (c) 2006 Volker Krause <vkrause@kde.org>
Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
......@@ -40,9 +40,9 @@ class MAILTRANSPORT_EXPORT Transport : public TransportBase
typedef QList<Transport*> List;
/**
Returns true if this transport is empty.
Returns true if this transport is valid, ie. has all necessary data set.
*/
bool isNull() const;
bool isValid() const;
/**
Returns the password of this transport.
......@@ -77,13 +77,25 @@ class MAILTRANSPORT_EXPORT Transport : public TransportBase
virtual void usrReadConfig();
virtual void usrWriteConfig();
/**
Returns true if the password was not stored in the wallet.
*/
bool needsWalletMigration() const;
/**
Try to migrate the password from the config file to the wallet.
*/
void migrateToWallet();
private:
void readPassword();
private:
private:
QString mPassword;
bool mPasswordLoaded;
bool mPasswordDirty;
bool mStorePasswordInFile;
bool mNeedsWalletMigration;
};
}
......
/*
Copyright (c) 2006 Volker Krause <vkrause@kde.org>
Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
......@@ -26,6 +26,8 @@
#include <kconfig.h>
#include <kconfigbase.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <krandom.h>
#include <kstaticdeleter.h>
#include <kwallet.h>
......@@ -50,7 +52,6 @@ TransportManager::TransportManager() :
mDefaultTransportId( -1 )
{
mConfig = new KConfig( "mailtransports" );
readConfig();
QDBusConnection::sessionBus().registerObject( DBUS_OBJECT_PATH, this,
QDBusConnection::ExportScriptableSlots | QDBusConnection::ExportScriptableSignals );
......@@ -67,8 +68,10 @@ TransportManager::~TransportManager()
TransportManager* TransportManager::self()
{
if ( !mInstance )
if ( !mInstance ) {
sTransportManagerDeleter.setObject( mInstance, new TransportManager() );
mInstance->readConfig();
}
return mInstance;
}
......@@ -186,14 +189,28 @@ void TransportManager::removeTransport(int id)
void TransportManager::readConfig()
{
qDeleteAll( mTransports );
QList<Transport*> oldTransports = mTransports;
mTransports.clear();
QRegExp re( "^Transport (.+)$" );
QStringList groups = mConfig->groupList().filter( re );
foreach ( QString s, groups ) {
re.indexIn( s );
Transport* t = new Transport( re.cap( 1 ) );
Transport *t = 0;
// see if we happen to have that one already
foreach ( Transport *old, oldTransports ) {
if ( old->currentGroup() == "Transport " + re.cap( 1 ) ) {
kDebug() << k_funcinfo << "reloading existing transport: " << s << endl;
t = old;
t->readConfig();
oldTransports.removeAll( old );
break;
}
}
if ( !t )
t = new Transport( re.cap( 1 ) );
if ( t->id() <= 0 ) {
t->setId( createId() );
t->writeConfig();
......@@ -201,6 +218,9 @@ void TransportManager::readConfig()
mTransports.append( t );
}
qDeleteAll( oldTransports );
oldTransports.clear();
// read default transport
KConfigGroup group( mConfig, "General" );
mDefaultTransportId = group.readEntry( "default-transport", 0 );
......@@ -216,6 +236,7 @@ void TransportManager::readConfig()
}
}
validateDefault();
migrateToWallet();
}
void TransportManager::writeConfig()
......@@ -362,4 +383,37 @@ void TransportManager::validateDefault()
}
}
void TransportManager::migrateToWallet()
{
// check if we tried this already
static bool firstRun = true;
if ( !firstRun )
return;
firstRun = false;
// TODO: check if we are the first instance
// check if migration is needed
QStringList names;
foreach ( Transport *t, mTransports )
if ( t->needsWalletMigration() )
names << t->name();
if ( names.isEmpty() )
return;
// ask user if he wants to migrate
int result = KMessageBox::questionYesNoList( 0,
i18n("The following mail transports store passwords in the configuration file instead in KWallet.\n"
"It is recommended to use KWallet for password storage for security reasons.\n"
"Do you want to migrate your passwords to KWallet?"),
names );
if ( result != KMessageBox::Yes )
return;
// perform migration
foreach ( Transport *t, mTransports )
if ( t->needsWalletMigration() )
t->migrateToWallet();
}
#include "transportmanager.moc"
/*
Copyright (c) 2006 Volker Krause <vkrause@kde.org>
Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
......@@ -187,6 +187,7 @@ class MAILTRANSPORT_EXPORT TransportManager : public QObject
int createId() const;
void prepareWallet();
void validateDefault();
void migrateToWallet();
private slots:
void slotTransportsChanged();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment