1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/KeyRing.cc
13 //#include "zypp/base/Logger.h"
15 #include "zypp/ZYppFactory.h"
16 #include "zypp/ZYpp.h"
18 #include <boost/regex.hpp>
20 #include "zypp/base/String.h"
21 #include "zypp/KeyRing.h"
22 #include "zypp/ExternalProgram.h"
25 using namespace boost;
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
31 static void dumpRegexpResults( const boost::smatch &what )
33 for ( unsigned int k=0; k < what.size(); k++)
35 XXX << "[match "<< k << "] [" << what[k] << "]" << std::endl;
39 ///////////////////////////////////////////////////////////////////
41 // CLASS NAME : KeyRing::Impl
43 /** KeyRing implementation. */
49 Impl( const Pathname &general_kr, const Pathname &trusted_kr )
51 _general_kr = general_kr;
52 _trusted_kr = trusted_kr;
55 PublicKey importKey( const Pathname &keyfile, bool trusted );
56 void deleteKey( const std::string &id, bool trusted );
57 std::list<PublicKey> trustedPublicKeys();
58 std::list<PublicKey> publicKeys();
60 //mutable std::map<Locale, std::string> translations;
62 PublicKey importKey( const Pathname &keyfile, const Pathname &keyring);
63 void deleteKey( const std::string &id, const Pathname &keyring );
64 std::list<PublicKey> publicKeys(const Pathname &keyring);
69 /** Offer default Impl. */
70 static shared_ptr<Impl> nullimpl()
72 static shared_ptr<Impl> _nullimpl( new Impl );
77 friend Impl * rwcowClone<Impl>( const Impl * rhs );
78 /** clone for RWCOW_pointer */
80 { return new Impl( *this ); }
83 PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
85 return importKey( keyfile, trusted ? _trusted_kr : _general_kr );
88 void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
90 deleteKey( id, trusted ? _trusted_kr : _general_kr );
93 std::list<PublicKey> KeyRing::Impl::publicKeys()
95 return publicKeys( _general_kr );
98 std::list<PublicKey> KeyRing::Impl::trustedPublicKeys()
100 return publicKeys( _trusted_kr );
104 std::list<PublicKey> KeyRing::Impl::publicKeys(const Pathname &keyring)
112 "--with-fingerprint",
114 keyring.asString().c_str(),
118 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
121 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
123 MIL << line << std::endl;
128 PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
135 keyring.asString().c_str(),
137 keyfile.asString().c_str(),
141 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
145 //"gpg: key 9C800ACA: public key "SuSE Package Signing Key <build@suse.de>" imported"
146 //TODO parse output and return key id
148 boost::regex rxImported("^gpg: key [[[:digit:]][[:word:]]]+ \"(.+)\" imported$");
151 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
153 MIL << line << std::endl;
155 if(boost::regex_match(line, what, rxImported, boost::match_extra))
158 dumpRegexpResults(what);
164 throw Exception("failed to import key");
167 void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
176 keyring.asString().c_str(),
181 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
185 ///////////////////////////////////////////////////////////////////
187 ///////////////////////////////////////////////////////////////////
189 // CLASS NAME : KeyRing
191 ///////////////////////////////////////////////////////////////////
193 ///////////////////////////////////////////////////////////////////
195 // METHOD NAME : KeyRing::KeyRing
196 // METHOD TYPE : Ctor
199 : _pimpl( Impl::nullimpl() )
202 ///////////////////////////////////////////////////////////////////
204 // METHOD NAME : KeyRing::KeyRing
205 // METHOD TYPE : Ctor
207 KeyRing::KeyRing( const Pathname &general_kr, const Pathname &trusted_kr )
208 : _pimpl( new Impl(general_kr, trusted_kr) )
211 ///////////////////////////////////////////////////////////////////
213 // METHOD NAME : KeyRing::~KeyRing
214 // METHOD TYPE : Dtor
219 ///////////////////////////////////////////////////////////////////
221 // Forward to implementation:
223 ///////////////////////////////////////////////////////////////////
225 PublicKey KeyRing::importKey( const Pathname &keyfile, bool trusted)
227 return _pimpl->importKey(keyfile, trusted);
230 void KeyRing::deleteKey( const std::string &id, bool trusted )
232 _pimpl->deleteKey(id, trusted);
235 std::list<PublicKey> KeyRing::publicKeys()
237 return _pimpl->publicKeys();
240 std::list<PublicKey> KeyRing::trustedPublicKeys()
242 return _pimpl->trustedPublicKeys();
245 /////////////////////////////////////////////////////////////////
247 ///////////////////////////////////////////////////////////////////