- new keyring callbacks
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Thu, 24 Aug 2006 13:41:26 +0000 (13:41 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Thu, 24 Aug 2006 13:41:26 +0000 (13:41 +0000)
- separate trust key from import key
- use PublicKey class instead of params, to be able
  to add more info like photos later (pending #181682)
- update zmart with those callbacks.
- better error handling (Exception types)
- make tmp file names more readable depending on the context
- r4026

15 files changed:
configure.ac
devel/devel.dmacvicar/deltarpms.cc [new file with mode: 0644]
devel/devel.dmacvicar/testbed.cc
package/libzypp.changes
testsuite/zypp/PublicKey.cc
tools/zmart/Makefile.am
tools/zmart/zmart-keyring-callbacks.h
tools/zmart/zmart.cc
zypp/KeyRing.cc
zypp/KeyRing.h
zypp/PublicKey.cc
zypp/PublicKey.h
zypp/source/susetags/SuseTagsImpl.cc
zypp/source/yum/YUMSourceImpl.cc
zypp/target/rpm/RpmDb.cc

index 2254cee..b5669f5 100644 (file)
@@ -242,6 +242,8 @@ AC_OUTPUT(   po/Makefile.in\
        testsuite/target/Makefile       \
        testsuite/target/tests/Makefile \
        testsuite/media/Makefile        \
+       tools/Makefile  \
+       tools/zmart/Makefile    \
   examples/Makefile  \
   zypp/Makefile                        \
        zypp/base/Makefile              \
diff --git a/devel/devel.dmacvicar/deltarpms.cc b/devel/devel.dmacvicar/deltarpms.cc
new file mode 100644 (file)
index 0000000..7d5c235
--- /dev/null
@@ -0,0 +1,65 @@
+#include <iostream>
+
+#include <zypp/base/Logger.h>
+#include <zypp/ZYpp.h>
+#include <zypp/ZYppFactory.h>
+
+#include "zypp/Product.h"
+#include "zypp/Package.h"
+#include "zypp/detail/PackageImplIf.h"
+#include "zypp/source/PackageDelta.h"
+#include "zypp/detail/ImplConnect.h"
+
+#include "zypp/SourceFactory.h"
+#include "testsuite/src/utils/TestUtils.h"
+
+using namespace std;
+using namespace zypp;
+using namespace zypp::detail;
+using namespace zypp::source;
+using namespace zypp::packagedelta;
+//using namespace DbXml;
+
+int main()
+{
+  try
+  {
+    ZYpp::Ptr z = getZYpp();
+    //z->initializeTarget("/");
+    
+    //SourceManager_Ptr manager = SourceManager::sourceManager();
+    
+    //Source_Ref source = SourceFactory().createFrom( Url("dir:/space/rpms/duncan/vim/i386"), "/", "bleh", Pathname() );
+    //Source_Ref source = SourceFactory().createFrom( Url("http://ftp.gwdg.de/pub/suse/update/10.1"), "/", "bleh", Pathname() );
+    Source_Ref source = SourceFactory().createFrom( Url("dir:/mounts/mirror/SuSE/ftp.suse.com/pub/suse/update/10.1"), "/", "bleh", Pathname() );
+    ResStore store = source.resolvables();
+    //zypp::testsuite::utils::dump(store, true, true);
+    
+    for (ResStore::const_iterator it = store.begin(); it != store.end(); ++it)
+    {
+      zypp::Package::Ptr res = asKind<zypp::Package>( *it );
+      std::cout << "------------------------------------------" << std::endl;
+      std::cout << res->name() << " " << res->edition() << " " << res->location() << std::endl;
+      
+      ResImplTraits<Package::Impl>::constPtr impl( ImplConnect::resimpl( res ) );
+      std::cout << "  delta rpms -----------------------------" << std::endl;
+      for ( std::list<DeltaRpm>::const_iterator it = impl->deltaRpms().begin(); it != impl->deltaRpms().end(); ++it )
+      {
+        std::cout << "    " << *it << std::endl;
+      }
+      std::cout << "  patch rpms -----------------------------" << std::endl;
+      for ( std::list<PatchRpm>::const_iterator it = impl->patchRpms().begin(); it != impl->patchRpms().end(); ++it )
+      {
+        //std::cout << "    " <<  *it << std::endl;
+      }
+    }
+        
+  }
+  catch ( const Exception &e )
+  {
+    std::cout << "Sorry, bye" << endl;
+  }
+}
+
+
+
index 6ad0ced..1db0833 100644 (file)
@@ -1,45 +1,39 @@
+#include <iostream>
+
 #include <zypp/base/Logger.h>
 #include <zypp/ZYpp.h>
 #include <zypp/ZYppFactory.h>
 
 #include "zypp/Product.h"
 #include "zypp/Package.h"
+#include "zypp/PublicKey.h"
+#include "zypp/detail/PackageImplIf.h"
+#include "zypp/source/PackageDelta.h"
+#include "zypp/detail/ImplConnect.h"
 
 #include "zypp/SourceFactory.h"
 #include "testsuite/src/utils/TestUtils.h"
 
-using namespace zypp::detail;
-
 using namespace std;
 using namespace zypp;
+using namespace zypp::detail;
 using namespace zypp::source;
+using namespace zypp::packagedelta;
 //using namespace DbXml;
 
-#define TestKind Selection
-
 int main()
 {
   try
   {
     ZYpp::Ptr z = getZYpp();
-    //z->initializeTarget("/");
-    
-    //SourceManager_Ptr manager = SourceManager::sourceManager();
-    
-    Source_Ref source = SourceFactory().createFrom( Url("dir:/space/rpms/duncan/vim/i386"), "/", "bleh", Pathname() );
-    ResStore store = source.resolvables();
-    //zypp::testsuite::utils::dump(store, true, true);
     
-    for (ResStore::const_iterator it = store.begin(); it != store.end(); ++it)
-    {
-      zypp::Package::Ptr res = asKind<zypp::Package>( *it );
-      MIL << res->name() << " " << res->edition() << " " << res->location() << std::endl;
-    }
+    PublicKey key("repomd.xml.key");
+    cout << key << endl;
         
   }
   catch ( const Exception &e )
   {
-    MIL << "Sorry, bye" << endl;
+    std::cout << e.msg() << endl;
   }
 }
 
index 1d20920..678ee9b 100644 (file)
@@ -1,4 +1,24 @@
 -------------------------------------------------------------------
+Thu Aug 24 15:34:45 CEST 2006 - dmacvicar@suse.de
+
+- new keyring callbacks
+- separate trust key from import key
+- use PublicKey class instead of params, to be able
+  to add more info like photos later (pending #181682)
+- update zmart with those callbacks.
+- better error handling (Exception types)
+- make tmp file names more readable depending on the context
+- r4026 
+
+-------------------------------------------------------------------
+Tue Aug 22 22:35:30 CEST 2006 - dmacvicar@suse.de
+
+- decouple probing from source creation, using the new
+  media test for existence functions.
+- r4019 
+
+-------------------------------------------------------------------
+
 Tue Aug 22 17:56:53 CEST 2006 - mt@suse.de
 
 - Fixed getDoesFileExist to reset the transfer range
index 0bc0137..ac0b6b2 100644 (file)
@@ -237,7 +237,7 @@ size_t *susekey_size=&_susekey_size;
 
 void publickey_test()
 {
-  BOOST_CHECK_THROW( zypp::devel::PublicKey("nonexistant"), Exception );
+  BOOST_CHECK_THROW( zypp::PublicKey("nonexistant"), Exception );
   
   filesystem::TmpFile file;
   ofstream str(file.path().asString().c_str(),ofstream::out);
@@ -249,12 +249,16 @@ void publickey_test()
   str.flush();
   str.close();
  
-  zypp::devel::PublicKey k2(file.path());
+  zypp::PublicKey k2(file.path());
   
   BOOST_CHECK_EQUAL( k2.id(), "A84EDAE89C800ACA" );
   BOOST_CHECK_EQUAL( k2.name(), "SuSE Package Signing Key <build@suse.de>" );
   BOOST_CHECK_EQUAL( k2.fingerprint(), "79C179B2E1C820C1890F9994A84EDAE89C800ACA" );
-    
+  BOOST_REQUIRE( k2.path() != Pathname() );
+  BOOST_REQUIRE( k2 == k2 );
+  // test for a empty key
+  zypp::PublicKey empty_key;
+  BOOST_REQUIRE( ! empty_key.isValid() );
 }
 
 test_suite*
index b517ebe..dcfb66b 100644 (file)
@@ -16,7 +16,7 @@ LDADD =               -L$(top_srcdir)/zypp/.libs -lzypp -lboost_regex -lboost_program_options
 ## ##################################################
 
 zmart_SOURCES =        zmart.cc
-zmart_LDFLAGS =        -static
+#zmart_LDFLAGS =       -static
 
 .PHONY:        always
 
index a7e72dc..1d4ad1b 100644 (file)
@@ -29,9 +29,17 @@ namespace zypp {
     //   or '1\n' -> true
     // reads characters from stdin until newline. Defaults to 'false'
     static bool
-    readCallbackAnswer()
+    readBoolAnswer()
     {
+      char c = 0;
+      int  count = 0;
+      while ( (c != 'y') && (c != 'Y') && (c != 'N') && (c != 'n') )
+        cin >> c ;
+      
+      if ( ( c == 'y' ) || ( c == 'Y' ) ) 
         return true;
+      else
+        return false;
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -41,19 +49,30 @@ namespace zypp {
     {
       virtual bool askUserToAcceptUnsignedFile( const std::string &file )
       {
-        return readCallbackAnswer();
+        cout << file << " is unsigned, continue? [y/n]: " << endl;
+        return readBoolAnswer();
       }
-      virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+      
+      virtual bool askUserToImportKey( const PublicKey &key )
       {
-        return readCallbackAnswer();
+        cout << "Import key " << key.id() << " in trusted keyring? [y/n]: " << endl;
+        return readBoolAnswer();
+      } 
+      
+      virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &id )
+      {
+        cout << file << " is signed with an unknown key id: " << id << ", continue? [y/n]: " << endl;
+        return readBoolAnswer();
       }
-      virtual bool askUserToTrustKey( const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+      virtual bool askUserToTrustKey( const PublicKey &key )
       {
-        return readCallbackAnswer();
+        cout  << "Do you want to trust key id " << key.id() << " " << key.name() << " fingerprint:" << key.fingerprint() << " ? [y/n]: " << endl;
+        return readBoolAnswer();
       }
-      virtual bool askUserToAcceptVerificationFailed( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+      virtual bool askUserToAcceptVerificationFailed( const std::string &file,const PublicKey &key )
       {
-        return readCallbackAnswer();
+        cout << file << "Signature verification for " << file << " with public key id " << key.id() << " " << key.name() << " fingerprint:" << key.fingerprint() << " failed, THIS IS RISKY!. continue? [y/n]: " << endl;
+        return readBoolAnswer();
       }
     };
 
@@ -62,15 +81,15 @@ namespace zypp {
     {
       virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
       {
-        return readCallbackAnswer();
+        return readBoolAnswer();
       }
       virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
       {
-        return readCallbackAnswer();
+        return readBoolAnswer();
       }
       virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
       {
-        return readCallbackAnswer();
+        return readBoolAnswer();
       }
     };
 
index b3d90be..9022053 100644 (file)
@@ -312,7 +312,7 @@ int main(int argc, char **argv)
   
   if (vm.count("sources"))
   {
-    vector<string> sources = vm["input-file"].as< vector<string> >();
+    vector<string> sources = vm["sources"].as< vector<string> >();
     //MIL << "Additional sources are: " << sources << "\n";
     for ( vector<string>::const_iterator it = sources.begin(); it != sources.end(); ++it )
     {
index bdcacaf..de52edb 100644 (file)
@@ -66,15 +66,18 @@ namespace zypp
   bool KeyRingReport::askUserToAcceptUnsignedFile( const std::string &file )
   { return _keyRingDefaultAccept; }
 
-  bool KeyRingReport::askUserToAcceptUnknownKey( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+  bool KeyRingReport::askUserToAcceptUnknownKey( const std::string &file, const std::string &id )
   { return _keyRingDefaultAccept; }
 
-  bool KeyRingReport::askUserToTrustKey( const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+  bool KeyRingReport::askUserToTrustKey( const PublicKey &key )
   { return _keyRingDefaultAccept; }
 
-  bool KeyRingReport::askUserToAcceptVerificationFailed( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+  bool KeyRingReport::askUserToImportKey( const PublicKey &key)
   { return _keyRingDefaultAccept; }
-
+  
+  bool KeyRingReport::askUserToAcceptVerificationFailed( const std::string &file, const PublicKey &key )
+  { return _keyRingDefaultAccept; }
+  
   ///////////////////////////////////////////////////////////////////
   //
   //   CLASS NAME : KeyRing::Impl
@@ -83,11 +86,11 @@ namespace zypp
   struct KeyRing::Impl
   {
     Impl(const Pathname &baseTmpDir)
-    : _trusted_tmp_dir(baseTmpDir)
-    ,  _general_tmp_dir(baseTmpDir)
+    : _trusted_tmp_dir(baseTmpDir, "zypp-trusted-kr")
+   ,  _general_tmp_dir(baseTmpDir, "zypp-general-kr")
+   , _base_dir( baseTmpDir )
 
     {
-      _base_dir = baseTmpDir;
     }
 
     /*
@@ -101,11 +104,11 @@ namespace zypp
     }
     */
 
-    void importKey( const Pathname &keyfile, bool trusted = false);
-    PublicKey readPublicKey( const Pathname &keyfile );
-    std::string readSignatureKeyId( const Pathname &signature );
-
+    void importKey( const PublicKey &key, bool trusted = false);
     void deleteKey( const std::string &id, bool trusted );
+    
+    std::string readSignatureKeyId( const Pathname &signature );
+    
     std::list<PublicKey> trustedPublicKeys();
     std::list<PublicKey> publicKeys();
 
@@ -119,9 +122,8 @@ namespace zypp
     //mutable std::map<Locale, std::string> translations;
     bool verifyFile( const Pathname &file, const Pathname &signature, const Pathname &keyring);
     void importKey( const Pathname &keyfile, const Pathname &keyring);
-
-    void exportKey( std::string id, const Pathname &keyfile, bool trusted);
-
+    PublicKey exportKey( std::string id, const Pathname &keyring);
+    void dumpPublicKey( const std::string &id, const Pathname &keyring, std::ostream &stream );
     void deleteKey( const std::string &id, const Pathname &keyring );
     std::list<PublicKey> publicKeys(const Pathname &keyring);
 
@@ -160,9 +162,9 @@ namespace zypp
     return _trusted_tmp_dir.path();
   }
 
-  void KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
+  void KeyRing::Impl::importKey( const PublicKey &key, bool trusted)
   {
-    importKey( keyfile, trusted ? trustedKeyRing() : generalKeyRing() );
+    importKey( key.path(), trusted ? trustedKeyRing() : generalKeyRing() );
   }
 
   void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
@@ -196,28 +198,44 @@ namespace zypp
     std::list<PublicKey> keys = publicKeys(keyring);
     for (std::list<PublicKey>::const_iterator it = keys.begin(); it != keys.end(); it++)
     {
-      if ( id == (*it).id )
+      if ( id == (*it).id() )
         return true;
     }
     return false;
   }
-
-  void KeyRing::Impl::exportKey( std::string id, const Pathname &keyfile, bool trusted)
+  
+  PublicKey KeyRing::Impl::exportKey( std::string id, const Pathname &keyring)
   {
+    TmpFile tmp_file( _base_dir, "pubkey-"+id+"-" );
+    Pathname keyfile = tmp_file.path();
+    MIL << "Going to export key " << id << " from " << keyring << " to " << keyfile << endl;
+     
     try {
       std::ofstream os(keyfile.asString().c_str());
-      dumpPublicKey( id, trusted, os );
+      dumpPublicKey( id, keyring, os );
       os.close();
+      PublicKey key(keyfile);
+      return key;
+    }
+    catch (BadKeyException &e)
+    {
+      ERR << "Cannot create public key " << id << " from " << keyring << " keyring  to file " << e.keyFile() << std::endl;
+      ZYPP_THROW(Exception("Cannot create public key " + id + " from " + keyring.asString() + " keyring to file " + e.keyFile().asString() ) );
     }
     catch (std::exception &e)
     {
-      ERR << "Cannot export key " << id << " from " << (trusted ? "trusted" : "untrusted ") << " keyring  to file " << keyfile << std::endl;
+      ERR << "Cannot export key " << id << " from " << keyring << " keyring  to file " << keyfile << std::endl;
     }
+    return PublicKey();
   }
 
   void KeyRing::Impl::dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream )
   {
-    Pathname keyring = trusted ? trustedKeyRing() : generalKeyRing();
+     dumpPublicKey( id, ( trusted ? trustedKeyRing() : generalKeyRing() ), stream );
+  }
+  
+  void KeyRing::Impl::dumpPublicKey( const std::string &id, const Pathname &keyring, std::ostream &stream )
+  {
     const char* argv[] =
     {
       "gpg",
@@ -254,9 +272,9 @@ namespace zypp
     // if signature does not exists, ask user if he wants to accept unsigned file.
     if( signature.empty() || (!PathInfo(signature).isExist()) )
     {
-       bool res = report->askUserToAcceptUnsignedFile( filedesc );
-       MIL << "User decision on unsigned file: " << res << endl;
-       return res;
+      bool res = report->askUserToAcceptUnsignedFile( filedesc );
+      MIL << "User decision on unsigned file: " << res << endl;
+      return res;
     }
 
     // get the id of the signature
@@ -265,38 +283,44 @@ namespace zypp
     // doeskey exists in trusted keyring
     if ( publicKeyExists( id, trustedKeyRing() ) )
     {
-      TmpFile trustedKey(_base_dir);
-      exportKey( id, trustedKey.path(), true);
-      PublicKey key = readPublicKey(trustedKey.path());
-      MIL << "Key " << id << " " << key.name << " is trusted" << std::endl;
+      PublicKey key = exportKey( id, trustedKeyRing() );
+      
+      MIL << "Key " << id << " " << key.name() << " is trusted" << std::endl;
       // it exists, is trusted, does it validates?
       if ( verifyFile( file, signature, trustedKeyRing() ) )
         return true;
       else
-        return report->askUserToAcceptVerificationFailed( filedesc, key.id, key.name, key.fingerprint );
+        return report->askUserToAcceptVerificationFailed( filedesc, key );
     }
     else
     {
       if ( publicKeyExists( id, generalKeyRing() ) )
       {
-        TmpFile unKey(_base_dir);
-        exportKey( id, unKey.path(), false);
-        MIL << "Exported key " << id << " to " << unKey << std::endl;
-
-        PublicKey key = readPublicKey(unKey.path());
-        MIL << "Key " << id << " " << key.name << " is not trusted" << std::endl;
+        PublicKey key =  exportKey( id, generalKeyRing());
+        MIL << "Exported key " << id << " to " << key.path() << std::endl;
+        MIL << "Key " << id << " " << key.name() << " is not trusted" << std::endl;
         // ok the key is not trusted, ask the user to trust it or not
-#warning We need the key details passed to the callback
-        if ( report->askUserToTrustKey(key.id, key.name, key.fingerprint) )
+        #warning We need the key details passed to the callback
+        if ( report->askUserToTrustKey( key ) )
         {
-          MIL << "User wants to trust key " << id << " " << key.name << std::endl;
+          MIL << "User wants to trust key " << id << " " << key.name() << std::endl;
           //dumpFile(unKey.path());
 
-          importKey( unKey.path(), trustedKeyRing() );
-          emitSignal->trustedKeyAdded( (const KeyRing &)(*this), id, key.name, key.fingerprint );
+          Pathname which_keyring;
+          if ( report->askUserToImportKey( key ) )
+          {
+            MIL << "User wants to import key " << id << " " << key.name() << std::endl;
+            importKey( key.path(), trustedKeyRing() );
+            emitSignal->trustedKeyAdded( (const KeyRing &)(*this), key );
+            which_keyring = trustedKeyRing();
+          }
+          else
+          {
+            which_keyring = generalKeyRing();
+          }
 
           // emit key added
-          if ( verifyFile( file, signature, trustedKeyRing() ) )
+          if ( verifyFile( file, signature, which_keyring ) )
           {
             MIL << "File signature is verified" << std::endl;
             return true;
@@ -304,7 +328,7 @@ namespace zypp
           else
           {
             MIL << "File signature check fails" << std::endl;
-            if ( report->askUserToAcceptVerificationFailed( filedesc, key.id, key.name, key.fingerprint ) )
+            if ( report->askUserToAcceptVerificationFailed( filedesc, key ) )
             {
               MIL << "User continues anyway." << std::endl;
               return true;
@@ -318,7 +342,7 @@ namespace zypp
         }
         else
         {
-          MIL << "User does not want to trust key " << id << " " << key.name << std::endl;
+          MIL << "User does not want to trust key " << id << " " << key.name() << std::endl;
           return false;
         }
       }
@@ -326,7 +350,7 @@ namespace zypp
       {
         // unknown key...
         MIL << "File [" << file << "] ( " << filedesc << " ) signed with unknown key [" << id << "]" << std::endl;
-        if ( report->askUserToAcceptUnknownKey( filedesc, id, "", "" ) )
+        if ( report->askUserToAcceptUnknownKey( filedesc, id ) )
         {
           MIL << "User wants to accept unknown key " << id << std::endl;
           return true;
@@ -341,62 +365,6 @@ namespace zypp
     return false;
   }
 
-
-  PublicKey KeyRing::Impl::readPublicKey( const Pathname &keyfile )
-  {
-    TmpDir dir(_base_dir);
-
-    const char* argv[] =
-    {
-      "gpg",
-      "--no-default-keyring",
-      "--homedir",
-      dir.path().asString().c_str(),
-      "--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;
-
-    str::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;
-      str::smatch what;
-      if(str::regex_match(line, what, rxColons, str::match_extra))
-      {
-        if ( what[1] == "pub" )
-        {
-          key.id = what[5];
-          key.name = what[10];
-          //return key;
-        }
-        else if ( what[1] == "fpr" )
-        {
-          key.fingerprint = what[10];
-        }
-        //dumpRegexpResults(what);
-      }
-    }
-    prog.close();
-    return key;
-  }
-
   std::list<PublicKey> KeyRing::Impl::publicKeys(const Pathname &keyring)
   {
     const char* argv[] =
@@ -431,12 +399,11 @@ namespace zypp
       str::smatch what;
       if(str::regex_match(line, what, rxColons, str::match_extra))
       {
-        PublicKey key;
+        string id;
         if ( what[1] == "pub" )
         {
-          key.id = what[5];
-          key.name = what[10];
-
+          id = what[5];
+          
           std::string line2;
           for(line2 = prog.receiveLine(); !line2.empty(); line2 = prog.receiveLine(), count++ )
           {
@@ -445,13 +412,14 @@ namespace zypp
             {
               if ( (what2[1] == "fpr") && (what2[1] != "pub") && (what2[1] !="sub"))
               {
-                key.fingerprint = what2[10];
+                //key.fingerprint = what2[10];
                 break;
               }
             }
           }
+          PublicKey key(exportKey( id, keyring ));
           keys.push_back(key);
-          MIL << "Found key " << "[" << key.id << "]" << " [" << key.name << "]" << " [" << key.fingerprint << "]" << std::endl;
+          MIL << "Found key " << "[" << key.id() << "]" << " [" << key.name() << "]" << " [" << key.fingerprint() << "]" << std::endl;
         }
         //dumpRegexpResults(what);
       }
@@ -459,9 +427,12 @@ namespace zypp
     prog.close();
     return keys;
   }
-
+    
   void KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
   {
+    if ( ! PathInfo(keyfile).isExist() )
+      ZYPP_THROW(KeyRingException("Tried to import not existant key " + keyfile.asString() + " into keyring " + keyring.asString()));
+    
     const char* argv[] =
     {
       "gpg",
@@ -520,8 +491,8 @@ namespace zypp
   {
     MIL << "Deetermining key id if signature " << signature << std::endl;
     // HACK create a tmp keyring with no keys
-    TmpDir dir(_base_dir);
-    TmpFile fakeData(_base_dir);
+    TmpDir dir(_base_dir, "fake-keyring");
+    TmpFile fakeData(_base_dir, "fake-data");
 
     const char* argv[] =
     {
@@ -639,16 +610,12 @@ namespace zypp
   //
   ///////////////////////////////////////////////////////////////////
 
-  void KeyRing::importKey( const Pathname &keyfile, bool trusted)
+  
+  void KeyRing::importKey( const PublicKey &key, bool trusted )
   {
-    _pimpl->importKey(keyfile, trusted);
+    _pimpl->importKey( key.path(), trusted );
   }
-
-  PublicKey KeyRing::readPublicKey( const Pathname &keyfile )
-  {
-    return _pimpl->readPublicKey(keyfile);
-  }
-
+  
   std::string KeyRing::readSignatureKeyId( const Pathname &signature )
   {
     return _pimpl->readSignatureKeyId(signature);
index 035684a..da23a94 100644 (file)
@@ -22,6 +22,7 @@
 #include "zypp/Callback.h"
 #include "zypp/base/PtrTypes.h"
 #include "zypp/Locale.h"
+#include "zypp/PublicKey.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -32,32 +33,39 @@ namespace zypp
   struct KeyRingReport : public callback::ReportBase
   {
     virtual bool askUserToAcceptUnsignedFile( const std::string &file );
-    virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint );
-    virtual bool askUserToTrustKey( const std::string &keyid, const std::string &keyname, const std::string &fingerprint);
-    virtual bool askUserToAcceptVerificationFailed( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint );
+    virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &id );
+    virtual bool askUserToTrustKey( const PublicKey &key);
+    virtual bool askUserToImportKey( const PublicKey &key);
+    virtual bool askUserToAcceptVerificationFailed( const std::string &file, const PublicKey &key );
   };
   
   struct KeyRingSignals : public callback::ReportBase
   {
-    virtual void trustedKeyAdded( const KeyRing &keyring, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+    virtual void trustedKeyAdded( const KeyRing &keyring, const PublicKey &key )
     {}
-    virtual void trustedKeyRemoved( const KeyRing &keyring, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+    virtual void trustedKeyRemoved( const KeyRing &keyring, const PublicKey &key )
     {}
   };
-  
-  struct PublicKey
-  {
-    bool operator==(PublicKey b)
-    { return (b.id == id) && (b.fingerprint == fingerprint); }
-    
-    bool operator==(std::string sid)
-    { return sid == id; }
-    
-    std::string id;
-    std::string name;
-    std::string fingerprint;
-  };
 
+  class KeyRingException : public Exception
+   {
+     public:
+       /** Ctor taking message.
+      * Use \ref ZYPP_THROW to throw exceptions.
+        */
+       KeyRingException()
+       : Exception( "Bad Key Exception" )
+       {}
+       /** Ctor taking message.
+        * Use \ref ZYPP_THROW to throw exceptions.
+        */
+       KeyRingException( const std::string & msg_r )
+       : Exception( msg_r )
+       {}
+       /** Dtor. */
+       virtual ~KeyRingException() throw() {};
+   };
+  
   ///////////////////////////////////////////////////////////////////
   //
   //   CLASS NAME : KeyRing
@@ -82,8 +90,7 @@ namespace zypp
      * imports a key from a file.
      * throw if key was not imported
      */
-    void importKey( const Pathname &keyfile, bool trusted = false);
-    PublicKey readPublicKey( const Pathname &keyfile );
+    void importKey( const PublicKey &key, bool trusted = false);
     
     void dumpTrustedPublicKey( const std::string &id, std::ostream &stream )
     { dumpPublicKey(id, true, stream); }
@@ -112,7 +119,6 @@ namespace zypp
      * The boolr eturned depends on user desicion to trust or not.
      */
     bool verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature);
-    
     bool verifyFileSignature( const Pathname &file, const Pathname &signature);
     bool verifyFileTrustedSignature( const Pathname &file, const Pathname &signature);
 
index 069d1f2..04f8c53 100644 (file)
@@ -24,10 +24,7 @@ using std::endl;
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
-{ /////////////////////////////////////////////////////////////////
-namespace devel
-{      
-  ///////////////////////////////////////////////////////////////////
+{ /////////////////////////////////////////////////////////////////    
   //
   //   CLASS NAME : PublicKey::Impl
   //
@@ -54,7 +51,7 @@ namespace devel
      
     std::string asString() const
     {
-      return "GPG KEY";  
+      return "[" + id() + "] [" + name() + "] [" + fingerprint() + "]";
     }
     
     std::string armoredData() const
@@ -69,13 +66,25 @@ namespace devel
     std::string fingerprint() const
     { return _fingerprint; }
     
+    Pathname path() const
+    { 
+      return _data_file.path();
+      //return _data_file;
+    }
+    
     protected:
       
      void readFromFile( const Pathname &keyfile)
      {
-       if ( !PathInfo(keyfile).isExist() )
+       PathInfo info(keyfile);
+       MIL << "Reading pubkey from " << keyfile << " of size " << info.size() << " and sha1 " << filesystem::checksum(keyfile, "sha1")<< endl; 
+       if ( !info.isExist() )
          ZYPP_THROW(Exception("Can't read public key from " + keyfile.asString() + ", file not found"));
          
+       if ( copy( keyfile, _data_file.path() ) != 0 )
+         ZYPP_THROW(Exception("Can't copy public key data from " + keyfile.asString() + " to " +  _data_file.path().asString() ));
+
+       
        filesystem::TmpDir dir;
   
         const char* argv[] =
@@ -92,7 +101,7 @@ namespace devel
           "--batch",
           "--status-fd",
           "1",
-          keyfile.asString().c_str(),
+          _data_file.path().asString().c_str(),
           NULL
         };
   
@@ -127,7 +136,7 @@ namespace devel
         prog.close();
         
         if (_id.size() == 0 )
-          ZYPP_THROW(Exception("Can't read public key from " + keyfile.asString()));
+          ZYPP_THROW(BadKeyException("File " + keyfile.asString() + " doesn't contain public key data" , keyfile));
      }
     
   private:
@@ -135,6 +144,8 @@ namespace devel
     std::string _name;
     std::string _fingerprint;
     std::string _data;
+    filesystem::TmpFile _data_file;
+    //Pathname _data_file;
   private:
     friend Impl * rwcowClone<Impl>( const Impl * rhs );
     /** clone for RWCOW_pointer */
@@ -152,7 +163,7 @@ namespace devel
   : _pimpl( Impl::nullimpl() )
   {}
 
-  PublicKey::PublicKey(   const Pathname &file )
+  PublicKey::PublicKey( const Pathname &file )
   : _pimpl( new Impl(file) )
   {}
   ///////////////////////////////////////////////////////////////////
@@ -185,11 +196,10 @@ namespace devel
     
   std::string PublicKey::fingerprint() const
   { return _pimpl->fingerprint(); }
-    
-//   std::string PublicKey::text( const Locale &lang ) const
-//   { return _pimpl->text( lang ); }
+  
+  Pathname PublicKey::path() const
+  { return _pimpl->path(); }
 
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
-///////////////////////////////////////////////////////////////////
-}
\ No newline at end of file
+///////////////////////////////////////////////////////////////////
\ No newline at end of file
index 6742af3..3fc4124 100644 (file)
 #include <string>
 
 #include "zypp/base/PtrTypes.h"
+#include "zypp/base/Exception.h"
 #include "zypp/Pathname.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
-namespace devel
-{
+  
+  
+  class BadKeyException : public Exception
+  {
+    public:
+      /** Ctor taking message.
+     * Use \ref ZYPP_THROW to throw exceptions.
+       */
+      BadKeyException()
+      : Exception( "Bad Key Exception" )
+      {}
+      
+      Pathname keyFile() const
+      { return _keyfile; }
+      
+      /** Ctor taking message.
+       * Use \ref ZYPP_THROW to throw exceptions.
+       */
+      BadKeyException( const std::string & msg_r, const Pathname &keyfile = Pathname() )
+      : Exception( msg_r ), _keyfile(keyfile)
+      {}
+      /** Dtor. */
+      virtual ~BadKeyException() throw() {};
+    private:
+      Pathname _keyfile;
+  };
+  
+  
   ///////////////////////////////////////////////////////////////////
   //
   //   CLASS NAME : PublicKey
@@ -46,13 +73,24 @@ namespace devel
     * \throws when data does not make a key
     */
     PublicKey(const Pathname &file);
+    
     ~PublicKey();
+   
+    bool isValid() const
+    { return ( ! id().empty() && ! fingerprint().empty() && !path().empty() ); }
     
     std::string asString() const;
     std::string armoredData() const;
     std::string id() const;
     std::string name() const;
     std::string fingerprint() const;
+    Pathname path() const; 
+    
+    bool operator==( PublicKey b )
+    { return (b.id() == id()) && (b.fingerprint() == fingerprint() ); }
+    
+    bool operator==( std::string sid )
+    { return sid == id(); }
     
   private:
     /** Pointer to implementation */
@@ -66,6 +104,5 @@ namespace devel
 
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
-}
 ///////////////////////////////////////////////////////////////////
 #endif // ZYPP_PUBLICKEY_H
index d2e7930..a010862 100644 (file)
@@ -270,7 +270,7 @@ namespace zypp
               ZYPP_THROW(Exception("Unable to copy " + filename + " to " + (local_dir + "/DATA").asString()));
             
             if ( filename == "content.key" )
-              z->keyRing()->importKey(contentFileKey(), false);  
+              z->keyRing()->importKey(local_dir + "DATA/content.key", false);  
               
             MIL << "cached " << filename << std::endl;
           }
index f2764c1..31a50b6 100644 (file)
@@ -165,6 +165,7 @@ namespace zypp
           copy_result = filesystem::copy( remote_repomd_key, local_dir + "/repodata/repomd.xml.key");
           if ( copy_result != 0 )
             ZYPP_THROW(Exception("Can't copy " + remote_repomd_key.asString() + " to " + local_dir.asString() + "/repodata/repomd.xml.key"));
+          
           getZYpp()->keyRing()->importKey(local_dir + "/repodata/repomd.xml.key" , false);
         }
 
index 565a584..8b0110c 100644 (file)
@@ -30,6 +30,7 @@
 #include "zypp/Date.h"
 #include "zypp/Pathname.h"
 #include "zypp/PathInfo.h"
+#include "zypp/PublicKey.h"
 
 #include "zypp/target/rpm/RpmDb.h"
 #include "zypp/target/rpm/RpmCallbacks.h"
@@ -66,14 +67,14 @@ namespace zypp {
           disconnect();
         }
 
-        virtual void trustedKeyAdded( const KeyRing &keyring, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+        virtual void trustedKeyAdded( const KeyRing &keyring, const PublicKey &key )
         {
           MIL << "trusted key added to zypp Keyring. Syncronizing keys with rpm keyring" << std::endl;
           _rpmdb.importZyppKeyRingTrustedKeys();
           _rpmdb.exportTrustedKeysInZyppKeyRing();
         }
 
-        virtual void trustedKeyRemoved( const KeyRing &keyring, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
+        virtual void trustedKeyRemoved( const KeyRing &keyring, const PublicKey &key  )
         {
 
         }
@@ -928,37 +929,19 @@ void RpmDb::importZyppKeyRingTrustedKeys()
     std::list<PublicKey>::iterator ik = find( rpm_keys.begin(), rpm_keys.end(), (*it));
     if ( ik != rpm_keys.end() )
     {
-      MIL << "Key " << (*it).id << " (" << (*it).name << ") is already in rpm database." << std::endl;
+      MIL << "Key " << (*it).id() << " (" << (*it).name() << ") is already in rpm database." << std::endl;
     }
     else
     {
-      // key does not exists, we need to import it into rpm
-      // create a temporary file
-      TmpFile file(getZYpp()->tmpPath());
-      // open the file for writing
-      std::ofstream os;
-      try
-      {
-        os.open(file.path().asString().c_str());
-        // dump zypp key into the tmp file
-        getZYpp()->keyRing()->dumpTrustedPublicKey( (*it).id, os );
-        os.close();
-      }
-      catch (std::exception &e)
-      {
-        ERR << "Could not dump key " << (*it).id << " (" << (*it).name << ") in tmp file " << file.path() << std::endl;
-        // just ignore the key
-      }
-
       // now import the key in rpm
       try
       {
-        importPubkey(file.path());
-        MIL << "Trusted key " << (*it).id << " (" << (*it).name << ") imported in rpm database." << std::endl;
+        importPubkey((*it).path());
+        MIL << "Trusted key " << (*it).id() << " (" << (*it).name() << ") imported in rpm database." << std::endl;
       }
       catch (RpmException &e)
       {
-        ERR << "Could not dump key " << (*it).id << " (" << (*it).name << ") in tmp file " << file.path() << std::endl;
+        ERR << "Could not import key " << (*it).id() << " (" << (*it).name() << " from " << (*it).path() << " in rpm database" << std::endl;
       }
     }
   }
@@ -1034,7 +1017,7 @@ list<PublicKey> RpmDb::pubkeys() const
         //MIL << "-----------------------------------------------" << std::endl;
         os.close();
         // read the public key from the dumped file
-        PublicKey key = getZYpp()->keyRing()->readPublicKey(file.path());
+        PublicKey key(file.path());
         ret.push_back(key);
       }
       catch (std::exception &e)