backup. and more functions
[platform/upstream/libzypp.git] / zypp / KeyRing.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/KeyRing.cc
10  *
11 */
12 #include <iostream>
13 //#include "zypp/base/Logger.h"
14
15 #include "zypp/ZYppFactory.h"
16 #include "zypp/ZYpp.h"
17
18 #include <boost/regex.hpp>
19
20 #include "zypp/base/String.h"
21 #include "zypp/KeyRing.h"
22 #include "zypp/ExternalProgram.h"
23
24 using std::endl;
25 using namespace boost;
26
27 ///////////////////////////////////////////////////////////////////
28 namespace zypp
29 { /////////////////////////////////////////////////////////////////
30
31   static void dumpRegexpResults( const boost::smatch &what )
32   {
33     for ( unsigned int k=0; k < what.size(); k++)
34     {
35       XXX << "[match "<< k << "] [" << what[k] << "]" << std::endl;
36     }
37   }
38   
39   ///////////////////////////////////////////////////////////////////
40   //
41   //    CLASS NAME : KeyRing::Impl
42   //
43   /** KeyRing implementation. */
44   struct KeyRing::Impl
45   {
46     Impl()
47     {}
48
49     Impl( const Pathname &general_kr, const Pathname &trusted_kr )
50     {
51       _general_kr = general_kr;
52       _trusted_kr = trusted_kr;
53     }
54
55     PublicKey importKey( const Pathname &keyfile, bool trusted );
56     void deleteKey( const std::string &id, bool trusted );
57   private:
58     //mutable std::map<Locale, std::string> translations;
59     
60     PublicKey importKey( const Pathname &keyfile, const Pathname &keyring);
61     void deleteKey( const std::string &id, const Pathname &keyring );
62     
63     Pathname _general_kr;
64     Pathname _trusted_kr;
65   public:
66     /** Offer default Impl. */
67     static shared_ptr<Impl> nullimpl()
68     {
69       static shared_ptr<Impl> _nullimpl( new Impl );
70       return _nullimpl;
71     }
72
73   private:
74     friend Impl * rwcowClone<Impl>( const Impl * rhs );
75     /** clone for RWCOW_pointer */
76     Impl * clone() const
77     { return new Impl( *this ); }
78   };
79   
80   PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
81   {
82     return importKey( keyfile, trusted ? _trusted_kr : _general_kr );
83   }
84   
85   void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
86   {
87     deleteKey( id, trusted ? _trusted_kr : _general_kr );
88   }
89   
90   PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
91   {
92     const char* argv[] =
93     {
94       "gpg",
95       "--batch",
96       "--homedir",
97       keyring.asString().c_str(),
98       "--import",
99       keyfile.asString().c_str(),
100       NULL
101     };
102     
103     ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
104
105     //if(!prog)
106     //  return 2;
107     //"gpg: key 9C800ACA: public key "SuSE Package Signing Key <build@suse.de>" imported"
108     //TODO parse output and return key id
109     MIL << std::endl;
110     boost::regex rxImported("^gpg: key [[[:digit:]][[:word:]]]+ \"(.+)\" imported$");
111     std::string line;
112     int count = 0;
113     for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
114     {
115       MIL << std::endl;
116       boost::smatch what;
117       if(boost::regex_match(line, what, rxImported, boost::match_extra))
118       {
119         MIL << std::endl;
120         dumpRegexpResults(what);
121         prog.close();
122         return PublicKey();
123       }
124     }
125     prog.close();
126     throw Exception("failed to import key");
127   }
128   
129   void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
130   {
131     const char* argv[] =
132     {
133       "gpg",
134       "--batch",
135       "--yes",
136       "--delete-keys",
137       "--homedir",
138       keyring.asString().c_str(),
139       id.c_str(),
140       NULL
141     };
142     
143     ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
144     prog.close();
145   }    
146       
147   ///////////////////////////////////////////////////////////////////
148
149   ///////////////////////////////////////////////////////////////////
150   //
151   //    CLASS NAME : KeyRing
152   //
153   ///////////////////////////////////////////////////////////////////
154
155   ///////////////////////////////////////////////////////////////////
156   //
157   //    METHOD NAME : KeyRing::KeyRing
158   //    METHOD TYPE : Ctor
159   //
160   KeyRing::KeyRing()
161   : _pimpl( Impl::nullimpl() )
162   {}
163
164   ///////////////////////////////////////////////////////////////////
165   //
166   //    METHOD NAME : KeyRing::KeyRing
167   //    METHOD TYPE : Ctor
168   //
169   KeyRing::KeyRing( const Pathname &general_kr, const Pathname &trusted_kr )
170   : _pimpl( new Impl(general_kr, trusted_kr) )
171   {}
172
173   ///////////////////////////////////////////////////////////////////
174   //
175   //    METHOD NAME : KeyRing::~KeyRing
176   //    METHOD TYPE : Dtor
177   //
178   KeyRing::~KeyRing()
179   {}
180
181   ///////////////////////////////////////////////////////////////////
182   //
183   // Forward to implementation:
184   //
185   ///////////////////////////////////////////////////////////////////
186
187   PublicKey KeyRing::importKey( const Pathname &keyfile, bool trusted)
188   {
189     return _pimpl->importKey(keyfile, trusted);
190   }
191   
192   void KeyRing::deleteKey( const std::string &id, bool trusted )
193   {
194     _pimpl->deleteKey(id, trusted);
195   }
196   
197   std::list<PublicKey> publicKeys()
198   {
199     return std::list<PublicKey>();
200   }
201   
202   std::list<PublicKey> trustedPublicKeys()
203   {
204     return std::list<PublicKey>();
205   }
206   
207   /*
208   std::string KeyRing::text( const Locale &lang ) const
209   { return _pimpl->text( lang ); }
210
211   void KeyRing::setText( const std::string &text, const Locale &lang )
212   { _pimpl->setText( text, lang ); }
213
214   std::set<Locale> KeyRing::locales() const
215   {
216     return _pimpl->locales();
217   }
218
219   void KeyRing::setText( const std::list<std::string> &text, const Locale &lang )
220   { _pimpl->setText( text, lang ); }
221
222   Locale KeyRing::detectLanguage() const
223   { return _pimpl->detectLanguage(); }
224   */
225   
226   /////////////////////////////////////////////////////////////////
227 } // namespace zypp
228 ///////////////////////////////////////////////////////////////////