integrate keyring into zypp
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Wed, 29 Mar 2006 15:01:26 +0000 (15:01 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Wed, 29 Mar 2006 15:01:26 +0000 (15:01 +0000)
zypp/KeyRing.cc
zypp/KeyRing.h
zypp/zypp_detail/ZYppImpl.cc
zypp/zypp_detail/ZYppImpl.h

index 35e13c5..c08bd6d 100644 (file)
@@ -35,6 +35,8 @@ using namespace std;
 namespace zypp
 { /////////////////////////////////////////////////////////////////
 
+  IMPL_PTR_TYPE(KeyRing);
+  
   static void dumpRegexpResults( const boost::smatch &what )
   {
     for ( unsigned int k=0; k < what.size(); k++)
@@ -55,11 +57,15 @@ namespace zypp
 
     Impl( const Pathname &general_kr, const Pathname &trusted_kr )
     {
+      filesystem::assert_dir(general_kr);
+      filesystem::assert_dir(trusted_kr);
+      
       _general_kr = general_kr;
       _trusted_kr = trusted_kr;
     }
 
-    PublicKey importKey( const Pathname &keyfile, bool trusted );
+    void importKey( const Pathname &keyfile, bool trusted = false);
+    PublicKey readPublicKey( const Pathname &keyfile );
     void deleteKey( const std::string &id, bool trusted );
     std::list<PublicKey> trustedPublicKeys();
     std::list<PublicKey> publicKeys();
@@ -68,7 +74,7 @@ namespace zypp
   private:
     //mutable std::map<Locale, std::string> translations;
     bool verifyFile( const Pathname &file, const Pathname &signature, const Pathname &keyring);
-    PublicKey importKey( const Pathname &keyfile, const Pathname &keyring);
+    void importKey( const Pathname &keyfile, const Pathname &keyring);
     void deleteKey( const std::string &id, const Pathname &keyring );
     std::list<PublicKey> publicKeys(const Pathname &keyring);
     
@@ -89,9 +95,9 @@ namespace zypp
     { return new Impl( *this ); }
   };
   
-  PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
+  void KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
   {
-    return importKey( keyfile, trusted ? _trusted_kr : _general_kr );
+    importKey( keyfile, trusted ? _trusted_kr : _general_kr );
   }
   
   void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
@@ -119,6 +125,52 @@ namespace zypp
     return verifyFile( file, signature, _general_kr );
   }
   
+  PublicKey KeyRing::Impl::readPublicKey( const Pathname &keyfile )
+  {  
+    const char* argv[] =
+    {
+      "gpg",
+      "--with-fingerprint",
+      "--with-colons",
+      "--quiet",
+      "--no-tty",
+      "--no-greeting",
+      "--batch",
+      "--status-fd",
+      "1",  
+      keyfile.asString().c_str(),
+      NULL
+    };
+    
+    ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
+    
+    std::string line;
+    int count = 0;
+    
+    boost::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
+    
+    // pub:-:1024:17:A84EDAE89C800ACA:2000-10-19:2008-06-21::-:SuSE Package Signing Key <build@suse.de>:
+    
+    PublicKey key;
+    for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
+    {
+      //MIL << "[" << line << "]" << std::endl;
+      boost::smatch what;
+      if(boost::regex_match(line, what, rxColons, boost::match_extra))
+      {
+        if ( what[1] == "pub" )
+        {
+          key.id = what[5];
+          key.name = what[10];
+          return key;
+        }
+        //dumpRegexpResults(what);
+      }
+    }
+    prog.close();
+    return key;
+  }
+  
   std::list<PublicKey> KeyRing::Impl::publicKeys(const Pathname &keyring)
   {
     const char* argv[] =
@@ -138,11 +190,11 @@ namespace zypp
     std::string line;
     int count = 0;
     
-    boost::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
+    boost::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
     
     for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
     {
-      MIL << line << std::endl;
+      //MIL << line << std::endl;
       boost::smatch what;
       if(boost::regex_match(line, what, rxColons, boost::match_extra))
       {
@@ -151,16 +203,17 @@ namespace zypp
           PublicKey key;
           key.id = what[5];
           key.name = what[10];
+          MIL << "Found key " << key.id << " [" << key.name << "]" << std::endl;
           keys.push_back(key);
         }
-        //dumpRegexpResults(what);
+        dumpRegexpResults(what);
       }
     }
     prog.close();
     return keys;
   }
   
