From 74de1cd2c79d53a9d6167e00c977db38663bbd2b Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Wed, 6 Feb 2008 16:56:47 +0000 Subject: [PATCH] merge lslezak keyring changes --- package/libzypp.changes | 7 ++++ zypp/KeyRing.cc | 16 +++++++++ zypp/PublicKey.cc | 52 ++++++++++++++++++++++++++++ zypp/PublicKey.h | 17 +++++++++- zypp/target/rpm/RpmDb.cc | 88 +++++++++++++++++++++++++++++++++++++++++++++++- zypp/target/rpm/RpmDb.h | 8 +++++ 6 files changed, 186 insertions(+), 2 deletions(-) diff --git a/package/libzypp.changes b/package/libzypp.changes index 8a69cc8..002ee6d 100644 --- a/package/libzypp.changes +++ b/package/libzypp.changes @@ -5,6 +5,13 @@ Wed Jan 23 11:16:50 CET 2008 - ma@suse.de text locale. ------------------------------------------------------------------- +Tue Jan 15 14:56:21 CET 2008 - lslezak@suse.cz + +- added RpmDb::removePubkey(), call it from + KeyRing::Impl::deleteKey() - remove the GPG key from RPM when it + is removed from the trusted keyring + +------------------------------------------------------------------- Tue Dec 18 12:28:22 CET 2007 - aschnell@suse.de - fixed password handling in URLs (bug #347273) diff --git a/zypp/KeyRing.cc b/zypp/KeyRing.cc index 3094213..006107f 100644 --- a/zypp/KeyRing.cc +++ b/zypp/KeyRing.cc @@ -176,7 +176,23 @@ namespace zypp void KeyRing::Impl::deleteKey( const string &id, bool trusted) { + PublicKey key; + + if (trusted) + { + key = exportKey(id, trustedKeyRing()); + } + deleteKey( id, trusted ? trustedKeyRing() : generalKeyRing() ); + + if ( trusted ) + { + callback::SendReport rpmdbEmitSignal; + callback::SendReport emitSignal; + + rpmdbEmitSignal->trustedKeyRemoved( key ); + emitSignal->trustedKeyRemoved( key ); + } } list KeyRing::Impl::publicKeys() diff --git a/zypp/PublicKey.cc b/zypp/PublicKey.cc index da77013..c76ed33 100644 --- a/zypp/PublicKey.cc +++ b/zypp/PublicKey.cc @@ -20,6 +20,9 @@ #include "zypp/PathInfo.h" #include "zypp/base/Exception.h" #include "zypp/base/Logger.h" +#include "zypp/Date.h" + +#include using std::endl; @@ -66,6 +69,12 @@ namespace zypp std::string fingerprint() const { return _fingerprint; } + + Date created() const + { return _created; } + + Date expires() const + { return _expires; } Pathname path() const { @@ -74,6 +83,38 @@ namespace zypp } protected: + + // create Date from a string in format YYYY-MM-DD + Date createDate(const std::string &datestr) + { + // empty input + if (datestr.empty()) + { + return Date(); + } + + tm date; + + try + { + // set the date + date.tm_year = str::strtonum(std::string(datestr, 0, 4)) - 1900; // years since 1900 + date.tm_mon = str::strtonum(std::string(datestr, 5, 2)) - 1; // months since January + date.tm_mday = str::strtonum(std::string(datestr, 9, 2)); // day + } + catch(...) + { + WAR << "Cannot parse date string: " << datestr << std::endl; + return Date(); + } + + // reset time (set 00:00:00) + date.tm_sec = date.tm_min = date.tm_hour = 0; + + time_t time_epoch = ::mktime(&date); + + return Date(time_epoch); + } void readFromFile( const Pathname &keyfile) { @@ -125,6 +166,9 @@ namespace zypp { _id = what[5]; _name = what[10]; + + _created = createDate(what[6]); + _expires = createDate(what[7]); //return key; } else if ( what[1] == "fpr" ) @@ -146,6 +190,8 @@ namespace zypp std::string _fingerprint; std::string _data; filesystem::TmpFile _data_file; + Date _created; + Date _expires; //Pathname _data_file; private: friend Impl * rwcowClone( const Impl * rhs ); @@ -197,6 +243,12 @@ namespace zypp std::string PublicKey::fingerprint() const { return _pimpl->fingerprint(); } + + Date PublicKey::created() const + { return _pimpl->created(); } + + Date PublicKey::expires() const + { return _pimpl->expires(); } Pathname PublicKey::path() const { return _pimpl->path(); } diff --git a/zypp/PublicKey.h b/zypp/PublicKey.h index c6e67af..92af3fe 100644 --- a/zypp/PublicKey.h +++ b/zypp/PublicKey.h @@ -55,7 +55,10 @@ namespace zypp Pathname _keyfile; }; - + + // forward declaration of class Date + class Date; + /////////////////////////////////////////////////////////////////// // // CLASS NAME : PublicKey @@ -88,6 +91,18 @@ namespace zypp std::string id() const; std::string name() const; std::string fingerprint() const; + + /** + * Date when the key was created (time is 00:00:00) + */ + Date created() const; + + /** + * Date when the key expires (time is 00:00:00) + * If the key never expires the date is Date() (i.e. 0 seconds since the epoch (1.1.1970)) + */ + Date expires() const; + Pathname path() const; bool operator==( PublicKey b ) const; diff --git a/zypp/target/rpm/RpmDb.cc b/zypp/target/rpm/RpmDb.cc index e44202e..692e0ec 100644 --- a/zypp/target/rpm/RpmDb.cc +++ b/zypp/target/rpm/RpmDb.cc @@ -100,7 +100,19 @@ struct KeyRingSignalReceiver : callback::ReceiveReport } virtual void trustedKeyRemoved( const PublicKey &key ) - {} + { + MIL << "Trusted key removed from zypp Keyring. Removing..." << endl; + + // remove the key from rpm + try + { + _rpmdb.removePubkey( key ); + } + catch (RpmException &e) + { + ERR << "Could not remove key " << key.id() << " (" << key.name() << ") from rpm database" << endl; + } + } RpmDb &_rpmdb; }; @@ -1040,6 +1052,80 @@ void RpmDb::importPubkey( const PublicKey & pubkey_r ) /////////////////////////////////////////////////////////////////// // // +// METHOD NAME : RpmDb::removePubkey +// METHOD TYPE : PMError +// +void RpmDb::removePubkey( const PublicKey & pubkey_r ) +{ + FAILIFNOTINITIALIZED; + + // check if the key is in the rpm database and just + // return if it does not. + set rpm_keys = pubkeyEditions(); + + // search the key + set::const_iterator found_edition = rpm_keys.end(); + + for ( set::const_iterator it = rpm_keys.begin(); it != rpm_keys.end(); ++it) + { + string id = str::toUpper( (*it).version() ); + string keyshortid = pubkey_r.id().substr(8,8); + MIL << "Comparing '" << id << "' to '" << keyshortid << "'" << endl; + if ( id == keyshortid ) + { + found_edition = it; + break; + } + } + + // the key does not exist, cannot be removed + if (found_edition == rpm_keys.end()) + { + WAR << "Key " << pubkey_r.id() << " is not in rpm db" << endl; + return; + } + + string rpm_name("gpg-pubkey-" + found_edition->asString()); + + RpmArgVec opts; + opts.push_back ( "-e" ); + opts.push_back ( "--" ); + opts.push_back ( rpm_name.c_str() ); + + // don't call modifyDatabase because it would remove the old + // rpm3 database, if the current database is a temporary one. + // But do invalidate packages list. + _packages._valid = false; + run_rpm( opts, ExternalProgram::Stderr_To_Stdout ); + + string line; + while ( systemReadLine( line ) ) + { + if ( line.substr( 0, 6 ) == "error:" ) + { + WAR << line << endl; + } + else + { + DBG << line << endl; + } + } + + int rpm_status = systemStatus(); + + if ( rpm_status != 0 ) + { + ZYPP_THROW(RpmSubprocessException(string("Failed to remove public key ") + pubkey_r.asString() + string(": rpm returned ") + str::numstring(rpm_status))); + } + else + { + MIL << "Key " << pubkey_r << " has been removed from RPM trusted keyring" << endl; + } +} + +/////////////////////////////////////////////////////////////////// +// +// // METHOD NAME : RpmDb::pubkeys // METHOD TYPE : set // diff --git a/zypp/target/rpm/RpmDb.h b/zypp/target/rpm/RpmDb.h index 5ae6918..e95b516 100644 --- a/zypp/target/rpm/RpmDb.h +++ b/zypp/target/rpm/RpmDb.h @@ -220,6 +220,14 @@ public: void importPubkey( const PublicKey & pubkey_r ); /** + * Remove a public key from the rpm database + * + * \throws RpmException + * + **/ + void removePubkey( const PublicKey & pubkey_r ); + + /** * Return the long ids of all installed public keys. **/ std::list pubkeys() const; -- 2.7.4