31#include <zypp-core/fs/WatchFile>
40#undef ZYPP_BASE_LOGGER_LOGGROUP
41#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::KeyRing"
87 {
return getData( keyring_r ); }
99 return getData( keyring_r, cache );
108 return cache_r.
_data;
124 if ( !
_source.exportKey(
id, keydata ) )
127 if ( !
_target.keyManagerCtx().importKey( keydata ) )
143 pDBG(
"Import empty key to", ring,
"skipped" );
149 if ( ring == Ring::Trusted )
151 auto myMustUpdateData = [
this]( std::string_view prefix,
const PublicKeyData & keyData ) ->
bool {
153 pMIL( prefix, fate, keyData,
"to", Ring::Trusted );
160 bool mustUpdate = myMustUpdateData(
"Import Tkey", key.
keyData() );
162 mustUpdate |= myMustUpdateData(
" ", hkeyData );
185 std::vector<PublicKeyData> trustedToUpdate;
186 auto myMustUpdateData = [
this,&trustedToUpdate]( std::string_view prefix,
const PublicKeyData & keyData ) ->
bool {
189 pMIL( prefix, fate, keyData,
"to", Ring::General );
192 pMIL( prefix,
"U", keyData,
"to", Ring::General );
193 trustedToUpdate.push_back( keyData );
195 pMIL( prefix, fate, keyData,
"to", Ring::General );
200 bool mustUpdate = myMustUpdateData(
"Import Gkey", key.
keyData() );
202 mustUpdate |= myMustUpdateData(
" ", hkeyData );
207 SendKeys toTrusted { *
this, Ring::General, Ring::Trusted };
210 toTrusted( keyData.id() );
219 pDBG(
"Import", keys.size(),
"keys to", ring );
233 if ( ! keyDataToDel )
235 WAR <<
"Key to delete [" <<
id <<
"] is not in " << ring << endl;
240 MIL <<
"Deleted key [" <<
id <<
"] from " << ring << endl;
242 if ( ring == Ring::Trusted ) {
249 if ( !
PathInfo( keyfile ).isExist() )
273 WAR <<
"No key [" <<
id <<
"] to export from " <<
keyring << endl;
290 if ( key.providesKey(
id ) )
303 std::list<PublicKey> ret;
308 ret.push_back( key );
309 MIL <<
"Found key " << key << endl;
323 MIL <<
"Going to export key [" <<
id <<
"] from " <<
keyring <<
" to " << tmpFile.
path() << endl;
325 std::ofstream os( tmpFile.
path().
c_str() );
333 if ( !
PathInfo( signature ).isFile() )
336 MIL <<
"Determining key id of signature " << signature << endl;
339 if ( ! fprs.empty() ) {
340 std::string &
id = fprs.back();
341 MIL <<
"Determined key id [" <<
id <<
"] for signature " << signature << endl;
344 return std::string();
356 std::set<Pathname> cachedirs;
359 cachedirs.insert(
"/usr/lib/rpm/gnupg/keys" );
362 cachedirs.insert( r /
"/usr/lib/rpm/gnupg/keys" );
366 cachedirs.insert( r /
"/usr/lib/rpm/gnupg/keys" );
374 std::list<PublicKey> newkeys;
375 for (
const auto & cache : cachedirs ) {
377 [&newkeys](
const Pathname & dir_r,
const char *
const file_r )->
bool {
378 static const str::regex rx {
"^gpg-pubkey-([[:xdigit:]]{8,})(-[[:xdigit:]]{8,})?\\.(asc|key)$" };
381 newkeys.push_back(
PublicKey( dir_r / file_r ) );
387 if ( not newkeys.empty() ) {
388 MIL <<
"Preload cached keys..." << endl;
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
bool exportKey(const std::string &id, std::ostream &stream)
Exports the key with id into the given stream, returns true on success.
std::list< PublicKeyData > listKeys()
Returns a list of all public keys found in the current keyring.
bool verify(const Pathname &file, const Pathname &signature)
Tries to verify file using signature, returns true on success.
static KeyManagerCtx createForOpenPGP()
Creates a new KeyManagerCtx for PGP using a volatile temp.
std::list< std::string > readSignatureFingerprints(const Pathname &signature)
Reads all fingerprints from the signature file , returns a list of all found fingerprints.
bool deleteKey(const std::string &id)
Tries to delete a key specified by id, returns true on success.
bool importKey(const Pathname &keyfile)
Tries to import a key from keyfile, returns true on success.
KeyRingException()
Ctor taking message.
Helper for in-memory key transfer between two keyrings.
void operator()(const std::string &id)
Transfer key with id from source to target keyring.
SendKeys(KeyRingImpl &keyRing, const Pathname &source, const Pathname &target)
Ctor creates the context for reading source and writing target.
CachedPublicKeyData::Manip _target
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyAdded
std::list< PublicKey > publicKeys(const Ring ring) const
@ Update
old version of Key is in Ring
void importKey(const PublicKey &key, const Ring ring)
Import PublicKeys into a Ring.
filesystem::TmpFile dumpPublicKeyToTmp(const std::string &id, const Pathname &keyring) const
MustUpdate mustUpdateData(const PublicKeyData &keyData, const Ring ring) const
Helper computing PublicKeyData's status in a Ring.
filesystem::TmpDir _general_tmp_dir
bool verifyFile(const Pathname &file, const Pathname &signature, const Ring ring)
KeyRingImpl(const Pathname &baseTmpDir)
PublicKeyData publicKeyData(const std::string &id, const Ring ring) const
void preloadCachedKeys()
Load key files cached on the system into the generalKeyRing.
void multiKeyImport(const Pathname &keyfile_r, const Ring ring)
Used by RpmDB to import the trusted keys.
const Pathname keyRingPath(const Ring ring) const
void deleteKey(const std::string &id, const Ring ring)
PublicKey exportKey(const std::string &id, const Ring ring) const
std::string readSignatureKeyId(const Pathname &signature)
void importKeys(const std::list< PublicKey > &keys, const Ring ring)
CachedPublicKeyData::Manip keyRingManip(const Pathname &keyring)
Impl helper providing on demand a KeyManagerCtx to manip a cached keyring.
void dumpPublicKey(const std::string &id, const Ring ring, std::ostream &stream)
filesystem::TmpDir _trusted_tmp_dir
Class representing one GPG Public Keys data.
std::string id() const
Key ID.
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Pathname path() const
File containing the ASCII armored key.
const std::list< PublicKeyData > & hiddenKeys() const
Additional keys data in case the ASCII armored blob contains multiple keys.
const PublicKeyData & keyData() const
The public keys data (.
WatchFile(const Pathname &path_r=Pathname(), Initial mode=INIT)
Interim helper class to collect global options and settings.
Pathname repoManagerRoot() const
The RepoManager root directory.
Pathname systemRoot() const
The target root directory.
static ZConfig & instance()
Singleton ctor.
Pathname pubkeyCachePath() const
Path where the pubkey caches.
const char * c_str() const
String representation.
const std::string & asString() const
String representation.
bool empty() const
Test for an empty path.
Provide a new empty temporary file and delete it when no longer needed.
Regular expression match result.
bool regex_match(const char *s, smatch &matches, const regex ®ex) ZYPP_API
Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
scoped_ptr< WatchFile > _keyringP
void assertCache(const Pathname &keyring_r)
std::list< PublicKeyData > _data
scoped_ptr< WatchFile > _keyringK
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
std::optional< KeyManagerCtx > _context
KeyManagerCtx & keyManagerCtx()
Manip(CachedPublicKeyData &cache_r, Pathname keyring_r)
CachedPublicKeyData & _cache
Functor returning the keyrings data (cached).
void setDirty(const Pathname &keyring_r)
const std::list< PublicKeyData > & operator()(const Pathname &keyring_r) const
const std::list< PublicKeyData > & getData(const Pathname &keyring_r) const
Manip manip(Pathname keyring_r)
Helper providing on demand a KeyManagerCtx to manip the cached keyring.