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 );
58 //mutable std::map<Locale, std::string> translations;
60 PublicKey importKey( const Pathname &keyfile, const Pathname &keyring);
61 void deleteKey( const std::string &id, const Pathname &keyring );
66 /** Offer default Impl. */
67 static shared_ptr<Impl> nullimpl()
69 static shared_ptr<Impl> _nullimpl( new Impl );
74 friend Impl * rwcowClone<Impl>( const Impl * rhs );
75 /** clone for RWCOW_pointer */
77 { return new Impl( *this ); }
80 PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
82 return importKey( keyfile, trusted ? _trusted_kr : _general_kr );
85 void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
87 deleteKey( id, trusted ? _trusted_kr : _general_kr );
90 PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
97 keyring.asString().c_str(),
99 keyfile.asString().c_str(),
103 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
107 //"gpg: key 9C800ACA: public key "SuSE Package Signing Key <build@suse.de>" imported"
108 //TODO parse output and return key id
110 boost::regex rxImported("^gpg: key [[[:digit:]][[:word:]]]+ \"(.+)\" imported$");
113 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
117 if(boost::regex_match(line, what, rxImported, boost::match_extra))
120 dumpRegexpResults(what);
126 throw Exception("failed to import key");
129 void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
138 keyring.asString().c_str(),
143 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
147 ///////////////////////////////////////////////////////////////////
149 ///////////////////////////////////////////////////////////////////
151 // CLASS NAME : KeyRing
153 ///////////////////////////////////////////////////////////////////
155 ///////////////////////////////////////////////////////////////////
157 // METHOD NAME : KeyRing::KeyRing
158 // METHOD TYPE : Ctor
161 : _pimpl( Impl::nullimpl() )
164 ///////////////////////////////////////////////////////////////////
166 // METHOD NAME : KeyRing::KeyRing
167 // METHOD TYPE : Ctor
169 KeyRing::KeyRing( const Pathname &general_kr, const Pathname &trusted_kr )
170 : _pimpl( new Impl(general_kr, trusted_kr) )
173 ///////////////////////////////////////////////////////////////////
175 // METHOD NAME : KeyRing::~KeyRing
176 // METHOD TYPE : Dtor
181 ///////////////////////////////////////////////////////////////////
183 // Forward to implementation:
185 ///////////////////////////////////////////////////////////////////
187 PublicKey KeyRing::importKey( const Pathname &keyfile, bool trusted)
189 return _pimpl->importKey(keyfile, trusted);
192 void KeyRing::deleteKey( const std::string &id, bool trusted )
194 _pimpl->deleteKey(id, trusted);
197 std::list<PublicKey> publicKeys()
199 return std::list<PublicKey>();
202 std::list<PublicKey> trustedPublicKeys()
204 return std::list<PublicKey>();
208 std::string KeyRing::text( const Locale &lang ) const
209 { return _pimpl->text( lang ); }
211 void KeyRing::setText( const std::string &text, const Locale &lang )
212 { _pimpl->setText( text, lang ); }
214 std::set<Locale> KeyRing::locales() const
216 return _pimpl->locales();
219 void KeyRing::setText( const std::list<std::string> &text, const Locale &lang )
220 { _pimpl->setText( text, lang ); }
222 Locale KeyRing::detectLanguage() const
223 { return _pimpl->detectLanguage(); }
226 /////////////////////////////////////////////////////////////////
228 ///////////////////////////////////////////////////////////////////