29 #define GPG_BINARY "/usr/bin/gpg2" 53 static shared_ptr<Impl> _nullimpl(
new Impl );
58 friend Impl * rwcowClone<Impl>(
const Impl * rhs );
61 {
return new Impl( *
this ); }
76 PublicKeyData::operator
bool()
const 103 if ( exp < 0 ) ret -= 1;
113 return _(
"(does not expire)");
122 ret +=
_(
"(EXPIRED)");
126 ret +=
_(
"(expires within 24h)");
130 ret +=
str::form(
PL_(
"(expires in %d day)",
"(expires in %d days)", ttl ), ttl );
144 return str::form(
"[%s-%s] [%s] [%s] [TTL %d]",
154 str <<
"[" << obj.
name() <<
"]" << endl;
156 str <<
" id " << obj.
id() << endl;
176 enum { pNONE, pPUB, pSIG, pFPR, pUID } _parseEntry;
180 : _parseEntry( pNONE )
184 void scan( std::string & line_r, std::list<PublicKeyData> & keys_r )
194 if ( line_r.empty() )
202 if ( line_r[1] ==
'u' && line_r[2] ==
'b' && line_r[3] ==
':' )
210 if ( line_r[1] ==
'p' && line_r[2] ==
'r' && line_r[3] ==
':' )
215 if ( line_r[1] ==
'i' && line_r[2] ==
'd' && line_r[3] ==
':' )
220 if ( line_r[1] ==
'i' && line_r[2] ==
'g' && line_r[3] ==
':' )
222 else if ( line_r[1] ==
'u' && line_r[2] ==
'b' && line_r[3] ==
':' )
229 if ( _parseOff || _parseEntry == pNONE )
232 if ( line_r[line_r.size()-1] ==
'\n' )
233 line_r.erase( line_r.size()-1 );
241 switch ( _parseEntry )
245 key = &keys_r.back();
246 key->
_pimpl->_id = _words[4];
248 key->_pimpl->_created =
Date(str::strtonum<Date::ValueType>(_words[5]));
249 key->_pimpl->_expires =
Date(str::strtonum<Date::ValueType>(_words[6]));
254 if ( ( _words.size() > 10 && _words[10] ==
"13x" && !_words[9].empty() && _words[9] !=
"[User ID not found]" )
255 || ( _words.size() > 12 && _words[12] ==
"13x" ) )
257 Date cdate(str::strtonum<Date::ValueType>(_words[5]));
258 if ( key->_pimpl->_created < cdate )
259 key->_pimpl->_created = cdate;
264 if ( key->_pimpl->_fingerprint.empty() )
265 key->_pimpl->_fingerprint = _words[9];
269 if ( ! _words[9].empty() && _words[9] !=
"[User ID not found]" )
304 Impl(
const Pathname & keyFile_r )
306 PathInfo info( keyFile_r );
307 MIL <<
"Taking pubkey from " << keyFile_r <<
" of size " << info.size() <<
" and sha1 " <<
filesystem::checksum(keyFile_r,
"sha1") << endl;
309 if ( !info.isExist() )
310 ZYPP_THROW(
Exception(
"Can't read public key from " + keyFile_r.asString() +
", file not found"));
313 ZYPP_THROW(
Exception(
"Can't copy public key data from " + keyFile_r.asString() +
" to " + _dataFile.path().asString() ));
319 : _dataFile( sharedFile_r )
323 : _dataFile( sharedFile_r )
324 , _keyData( keyData_r )
328 WAR <<
"Invalid PublicKeyData supplied: scanning from file" << endl;
338 {
return _dataFile.path(); }
341 {
return _hiddenKeys; }
349 PathInfo info( _dataFile.path() );
350 MIL <<
"Reading pubkey from " << info.path() <<
" of size " << info.size() <<
" and sha1 " <<
filesystem::checksum(info.path(),
"sha1") << endl;
352 static std::string tmppath( _initHomeDir() );
353 std::string datapath( _dataFile.path().asString() );
359 "--no-default-keyring",
361 "--with-fingerprint",
378 scanner.
scan( line );
380 int ret = prog.
close();
382 switch ( scanner.
_keys.size() )
388 ZYPP_THROW(
BadKeyException(
"File " + _dataFile.path().asString() +
" doesn't contain public key data" , _dataFile.path() ) );
393 _keyData = scanner.
_keys.back();
398 WAR <<
"File " << _dataFile.path().asString() <<
" contains multiple keys: " << scanner.
_keys << endl;
399 _keyData = scanner.
_keys.back();
400 scanner.
_keys.pop_back();
401 _hiddenKeys.swap( scanner.
_keys );
405 MIL <<
"Read pubkey from " << info.path() <<
": " << _keyData << endl;
417 static shared_ptr<Impl> _nullimpl(
new Impl );
422 friend Impl * rwcowClone<Impl>(
const Impl * rhs );
425 {
return new Impl( *
this ); }
497 {
return sid ==
id(); }
std::string name() const
Key name.
static const ValueType day
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Date expires() const
Expiry date, or Date() if the key never expires.
Impl(const filesystem::TmpFile &sharedFile_r, const PublicKeyData &keyData_r)
std::string gpgPubkeyRelease() const
Gpg-pubkey release as computed by rpm (hexencoded created)
int daysToLive() const
Number of days (24h) until the key expires (or since it exired).
std::list< PublicKeyData > _hiddenKeys
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
std::string _initHomeDir()
std::list< PublicKeyData > _keys
Extracted keys.
Impl * clone() const
clone for RWCOW_pointer
Class representing one GPG Public Keys data.
Exception thrown when the supplied key is not a valid gpg key.
const std::list< PublicKeyData > & hiddenKeys() const
Additional keys data in case the ASCII armored blob containes multiple keys.
std::string asString() const
String related utilities and Regular expression matching.
void scan(std::string line_r)
Feed gpg output line by line into scan.
RW_pointer< Impl, rw_pointer::Scoped< Impl > > _pimpl
std::string gpgPubkeyVersion() const
Gpg-pubkey version as computed by rpm (trailing 8 byte id)
PublicKeyScanner implementation.
std::string expiresAsString() const
Provide a new empty temporary file and delete it when no longer needed.
const std::list< PublicKeyData > & hiddenKeys() const
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
PublicKeyData()
Default constructed: empty data.
#define PL_(MSG1, MSG2, N)
std::string gpgPubkeyVersion() const
std::string & replaceAll(std::string &str_r, const std::string &from_r, const std::string &to_r)
Replace all occurrences of from_r with to_r in str_r (inplace).
Pathname path() const
File containig the ASCII armored key.
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Store and operate on date (time_t).
Impl(const Pathname &keyFile_r)
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
std::string fingerprint() const
Key fingerprint.
std::ostream & dumpOn(std::ostream &str, const PublicKey &obj)
std::string expiresAsString() const
bool expired() const
Whether the key has expired.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Pathname myTmpDir()
Global access to the zypp.TMPDIR (created on demand, deleted when libzypp is unloaded) ...
static shared_ptr< Impl > nullimpl()
Offer default Impl.
std::string receiveLine()
Read one line from the input stream.
Scan abstract from 'gpg –with-colons' key listings.
std::string toLower(const std::string &s)
Return lowercase version of s.
std::vector< std::string > _words
PublicKey implementation.
int close()
Wait for the progamm to complete.
unsigned splitFields(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
std::ostream & dumpOn(std::ostream &str, const PublicKeyData &obj)
Base class for Exception.
Impl(const filesystem::TmpFile &sharedFile_r)
const PublicKeyData & keyData() const
The public keys data (.
static Date now()
Return the current time.
std::string asString() const
Default string representation of Date.
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
RWCOW_pointer< Impl > _pimpl
Impl * clone() const
clone for RWCOW_pointer
std::string id() const
Key ID.
static shared_ptr< Impl > nullimpl()
Offer default Impl.
void scan(std::string &line_r, std::list< PublicKeyData > &keys_r)
PublicKeyData implementation.
std::string fingerprint() const
filesystem::TmpFile _dataFile
Date created() const
Creation / last modification date (latest selfsig).
Easy-to use interface to the ZYPP dependency resolver.
std::string gpgPubkeyRelease() const
std::string hexstring(char n, int w=4)
bool operator==(const PublicKey &rhs) const
std::string asString() const
Simple string representation.
const PublicKeyData & keyData() const
bool operator==(const PublicKeyData &lhs, const PublicKeyData &rhs)