backup. need to do the parsing now.
[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     std::list<PublicKey> trustedPublicKeys();
58     std::list<PublicKey> publicKeys();
59   private:
60     //mutable std::map<Locale, std::string> translations;
61     
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);
65     
66     Pathname _general_kr;
67     Pathname _trusted_kr;
68   public:
69     /** Offer default Impl. */
70     static shared_ptr<Impl> nullimpl()
71     {
72       static shared_ptr<Impl> _nullimpl( new Impl );
73       return _nullimpl;
74     }
75
76   private:
77     friend Impl * rwcowClone<Impl>( const Impl * rhs );
78     /** clone for RWCOW_pointer */
79     Impl * clone() const
80     { return new Impl( *this ); }
81   };
82   
83   PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
84   {
85     return importKey( keyfile, trusted ? _trusted_kr : _general_kr );
86   }
87   
88   void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
89   {
90     deleteKey( id, trusted ? _trusted_kr : _general_kr );
91   }
92   
93   std::list<PublicKey> KeyRing::Impl::publicKeys()
94   {
95     return publicKeys( _general_kr );
96   }
97   
98   std::list<PublicKey> KeyRing::Impl::trustedPublicKeys()
99   {
100     return publicKeys( _trusted_kr );
101   }
102
103   
104   std::list<PublicKey> KeyRing::Impl::publicKeys(const Pathname &keyring)
105   {
106     const char* argv[] =
107     {
108       "gpg",
109       "--quiet",
110       "--list-keys",
111       "--with-colons",
112       "--with-fingerprint",
113       "--homedir",
114       keyring.asString().c_str(),
115       NULL
116     };
117     
118     ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
119     std::string line;
120     int count = 0;
121     for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
122     {
123       MIL << line << std::endl;
124     }
125     prog.close();
126   }
127   
128   PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
129   {
130     const char* argv[] =
131     {
132       "gpg",
133       "--batch",
134       "--homedir",
135       keyring.asString().c_str(),
136       "--import",
137       keyfile.asString().c_str(),
138       NULL
139     };
140     
141     ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
142
143     //if(!prog)
144     //  return 2;
145     //"gpg: key 9C800ACA: public key "SuSE Package Signing Key <build@suse.de>" imported"
146     //TODO parse output and return key id
147     MIL << std::endl;
148     boost::regex rxImported("^gpg: key [[[:digit:]][[:word:]]]+ \"(.+)\" imported$");
149     std::string line;
150     int count = 0;
151     for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
152     {
153       MIL << line << std::endl;
154       boost::smatch what;
155       if(boost::regex_match(line, what, rxImported, boost::match_extra))
156       {
157         MIL << std::endl;
158         dumpRegexpResults(what);
159         prog.close();
160         return PublicKey();
161       }
162     }
163     prog.close();
164     throw Exception("failed to import key");
165   }
166   
167   void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
168   {
169     const char* argv[] =
170     {
171       "gpg",
172       "--batch",
173       "--yes",
174       "--delete-keys",
175       "--homedir",
176       keyring.asString().c_str(),
177       id.c_str(),
178       NULL
179     };
180     
181     ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
182     prog.close();
183   }    
184   
185   ///////////////////////////////////////////////////////////////////
186
187   ///////////////////////////////////////////////////////////////////
188   //
189   //    CLASS NAME : KeyRing
190   //
191   ///////////////////////////////////////////////////////////////////
192
193   ///////////////////////////////////////////////////////////////////
194   //
195   //    METHOD NAME : KeyRing::KeyRing
196   //    METHOD TYPE : Ctor
197   //
198   KeyRing::KeyRing()
199   : _pimpl( Impl::nullimpl() )
200   {}
201
202   ///////////////////////////////////////////////////////////////////
203   //
204   //    METHOD NAME : KeyRing::KeyRing
205   //    METHOD TYPE : Ctor
206   //
207   KeyRing::KeyRing( const Pathname &general_kr, const Pathname &trusted_kr )
208   : _pimpl( new Impl(general_kr, trusted_kr) )
209   {}
210
211   ///////////////////////////////////////////////////////////////////
212   //
213   //    METHOD NAME : KeyRing::~KeyRing
214   //    METHOD TYPE : Dtor
215   //
216   KeyRing::~KeyRing()
217   {}
218
219   ///////////////////////////////////////////////////////////////////
220   //
221   // Forward to implementation:
222   //
223   ///////////////////////////////////////////////////////////////////
224
225   PublicKey KeyRing::importKey( const Pathname &keyfile, bool trusted)
226   {
227     return _pimpl->importKey(keyfile, trusted);
228   }
229   
230   void KeyRing::deleteKey( const std::string &id, bool trusted )
231   {
232     _pimpl->deleteKey(id, trusted);
233   }
234   
235   std::list<PublicKey> KeyRing::publicKeys()
236   {
237     return _pimpl->publicKeys();
238   }
239   
240   std::list<PublicKey> KeyRing::trustedPublicKeys()
241   {
242     return _pimpl->trustedPublicKeys();
243   }
244   
245   /////////////////////////////////////////////////////////////////
246 } // namespace zypp
247 ///////////////////////////////////////////////////////////////////