libzypp  17.38.17
keyring_p.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_PRIVATE_KEYRINGIMPL_H
13 #define ZYPP_PRIVATE_KEYRINGIMPL_H
14 
16 #include <zypp-core/fs/TmpPath.h>
17 #include <zypp-core/fs/WatchFile>
18 #include <zypp-common/KeyManager.h>
20 #include <optional>
21 
22 namespace zypp {
23 
24 
33  {
34  const std::list<PublicKeyData> & operator()( const Pathname & keyring_r ) const;
35 
36  void setDirty( const Pathname & keyring_r );
37 
44  struct Manip {
46  Manip( CachedPublicKeyData & cache_r, Pathname keyring_r );
47 
49  private:
52  std::optional<KeyManagerCtx> _context;
53  };
55 
57  Manip manip( Pathname keyring_r );
58 
59  private:
60  struct Cache
61  {
62  Cache();
63 
64  void setDirty();
65 
66  void assertCache( const Pathname & keyring_r );
67 
68  bool hasChanged() const;
69 
70  std::list<PublicKeyData> _data;
71 
72  private:
73 
74  scoped_ptr<WatchFile> _keyringK;
75  scoped_ptr<WatchFile> _keyringP;
76  };
77 
78  using CacheMap = std::map<Pathname, Cache>;
79 
80  const std::list<PublicKeyData> & getData( const Pathname & keyring_r ) const;
81 
82  const std::list<PublicKeyData> & getData( const Pathname & keyring_r, Cache & cache_r ) const;
83 
85  };
86 
91  {
92  public:
93  enum class Ring : bool {
94  General = false,
95  Trusted = true,
96  };
97 
99  { return _sigTrustedKeyAdded; }
100 
102  { return _sigTrustedKeyRemoved; }
103 
104  public:
105  KeyRingImpl( const Pathname & baseTmpDir );
106 
107  public:
108  void allowPreload( bool yesno_r ) //< General keyring may be preloaded with keys cached on the system.
109  { _allowPreload = yesno_r; }
110 
115  void importKey ( const PublicKey & key, const Ring ring );
116  void importKeys( const std::list<PublicKey> & keys, const Ring ring );
117 
118  PublicKey exportKey( const std::string & id, const Ring ring ) const
119  { return exportKey( id, keyRingPath( ring ) ); }
120  PublicKey exportKey( const PublicKeyData & keyData, const Ring ring ) const
121  { return exportKey( keyData, keyRingPath( ring ) ); }
122  PublicKey exportKey( const PublicKey & key, const Ring ring ) const
123  { return exportKey( key.keyData(), ring ); }
124 
125  void deleteKey( const std::string & id, const Ring ring );
126 
127  void dumpPublicKey( const std::string & id, const Ring ring, std::ostream & stream )
128  { dumpPublicKey( id, keyRingPath( ring ), stream ); }
129 
130  bool verifyFile( const Pathname & file, const Pathname & signature, const Ring ring )
131  { return verifyFile( file, signature, keyRingPath( ring ) ); }
132 
133  std::string readSignatureKeyId( const Pathname & signature );
134 
140  void multiKeyImport( const Pathname & keyfile_r, const Ring ring );
141 
142  public:
143  enum class MustUpdate {
144  Present = 0,
145  Missing = 1,
146  Update = 2,
147  };
149  MustUpdate mustUpdateData( const PublicKeyData & keyData, const Ring ring ) const
150  {
151  PublicKeyData ringKeyData { publicKeyData( keyData, ring ) };
152  if ( not ringKeyData ) {
153  return MustUpdate::Missing;
154  }
155  if ( keyData.isUpdateFor( ringKeyData ) ) {
156  return MustUpdate::Update;
157  }
158  return MustUpdate::Present;
159  }
160 
161  bool isKeyTrusted( const std::string & id ) const
162  { return bool(publicKeyData( id, Ring::Trusted )); }
163 
164  bool isKeyKnown( const std::string & id ) const
165  { return publicKeyData( id, Ring::Trusted ) || publicKeyData( id, Ring::General ); }
166 
167  PublicKeyData publicKeyData( const std::string & id, const Ring ring ) const
168  { return publicKeyData( id, keyRingPath( ring ) ); }
169  PublicKeyData publicKeyData( const PublicKeyData & keyData, const Ring ring ) const
170  { return publicKeyData( keyData.id(), ring ); }
171  PublicKeyData publicKeyData( const PublicKey & key, const Ring ring ) const
172  { return publicKeyData( key.keyData().id(), ring ); }
173 
174  const std::list<PublicKeyData> & publicKeyData( const Ring ring ) const
175  { return publicKeyData( keyRingPath( ring ) ); }
176 
177  std::list<PublicKey> publicKeys( const Ring ring ) const
178  { return publicKeys( keyRingPath( ring ) ); }
179 
180 
181  private:
183  class SendKeys
184  {
185  public:
187  SendKeys( KeyRingImpl & keyRing, const Pathname & source, const Pathname & target );
189  SendKeys( KeyRingImpl & keyRing, const Ring source, const Ring target );
190  SendKeys( const SendKeys & ) = delete;
191  SendKeys( SendKeys && ) = delete;
192  SendKeys & operator=( const SendKeys & ) = delete;
193  SendKeys & operator=( SendKeys && ) = delete;
194 
198  void operator()( const std::string & id );
199 
200  private:
203  };
204 
205  private:
206  const Pathname keyRingPath( const Ring ring ) const
207  { return ring == Ring::General ? _general_tmp_dir.path() : _trusted_tmp_dir.path(); }
208 
209  void importKey( const Pathname & keyfile, const Pathname & keyring );
210 
211  PublicKey exportKey( const std::string & id, const Pathname & keyring ) const;
212  PublicKey exportKey( const PublicKeyData & keyData, const Pathname & keyring ) const;
213 
214  void deleteKey( const std::string & id, const Pathname & keyring );
215 
216  PublicKeyData publicKeyData( const std::string & id, const Pathname & keyring ) const;
217 
218  const std::list<PublicKeyData> & publicKeyData( const Pathname & keyring ) const
219  {
220  if ( _allowPreload && keyring == keyRingPath( Ring::General ) ) {
221  KeyRingImpl * lazyinit = const_cast<KeyRingImpl*>(this);
222  lazyinit->_allowPreload = false;
223  lazyinit->preloadCachedKeys();
224  }
225  return cachedPublicKeyData( keyring );
226  }
227 
228  std::list<PublicKey> publicKeys( const Pathname & keyring ) const;
229 
230  void dumpPublicKey( const std::string & id, const Pathname & keyring, std::ostream & stream ) const;
231  filesystem::TmpFile dumpPublicKeyToTmp( const std::string & id, const Pathname & keyring ) const;
232 
233  bool verifyFile( const Pathname & file, const Pathname & signature, const Pathname & keyring );
234 
235  private:
237  void preloadCachedKeys();
238 
241  { return cachedPublicKeyData.manip( keyring ); }
242 
243  private:
244  // Used for trusted and untrusted keyrings
248  bool _allowPreload = false; //< General keyring may be preloaded with keys cached on the system.
249 
256 
257  zyppng::Signal<void ( const PublicKey &/*key*/ )> _sigTrustedKeyAdded;
259  };
260 
261  inline std::ostream & operator<<( std::ostream & str, KeyRingImpl::Ring ring )
262  { return str << ( ring == KeyRingImpl::Ring::General ? "generalKeyRing" : "trustedKeyRing" ); }
263 
264  inline std::ostream & operator<<( std::ostream & str, KeyRingImpl::MustUpdate flag )
265  { return str << ( flag == KeyRingImpl::MustUpdate::Present ? "=" : flag == KeyRingImpl::MustUpdate::Missing ? "+" : ">" ); }
266 }
267 
268 
269 #endif
bool verifyFile(const Pathname &file, const Pathname &signature, const Ring ring)
Definition: keyring_p.h:130
filesystem::TmpDir _trusted_tmp_dir
Definition: keyring_p.h:245
Pathname _base_dir
Definition: keyring_p.h:247
filesystem::TmpDir _general_tmp_dir
Definition: keyring_p.h:246
PublicKeyData publicKeyData(const PublicKey &key, const Ring ring) const
Definition: keyring_p.h:171
bool isKeyTrusted(const std::string &id) const
Definition: keyring_p.h:161
const PublicKeyData & keyData() const
The public keys data (.
Definition: PublicKey.cc:640
Functor returning the keyrings data (cached).
Definition: keyring_p.h:32
KeyRing implementation, shared between zyppng and zypp.
Definition: keyring_p.h:90
Manip(CachedPublicKeyData &cache_r, Pathname keyring_r)
Definition: keyring_p.cc:46
SendKeys & operator=(const SendKeys &)=delete
Class representing one GPG Public Keys data.
Definition: PublicKey.h:200
void preloadCachedKeys()
Load key files cached on the system into the generalKeyRing.
Definition: keyring_p.cc:351
String related utilities and Regular expression matching.
void importKeys(const std::list< PublicKey > &keys, const Ring ring)
Definition: keyring_p.cc:217
Pathname path() const
Definition: TmpPath.cc:124
Manip manip(Pathname keyring_r)
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition: keyring_p.cc:92
Provide a new empty temporary file and delete it when no longer needed.
Definition: TmpPath.h:117
PublicKey exportKey(const PublicKey &key, const Ring ring) const
Definition: keyring_p.h:122
std::list< PublicKey > publicKeys(const Ring ring) const
Definition: keyring_p.h:177
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyAdded
Definition: keyring_p.h:257
std::string readSignatureKeyId(const Pathname &signature)
Definition: keyring_p.cc:331
PublicKeyData publicKeyData(const PublicKeyData &keyData, const Ring ring) const
Definition: keyring_p.h:169
std::map< Pathname, Cache > CacheMap
Definition: keyring_p.h:78
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition: TmpPath.h:172
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyRemoved
Definition: keyring_p.h:258
void operator()(const std::string &id)
Transfer key with id from source to target keyring.
Definition: keyring_p.cc:121
zyppng::SignalProxy< void(const PublicKey &)> sigTrustedKeyAdded()
Definition: keyring_p.h:98
CachedPublicKeyData & _cache
Definition: keyring_p.h:50
KeyRingImpl(const Pathname &baseTmpDir)
Definition: keyring_p.cc:132
scoped_ptr< WatchFile > _keyringK
Definition: keyring_p.h:74
MustUpdate mustUpdateData(const PublicKeyData &keyData, const Ring ring) const
Helper computing PublicKeyData&#39;s status in a Ring.
Definition: keyring_p.h:149
old version of Key is in Ring
bool isUpdateFor(const PublicKeyData &rhs) const
Whether this could replace rhs in a keyring.
Definition: PublicKey.h:287
const std::list< PublicKeyData > & getData(const Pathname &keyring_r) const
Definition: keyring_p.cc:94
KeyManagerCtx & keyManagerCtx()
Definition: keyring_p.cc:51
const Pathname keyRingPath(const Ring ring) const
Definition: keyring_p.h:206
void multiKeyImport(const Pathname &keyfile_r, const Ring ring)
Used by RpmDB to import the trusted keys.
Definition: keyring_p.cc:224
filesystem::TmpFile dumpPublicKeyToTmp(const std::string &id, const Pathname &keyring) const
Definition: keyring_p.cc:320
PublicKeyData publicKeyData(const std::string &id, const Ring ring) const
Definition: keyring_p.h:167
zyppng::SignalProxy< void(const PublicKey &)> sigTrustedKeyRemoved()
Definition: keyring_p.h:101
void allowPreload(bool yesno_r)
Definition: keyring_p.h:108
Helper for in-memory key transfer between two keyrings.
Definition: keyring_p.h:183
void assertCache(const Pathname &keyring_r)
Definition: keyring_p.cc:70
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition: PublicKey.h:377
const std::list< PublicKeyData > & publicKeyData(const Pathname &keyring) const
Definition: keyring_p.h:218
std::string id() const
Key ID.
Definition: PublicKey.cc:412
void deleteKey(const std::string &id, const Ring ring)
Definition: keyring_p.cc:230
CachedPublicKeyData::Manip _target
Definition: keyring_p.h:202
CachedPublicKeyData cachedPublicKeyData
Functor returning the keyrings data (cached).
Definition: keyring_p.h:255
const std::list< PublicKeyData > & operator()(const Pathname &keyring_r) const
Definition: keyring_p.cc:86
std::list< PublicKeyData > _data
Definition: keyring_p.h:70
void setDirty(const Pathname &keyring_r)
Definition: keyring_p.cc:89
std::optional< KeyManagerCtx > _context
Definition: keyring_p.h:52
CachedPublicKeyData::Manip keyRingManip(const Pathname &keyring)
Impl helper providing on demand a KeyManagerCtx to manip a cached keyring.
Definition: keyring_p.h:240
scoped_ptr< WatchFile > _keyringP
Definition: keyring_p.h:75
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition: Capabilities.cc:65
PublicKey exportKey(const PublicKeyData &keyData, const Ring ring) const
Definition: keyring_p.h:120
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
const std::list< PublicKeyData > & publicKeyData(const Ring ring) const
Definition: keyring_p.h:174
bool isKeyKnown(const std::string &id) const
Definition: keyring_p.h:164
PublicKey exportKey(const std::string &id, const Ring ring) const
Definition: keyring_p.h:118
void importKey(const PublicKey &key, const Ring ring)
Import PublicKeys into a Ring.
Definition: keyring_p.cc:140
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition: keyring_p.h:44
SendKeys(KeyRingImpl &keyRing, const Pathname &source, const Pathname &target)
Ctor creates the context for reading source and writing target.
Definition: keyring_p.cc:112
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
void dumpPublicKey(const std::string &id, const Ring ring, std::ostream &stream)
Definition: keyring_p.h:127