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.
This commit is contained in:
Stephan Erb 2007-12-28 18:49:28 +00:00
parent f671b9bff7
commit fef742c863
8 changed files with 42 additions and 53 deletions

View File

@ -28,24 +28,12 @@
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
##
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:

View File

@ -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:

View File

@ -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)

View File

@ -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]

View File

@ -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()

View File

@ -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

View File

@ -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 = []

View File

@ -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':