From fef742c86367170dfaf311f3ea3ba90a70314b56 Mon Sep 17 00:00:00 2001 From: Stephan Erb Date: Fri, 28 Dec 2007 18:49:28 +0000 Subject: [PATCH] Refactor the way we decide whether GPG is usable or not: Ability to change keys when no valid passphrase could be entered. See #1210. Workaround for unavailable keys. Remove useless except block: import of GnuPGInterface is the same on Debian and non Debian systems. --- src/common/GnuPG.py | 28 ++----------------- src/common/connection.py | 19 +++++++------ src/common/connection_handlers.py | 5 ++-- src/common/gajim.py | 11 ++++++++ .../zeroconf/connection_handlers_zeroconf.py | 3 +- src/common/zeroconf/connection_zeroconf.py | 19 +++++++------ src/config.py | 6 ++-- src/features_window.py | 4 +-- 8 files changed, 42 insertions(+), 53 deletions(-) diff --git a/src/common/GnuPG.py b/src/common/GnuPG.py index 8870b6bd3..c7603b8b4 100644 --- a/src/common/GnuPG.py +++ b/src/common/GnuPG.py @@ -28,24 +28,12 @@ ## along with Gajim. If not, see . ## -import os +import gajim from os import tmpfile from common import helpers -USE_GPG = True - -try: - import GnuPGInterface # Debian package doesn't distribute 'our' file -except ImportError: - try: - from common import GnuPGInterface # use 'our' file - except ImportError: - USE_GPG = False # user can't do OpenGPG only if he or she removed the file! - -else: - status = os.system('gpg -h >/dev/null 2>&1') - if status != 0: - USE_GPG = False +if gajim.HAVE_GPG: + import GnuPGInterface class GnuPG(GnuPGInterface.GnuPG): def __init__(self, use_agent = False): @@ -88,8 +76,6 @@ else: return resp def encrypt(self, str, recipients): - if not USE_GPG: - return str, 'GnuPG not usable' self.options.recipients = recipients # a list! proc = self.run(['--encrypt'], create_fhs=['stdin', 'stdout', 'status', @@ -125,8 +111,6 @@ else: return self._stripHeaderFooter(output), error def decrypt(self, str, keyID): - if not USE_GPG: - return str proc = self.run(['--decrypt', '-q', '-u %s'%keyID], create_fhs=['stdin', 'stdout']) enc = self._addHeaderFooter(str, 'MESSAGE') proc.handles['stdin'].write(enc) @@ -140,8 +124,6 @@ else: return output def sign(self, str, keyID): - if not USE_GPG: - return str proc = self.run(['-b', '-u %s'%keyID], create_fhs=['stdin', 'stdout', 'status', 'stderr']) proc.handles['stdin'].write(str) try: @@ -170,8 +152,6 @@ else: return 'BAD_PASSPHRASE' def verify(self, str, sign): - if not USE_GPG: - return str if str == None: return '' f = tmpfile() @@ -200,8 +180,6 @@ else: return keyid def get_keys(self, secret = False): - if not USE_GPG: - return {} if secret: opt = '--list-secret-keys' else: diff --git a/src/common/connection.py b/src/common/connection.py index ae697fd7a..0f8441e17 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -48,7 +48,6 @@ from common import passwords from common import exceptions from connection_handlers import * -USE_GPG = GnuPG.USE_GPG from common.rst_xhtml_generator import create_xhtml @@ -105,7 +104,9 @@ class Connection(ConnectionHandlers): self.last_connection = None # last ClientCommon instance self.is_zeroconf = False self.gpg = None - if USE_GPG: + self.USE_GPG = False + if gajim.HAVE_GPG: + self.USE_GPG = True self.gpg = GnuPG.GnuPG(gajim.config.get('use_gpg_agent')) self.status = '' self.priority = gajim.get_priority(name, 'offline') @@ -179,7 +180,7 @@ class Connection(ConnectionHandlers): self.dispatch('STATUS', 'connecting') self.retrycount += 1 self.on_connect_auth = self._init_roster - self.connect_and_init(self.old_show, self.status, self.gpg != None) + self.connect_and_init(self.old_show, self.status, self.USE_GPG) else: # reconnect succeeded self.time_to_reconnect = None @@ -268,7 +269,8 @@ class Connection(ConnectionHandlers): if not common.xmpp.isResultNode(result): self.dispatch('ACC_NOT_OK', (result.getError())) return - if USE_GPG: + if gajim.HAVE_GPG: + self.USE_GPG = True self.gpg = GnuPG.GnuPG(gajim.config.get( 'use_gpg_agent')) self.dispatch('ACC_OK', (self.new_account_info)) @@ -796,7 +798,7 @@ class Connection(ConnectionHandlers): callback is the function to call when user give the passphrase''' signed = '' keyID = gajim.config.get_per('accounts', self.name, 'keyid') - if keyID and self.gpg: + if keyID and self.USE_GPG: use_gpg_agent = gajim.config.get('use_gpg_agent') if self.gpg.passphrase is None and not use_gpg_agent: # We didn't set a passphrase @@ -804,7 +806,7 @@ class Connection(ConnectionHandlers): if self.gpg.passphrase is not None or use_gpg_agent: signed = self.gpg.sign(msg, keyID) if signed == 'BAD_PASSPHRASE': - self.gpg = None + self.USE_GPG = False signed = '' self.dispatch('BAD_PASSPHRASE', ()) return signed @@ -881,7 +883,8 @@ class Connection(ConnectionHandlers): safe_substitute({ 'hostname': socket.gethostname() }) - if USE_GPG: + if gajim.HAVE_GPG: + self.USE_GPG = True self.gpg = GnuPG.GnuPG(gajim.config.get('use_gpg_agent')) self.connect_and_init(show, msg, sign_msg) @@ -958,7 +961,7 @@ class Connection(ConnectionHandlers): fjid += '/' + resource msgtxt = msg msgenc = '' - if keyID and self.gpg: + if keyID and self.USE_GPG: #encrypt msgenc, error = self.gpg.encrypt(msg, [keyID]) if msgenc and not error: diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py index 27a5ffb80..33af3e670 100644 --- a/src/common/connection_handlers.py +++ b/src/common/connection_handlers.py @@ -35,7 +35,6 @@ from calendar import timegm import socks5 import common.xmpp -from common import GnuPG from common import helpers from common import gajim from common import atom @@ -1629,7 +1628,7 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco, if not user_nick: user_nick = '' - if encTag and GnuPG.USE_GPG: + if encTag and self.USE_GPG: #decrypt encmsg = encTag.getData() @@ -1901,7 +1900,7 @@ returns the session that we last sent a message to.''' except: prio = 0 keyID = '' - if sigTag and self.gpg: + if sigTag and self.USE_GPG: # verify sigmsg = sigTag.getData() keyID = self.gpg.verify(status, sigmsg) diff --git a/src/common/gajim.py b/src/common/gajim.py index dcb294034..25e152635 100644 --- a/src/common/gajim.py +++ b/src/common/gajim.py @@ -154,6 +154,17 @@ try: except ImportError: HAVE_PYSEXY = False +HAVE_GPG = True +try: + import GnuPGInterface +except ImportError: + HAVE_GPG = False +else: + import os + status = os.system('gpg -h >/dev/null 2>&1') + if status != 0: + HAVE_GPG = False + def get_nick_from_jid(jid): pos = jid.find('@') return jid[:pos] diff --git a/src/common/zeroconf/connection_handlers_zeroconf.py b/src/common/zeroconf/connection_handlers_zeroconf.py index 8ccc68d64..c955c1c1e 100644 --- a/src/common/zeroconf/connection_handlers_zeroconf.py +++ b/src/common/zeroconf/connection_handlers_zeroconf.py @@ -32,7 +32,6 @@ from calendar import timegm from common import socks5 import common.xmpp -from common import GnuPG from common import helpers from common import gajim from common.zeroconf import zeroconf @@ -726,7 +725,7 @@ class ConnectionHandlersZeroconf(ConnectionVcard, ConnectionBytestream): if not user_nick: user_nick = '' - if encTag and GnuPG.USE_GPG: + if encTag and self.USE_GPG: #decrypt encmsg = encTag.getData() diff --git a/src/common/zeroconf/connection_zeroconf.py b/src/common/zeroconf/connection_zeroconf.py index 1a497e3eb..1805587b1 100644 --- a/src/common/zeroconf/connection_zeroconf.py +++ b/src/common/zeroconf/connection_zeroconf.py @@ -49,8 +49,6 @@ from common.zeroconf import client_zeroconf from common.zeroconf import zeroconf from connection_handlers_zeroconf import * -USE_GPG = GnuPG.USE_GPG - class ConnectionZeroconf(ConnectionHandlersZeroconf): '''Connection class''' def __init__(self, name): @@ -62,7 +60,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): self.connected = 0 # offline self.connection = None self.gpg = None - if USE_GPG: + self.USE_GPG = False + if gajim.HAVE_GPG: + self.USE_GPG = True self.gpg = GnuPG.GnuPG(gajim.config.get('use_gpg_agent')) self.is_zeroconf = True self.privacy_rules_supported = False @@ -91,7 +91,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): self.activity = {} # Do we continue connection when we get roster (send presence,get vcard...) self.continue_connect_info = None - if USE_GPG: + if gajim.HAVE_GPG: + self.USE_GPG = True self.gpg = GnuPG.GnuPG(gajim.config.get('use_gpg_agent')) self.get_config_values_or_default() @@ -163,7 +164,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): def get_signed_msg(self, msg): signed = '' keyID = gajim.config.get_per('accounts', self.name, 'keyid') - if keyID and USE_GPG: + if keyID and self.USE_GPG: use_gpg_agent = gajim.config.get('use_gpg_agent') if self.connected < 2 and self.gpg.passphrase is None and \ not use_gpg_agent: @@ -373,7 +374,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): msgtxt = msg msgenc = '' - if keyID and USE_GPG: + if keyID and self.USE_GPG: # encrypt msgenc, error = self.gpg.encrypt(msg, [keyID]) if msgenc and not error: @@ -510,7 +511,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): gajim.log.debug('This should not happen (send_agent_status)') def gpg_passphrase(self, passphrase): - if USE_GPG: + if self.gpg: use_gpg_agent = gajim.config.get('use_gpg_agent') if use_gpg_agent: self.gpg.passphrase = None @@ -518,13 +519,13 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): self.gpg.passphrase = passphrase def ask_gpg_keys(self): - if USE_GPG: + if self.gpg: keys = self.gpg.get_keys() return keys return None def ask_gpg_secrete_keys(self): - if USE_GPG: + if self.gpg: keys = self.gpg.get_secret_keys() return keys return None diff --git a/src/config.py b/src/config.py index 8f92d5da0..c08d81a16 100644 --- a/src/config.py +++ b/src/config.py @@ -2079,8 +2079,7 @@ class AccountsWindow: # self.current_account is None and/or gajim.connections is {} else: - from common import GnuPG - if GnuPG.USE_GPG: + if gajim.HAVE_GPG: secret_keys = GnuPG.GnuPG().get_secret_keys() else: secret_keys = [] @@ -3674,8 +3673,7 @@ class ZeroconfPropertiesWindow: # self.account is None and/or gajim.connections is {} else: - from common import GnuPG - if GnuPG.USE_GPG: + if gajim.HAVE_GPG: secret_keys = GnuPG.GnuPG().get_secret_keys() else: secret_keys = [] diff --git a/src/features_window.py b/src/features_window.py index 2403c2a43..9953e7f91 100644 --- a/src/features_window.py +++ b/src/features_window.py @@ -179,8 +179,8 @@ class FeaturesWindow: def gpg_available(self): if os.name == 'nt': return False - from common import GnuPG - return GnuPG.USE_GPG + from common import gajim + return gajim.HAVE_GPG def network_manager_available(self): if os.name == 'nt':