new PublicKey class to replace the dumb struct in KeyRing later
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Tue, 20 Jun 2006 16:40:57 +0000 (16:40 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Tue, 20 Jun 2006 16:40:57 +0000 (16:40 +0000)
zypp/Makefile.am
zypp/PublicKey.cc [new file with mode: 0644]
zypp/PublicKey.h [new file with mode: 0644]

index 5b498a5..350c715 100644 (file)
@@ -87,6 +87,7 @@ pkginclude_HEADERS = NeedAType.h \
        ZYppFactory.h   \
        ZYppCallbacks.h \
        SilentCallbacks.h \
+       PublicKey.h \
        KeyRing.h
 
 ## ##################################################
@@ -160,6 +161,7 @@ lib@PACKAGE@_la_SOURCES = \
        ZYppCommitResult.cc     \
        TranslatedText.cc \
        ZYppFactory.cc \
+       PublicKey.cc \
        KeyRing.cc
 
 
diff --git a/zypp/PublicKey.cc b/zypp/PublicKey.cc
new file mode 100644 (file)
index 0000000..8a6550b
--- /dev/null
@@ -0,0 +1,199 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/PublicKey.cc
+ *
+*/
+#include <iostream>
+//#include "zypp/base/Logger.h"
+
+#include "zypp/base/String.h"
+#include "zypp/PublicKey.h"
+#include "zypp/ExternalProgram.h"
+#include "zypp/TmpPath.h"
+#include "zypp/base/Exception.h"
+
+using std::endl;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   CLASS NAME : PublicKey::Impl
+  //
+  /** PublicKey implementation. */
+  struct PublicKey::Impl
+  {
+    Impl()
+    {}
+
+    Impl(const Pathname &file)
+    {
+    
+    }
+    
+    Impl(const std::istream &data)
+    {}
+    
+    public:
+      /** Offer default Impl. */
+      static shared_ptr<Impl> nullimpl()
+      {
+        static shared_ptr<Impl> _nullimpl( new Impl );
+        return _nullimpl;
+      }
+
+     
+    std::string asString() const
+    {
+      return "GPG KEY";  
+    }
+    
+    std::string armoredData() const
+    { return _data; }
+    
+    std::string id() const
+    { return _id; }
+    
+    std::string name() const
+    { return _name; }
+    
+    std::string fingerprint() const
+    { return _fingerprint; }
+    
+    protected:
+      
+     void readFromFile( const Pathname &keyfile)
+     {
+       filesystem::TmpDir 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>:
+  
+        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" )
+            {
+              _id = what[5];
+              _name = what[10];
+            //return key;
+            }
+            else if ( what[1] == "fpr" )
+            {
+                _fingerprint = what[10];
+            }
+          //dumpRegexpResults(what);
+          }
+        }
+        prog.close();
+        
+        if (_id.size() == 0 )
+          ZYPP_THROW(Exception("Can't read public key from " + keyfile.asString()));
+     }
+    
+  private:
+    std::string _id;
+    std::string _name;
+    std::string _fingerprint;
+    std::string _data;
+  private:
+    friend Impl * rwcowClone<Impl>( const Impl * rhs );
+    /** clone for RWCOW_pointer */
+    Impl * clone() const
+    { return new Impl( *this ); }
+  };
+  ///////////////////////////////////////////////////////////////////
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   METHOD NAME : PublicKey::PublicKey
+  //   METHOD TYPE : Ctor
+  //
+  PublicKey::PublicKey()
+  : _pimpl( Impl::nullimpl() )
+  {}
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   METHOD NAME : PublicKey::PublicKey
+  //   METHOD TYPE : Ctor
+  //
+  PublicKey::PublicKey( const std::istream &data )
+  : _pimpl( new Impl(data) )
+  {}
+
+  PublicKey::PublicKey(   const Pathname &file )
+  : _pimpl( new Impl(file) )
+  {}
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   METHOD NAME : PublicKey::~PublicKey
+  //   METHOD TYPE : Dtor
+  //
+  PublicKey::~PublicKey()
+  {}
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  // Forward to implementation:
+  //
+  ///////////////////////////////////////////////////////////////////
+
+  std::string PublicKey::asString() const
+  {
+    return _pimpl->asString();
+  }
+  
+  std::string PublicKey::armoredData() const
+  { return _pimpl->armoredData(); }
+    
+  std::string PublicKey::id() const
+  { return _pimpl->id(); }
+    
+  std::string PublicKey::name() const
+  { return _pimpl->name(); }
+    
+  std::string PublicKey::fingerprint() const
+  { return _pimpl->fingerprint(); }
+    
+//   std::string PublicKey::text( const Locale &lang ) const
+//   { return _pimpl->text( lang ); }
+
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
diff --git a/zypp/PublicKey.h b/zypp/PublicKey.h
new file mode 100644 (file)
index 0000000..c8514ea
--- /dev/null
@@ -0,0 +1,70 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/PublicKey.h
+ *
+*/
+#ifndef ZYPP_PUBLICKEY_H
+#define ZYPP_PUBLICKEY_H
+
+#include <iosfwd>
+#include <map>
+#include <list>
+#include <set>
+#include <string>
+
+#include "zypp/base/PtrTypes.h"
+#include "zypp/Pathname.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   CLASS NAME : PublicKey
+  //
+  /** Class that represent a GPG Public Key
+  */
+  class PublicKey
+  {
+    friend std::ostream & operator<<( std::ostream & str, const PublicKey & obj );
+
+  public:
+    /** Implementation  */
+    class Impl;
+
+  public:
+    PublicKey();
+   /** Ctor 
+    * \throws when data does not make a key
+    */
+    PublicKey(const std::istream &data);
+    PublicKey(const Pathname &file);
+    ~PublicKey();
+    
+    std::string asString() const;
+    std::string armoredData() const;
+    std::string id() const;
+    std::string name() const;
+    std::string fingerprint() const;
+    
+  private:
+    /** Pointer to implementation */
+    RWCOW_pointer<Impl> _pimpl;
+  };
+  ///////////////////////////////////////////////////////////////////
+
+  /** \relates PublicKey Stream output */
+  inline std::ostream & operator<<( std::ostream & str, const PublicKey & obj )
+  { return str << obj.asString(); }
+
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_PUBLICKEY_H