generic: port to KPasswdStore

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-04-05 01:40:28 +03:00
parent dcf5fe10d3
commit 9d6895e488
22 changed files with 122 additions and 190 deletions

View file

@ -232,6 +232,7 @@ target_link_libraries(kget
${KDE4_KIO_LIBS}
${KDE4_KCMUTILS_LIBS}
${KDE4_KNOTIFYCONFIG_LIBS}
${KDE4_KPASSWDSTORE_LIBS}
kgetcore
)

View file

@ -14,11 +14,10 @@
#include <KMessageBox>
#include <KLocale>
#include <kwallet.h>
DlgWebinterface::DlgWebinterface(KDialog *parent)
: QWidget(parent),
m_wallet(0)
m_passwdstore(nullptr)
{
setupUi(this);
@ -30,41 +29,28 @@ DlgWebinterface::DlgWebinterface(KDialog *parent)
DlgWebinterface::~DlgWebinterface()
{
delete m_wallet;
}
void DlgWebinterface::readConfig()
{
if (Settings::webinterfaceEnabled()) {
m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(),
winId(),///Use MainWindow?
KWallet::Wallet::Asynchronous);
if (m_wallet) {
connect(m_wallet, SIGNAL(walletOpened(bool)), SLOT(walletOpened(bool)));
} else {
KMessageBox::error(0, i18n("Could not open KWallet"));
if (!m_passwdstore) {
m_passwdstore = new KPasswdStore(this);
m_passwdstore->setStoreID("KGet");
}
}
}
void DlgWebinterface::walletOpened(bool opened)
{
if (opened &&
(m_wallet->hasFolder("KGet") ||
m_wallet->createFolder("KGet")) &&
m_wallet->setFolder("KGet")) {
QString pwd;
m_wallet->readPassword("Webinterface", pwd);
webinterfacePwd->setText(pwd);
} else {
KMessageBox::error(0, i18n("Could not open KWallet"));
if (m_passwdstore->openStore(winId())) {
webinterfacePwd->setText(m_passwdstore->getPasswd("Webinterface", winId()));
} else {
KMessageBox::error(nullptr, i18n("Could not open KPasswdStore"));
}
}
}
void DlgWebinterface::saveSettings()
{
if (m_wallet) {
m_wallet->writePassword("Webinterface", webinterfacePwd->text());
if (m_passwdstore && m_passwdstore->openStore(winId())) {
m_passwdstore->storePasswd("Webinterface", webinterfacePwd->text(), winId());
}
emit saved();
}

View file

@ -13,13 +13,10 @@
#include <QWidget>
#include <KDialog>
#include <kpasswdstore.h>
#include "ui_dlgwebinterface.h"
namespace KWallet {
class Wallet;
}
class DlgWebinterface : public QWidget, public Ui::DlgWebinterface
{
Q_OBJECT
@ -35,10 +32,9 @@ signals:
private Q_SLOTS:
void readConfig();
void saveSettings();
void walletOpened(bool);
private:
KWallet::Wallet *m_wallet;
KPasswdStore *m_passwdstore;
};
#endif

View file

@ -21,7 +21,6 @@
#include <KDebug>
#include <KGlobalSettings>
#include <KStandardDirs>
#include <kwallet.h>
#include <QTcpServer>
#include <QTcpSocket>
@ -68,46 +67,34 @@ void HttpHeaderParser::parseHeader(const QByteArray &header)
HttpServer::HttpServer(QWidget *parent)
: QObject(parent),
m_wallet(0)
m_passwdstore(nullptr)
{
m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(),
parent->winId(),///Use MainWindow?
KWallet::Wallet::Asynchronous);
if (m_wallet) {
connect(m_wallet, SIGNAL(walletOpened(bool)), SLOT(init(bool)));
m_passwdstore = new KPasswdStore(this);
m_passwdstore->setStoreID("KGet");
if (m_passwdstore && m_passwdstore->openStore(parent->winId())) {
m_pwd = m_passwdstore->getPasswd("Webinterface", parent->winId());
m_tcpServer = new QTcpServer(this);
if (!m_tcpServer->listen(QHostAddress::Any, Settings::webinterfacePort())) {
KGet::showNotification(parent, "error", i18nc("@info", "Unable to start WebInterface: %1", m_tcpServer->errorString()));
return;
}
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(handleRequest()));
} else {
KGet::showNotification(parent, "error", i18n("Unable to start WebInterface: Could not open KWallet"));
KGet::showNotification(parent, "error", i18n("Unable to start WebInterface: Could not open KPasswdStore"));
}
}
HttpServer::~HttpServer()
{
delete m_wallet;
}
void HttpServer::init(bool opened)
{
if (opened &&
m_wallet->hasFolder("KGet") &&
m_wallet->setFolder("KGet")) {
m_wallet->readPassword("Webinterface", m_pwd);
} else {
KGet::showNotification(static_cast<QWidget*>(parent()), "error", i18n("Unable to start WebInterface: Could not open KWallet"));
return;
}
m_tcpServer = new QTcpServer(this);
if (!m_tcpServer->listen(QHostAddress::Any, Settings::webinterfacePort())) {
KGet::showNotification(static_cast<QWidget*>(parent()), "error", i18nc("@info", "Unable to start WebInterface: %1", m_tcpServer->errorString()));
return;
}
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(handleRequest()));
}
void HttpServer::settingsChanged()
{
if (m_wallet) {
m_wallet->readPassword("Webinterface", m_pwd);
if (m_passwdstore && m_passwdstore->openStore(static_cast<QWidget*>(parent())->winId())) {
m_pwd = m_passwdstore->getPasswd("Webinterface");
}
}

View file

@ -12,12 +12,9 @@
#define HTTPSERVER_H
#include <QWidget>
#include <QTcpServer>
namespace KWallet {
class Wallet;
}
#include <kpasswdstore.h>
class HttpServer : public QObject
{
@ -30,11 +27,10 @@ public:
void settingsChanged();
private slots:
void init(bool);
void handleRequest();
private:
KWallet::Wallet *m_wallet;
KPasswdStore *m_passwdstore;
QTcpServer *m_tcpServer;
QString m_pwd;
};

View file

@ -28,7 +28,7 @@
<item>
<widget class="QCheckBox" name="kcfg_WalletSupport">
<property name="text">
<string>Remember passwords (KWallet)</string>
<string>Remember passwords (KPasswdStore)</string>
</property>
</widget>
</item>

View file

@ -10,7 +10,11 @@ kde4_add_kcfg_files(krdccore_SRCS settings.kcfgc)
add_library(krdccore SHARED ${krdccore_SRCS})
target_link_libraries(krdccore ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS})
target_link_libraries(krdccore
${KDE4_KDECORE_LIBS}
${KDE4_KDEUI_LIBS}
${KDE4_KPASSWDSTORE_LIBS}
)
set_target_properties(krdccore PROPERTIES
VERSION ${GENERIC_LIB_VERSION}

View file

@ -206,7 +206,7 @@ bool HostPreferences::showDialog(QWidget *parent)
showAgainCheckBox->setChecked(showConfigAgain());
walletSupportCheckBox = new QCheckBox(mainWidget);
walletSupportCheckBox->setText(i18n("Remember password (KWallet)"));
walletSupportCheckBox->setText(i18n("Remember password (KPasswdStore)"));
walletSupportCheckBox->setChecked(walletSupport());
layout->addWidget(showAgainCheckBox);

View file

@ -41,7 +41,7 @@ RemoteView::RemoteView(QWidget *parent)
m_scale(false),
m_keyboardIsGrabbed(false),
#ifndef QTONLY
m_wallet(0),
m_passwdStore(0),
#endif
m_dotCursorState(CursorOff)
{
@ -50,7 +50,7 @@ RemoteView::RemoteView(QWidget *parent)
RemoteView::~RemoteView()
{
#ifndef QTONLY
delete m_wallet;
delete m_passwdStore;
#endif
}
@ -198,31 +198,25 @@ KUrl RemoteView::url()
#ifndef QTONLY
QString RemoteView::readWalletPassword(bool fromUserNameOnly)
{
const QString KRDCFOLDER = "KRDC";
window()->setDisabled(true); // WORKAROUND: disable inputs so users cannot close the current tab (see #181230)
m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), window()->winId());
m_passwdStore = new KPasswdStore(this);
m_passwdStore->setStoreID("KRDC");
window()->setDisabled(false);
if (m_wallet) {
bool walletOK = m_wallet->hasFolder(KRDCFOLDER);
if (!walletOK) {
walletOK = m_wallet->createFolder(KRDCFOLDER);
kDebug(5010) << "Wallet folder created";
}
if (m_passwdStore) {
const qlonglong windowId = window()->winId();
bool walletOK = m_passwdStore->openStore(windowId);
if (walletOK) {
kDebug(5010) << "Wallet OK";
m_wallet->setFolder(KRDCFOLDER);
QString password;
QString key;
if (fromUserNameOnly)
key = m_url.userName();
else
key = m_url.prettyUrl(KUrl::RemoveTrailingSlash);
if (m_wallet->hasEntry(key) &&
!m_wallet->readPassword(key, password)) {
QString password = m_passwdStore->getPasswd(key.toUtf8(), windowId);
if (!password.isEmpty()) {
kDebug(5010) << "Password read OK";
return password;
@ -240,9 +234,10 @@ void RemoteView::saveWalletPassword(const QString &password, bool fromUserNameOn
else
key = m_url.prettyUrl(KUrl::RemoveTrailingSlash);
if (m_wallet && m_wallet->isOpen()) {
const qlonglong windowId = window()->winId();
if (m_passwdStore && m_passwdStore->openStore(windowId)) {
kDebug(5010) << "Write wallet password";
m_wallet->writePassword(key, password);
m_passwdStore->storePasswd(key.toUtf8(), password, windowId);
}
}
#endif

View file

@ -31,7 +31,7 @@
#define KRDCCORE_EXPORT
#else
#include <KUrl>
#include <KWallet/Wallet>
#include <kpasswdstore.h>
#include "krdccore_export.h"
#endif
@ -406,7 +406,7 @@ protected:
#ifndef QTONLY
QString readWalletPassword(bool fromUserNameOnly = false);
void saveWalletPassword(const QString &password, bool fromUserNameOnly = false);
KWallet::Wallet *m_wallet;
KPasswdStore *m_passwdStore;
#endif
DotCursorState m_dotCursorState;

View file

@ -74,6 +74,7 @@ target_link_libraries(krfb
${QT_QTNETWORK_LIBRARY}
${KDE4_KDNSSD_LIBS}
${KDE4_KDEUI_LIBS}
${KDE4_KPASSWDSTORE_LIBS}
${LIBVNCSERVER_LIBRARIES}
)

View file

@ -22,6 +22,7 @@
#include "invitationsrfbclient.h"
#include "krfbconfig.h"
#include "rfbservermanager.h"
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtNetwork/QHostInfo>
@ -31,9 +32,7 @@
#include <KUser>
#include <KRandom>
#include <KStringHandler>
#include <KWallet/Wallet>
#include <DNSSD/PublicService>
using KWallet::Wallet;
//static
InvitationsRfbServer *InvitationsRfbServer::instance;
@ -52,11 +51,38 @@ void InvitationsRfbServer::init()
instance->setListeningPort(KrfbConfig::port());
instance->setPasswordRequired(true);
instance->m_wallet = Wallet::openWallet(
Wallet::NetworkWallet(), 0, Wallet::Asynchronous);
if(instance->m_wallet) {
connect(instance->m_wallet, SIGNAL(walletOpened(bool)),
instance, SLOT(walletOpened(bool)));
instance->m_passwdStore = new KPasswdStore(instance);
instance->m_passwdStore->setStoreID("krfb");
QString desktopPassword;
QString unattendedPassword;
if ( !instance->m_passwdStore->openStore() ) {
desktopPassword = instance->m_passwdStore->getPasswd("desktopSharingPassword");
if (!desktopPassword.isEmpty()) {
instance->m_desktopPassword = desktopPassword;
emit instance->passwordChanged(instance->m_desktopPassword);
}
unattendedPassword = instance->m_passwdStore->getPasswd("unattendedAccessPassword");
if (!unattendedPassword.isEmpty()) {
instance->m_unattendedPassword = unattendedPassword;
}
} else {
kDebug() << "KPasswdStore is disabled, falling back to config file";
KSharedConfigPtr config = KGlobal::config();
KConfigGroup krfbConfig(config,"Security");
desktopPassword = KStringHandler::obscure(krfbConfig.readEntry(
"desktopPassword", QString()));
if(!desktopPassword.isEmpty()) {
instance->m_desktopPassword = desktopPassword;
emit instance->passwordChanged(instance->m_desktopPassword);
}
unattendedPassword = KStringHandler::obscure(krfbConfig.readEntry(
"unattendedPassword", QString()));
if(!unattendedPassword.isEmpty()) {
instance->m_unattendedPassword = unattendedPassword;
}
}
}
@ -123,16 +149,9 @@ InvitationsRfbServer::~InvitationsRfbServer()
KSharedConfigPtr config = KGlobal::config();
KConfigGroup krfbConfig(config,"Security");
krfbConfig.writeEntry("allowUnattendedAccess",m_allowUnattendedAccess);
if(m_wallet && m_wallet->isOpen()) {
if( (m_wallet->currentFolder()=="krfb") ||
((m_wallet->hasFolder("krfb") || m_wallet->createFolder("krfb")) &&
m_wallet->setFolder("krfb")) ) {
m_wallet->writePassword("desktopSharingPassword",m_desktopPassword);
m_wallet->writePassword("unattendedAccessPassword",m_unattendedPassword);
}
if (m_passwdStore && m_passwdStore->openStore()) {
m_passwdStore->storePasswd("desktopSharingPassword", m_desktopPassword);
m_passwdStore->storePasswd("unattendedAccessPassword", m_unattendedPassword);
} else {
krfbConfig.writeEntry("desktopPassword",
KStringHandler::obscure(m_desktopPassword));
@ -148,48 +167,6 @@ PendingRfbClient* InvitationsRfbServer::newClient(rfbClientPtr client)
return new PendingInvitationsRfbClient(client, this);
}
void InvitationsRfbServer::walletOpened(bool opened)
{
QString desktopPassword;
QString unattendedPassword;
Q_ASSERT(m_wallet);
if( opened &&
( m_wallet->hasFolder("krfb") || m_wallet->createFolder("krfb") ) &&
m_wallet->setFolder("krfb") ) {
if(m_wallet->readPassword("desktopSharingPassword", desktopPassword)==0 &&
!desktopPassword.isEmpty()) {
m_desktopPassword = desktopPassword;
emit passwordChanged(m_desktopPassword);
}
if(m_wallet->readPassword("unattendedAccessPassword", unattendedPassword)==0 &&
!unattendedPassword.isEmpty()) {
m_unattendedPassword = unattendedPassword;
}
} else {
kDebug() << "Could not open KWallet, Falling back to config file";
KSharedConfigPtr config = KGlobal::config();
KConfigGroup krfbConfig(config,"Security");
desktopPassword = KStringHandler::obscure(krfbConfig.readEntry(
"desktopPassword", QString()));
if(!desktopPassword.isEmpty()) {
m_desktopPassword = desktopPassword;
emit passwordChanged(m_desktopPassword);
}
unattendedPassword = KStringHandler::obscure(krfbConfig.readEntry(
"unattendedPassword", QString()));
if(!unattendedPassword.isEmpty()) {
m_unattendedPassword = unattendedPassword;
}
}
}
// a random string that doesn't contain i, I, o, O, 1, l, 0
// based on KRandom::randomString()
QString InvitationsRfbServer::readableRandomString(int length)

View file

@ -22,9 +22,7 @@
#include "rfbserver.h"
namespace KWallet {
class Wallet;
}
#include <kpasswdstore.h>
namespace DNSSD {
class PublicService;
@ -56,15 +54,12 @@ protected:
virtual ~InvitationsRfbServer();
virtual PendingRfbClient* newClient(rfbClientPtr client);
private Q_SLOTS:
void walletOpened(bool);
private:
DNSSD::PublicService *m_publicService;
bool m_allowUnattendedAccess;
QString m_desktopPassword;
QString m_unattendedPassword;
KWallet::Wallet *m_wallet;
KPasswdStore *m_passwdStore;
QString readableRandomString(int);
Q_DISABLE_COPY(InvitationsRfbServer)

View file

@ -248,6 +248,7 @@ kde4_add_plugin(okularpart ${okularpart_SRCS})
target_link_libraries(okularpart
${KDE4_KPARTS_LIBS}
${KDE4_SOLID_LIBS}
${KDE4_KPASSWDSTORE_LIBS}
${QT_QTGUI_LIBRARY}
${MATH_LIB}
okularcore

View file

@ -48,7 +48,7 @@ In progress [working on]:
-> various backends: fill about data information
-> gui: add config for gfx (yes/no) and text (yes/no/kde) antialias
-> core: provide a binary compatible okularcore library to extend the format capabilities
-> KPDF -> okular conversion of configuration & datafiles {configuration(done), document data(in progress), kwallet passwords(missing)}
-> KPDF -> okular conversion of configuration & datafiles {configuration(done), document data(in progress), kpasswdstore passwords(missing)}
-> inverse search (BR113191)
find with an usability expert the "best" way to activate such a link
provide a configuration widget to choose the editor and configure a custom one

View file

@ -4240,12 +4240,12 @@ void Document::setAnnotationEditingEnabled( bool enable )
foreachObserver( notifySetup( d->m_pagesVector, 0 ) );
}
void Document::walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const
void Document::walletDataForFile( const QString &fileName, QString *walletName, QString *walletKey ) const
{
if (d->m_generator) {
d->m_generator->walletDataForFile( fileName, walletName, walletFolder, walletKey );
d->m_generator->walletDataForFile( fileName, walletName, walletKey );
} else if (d->m_walletGenerator) {
d->m_walletGenerator->walletDataForFile( fileName, walletName, walletFolder, walletKey );
d->m_walletGenerator->walletDataForFile( fileName, walletName, walletKey );
}
}

View file

@ -725,7 +725,7 @@ class OKULAR_EXPORT Document : public QObject
*
* @since 0.20 (KDE 4.14)
*/
void walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const;
void walletDataForFile( const QString &fileName, QString *walletName, QString *walletKey ) const;
public Q_SLOTS:
/**

View file

@ -18,7 +18,6 @@
#include <kdebug.h>
#include <kicon.h>
#include <klocale.h>
#include <kwallet.h>
#include "document.h"
#include "document_p.h"
@ -359,11 +358,10 @@ bool Generator::exportTo( const QString&, const ExportFormat& )
return false;
}
void Generator::walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const
void Generator::walletDataForFile( const QString &fileName, QString *walletName, QString *walletKey ) const
{
*walletKey = fileName.section('/', -1, -1);
*walletName = KWallet::Wallet::NetworkWallet();
*walletFolder = "KPdf";
*walletName = "KPdf";
}
bool Generator::hasFeature( GeneratorFeature feature ) const

View file

@ -415,7 +415,7 @@ class OKULAR_EXPORT Generator : public QObject
* Unless you have very special requirements to where wallet data should be stored you
* don't need to reimplement this method.
*/
virtual void walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const;
virtual void walletDataForFile( const QString &fileName, QString *walletName, QString *walletKey ) const;
/**
* Query for the specified @p feature.

View file

@ -14,7 +14,6 @@
#include <kaboutdata.h>
#include <klocale.h>
#include <kconfigdialog.h>
#include <kwallet.h>
static KAboutData createAboutData()
{
@ -46,9 +45,8 @@ void KOOOGenerator::addPages( KConfigDialog* dlg )
dlg->addPage( widget, generalSettings(), i18n("OpenDocument Text"), "application-vnd.oasis.opendocument.text", i18n("OpenDocument Text Backend Configuration") );
}
void KOOOGenerator::walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const
void KOOOGenerator::walletDataForFile( const QString &fileName, QString *walletName, QString *walletKey ) const
{
*walletKey = fileName + "/opendocument";
*walletName = KWallet::Wallet::LocalWallet();
*walletFolder = KWallet::Wallet::PasswordFolder();
*walletName = "okular_ooo_generator";
}

View file

@ -20,7 +20,7 @@ class KOOOGenerator : public Okular::TextDocumentGenerator
// [INHERITED] reparse configuration
void addPages( KConfigDialog* dlg );
virtual void walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const;
virtual void walletDataForFile( const QString &fileName, QString *walletName, QString *walletKey ) const;
};
#endif

View file

@ -62,7 +62,7 @@
#include <kdeprintdialog.h>
#include <kbookmarkmenu.h>
#include <kpassworddialog.h>
#include <kwallet.h>
#include <kpasswdstore.h>
// local includes
#include "aboutdata.h"
@ -1237,31 +1237,28 @@ Document::OpenResult Part::doOpenFile( const KMimeType::Ptr &mimeA, const QStrin
}
// if the file didn't open correctly it might be encrypted, so ask for a pass
QString walletName, walletFolder, walletKey;
m_document->walletDataForFile(fileNameToOpen, &walletName, &walletFolder, &walletKey);
QString walletName, walletKey;
m_document->walletDataForFile(fileNameToOpen, &walletName, &walletKey);
bool firstInput = true;
bool triedWallet = false;
KWallet::Wallet * wallet = 0;
bool keep = true;
while ( openResult == Document::OpenNeedsPassword )
{
KPasswdStore store;
bool storeopened = false;
const WId parentwid = widget()->effectiveWinId();
QString password;
// 1.A. try to retrieve the first password from the kde wallet system
// 1.A. try to retrieve the first password from the kde store system
if ( !triedWallet && !walletKey.isNull() )
{
const WId parentwid = widget()->effectiveWinId();
wallet = KWallet::Wallet::openWallet( walletName, parentwid );
if ( wallet )
storeopened = store.openStore( parentwid );
store.setStoreID(walletName);
if ( storeopened )
{
// use the KPdf folder (and create if missing)
if ( !wallet->hasFolder( walletFolder ) )
wallet->createFolder( walletFolder );
wallet->setFolder( walletFolder );
// look for the pass in that folder
QString retrievedPass;
if ( wallet->readPassword( walletKey, retrievedPass ) == 0)
QString retrievedPass = store.getPasswd( walletKey.toUtf8(), parentwid );
if (!retrievedPass.isEmpty())
password = retrievedPass;
}
triedWallet = true;
@ -1278,13 +1275,13 @@ Document::OpenResult Part::doOpenFile( const KMimeType::Ptr &mimeA, const QStrin
firstInput = false;
// if the user presses cancel, abort opening
KPasswordDialog dlg( widget(), wallet ? KPasswordDialog::ShowKeepPassword : KPasswordDialog::KPasswordDialogFlags() );
KPasswordDialog dlg( widget(), storeopened ? KPasswordDialog::ShowKeepPassword : KPasswordDialog::KPasswordDialogFlags() );
dlg.setCaption( i18n( "Document Password" ) );
dlg.setPrompt( prompt );
if( !dlg.exec() )
break;
password = dlg.password();
if ( wallet )
if ( storeopened )
keep = dlg.keepPassword();
}
@ -1300,9 +1297,9 @@ Document::OpenResult Part::doOpenFile( const KMimeType::Ptr &mimeA, const QStrin
}
// 3. if the password is correct and the user chose to remember it, store it to the wallet
if ( openResult == Document::OpenSuccess && wallet && /*safety check*/ wallet->isOpen() && keep )
if ( openResult == Document::OpenSuccess && storeopened && keep )
{
wallet->writePassword( walletKey, password );
store.storePasswd( walletKey.toUtf8(), password, parentwid );
}
}
}