-  PublicKey KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
+  void KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
   {
     const char* argv[] =
     {
@@ -177,27 +230,32 @@ namespace zypp
       NULL
     };
     
+    int code;
     ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
+    code = prog.close();
     
-    boost::regex rxImported("^\\[GNUPG:\\] IMPORTED ([^[:space:]]+) (.+)\n$");
-    std::string line;
-    int count = 0;
-    for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
-    {
-      MIL << line << std::endl;
-       boost::smatch what;
-       if(boost::regex_match(line, what, rxImported, boost::match_extra))
-       {
-         MIL << std::endl;
-         PublicKey key;
-         key.id = what[1];
-         key.name = what[2];
-         return key;
-       }
-    }
-    prog.close();
-    throw Exception("failed to import key");
-    return PublicKey();
+    if ( code != 0 )
+      ZYPP_THROW(Exception("failed to import key"));
+
+//     boost::regex rxImported("^\\[GNUPG:\\] IMPORTED ([^[:space:]]+) (.+)\n$");
+//     std::string line;
+//     int count = 0;
+//     for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
+//     {
+//       MIL << line << std::endl;
+//        boost::smatch what;
+//        if(boost::regex_match(line, what, rxImported, boost::match_extra))
+//        {
+//          MIL << std::endl;
+//          PublicKey key;
+//          key.id = what[1];
+//          key.name = what[2];
+//          return key;
+//        }
+//     }
+//     prog.close();
+//     throw Exception("failed to import key");
+//     return PublicKey();
   }
   
   void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
@@ -300,9 +358,14 @@ namespace zypp
   //
   ///////////////////////////////////////////////////////////////////
 
-  PublicKey KeyRing::importKey( const Pathname &keyfile, bool trusted)
+  void KeyRing::importKey( const Pathname &keyfile, bool trusted)
+  {
+    _pimpl->importKey(keyfile, trusted);
+  }
+  
+  PublicKey KeyRing::readPublicKey( const Pathname &keyfile )
   {
-    return _pimpl->importKey(keyfile, trusted);
+    return _pimpl->readPublicKey(keyfile);
   }
   
   void KeyRing::deleteKey( const std::string &id, bool trusted )
index c86ec17..8fdcf19 100644 (file)
 #include <set>
 #include <string>
 
+#include "zypp/base/ReferenceCounted.h"
+#include "zypp/Callback.h"
 #include "zypp/base/PtrTypes.h"
 #include "zypp/Locale.h"
 
+DEFINE_PTR_TYPE(KeyRing);
+
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
 
+  struct KeyRingReport : public callback::ReportBase
+  {
+    virtual bool askUserToTrustKey( const std::string keyid, const std::string keyname )
+    { return false; }
+  };
+  
+  
   struct PublicKey
   {
     std::string id;
@@ -38,7 +49,7 @@ namespace zypp
   //
   /** Class that represent a text and multiple translations.
   */
-  class KeyRing
+  class KeyRing  : public base::ReferenceCounted, private base::NonCopyable
   {
     friend std::ostream & operator<<( std::ostream & str, const KeyRing & obj );
 
@@ -52,11 +63,14 @@ namespace zypp
     /** Ctor \todo Make ctor it explicit */
     explicit
     KeyRing(const Pathname &general_kr, const Pathname &trusted_kr);
+    
     /**
-     * imports a key from a file and returns the imported key.
+     * imports a key from a file.
      * throw if key was not imported
      */
-    PublicKey importKey( const Pathname &keyfile, bool trusted = false);
+    void importKey( const Pathname &keyfile, bool trusted = false);
+    PublicKey readPublicKey( const Pathname &keyfile );
+    
     /**
      * removes a key from the keyring. 
      * If trusted is true, Remove it from trusted keyring too.
index 96ac0f3..cdbf2b4 100644 (file)
@@ -70,7 +70,10 @@ namespace zypp
     , _disk_usage()
     {
       MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
-
+      
+      MIL << "initializing keyring..." << std::endl;
+      // , _keyring( new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted")))
+      
       struct utsname buf;
       if (uname (&buf) < 0) {
        ERR << "Can't determine system architecture" << endl;
index 3732f38..6baee73 100644 (file)
@@ -19,6 +19,7 @@
 #include "zypp/Target.h"
 #include "zypp/Resolver.h"
 #include "zypp/Locale.h"
+#include "zypp/KeyRing.h"
 #include "zypp/ZYppCommitResult.h"
 
 ///////////////////////////////////////////////////////////////////
@@ -58,6 +59,11 @@ namespace zypp
       SourceFeed_Ref sourceFeed() const
       { return _sourceFeed; }
 
+      /** */
+      KeyRing_Ptr keyRing() const
+      { return _keyring; }
+
+      
       Resolver_Ptr resolver() const
       { return _resolver; }
 
@@ -140,6 +146,8 @@ namespace zypp
       Target_Ptr _target;
       /** */
       Resolver_Ptr _resolver;
+      
+      KeyRing_Ptr _keyring;
       /** */
       Arch _architecture;
       /** */