Bladeren bron

- correct handling of printing and listing
- add function zero message
- TODO: fix ui classes for MacOS

oz123 11 jaren geleden
bovenliggende
commit
b4d48c0984
2 gewijzigde bestanden met toevoegingen van 78 en 2 verwijderingen
  1. 64 2
      pwman/ui/cli.py
  2. 14 0
      pwman/util/crypto.py

+ 64 - 2
pwman/ui/cli.py

@@ -30,6 +30,7 @@ from pwman.data.nodes import Node
 from pwman.data.nodes import NewNode
 from pwman.data.nodes import NewNode
 from pwman.data.tags import Tag
 from pwman.data.tags import Tag
 from pwman.util.crypto import CryptoEngine
 from pwman.util.crypto import CryptoEngine
+from pwman.util.crypto import zerome
 #, CryptoBadKeyException, \
 #, CryptoBadKeyException, \
 #     CryptoPasswordMismatchException
 #     CryptoPasswordMismatchException
 from pwman.util.callback import Callback
 from pwman.util.callback import Callback
@@ -744,6 +745,58 @@ class PwmanCliNew(PwmanCli):
     all the methods related to tags, and
     all the methods related to tags, and
     newer Node format, so backward compatability is kept...
     newer Node format, so backward compatability is kept...
     """
     """
+    
+    def print_node(self, node):
+        width = str(_defaultwidth)
+        print "Node %d." % (node.get_id())
+        print ("%"+width+"s %s") % (typeset("Username:", ANSI.Red),
+                                    node.get_username())
+        print ("%"+width+"s %s") % (typeset("Password:", ANSI.Red),
+                                    node.get_password())
+        print ("%"+width+"s %s") % (typeset("Url:", ANSI.Red),
+                                    node.get_url())
+        print ("%"+width+"s %s") % (typeset("Notes:", ANSI.Red),
+                                    node.get_notes())
+        print typeset("Tags: ", ANSI.Red),
+        for t in node.get_tags():
+            print " %s " % t
+        print
+
+        def heardEnter():
+            i, o, e = uselect.select([sys.stdin], [], [], 0.0001)
+            for s in i:
+                if s == sys.stdin:
+                    sys.stdin.readline()
+                    return True
+                return False
+
+        def heardEnterWin():
+            import msvcrt
+            c = msvcrt.kbhit()
+            if c == 1:
+                ret = msvcrt.getch()
+                if ret is not None:
+                    return True
+            return False
+
+        def waituntil_enter(somepredicate, timeout, period=0.25):
+            mustend = time.time() + timeout
+            while time.time() < mustend:
+                cond = somepredicate()
+                if cond:
+                    break
+                time.sleep(period)
+            self.do_cls('')
+
+        flushtimeout = int(config.get_value("Global", "cls_timeout"))
+        if flushtimeout > 0:
+            if sys.platform != 'win32':
+                print "Type Enter to flush screen (autoflash in 5 sec.)"
+                waituntil_enter(heardEnter, flushtimeout)
+            else:
+                print "Press any key to flush screen (autoflash in 5 sec.)"
+                waituntil_enter(heardEnterWin, flushtimeout)
+                
     def do_tags(self, arg):
     def do_tags(self, arg):
         tags = self._db.listtags()
         tags = self._db.listtags()
         if len(tags) > 0:
         if len(tags) > 0:
@@ -789,8 +842,6 @@ class PwmanCliNew(PwmanCli):
         return tags
         return tags
 
 
     def do_list(self, args):
     def do_list(self, args):
-        import ipdb
-        ipdb.set_trace()
         if len(args.split()) > 0:
         if len(args.split()) > 0:
             self.do_clear('')
             self.do_clear('')
             self.do_filter(args)
             self.do_filter(args)
@@ -875,6 +926,17 @@ class PwmanCliNew(PwmanCli):
         except Exception, e:
         except Exception, e:
             self.error(e)
             self.error(e)
 
 
+    def do_print(self, arg):
+        for i in self.get_ids(arg):
+            try:
+                node = self._db.getnodes([i])
+                self.print_node(node[0])
+                # when done with node erase it
+                zerome(node[0]._password)
+            except Exception, e:
+                self.error(e)
+                
+                
 class PwmanCliMac(PwmanCli):
 class PwmanCliMac(PwmanCli):
     """
     """
     inherit from PwmanCli, override the right functions...
     inherit from PwmanCli, override the right functions...

+ 14 - 0
pwman/util/crypto.py

@@ -57,6 +57,20 @@ from pwman.util.callback import Callback
 import pwman.util.config as config
 import pwman.util.config as config
 import cPickle
 import cPickle
 import time
 import time
+import sys
+import ctypes
+
+def zerome(string):
+    """
+    securely erase strings ...
+    for windows: ctypes.cdll.msvcrt.memset
+    """
+    bufsize = len(string) + 1
+    offset = sys.getsizeof(string) - bufsize
+    ctypes.memset(id(string) + offset, 0, bufsize)
+
+# Use this to tell if crypto is successful or not
+_TAG = "PWMANCRYPTO"
 
 
 _INSTANCE = None
 _INSTANCE = None