17#include <boost/thread/once.hpp>
18#include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
24#undef ZYPP_BASE_LOGGER_LOGGROUP
25#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::gpg"
37 boost::once_flag gpgme_init_once = BOOST_ONCE_INIT;
41 const char *version = gpgme_check_version(NULL);
44 MIL <<
"Initialized libgpgme version: " << version << endl;
48 MIL <<
"Initialized libgpgme with unknown version" << endl;
53 using GpgmeDataPtr = boost::interprocess::scoped_ptr<gpgme_data, boost::function<void (gpgme_data_t)>>;
54 using GpgmeKeyPtr = boost::interprocess::scoped_ptr<_gpgme_key, boost::function<void (gpgme_key_t)>>;
55 using FILEPtr = boost::interprocess::scoped_ptr<FILE, boost::function<int (FILE *)>>;
59 GpgmeErr( gpgme_error_t err_r = GPG_ERR_NO_ERROR )
62 operator gpgme_error_t()
const {
return _err; }
67 std::ostream &
operator<<( std::ostream &
str,
const GpgmeErr & obj )
68 {
return str <<
"<" << gpgme_strsource(obj) <<
"> " << gpgme_strerror(obj); }
71 std::ostream &
operator<<( std::ostream &
str,
const _gpgme_op_import_result & obj )
73 str <<
"gpgme_op_import_result {" << endl;
74 str <<
" " << obj.considered <<
" The total number of considered keys." << endl;
75 str <<
" " << obj.no_user_id <<
" The number of keys without user ID." << endl;
76 str <<
" " << obj.imported <<
" The total number of imported keys." << endl;
77 str <<
" " << obj.imported_rsa <<
" imported RSA keys." << endl;
78 str <<
" " << obj.unchanged <<
" unchanged keys." << endl;
79 str <<
" " << obj.new_user_ids <<
" new user IDs." << endl;
80 str <<
" " << obj.new_sub_keys <<
" new sub keys." << endl;
81 str <<
" " << obj.new_signatures <<
" new signatures." << endl;
82 str <<
" " << obj.new_revocations <<
" new revocations." << endl;
83 str <<
" " << obj.secret_read <<
" secret keys read." << endl;
84 str <<
" " << obj.secret_imported <<
" imported secret keys." << endl;
85 str <<
" " << obj.secret_unchanged <<
" unchanged secret keys." << endl;
86 str <<
" " << obj.not_imported <<
" keys not imported." << endl;
87 for ( gpgme_import_status_t p = obj.imports; p; p = p->next )
89 str <<
" - " << p->fpr <<
": " << p->result << endl;
136 template<
typename Callback >
160 return std::list<std::string>();
164 ERR <<
"Unable to open signature file '" <<
signature_r <<
"'" <<endl;
165 return std::list<std::string>();
172 return std::list<std::string>();
188 return std::list<std::string>();
202 return std::list<std::string>();
208 return std::list<std::string>();
214 return std::list<std::string>();
218 if (!
res || !
res->signatures) {
219 ERR <<
"Unable to read signature fingerprints" <<endl;
220 return std::list<std::string>();
225 std::list<std::string> signatures;
234 std::string id(
sig->fpr );
235 if (
id.size() > 16 )
236 id =
id.substr(
id.size()-16 );
238 DBG <<
"Found signature with ID: " <<
id <<
" in " <<
file_r << std::endl;
239 signatures.push_back( std::move(
id) );
251 WAR <<
"Failed signature check: " <<
file_r <<
" " << GpgmeErr(
sig->status) << endl;
257 WAR <<
"Legacy: Ignore expired or unknown key: " <<
file_r <<
" " << GpgmeErr(
sig->status) << endl;
327 std::list<PublicKeyData>
ret;
334 ERR <<
"gpgme_set_keylist_mode: " << err << endl;
339 ERR <<
"gpgme_op_keylist_start: " << err << endl;
349 ret.push_back( data );
363 std::list<PublicKeyData>
ret;
365 if (
_pimpl->_volatile ) {
380 return _pimpl->verifySignaturesFprs(file, signature);
393 if (key->subkeys &&
id ==
str::asString(key->subkeys->keyid)) {
402 WAR <<
"Key " <<
id <<
"not found" << endl;
428 ERR <<
"Unable to seek in exported key data" << endl;
440 ERR <<
"Unable to read exported key data" << endl;
444 ERR <<
"Error exporting key: "<< err << endl;
455 ERR <<
"Keyfile '" <<
keyfile <<
"' does not exist.";
464 ERR <<
"Error importing key: "<< err << endl;
478 ERR <<
"Error importing key: "<< err << endl;
482 return _pimpl->importKey( data, [&](){
return keydata.size(); } );
485template<
typename Callback>
491 ERR <<
"Error importing key: "<< err << endl;
502 ERR <<
"Error importing key: No keys considered (bsc#1127220, [libgpgme] signal received?)" << endl;
518 if (key->subkeys &&
id ==
str::asString(key->subkeys->keyid)) {
525 ERR <<
"Error deleting key: "<< err << endl;
534 WAR <<
"Key: '"<<
id <<
"' not found." << endl;
539{
return _pimpl->readSignaturesFprs(signature); }
542{
return _pimpl->readSignaturesFprs(keyData); }
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
void swap(AutoDispose &rhs) noexcept
Exchange the contents of two AutoDispose objects.
const Dispose & getDispose() const
Return the current dispose function.
shared_ptr< Impl > _pimpl
Impl & operator=(const Impl &)=delete
std::list< std::string > readSignaturesFprs(const Pathname &signature_r)
Return all fingerprints found in signature_r.
Impl(const Impl &)=delete
bool _volatile
readKeyFromFile workaround bsc#1140670
Impl & operator=(Impl &&)=delete
std::list< std::string > readSignaturesFprs(const ByteArray &signature_r)
Return all fingerprints found in signature_r.
std::list< std::string > readSignaturesFprsOptVerify(const Pathname &signature_r, const Pathname &file_r="/dev/null", bool *verify_r=nullptr)
Return all fingerprints found in signature_r and optionally verify the file_r on the fly.
bool verifySignaturesFprs(const Pathname &file_r, const Pathname &signature_r)
Tries to verify the file_r using signature_r.
bool importKey(GpgmeDataPtr &data, Callback &&calcDataSize)
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.
std::list< PublicKeyData > readKeyFromFile(const Pathname &file)
Returns a list of all PublicKeyData found in file.
RW_pointer< Impl > _pimpl
Pointer to implementation.
bool deleteKey(const std::string &id)
Tries to delete a key specified by id, returns true on success.
Pathname homedir() const
Return the homedir/keyring.
bool importKey(const Pathname &keyfile)
Tries to import a key from keyfile, returns true on success.
Class representing one GPG Public Keys data.
static PublicKeyData fromGpgmeKey(_gpgme_key *data)
Wrapper class for stat/lstat.
String related utilities and Regular expression matching.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
Easy-to use interface to the ZYPP dependency resolver.
Pathname myTmpDir()
Global access to the zypp.TMPDIR (created on demand, deleted when libzypp is unloaded)
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
GpgmeException(const std::string &in_r, const GpgmeErr &err_r)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.