All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Novotny <minovotn@redhat.com>
To: "'xen-devel@lists.xensource.com'" <xen-devel@lists.xensource.com>
Subject: [PATCH] Pygrub exception trap when import fails
Date: Mon, 07 Sep 2009 10:41:11 +0200	[thread overview]
Message-ID: <4AA4C727.6050002@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 503 bytes --]

Hi all,
this is the patch that fixes the issue when importing 'crypt' module or 
crypt.crypt fails in pygrub. The exception is written on the same line 
like "Failed!" message but only if there is an exception. If there is no 
exception, we don't bother users with details (probably the password 
they entered was wrong) so we just display "Failed!" message. Also, the 
code for hasPassword() was rewritten not to have try/except block here.

Michal

Signed-off-by: Michal Novotny <minovotn@redhat.com>

[-- Attachment #2: xen-pygrub-grub-exception-trap.patch --]
[-- Type: text/x-patch, Size: 2989 bytes --]

diff -r ead107bc25cb tools/pygrub/src/GrubConf.py
--- a/tools/pygrub/src/GrubConf.py	Fri Sep 04 08:43:05 2009 +0100
+++ b/tools/pygrub/src/GrubConf.py	Mon Sep 07 10:36:09 2009 +0200
@@ -158,6 +158,7 @@ class GrubConfigFile(object):
         self.timeout = -1
         self._default = 0
         self.passwordAccess = True
+        self.passExc = None
 
         if fn is not None:
             self.parse()
@@ -197,7 +198,6 @@ class GrubConfigFile(object):
             if self.commands.has_key(com):
                 if self.commands[com] is not None:
                     setattr(self, self.commands[com], arg.strip())
-                    #print "%s = %s => %s" % (com, self.commands[com], arg.strip() )
                 else:
                     logging.info("Ignored directive %s" %(com,))
             else:
@@ -216,25 +216,28 @@ class GrubConfigFile(object):
         self.passwordAccess = val
 
     def hasPassword(self):
-        try:
-            getattr(self, self.commands['password'])
+        return hasattr(self, 'password')
+
+    def checkPassword(self, password):
+        # Always allow if no password defined in grub.conf
+        if not self.hasPassword:
             return True
-        except:
-            return False
-
-    def checkPassword(self, password):
-        try:
-            pwd = getattr(self, self.commands['password']).split()
-            if pwd[0] == '--md5':
+
+        # If we're here, we're having 'password' attribute set
+        pwd = getattr(self, 'password').split()
+
+        # We check whether password is in MD5 hash for comparison
+        if pwd[0] == '--md5':
+            try:
                 import crypt
                 if crypt.crypt(password, pwd[1]) == pwd[1]:
                     return True
-
-            if pwd[0] == password:
-                return True
-
-            return False
-        except:
+            except Exception, e:
+                self.passExc = "Can't verify password: %s" % str(e)
+                return False
+
+        # ... and if not, we compare it as a plain text
+        if pwd[0] == password:
             return True
 
     def set(self, line):
diff -r ead107bc25cb tools/pygrub/src/pygrub
--- a/tools/pygrub/src/pygrub	Fri Sep 04 08:43:05 2009 +0100
+++ b/tools/pygrub/src/pygrub	Mon Sep 07 10:36:09 2009 +0200
@@ -483,7 +483,11 @@ class Grub:
                 pwd = self.text_win.getstr(6, 8)
                 if not self.cf.checkPassword(pwd):
                     self.text_win.addstr(6, 1, "Password: ")
-                    self.text_win.addstr(7, 0, "Failed!")
+                    if self.cf.passExc is not None:
+                        self.text_win.addstr(7, 0, "Exception: %s"
+                                                  % self.cf.passExc)
+                    else:
+                        self.text_win.addstr(7, 0, "Failed!")
                     self.cf.setPasswordAccess( False )
                 else:
                     self.cf.setPasswordAccess( True )

[-- Attachment #3: Type: text/plain, Size: 138 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel

                 reply	other threads:[~2009-09-07  8:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4AA4C727.6050002@redhat.com \
    --to=minovotn@redhat.com \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.