Git commit 2f67fd9dbc8740d127d9a4f2e470667b3557d0f1 by Till Adam. Committed on 25/01/2013 at 09:48. Pushed by tilladam into branch 'KDE/4.10'. Implement overwriting of existing values in the Mac kwallet version. REVIEW: 108578 M +41 -29 kdeui/util/kwallet_mac.cpp http://commits.kde.org/kdelibs/2f67fd9dbc8740d127d9a4f2e470667b3557d0f1 diff --git a/kdeui/util/kwallet_mac.cpp b/kdeui/util/kwallet_mac.cpp index 4854c4c..a6bf4c0 100644 --- a/kdeui/util/kwallet_mac.cpp +++ b/kdeui/util/kwallet_mac.cpp @@ -86,6 +86,25 @@ static QString appid() return qApp->applicationName(); } = +static OSStatus removeEntryImplementation(const QString& walletName, const= QString& key) { + const QByteArray serviceName( walletName.toUtf8() ); + const QByteArray accountName( key.toUtf8() ); + SecKeychainItemRef itemRef; + QString errMsg; + OSStatus result =3D SecKeychainFindGenericPassword( NULL, serviceName.= size(), serviceName.constData(), accountName.size(), accountName.constData(= ), NULL, NULL, &itemRef ); + if ( isError( result, &errMsg ) ) { + qWarning() << "Could not retrieve password:" << qPrintable(errMsg= ); + return result; + } + const CFReleaser itemReleaser( itemRef ); + result =3D SecKeychainItemDelete( itemRef ); + if ( isError( result, &errMsg ) ) { + qWarning() << "Could not delete password:" << qPrintable(errMsg); + return result; + } + return result; +} + = const QString Wallet::LocalWallet() { KConfigGroup cfg(KSharedConfig::openConfig("kwalletrc")->group("Wallet= ")); @@ -529,28 +548,35 @@ int Wallet::readPasswordList(const QString& key, QMap= & value) return -1; } = - -int Wallet::writeEntry(const QString& key, const QByteArray& password, Ent= ryType entryType) { - const QByteArray serviceName( walletName().toUtf8() ); +static OSStatus writeEntryImplementation( const QString& walletName, const= QString& key, const QByteArray& value ) { + const QByteArray serviceName( walletName.toUtf8() ); const QByteArray accountName( key.toUtf8() ); QString errMsg; - if ( isError( SecKeychainAddGenericPassword( NULL, serviceName.size(),= serviceName.constData(), accountName.size(), accountName.constData(), pass= word.size(), password.constData(), NULL ), &errMsg ) ) { + OSStatus err =3D SecKeychainAddGenericPassword( NULL, serviceName.size= (), serviceName.constData(), accountName.size(), accountName.constData(), v= alue.size(), value.constData(), NULL ); + if (err =3D=3D errSecDuplicateItem) { + err =3D removeEntryImplementation( walletName, key ); + if ( isError( err, &errMsg ) ) { + kWarning() << "Could not delete old key in keychain for replac= ing: " << qPrintable(errMsg); + return err; + } + } + if ( isError( err, &errMsg ) ) { kWarning() << "Could not store password in keychain: " << qPrintab= le(errMsg); - return -1; + return err; } - return 0; + kDebug() << "Succesfully written out key:" << key; + return err; + +} + +int Wallet::writeEntry(const QString& key, const QByteArray& password, Ent= ryType entryType) { + Q_UNUSED( entryType ) + return writeEntryImplementation( walletName(), key, password ); } = = int Wallet::writeEntry(const QString& key, const QByteArray& value) { - const QByteArray serviceName( walletName().toUtf8() ); - const QByteArray accountName( key.toUtf8() ); - QString errMsg; - if ( isError( SecKeychainAddGenericPassword( NULL, serviceName.size(),= serviceName.constData(), accountName.size(), accountName.constData(), valu= e.size(), value.constData(), NULL ), &errMsg ) ) { - kWarning() << "Could not store password in keychain: " << qPrintab= le(errMsg); - return -1; - } - return 0; + return writeEntryImplementation( walletName(), key, value ); } = = @@ -573,22 +599,8 @@ bool Wallet::hasEntry(const QString& key) { return !isError( SecKeychainFindGenericPassword( NULL, serviceName.siz= e(), serviceName.constData(), accountName.size(), accountName.constData(), = NULL, NULL, NULL ), 0 ); } = - int Wallet::removeEntry(const QString& key) { - const QByteArray serviceName( walletName().toUtf8() ); - const QByteArray accountName( key.toUtf8() ); - SecKeychainItemRef itemRef; - QString errMsg; - if ( isError( SecKeychainFindGenericPassword( NULL, serviceName.size()= , serviceName.constData(), accountName.size(), accountName.constData(), NUL= L, NULL, &itemRef ), &errMsg ) ) { - qWarning() << "Could not retrieve password:" << qPrintable(errMsg= ); - return -1; - } - const CFReleaser itemReleaser( itemRef ); - if ( isError( SecKeychainItemDelete( itemRef ), &errMsg ) ) { - qWarning() << "Could not delete password:" << qPrintable(errMsg); - return -1; - } - return 0; + return removeEntryImplementation( walletName(), key ); } = =20