1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/KeyRing.cc
14 //#include "zypp/base/Logger.h"
19 #include "zypp/ZYppFactory.h"
20 #include "zypp/ZYpp.h"
22 #include <boost/regex.hpp>
24 #include "zypp/base/String.h"
25 #include "zypp/KeyRing.h"
26 #include "zypp/ExternalProgram.h"
27 #include "zypp/TmpPath.h"
30 using namespace boost;
31 using namespace zypp::filesystem;
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
38 static void dumpRegexpResults( const boost::smatch &what )
40 for ( unsigned int k=0; k < what.size(); k++)
42 XXX << "[match "<< k << "] [" << what[k] << "]" << std::endl;
46 ///////////////////////////////////////////////////////////////////
48 // CLASS NAME : KeyRing::Impl
50 /** KeyRing implementation. */
56 Impl( const Pathname &general_kr, const Pathname &trusted_kr )
58 _general_kr = general_kr;
59 _trusted_kr = trusted_kr;
62 PublicKey importKey( const Pathname &keyfile, bool trusted );
63 void deleteKey( const std::string &id, bool trusted );
64 std::list<PublicKey> trustedPublicKeys();
65 std::list<PublicKey> publicKeys();
67 //mutable std::map<Locale, std::string> translations;
69 PublicKey importKey( const Pathname &keyfile, const Pathname &keyring);
70 void deleteKey( const std::string &id, const Pathname &keyring );
71 std::list<PublicKey> publicKeys(const Pathname &keyring);
76 /** Offer default Impl. */
77 static shared_ptr<Impl> nullimpl()
79 static shared_ptr<Impl> _nullimpl( new Impl );
84 friend Impl * rwcowClone<Impl>( const Impl * rhs );
85 /** clone for RWCOW_pointer */
87 { return new Impl( *this ); }
90 PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
92 return importKey( keyfile, trusted ? _trusted_kr : _general_kr );
95 void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
97 deleteKey( id, trusted ? _trusted_kr : _general_kr );
100 std::list<PublicKey> KeyRing::Impl::publicKeys()
102 return publicKeys( _general_kr );
105 std::list<PublicKey> KeyRing::Impl::trustedPublicKeys()
107 return publicKeys( _trusted_kr );
110 std::list<PublicKey> KeyRing::Impl::publicKeys(const Pathname &keyring)
118 "--with-fingerprint",
120 keyring.asString().c_str(),
123 std::list<PublicKey> keys;
125 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
129 boost::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
131 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
133 MIL << line << std::endl;
135 if(boost::regex_match(line, what, rxColons, boost::match_extra))
137 if ( what[1] == "pub" )
144 //dumpRegexpResults(what);
151 PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
162 keyring.asString().c_str(),
164 keyfile.asString().c_str(),
168 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
170 boost::regex rxImported("^\\[GNUPG:\\] IMPORTED ([^[:space:]]+) (.+)\n$");
173 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
175 MIL << line << std::endl;
177 if(boost::regex_match(line, what, rxImported, boost::match_extra))
187 throw Exception("failed to import key");
191 void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
200 keyring.asString().c_str(),
205 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
209 ///////////////////////////////////////////////////////////////////
211 ///////////////////////////////////////////////////////////////////
213 // CLASS NAME : KeyRing
215 ///////////////////////////////////////////////////////////////////
217 ///////////////////////////////////////////////////////////////////
219 // METHOD NAME : KeyRing::KeyRing
220 // METHOD TYPE : Ctor
223 : _pimpl( Impl::nullimpl() )
226 ///////////////////////////////////////////////////////////////////
228 // METHOD NAME : KeyRing::KeyRing
229 // METHOD TYPE : Ctor
231 KeyRing::KeyRing( const Pathname &general_kr, const Pathname &trusted_kr )
232 : _pimpl( new Impl(general_kr, trusted_kr) )
235 ///////////////////////////////////////////////////////////////////
237 // METHOD NAME : KeyRing::~KeyRing
238 // METHOD TYPE : Dtor
243 ///////////////////////////////////////////////////////////////////
245 // Forward to implementation:
247 ///////////////////////////////////////////////////////////////////
249 PublicKey KeyRing::importKey( const Pathname &keyfile, bool trusted)
251 return _pimpl->importKey(keyfile, trusted);
254 void KeyRing::deleteKey( const std::string &id, bool trusted )
256 _pimpl->deleteKey(id, trusted);
259 std::list<PublicKey> KeyRing::publicKeys()
261 return _pimpl->publicKeys();
264 std::list<PublicKey> KeyRing::trustedPublicKeys()
266 return _pimpl->trustedPublicKeys();
269 /////////////////////////////////////////////////////////////////
271 ///////////////////////////////////////////////////////////////////