2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/common/passwords.py
|
2006-10-06 01:48:54 +02:00
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2006 Gustavo J. A. M. Carneiro <gjcarneiro AT gmail.com>
|
|
|
|
## Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
## Copyright (C) 2006-2008 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
## Copyright (C) 2007 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Julien Pivotto <roidelapluie AT gmail.com>
|
2008-08-15 19:31:51 +02:00
|
|
|
## Copyright (C) 2008 Stephan Erb <steve-e AT h3c.de>
|
2009-07-15 21:02:47 +02:00
|
|
|
## Copyright (c) 2009 Thorsten Glaser <t.glaser AT tarent.de>
|
2006-10-06 01:48:54 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2006-10-06 01:48:54 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2006-10-06 01:48:54 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2006-10-06 01:48:54 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2006-10-06 01:48:54 +02:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2006-10-06 01:48:54 +02:00
|
|
|
|
|
|
|
__all__ = ['get_password', 'save_password']
|
|
|
|
|
2009-01-19 23:19:43 +01:00
|
|
|
import warnings
|
2006-10-06 01:48:54 +02:00
|
|
|
from common import gajim
|
2009-07-15 21:02:47 +02:00
|
|
|
from common import kwalletbinding
|
2006-10-06 01:48:54 +02:00
|
|
|
|
2006-12-29 19:30:18 +01:00
|
|
|
USER_HAS_GNOMEKEYRING = False
|
|
|
|
USER_USES_GNOMEKEYRING = False
|
2009-07-15 21:02:47 +02:00
|
|
|
USER_HAS_KWALLETCLI = False
|
2007-01-21 16:13:57 +01:00
|
|
|
gnomekeyring = None
|
2006-10-06 01:48:54 +02:00
|
|
|
|
2006-10-11 22:38:23 +02:00
|
|
|
class PasswordStorage(object):
|
|
|
|
def get_password(self, account_name):
|
|
|
|
raise NotImplementedError
|
|
|
|
def save_password(self, account_name, password):
|
|
|
|
raise NotImplementedError
|
2006-11-01 17:49:49 +01:00
|
|
|
|
2006-10-06 01:48:54 +02:00
|
|
|
|
2006-10-11 22:38:23 +02:00
|
|
|
class SimplePasswordStorage(PasswordStorage):
|
2006-10-06 01:48:54 +02:00
|
|
|
def get_password(self, account_name):
|
2006-12-27 22:13:42 +01:00
|
|
|
passwd = gajim.config.get_per('accounts', account_name, 'password')
|
2009-07-15 21:02:47 +02:00
|
|
|
if passwd and (passwd.startswith('gnomekeyring:') or \
|
|
|
|
passwd == '<kwallet>'):
|
|
|
|
# this is not a real password, it's either a gnome
|
|
|
|
# keyring token or stored in the KDE wallet
|
|
|
|
return None
|
2006-12-27 22:13:42 +01:00
|
|
|
else:
|
|
|
|
return passwd
|
2006-10-06 01:48:54 +02:00
|
|
|
|
|
|
|
def save_password(self, account_name, password):
|
2006-10-09 12:17:53 +02:00
|
|
|
gajim.config.set_per('accounts', account_name, 'password', password)
|
2007-11-05 23:41:12 +01:00
|
|
|
if account_name in gajim.connections:
|
|
|
|
gajim.connections[account_name].password = password
|
2006-10-06 01:48:54 +02:00
|
|
|
|
|
|
|
|
2006-10-11 22:38:23 +02:00
|
|
|
class GnomePasswordStorage(PasswordStorage):
|
2006-12-27 22:13:42 +01:00
|
|
|
def __init__(self):
|
2008-05-05 02:05:20 +02:00
|
|
|
self.keyring = gnomekeyring.get_default_keyring_sync()
|
|
|
|
if self.keyring is None:
|
2008-05-04 23:44:05 +02:00
|
|
|
self.keyring = 'default'
|
2006-12-27 22:13:42 +01:00
|
|
|
try:
|
|
|
|
gnomekeyring.create_sync(self.keyring, None)
|
|
|
|
except gnomekeyring.AlreadyExistsError:
|
2006-11-01 17:49:49 +01:00
|
|
|
pass
|
2006-10-06 01:48:54 +02:00
|
|
|
|
|
|
|
def get_password(self, account_name):
|
|
|
|
conf = gajim.config.get_per('accounts', account_name, 'password')
|
2009-07-15 21:02:47 +02:00
|
|
|
if conf is None or conf == '<kwallet>':
|
2006-10-07 13:35:44 +02:00
|
|
|
return None
|
2009-01-19 23:19:43 +01:00
|
|
|
if not conf.startswith('gnomekeyring:'):
|
2006-10-06 01:48:54 +02:00
|
|
|
password = conf
|
|
|
|
## migrate the password over to keyring
|
2006-10-11 22:38:23 +02:00
|
|
|
try:
|
|
|
|
self.save_password(account_name, password, update=False)
|
|
|
|
except gnomekeyring.NoKeyringDaemonError:
|
|
|
|
## no keyring daemon: in the future, stop using it
|
|
|
|
set_storage(SimplePasswordStorage())
|
2006-10-06 01:48:54 +02:00
|
|
|
return password
|
|
|
|
try:
|
2009-01-20 22:24:22 +01:00
|
|
|
server = gajim.config.get_per('accounts', account_name, 'hostname')
|
|
|
|
user = gajim.config.get_per('accounts', account_name, 'name')
|
|
|
|
attributes1 = dict(server=str(server), user=str(user), protocol='xmpp')
|
|
|
|
attributes2 = dict(account_name=str(account_name), gajim=1)
|
2009-01-19 23:19:43 +01:00
|
|
|
try:
|
|
|
|
items = gnomekeyring.find_items_sync(
|
2009-01-20 22:24:22 +01:00
|
|
|
gnomekeyring.ITEM_NETWORK_PASSWORD, attributes1)
|
2009-01-19 23:19:43 +01:00
|
|
|
except gnomekeyring.Error:
|
2009-01-20 22:24:22 +01:00
|
|
|
try:
|
|
|
|
items = gnomekeyring.find_items_sync(
|
|
|
|
gnomekeyring.ITEM_GENERIC_SECRET, attributes2)
|
|
|
|
if items:
|
|
|
|
# We found an old item, move it to new way of storing
|
|
|
|
password = items[0].secret
|
|
|
|
self.save_password(account_name, password)
|
|
|
|
gnomekeyring.item_delete_sync(items[0].keyring,
|
|
|
|
int(items[0].item_id))
|
|
|
|
except gnomekeyring.Error:
|
|
|
|
items = []
|
2009-01-19 23:19:43 +01:00
|
|
|
if len(items) > 1:
|
|
|
|
warnings.warn("multiple gnome keyring items found for account %s;"
|
|
|
|
" trying to use the first one..."
|
|
|
|
% account_name)
|
|
|
|
if items:
|
|
|
|
return items[0].secret
|
|
|
|
else:
|
|
|
|
return None
|
2006-10-06 01:48:54 +02:00
|
|
|
except gnomekeyring.DeniedError:
|
|
|
|
return None
|
2006-10-11 22:38:23 +02:00
|
|
|
except gnomekeyring.NoKeyringDaemonError:
|
|
|
|
## no keyring daemon: in the future, stop using it
|
|
|
|
set_storage(SimplePasswordStorage())
|
|
|
|
return None
|
2006-11-01 17:49:49 +01:00
|
|
|
|
2006-10-06 01:48:54 +02:00
|
|
|
def save_password(self, account_name, password, update=True):
|
2009-01-20 22:24:22 +01:00
|
|
|
server = gajim.config.get_per('accounts', account_name, 'hostname')
|
|
|
|
user = gajim.config.get_per('accounts', account_name, 'name')
|
|
|
|
display_name = _('XMPP account %s@%s') % (user, server)
|
|
|
|
attributes1 = dict(server=str(server), user=str(user), protocol='xmpp')
|
2007-08-08 18:10:38 +02:00
|
|
|
try:
|
|
|
|
auth_token = gnomekeyring.item_create_sync(
|
2009-01-20 22:24:22 +01:00
|
|
|
self.keyring, gnomekeyring.ITEM_NETWORK_PASSWORD,
|
|
|
|
display_name, attributes1, password, update)
|
2007-08-08 18:10:38 +02:00
|
|
|
except gnomekeyring.DeniedError:
|
|
|
|
set_storage(SimplePasswordStorage())
|
|
|
|
storage.save_password(account_name, password)
|
|
|
|
return
|
2009-01-19 23:19:43 +01:00
|
|
|
gajim.config.set_per('accounts', account_name, 'password',
|
|
|
|
'gnomekeyring:')
|
2008-10-07 22:41:59 +02:00
|
|
|
if account_name in gajim.connections:
|
2007-07-24 10:20:06 +02:00
|
|
|
gajim.connections[account_name].password = password
|
2006-10-06 01:48:54 +02:00
|
|
|
|
2009-07-15 21:02:47 +02:00
|
|
|
class KWalletPasswordStorage(PasswordStorage):
|
|
|
|
def get_password(self, account_name):
|
|
|
|
pw = gajim.config.get_per('accounts', account_name, 'password')
|
|
|
|
if not pw or pw.startswith('gnomekeyring:'):
|
|
|
|
# unset, empty or not ours
|
|
|
|
return None
|
|
|
|
if pw != '<kwallet>':
|
|
|
|
# migrate the password
|
|
|
|
if kwalletbinding.kwallet_put('gajim', account_name, pw):
|
|
|
|
gajim.config.set_per('accounts', account_name, 'password',
|
|
|
|
'<kwallet>')
|
|
|
|
else:
|
|
|
|
# stop using the KDE Wallet
|
|
|
|
set_storage(SimplePasswordStorage())
|
|
|
|
return pw
|
|
|
|
pw = kwalletbinding.kwallet_get('gajim', account_name)
|
|
|
|
if pw is None:
|
|
|
|
# stop using the KDE Wallet
|
|
|
|
set_storage(SimplePasswordStorage())
|
|
|
|
if not pw:
|
|
|
|
# False, None, or the empty string
|
|
|
|
return None
|
|
|
|
return pw
|
|
|
|
|
|
|
|
def save_password(self, account_name, password):
|
|
|
|
if not kwalletbinding.kwallet_put('gajim', account_name, password):
|
|
|
|
# stop using the KDE Wallet
|
|
|
|
set_storage(SimplePasswordStorage())
|
|
|
|
storage.save_password(account_name, password)
|
|
|
|
return
|
|
|
|
pwtoken = '<kwallet>'
|
|
|
|
if not password:
|
|
|
|
# no sense in looking up the empty string in the KWallet
|
|
|
|
pwtoken = ''
|
|
|
|
gajim.config.set_per('accounts', account_name, 'password', pwtoken)
|
|
|
|
if account_name in gajim.connections:
|
|
|
|
gajim.connections[account_name].password = password
|
|
|
|
|
|
|
|
|
2006-10-06 01:48:54 +02:00
|
|
|
storage = None
|
|
|
|
def get_storage():
|
|
|
|
global storage
|
2006-10-06 02:04:07 +02:00
|
|
|
if storage is None: # None is only in first time get_storage is called
|
2007-01-21 16:13:57 +01:00
|
|
|
if gajim.config.get('use_gnomekeyring'):
|
|
|
|
global gnomekeyring
|
|
|
|
try:
|
|
|
|
import gnomekeyring
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
global USER_HAS_GNOMEKEYRING
|
|
|
|
global USER_USES_GNOMEKEYRING
|
|
|
|
USER_HAS_GNOMEKEYRING = True
|
|
|
|
if gnomekeyring.is_available():
|
|
|
|
USER_USES_GNOMEKEYRING = True
|
|
|
|
else:
|
|
|
|
USER_USES_GNOMEKEYRING = False
|
2006-10-20 12:14:15 +02:00
|
|
|
if USER_USES_GNOMEKEYRING:
|
2006-10-11 22:38:23 +02:00
|
|
|
try:
|
|
|
|
storage = GnomePasswordStorage()
|
2009-07-15 21:02:47 +02:00
|
|
|
except (gnomekeyring.NoKeyringDaemonError, gnomekeyring.DeniedError):
|
|
|
|
storage = None
|
|
|
|
if storage is None:
|
|
|
|
if gajim.config.get('use_kwalletcli'):
|
|
|
|
global USER_HAS_KWALLETCLI
|
|
|
|
if kwalletbinding.kwallet_available():
|
|
|
|
USER_HAS_KWALLETCLI = True
|
|
|
|
if USER_HAS_KWALLETCLI:
|
|
|
|
storage = KWalletPasswordStorage()
|
|
|
|
if storage is None:
|
2006-10-06 01:48:54 +02:00
|
|
|
storage = SimplePasswordStorage()
|
|
|
|
return storage
|
|
|
|
|
2006-10-11 22:38:23 +02:00
|
|
|
def set_storage(storage_):
|
|
|
|
global storage
|
|
|
|
storage = storage_
|
|
|
|
|
|
|
|
|
2006-10-06 01:48:54 +02:00
|
|
|
def get_password(account_name):
|
|
|
|
return get_storage().get_password(account_name)
|
|
|
|
|
|
|
|
def save_password(account_name, password):
|
|
|
|
return get_storage().save_password(account_name, password)
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2008-08-15 05:20:23 +02:00
|
|
|
# vim: se ts=3:
|