code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503
5 changed files with 21394 additions and 10488 deletions

View file

@ -18,6 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.tools.common import regex
from sat.core import exceptions
@ -27,7 +28,7 @@ import mimetypes
import os.path
import time
DEFAULT_EXT = '.raw'
DEFAULT_EXT = ".raw"
class Cache(object):
@ -39,11 +40,11 @@ class Cache(object):
if None, the cache will be common for all profiles
"""
self.profile = profile
path_elts = [host.memory.getConfig('', 'local_dir'), C.CACHE_DIR]
path_elts = [host.memory.getConfig("", "local_dir"), C.CACHE_DIR]
if profile:
path_elts.extend([u'profiles',regex.pathEscape(profile)])
path_elts.extend([u"profiles", regex.pathEscape(profile)])
else:
path_elts.append(u'common')
path_elts.append(u"common")
self.cache_dir = os.path.join(*path_elts)
if not os.path.exists(self.cache_dir):
@ -54,8 +55,10 @@ class Cache(object):
@param filename(unicode): cached file name (cache data or actual file)
"""
if not filename or u'/' in filename:
log.error(u"invalid char found in file name, hack attempt? name:{}".format(filename))
if not filename or u"/" in filename:
log.error(
u"invalid char found in file name, hack attempt? name:{}".format(filename)
)
raise exceptions.DataError(u"Invalid char found")
return os.path.join(self.cache_dir, filename)
@ -76,26 +79,27 @@ class Cache(object):
return None
try:
with open(cache_url, 'rb') as f:
with open(cache_url, "rb") as f:
cache_data = pickle.load(f)
except IOError:
log.warning(u"can't read cache at {}".format(cache_url))
return None
except pickle.UnpicklingError:
log.warning(u'invalid cache found at {}'.format(cache_url))
log.warning(u"invalid cache found at {}".format(cache_url))
return None
try:
eol = cache_data['eol']
eol = cache_data["eol"]
except KeyError:
log.warning(u'no End Of Life found for cached file {}'.format(uid))
log.warning(u"no End Of Life found for cached file {}".format(uid))
eol = 0
if eol < time.time():
log.debug(u"removing expired cache (expired for {}s)".format(
time.time() - eol))
log.debug(
u"removing expired cache (expired for {}s)".format(time.time() - eol)
)
return None
cache_data['path'] = self.getPath(cache_data['filename'])
cache_data["path"] = self.getPath(cache_data["filename"])
return cache_data
def getFilePath(self, uid):
@ -107,7 +111,7 @@ class Cache(object):
"""
metadata = self.getMetadata(uid)
if metadata is not None:
return metadata['path']
return metadata["path"]
def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
"""create cache metadata and file object to use for actual data
@ -130,24 +134,27 @@ class Cache(object):
if mime_type:
ext = mimetypes.guess_extension(mime_type, strict=False)
if ext is None:
log.warning(u"can't find extension for MIME type {}".format(mime_type))
log.warning(
u"can't find extension for MIME type {}".format(mime_type)
)
ext = DEFAULT_EXT
elif ext == u'.jpe':
ext = u'.jpg'
elif ext == u".jpe":
ext = u".jpg"
else:
ext = DEFAULT_EXT
mime_type = None
filename = uid + ext
if max_age is None:
max_age = C.DEFAULT_MAX_AGE
cache_data = {u'source': source,
u'filename': filename,
u'eol': int(time.time()) + max_age,
u'mime_type': mime_type,
}
cache_data = {
u"source": source,
u"filename": filename,
u"eol": int(time.time()) + max_age,
u"mime_type": mime_type,
}
file_path = self.getPath(filename)
with open(cache_url, 'wb') as f:
with open(cache_url, "wb") as f:
pickle.dump(cache_data, f, protocol=2)
return open(file_path, 'wb')
return open(file_path, "wb")

View file

@ -47,13 +47,17 @@ class BlockCipher(object):
@param leave_empty (bool): if True, empty text will be returned "as is"
@return: Deferred: base-64 encoded str
"""
if leave_empty and text == '':
if leave_empty and text == "":
return succeed(text)
iv = BlockCipher.getRandomKey()
key = key.encode('utf-8')
key = key[:BlockCipher.MAX_KEY_SIZE] if len(key) >= BlockCipher.MAX_KEY_SIZE else BlockCipher.pad(key)
key = key.encode("utf-8")
key = (
key[: BlockCipher.MAX_KEY_SIZE]
if len(key) >= BlockCipher.MAX_KEY_SIZE
else BlockCipher.pad(key)
)
cipher = AES.new(key, AES.MODE_CFB, iv)
d = deferToThread(cipher.encrypt, BlockCipher.pad(text.encode('utf-8')))
d = deferToThread(cipher.encrypt, BlockCipher.pad(text.encode("utf-8")))
d.addCallback(lambda ciphertext: b64encode(iv + ciphertext))
return d
@ -68,12 +72,19 @@ class BlockCipher(object):
@param leave_empty (bool): if True, empty ciphertext will be returned "as is"
@return: Deferred: str or None if the password could not be decrypted
"""
if leave_empty and ciphertext == '':
return succeed('')
if leave_empty and ciphertext == "":
return succeed("")
ciphertext = b64decode(ciphertext)
iv, ciphertext = ciphertext[:BlockCipher.IV_SIZE], ciphertext[BlockCipher.IV_SIZE:]
key = key.encode('utf-8')
key = key[:BlockCipher.MAX_KEY_SIZE] if len(key) >= BlockCipher.MAX_KEY_SIZE else BlockCipher.pad(key)
iv, ciphertext = (
ciphertext[: BlockCipher.IV_SIZE],
ciphertext[BlockCipher.IV_SIZE :],
)
key = key.encode("utf-8")
key = (
key[: BlockCipher.MAX_KEY_SIZE]
if len(key) >= BlockCipher.MAX_KEY_SIZE
else BlockCipher.pad(key)
)
cipher = AES.new(key, AES.MODE_CFB, iv)
d = deferToThread(cipher.decrypt, ciphertext)
d.addCallback(lambda text: BlockCipher.unpad(text))
@ -81,7 +92,7 @@ class BlockCipher(object):
# a decrypted empty value and a decryption failure... both return
# the empty value. Fortunately, we detect empty passwords beforehand
# thanks to the "leave_empty" parameter which is used by default.
d.addCallback(lambda text: text.decode('utf-8') if text else None)
d.addCallback(lambda text: text.decode("utf-8") if text else None)
return d
@classmethod
@ -108,7 +119,7 @@ class BlockCipher(object):
@classmethod
def unpad(self, s):
"""Method from http://stackoverflow.com/a/12525165"""
return s[0:-ord(s[-1])]
return s[0 : -ord(s[-1])]
class PasswordHasher(object):
@ -124,9 +135,13 @@ class PasswordHasher(object):
@param leave_empty (bool): if True, empty password will be returned "as is"
@return: Deferred: base-64 encoded str
"""
if leave_empty and password == '':
if leave_empty and password == "":
return succeed(password)
salt = b64decode(salt)[:PasswordHasher.SALT_LEN] if salt else urandom(PasswordHasher.SALT_LEN)
salt = (
b64decode(salt)[: PasswordHasher.SALT_LEN]
if salt
else urandom(PasswordHasher.SALT_LEN)
)
d = deferToThread(PBKDF2, password, salt)
d.addCallback(lambda hashed: b64encode(salt + hashed))
return d
@ -139,7 +154,7 @@ class PasswordHasher(object):
@param hashed (str): the hash of the password
@return: Deferred: boolean
"""
leave_empty = hashed == ''
leave_empty = hashed == ""
d = PasswordHasher.hash(attempt, hashed, leave_empty)
d.addCallback(lambda hashed_attempt: hashed_attempt == hashed)
return d

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _
from sat.core import exceptions
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.words.protocols.jabber import jid
from twisted.words.protocols.jabber.error import StanzaError
@ -35,7 +36,8 @@ from hashlib import sha1
TIMEOUT = 15
CAP_HASH_ERROR = 'ERROR'
CAP_HASH_ERROR = "ERROR"
class HashGenerationError(Exception):
pass
@ -46,10 +48,10 @@ class ByteIdentity(object):
def __init__(self, identity, lang=None):
assert isinstance(identity, disco.DiscoIdentity)
self.category = identity.category.encode('utf-8')
self.idType = identity.type.encode('utf-8')
self.name = identity.name.encode('utf-8') if identity.name else ''
self.lang = lang.encode('utf-8') if lang is not None else ''
self.category = identity.category.encode("utf-8")
self.idType = identity.type.encode("utf-8")
self.name = identity.name.encode("utf-8") if identity.name else ""
self.lang = lang.encode("utf-8") if lang is not None else ""
def __str__(self):
return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name)
@ -63,8 +65,8 @@ class HashManager(object):
def __init__(self, persistent):
self.hashes = {
CAP_HASH_ERROR: disco.DiscoInfo(), # used when we can't get disco infos
}
CAP_HASH_ERROR: disco.DiscoInfo() # used when we can't get disco infos
}
self.persistent = persistent
def __getitem__(self, key):
@ -86,12 +88,16 @@ class HashManager(object):
element = xml_tools.ElementParser()(xml)
disco_info = disco.DiscoInfo.fromElement(element)
if not disco_info.features and not disco_info.identities:
log.warning(_(u"no feature/identity found in disco element (hash: {cap_hash}), ignoring: {xml}").format(
cap_hash=hash_, xml=xml))
log.warning(
_(
u"no feature/identity found in disco element (hash: {cap_hash}), ignoring: {xml}"
).format(cap_hash=hash_, xml=xml)
)
else:
self.hashes[hash_] = disco_info
log.info(u"Disco hashes loaded")
d = self.persistent.load()
d.addCallback(fillHashes)
return d
@ -110,7 +116,7 @@ class Discovery(object):
return self.hashes.load()
@defer.inlineCallbacks
def hasFeature(self, client, feature, jid_=None, node=u''):
def hasFeature(self, client, feature, jid_=None, node=u""):
"""Tell if an entity has the required feature
@param feature: feature namespace
@ -122,7 +128,7 @@ class Discovery(object):
defer.returnValue(feature in disco_infos.features)
@defer.inlineCallbacks
def checkFeature(self, client, feature, jid_=None, node=u''):
def checkFeature(self, client, feature, jid_=None, node=u""):
"""Like hasFeature, but raise an exception is feature is not Found
@param feature: feature namespace
@ -136,7 +142,7 @@ class Discovery(object):
raise failure.Failure(exceptions.FeatureNotFound)
@defer.inlineCallbacks
def checkFeatures(self, client, features, jid_=None, identity=None, node=u''):
def checkFeatures(self, client, features, jid_=None, identity=None, node=u""):
"""Like checkFeature, but check several features at once, and check also identity
@param features(iterable[unicode]): features to check
@ -153,7 +159,7 @@ class Discovery(object):
if identity is not None and identity not in disco_infos.identities:
raise failure.Failure(exceptions.FeatureNotFound())
def getInfos(self, client, jid_=None, node=u'', use_cache=True):
def getInfos(self, client, jid_=None, node=u"", use_cache=True):
"""get disco infos from jid_, filling capability hash if needed
@param jid_: jid of the target, or None for profile's server
@ -167,14 +173,19 @@ class Discovery(object):
if not use_cache:
# we ignore cache, so we pretend we haven't found it
raise KeyError
cap_hash = self.host.memory.getEntityData(jid_, [C.ENTITY_CAP_HASH], client.profile)[C.ENTITY_CAP_HASH]
cap_hash = self.host.memory.getEntityData(
jid_, [C.ENTITY_CAP_HASH], client.profile
)[C.ENTITY_CAP_HASH]
except (KeyError, exceptions.UnknownEntityError):
# capability hash is not available, we'll compute one
def infosCb(disco_infos):
cap_hash = self.generateHash(disco_infos)
self.hashes[cap_hash] = disco_infos
self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile)
self.host.memory.updateEntityData(
jid_, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile
)
return disco_infos
def infosEb(fail):
if fail.check(defer.CancelledError):
reason = u"request time-out"
@ -183,10 +194,17 @@ class Discovery(object):
reason = unicode(fail.value)
except AttributeError:
reason = unicode(fail)
log.warning(u"Error while requesting disco infos from {jid}: {reason}".format(jid=jid_.full(), reason=reason))
self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, CAP_HASH_ERROR, profile_key=client.profile)
log.warning(
u"Error while requesting disco infos from {jid}: {reason}".format(
jid=jid_.full(), reason=reason
)
)
self.host.memory.updateEntityData(
jid_, C.ENTITY_CAP_HASH, CAP_HASH_ERROR, profile_key=client.profile
)
disco_infos = self.hashes[CAP_HASH_ERROR]
return disco_infos
d = client.disco.requestInfo(jid_, nodeIdentifier=node)
d.addCallback(infosCb)
d.addErrback(infosEb)
@ -196,7 +214,7 @@ class Discovery(object):
return defer.succeed(disco_infos)
@defer.inlineCallbacks
def getItems(self, client, jid_=None, node=u'', use_cache=True):
def getItems(self, client, jid_=None, node=u"", use_cache=True):
"""get disco items from jid_, cache them for our own server
@param jid_(jid.JID): jid of the target, or None for profile's server
@ -211,7 +229,9 @@ class Discovery(object):
if jid_ == server_jid and not node:
# we cache items only for our own server and if node is not set
try:
items = self.host.memory.getEntityData(jid_, ["DISCO_ITEMS"], client.profile)["DISCO_ITEMS"]
items = self.host.memory.getEntityData(
jid_, ["DISCO_ITEMS"], client.profile
)["DISCO_ITEMS"]
log.debug(u"[%s] disco items are in cache" % jid_.full())
if not use_cache:
# we ignore cache, so we pretend we haven't found it
@ -219,22 +239,28 @@ class Discovery(object):
except (KeyError, exceptions.UnknownEntityError):
log.debug(u"Caching [%s] disco items" % jid_.full())
items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
self.host.memory.updateEntityData(jid_, "DISCO_ITEMS", items, profile_key=client.profile)
self.host.memory.updateEntityData(
jid_, "DISCO_ITEMS", items, profile_key=client.profile
)
else:
try:
items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
except StanzaError as e:
log.warning(u"Error while requesting items for {jid}: {reason}"
.format(jid=jid_.full(), reason=e.condition))
log.warning(
u"Error while requesting items for {jid}: {reason}".format(
jid=jid_.full(), reason=e.condition
)
)
items = disco.DiscoItems()
defer.returnValue(items)
def _infosEb(self, failure_, entity_jid):
failure_.trap(StanzaError)
log.warning(_(u"Error while requesting [%(jid)s]: %(error)s") % {'jid': entity_jid.full(),
'error': failure_.getErrorMessage()})
log.warning(
_(u"Error while requesting [%(jid)s]: %(error)s")
% {"jid": entity_jid.full(), "error": failure_.getErrorMessage()}
)
def findServiceEntity(self, client, category, type_, jid_=None):
"""Helper method to find first available entity from findServiceEntities
@ -265,14 +291,18 @@ class Discovery(object):
defers_list = []
for item in items:
info_d = self.getInfos(client, item.entity)
info_d.addCallbacks(infosCb, self._infosEb, [item.entity], None, [item.entity])
info_d.addCallbacks(
infosCb, self._infosEb, [item.entity], None, [item.entity]
)
defers_list.append(info_d)
return defer.DeferredList(defers_list)
d = self.getItems(client, jid_)
d.addCallback(gotItems)
d.addCallback(lambda dummy: found_entities)
reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout
reactor.callLater(
TIMEOUT, d.cancel
) # FIXME: one bad service make a general timeout
return d
def findFeaturesSet(self, client, features, identity=None, jid_=None):
@ -291,7 +321,7 @@ class Discovery(object):
def infosCb(infos, entity):
if entity is None:
log.warning(_(u'received an item without jid'))
log.warning(_(u"received an item without jid"))
return
if identity is not None and identity not in infos.identities:
return
@ -309,7 +339,9 @@ class Discovery(object):
d = self.getItems(client, jid_)
d.addCallback(gotItems)
d.addCallback(lambda dummy: found_entities)
reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout
reactor.callLater(
TIMEOUT, d.cancel
) # FIXME: one bad service make a general timeout
return d
def generateHash(self, services):
@ -320,25 +352,35 @@ class Discovery(object):
"""
s = []
byte_identities = [ByteIdentity(service) for service in services if isinstance(service, disco.DiscoIdentity)] # FIXME: lang must be managed here
byte_identities = [
ByteIdentity(service)
for service in services
if isinstance(service, disco.DiscoIdentity)
] # FIXME: lang must be managed here
byte_identities.sort(key=lambda i: i.lang)
byte_identities.sort(key=lambda i: i.idType)
byte_identities.sort(key=lambda i: i.category)
for identity in byte_identities:
s.append(str(identity))
s.append('<')
byte_features = [service.encode('utf-8') for service in services if isinstance(service, disco.DiscoFeature)]
s.append("<")
byte_features = [
service.encode("utf-8")
for service in services
if isinstance(service, disco.DiscoFeature)
]
byte_features.sort() # XXX: the default sort has the same behaviour as the requested RFC 4790 i;octet sort
for feature in byte_features:
s.append(feature)
s.append('<')
#TODO: manage XEP-0128 data form here
cap_hash = b64encode(sha1(''.join(s)).digest())
log.debug(_(u'Capability hash generated: [%s]') % cap_hash)
s.append("<")
# TODO: manage XEP-0128 data form here
cap_hash = b64encode(sha1("".join(s)).digest())
log.debug(_(u"Capability hash generated: [%s]") % cap_hash)
return cap_hash
@defer.inlineCallbacks
def _discoInfos(self, entity_jid_s, node=u'', use_cache=True, profile_key=C.PROF_KEY_NONE):
def _discoInfos(
self, entity_jid_s, node=u"", use_cache=True, profile_key=C.PROF_KEY_NONE
):
""" Discovery method for the bridge
@param entity_jid_s: entity we want to discover
@param use_cache(bool): if True, use cached data if available
@ -353,8 +395,8 @@ class Discovery(object):
for form_type, form in disco_infos.extensions.items():
fields = []
for field in form.fieldList:
data = {'type': field.fieldType}
for attr in ('var', 'label', 'desc'):
data = {"type": field.fieldType}
for attr in ("var", "label", "desc"):
value = getattr(field, attr)
if value is not None:
data[attr] = value
@ -364,9 +406,16 @@ class Discovery(object):
extensions[form_type or ""] = fields
defer.returnValue((disco_infos.features,
[(cat, type_, name or '') for (cat, type_), name in disco_infos.identities.items()],
extensions))
defer.returnValue(
(
disco_infos.features,
[
(cat, type_, name or "")
for (cat, type_), name in disco_infos.identities.items()
],
extensions,
)
)
def items2tuples(self, disco_items):
"""convert disco items to tuple of strings
@ -378,10 +427,12 @@ class Discovery(object):
if not item.entity:
log.warning(_(u"invalid item (no jid)"))
continue
yield (item.entity.full(), item.nodeIdentifier or '', item.name or '')
yield (item.entity.full(), item.nodeIdentifier or "", item.name or "")
@defer.inlineCallbacks
def _discoItems(self, entity_jid_s, node=u'', use_cache=True, profile_key=C.PROF_KEY_NONE):
def _discoItems(
self, entity_jid_s, node=u"", use_cache=True, profile_key=C.PROF_KEY_NONE
):
""" Discovery method for the bridge
@param entity_jid_s: entity we want to discover

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _
from sat.core.log import getLogger
log = getLogger(__name__)
import os.path
@ -44,11 +45,13 @@ import mimetypes
import time
PresenceTuple = namedtuple("PresenceTuple", ('show', 'priority', 'statuses'))
PresenceTuple = namedtuple("PresenceTuple", ("show", "priority", "statuses"))
MSG_NO_SESSION = "Session id doesn't exist or is finished"
class Sessions(object):
"""Sessions are data associated to key used for a temporary moment, with optional profile checking."""
DEFAULT_TIMEOUT = 600
def __init__(self, timeout=None, resettable_timeout=True):
@ -72,11 +75,15 @@ class Sessions(object):
if session_id is None:
session_id = str(uuid4())
elif session_id in self._sessions:
raise exceptions.ConflictError(u"Session id {} is already used".format(session_id))
raise exceptions.ConflictError(
u"Session id {} is already used".format(session_id)
)
timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
if session_data is None:
session_data = {}
self._sessions[session_id] = (timer, session_data) if profile is None else (timer, session_data, profile)
self._sessions[session_id] = (
(timer, session_data) if profile is None else (timer, session_data, profile)
)
return session_id, session_data
def _purgeSession(self, session_id):
@ -91,7 +98,12 @@ class Sessions(object):
# if the session is time-outed, the timer has been called
pass
del self._sessions[session_id]
log.debug(u"Session {} purged{}".format(session_id, u' (profile {})'.format(profile) if profile is not None else u''))
log.debug(
u"Session {} purged{}".format(
session_id,
u" (profile {})".format(profile) if profile is not None else u"",
)
)
def __len__(self):
return len(self._sessions)
@ -103,7 +115,9 @@ class Sessions(object):
try:
timer, session_data, profile_set = self._sessions[session_id]
except ValueError:
raise exceptions.InternalError("You need to use __getitem__ when profile is not set")
raise exceptions.InternalError(
"You need to use __getitem__ when profile is not set"
)
except KeyError:
raise failure.Failure(KeyError(MSG_NO_SESSION))
if profile_set != profile:
@ -116,7 +130,9 @@ class Sessions(object):
try:
timer, session_data = self._sessions[session_id]
except ValueError:
raise exceptions.InternalError("You need to use profileGet instead of __getitem__ when profile is set")
raise exceptions.InternalError(
"You need to use profileGet instead of __getitem__ when profile is set"
)
except KeyError:
raise failure.Failure(KeyError(MSG_NO_SESSION))
if self.resettable_timeout:
@ -169,8 +185,12 @@ class ProfileSessions(Sessions):
"""
ids = self._profileGetAllIds(profile)
if len(ids) > 1:
raise exceptions.InternalError('profileGetUnique has been used but more than one session has been found!')
return self.profileGet(ids[0], profile) if len(ids) == 1 else None # XXX: timeout might be reset
raise exceptions.InternalError(
"profileGetUnique has been used but more than one session has been found!"
)
return (
self.profileGet(ids[0], profile) if len(ids) == 1 else None
) # XXX: timeout might be reset
def profileDelUnique(self, profile):
"""Delete the unique session that is associated to the given profile.
@ -180,7 +200,9 @@ class ProfileSessions(Sessions):
"""
ids = self._profileGetAllIds(profile)
if len(ids) > 1:
raise exceptions.InternalError('profileDelUnique has been used but more than one session has been found!')
raise exceptions.InternalError(
"profileDelUnique has been used but more than one session has been found!"
)
if len(ids) == 1:
del self._sessions[ids[0]]
@ -194,7 +216,9 @@ class PasswordSessions(ProfileSessions):
ProfileSessions.__init__(self, timeout, resettable_timeout=False)
def _purgeSession(self, session_id):
log.debug("FIXME: PasswordSessions should ask for the profile password after the session expired")
log.debug(
"FIXME: PasswordSessions should ask for the profile password after the session expired"
)
# XXX: tmp update code, will be removed in the future
@ -211,16 +235,20 @@ def fixLocalDir(silent=True):
except:
pass # file is readable but its structure if wrong
try:
current_value = user_config.get('DEFAULT', 'local_dir')
current_value = user_config.get("DEFAULT", "local_dir")
except (NoOptionError, NoSectionError):
current_value = ''
current_value = ""
if current_value:
return # nothing to do
old_default = '~/.sat'
if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE):
old_default = "~/.sat"
if os.path.isfile(os.path.expanduser(old_default) + "/" + C.SAVEFILE_DATABASE):
if not silent:
log.warning(_(u"A database has been found in the default local_dir for previous versions (< 0.5)"))
tools_config.fixConfigOption('', 'local_dir', old_default, silent)
log.warning(
_(
u"A database has been found in the default local_dir for previous versions (< 0.5)"
)
)
tools_config.fixConfigOption("", "local_dir", old_default, silent)
class Memory(object):
@ -230,17 +258,19 @@ class Memory(object):
log.info(_("Memory manager init"))
self.initialized = defer.Deferred()
self.host = host
self._entities_cache = {} # XXX: keep presence/last resource/other data in cache
# /!\ an entity is not necessarily in roster
# main key is bare jid, value is a dict
# where main key is resource, or None for bare jid
self._key_signals = set() # key which need a signal to frontends when updated
self._entities_cache = {} # XXX: keep presence/last resource/other data in cache
# /!\ an entity is not necessarily in roster
# main key is bare jid, value is a dict
# where main key is resource, or None for bare jid
self._key_signals = set() # key which need a signal to frontends when updated
self.subscriptions = {}
self.auth_sessions = PasswordSessions() # remember the authenticated profiles
self.disco = Discovery(host)
fixLocalDir(False) # XXX: tmp update code, will be removed in the future
self.config = tools_config.parseMainConf()
database_file = os.path.expanduser(os.path.join(self.getConfig('', 'local_dir'), C.SAVEFILE_DATABASE))
database_file = os.path.expanduser(
os.path.join(self.getConfig("", "local_dir"), C.SAVEFILE_DATABASE)
)
self.storage = SqliteStorage(database_file, host.version)
PersistentDict.storage = self.storage
self.params = Params(host, self.storage)
@ -290,7 +320,7 @@ class Memory(object):
"""
if not filename:
return False
#TODO: need to encrypt files (at least passwords !) and set permissions
# TODO: need to encrypt files (at least passwords !) and set permissions
filename = os.path.expanduser(filename)
try:
self.params.save_xml(filename)
@ -302,7 +332,7 @@ class Memory(object):
def load(self):
"""Load parameters and all memory things from db"""
#parameters data
# parameters data
return self.params.loadGenParams()
def loadIndividualParams(self, profile):
@ -340,7 +370,9 @@ class Memory(object):
session_d = self._entities_cache[profile]
except KeyError:
# else we do request the params
session_d = self._entities_cache[profile] = self.loadIndividualParams(profile)
session_d = self._entities_cache[profile] = self.loadIndividualParams(
profile
)
session_d.addCallback(createSession)
finally:
return session_d
@ -396,12 +428,20 @@ class Memory(object):
def check_result(result):
if not result:
log.warning(u'Authentication failure of profile {}'.format(profile))
raise failure.Failure(exceptions.PasswordError(u"The provided profile password doesn't match."))
if not session_data: # avoid to create two profile sessions when password if specified
log.warning(u"Authentication failure of profile {}".format(profile))
raise failure.Failure(
exceptions.PasswordError(
u"The provided profile password doesn't match."
)
)
if (
not session_data
): # avoid to create two profile sessions when password if specified
return self.newAuthSession(password, profile)
d = self.asyncGetParamA(C.PROFILE_PASS_PATH[1], C.PROFILE_PASS_PATH[0], profile_key=profile)
d = self.asyncGetParamA(
C.PROFILE_PASS_PATH[1], C.PROFILE_PASS_PATH[0], profile_key=profile
)
d.addCallback(lambda sat_cipher: PasswordHasher.verify(password, sat_cipher))
return d.addCallback(check_result)
@ -414,10 +454,13 @@ class Memory(object):
@param profile: %(doc_profile)s
@return: a deferred None value
"""
def gotPersonalKey(personal_key):
"""Create the session for this profile and store the personal key"""
self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=profile)
log.debug(u'auth session created for profile %s' % profile)
self.auth_sessions.newSession(
{C.MEMORY_CRYPTO_KEY: personal_key}, profile=profile
)
log.debug(u"auth session created for profile %s" % profile)
d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load()
d.addCallback(lambda data: BlockCipher.decrypt(key, data[C.MEMORY_CRYPTO_KEY]))
@ -431,7 +474,12 @@ class Memory(object):
try:
del self._entities_cache[profile]
except KeyError:
log.error(_(u"Trying to purge roster status cache for a profile not in memory: [%s]") % profile)
log.error(
_(
u"Trying to purge roster status cache for a profile not in memory: [%s]"
)
% profile
)
def getProfilesList(self, clients=True, components=False):
"""retrieve profiles list
@ -472,7 +520,7 @@ class Memory(object):
# we want to be sure that the profile exists
profile = self.getProfileName(profile)
self.memory_data['Profile_default'] = profile
self.memory_data["Profile_default"] = profile
def createProfile(self, name, password, component=None):
"""Create a new profile
@ -486,9 +534,9 @@ class Memory(object):
"""
if not name:
raise ValueError(u"Empty profile name")
if name[0] == '@':
if name[0] == "@":
raise ValueError(u"A profile name can't start with a '@'")
if '\n' in name:
if "\n" in name:
raise ValueError(u"A profile name can't contain line feed ('\\n')")
if name in self._entities_cache:
@ -496,19 +544,28 @@ class Memory(object):
if component:
if not component in self.host.plugins:
raise exceptions.NotFound(_(u"Can't find component {component} entry point".format(
component = component)))
raise exceptions.NotFound(
_(
u"Can't find component {component} entry point".format(
component=component
)
)
)
# FIXME: PLUGIN_INFO is not currently accessible after import, but type shoul be tested here
# if self.host.plugins[component].PLUGIN_INFO[u"type"] != C.PLUG_TYPE_ENTRY_POINT:
#  raise ValueError(_(u"Plugin {component} is not an entry point !".format(
#  component = component)))
#  if self.host.plugins[component].PLUGIN_INFO[u"type"] != C.PLUG_TYPE_ENTRY_POINT:
#   raise ValueError(_(u"Plugin {component} is not an entry point !".format(
#   component = component)))
d = self.params.createProfile(name, component)
def initPersonalKey(dummy):
# be sure to call this after checking that the profile doesn't exist yet
personal_key = BlockCipher.getRandomKey(base64=True) # generated once for all and saved in a PersistentDict
self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=name) # will be encrypted by setParam
personal_key = BlockCipher.getRandomKey(
base64=True
) # generated once for all and saved in a PersistentDict
self.auth_sessions.newSession(
{C.MEMORY_CRYPTO_KEY: personal_key}, profile=name
) # will be encrypted by setParam
def startFakeSession(dummy):
# avoid ProfileNotConnected exception in setParam
@ -521,7 +578,11 @@ class Memory(object):
d.addCallback(initPersonalKey)
d.addCallback(startFakeSession)
d.addCallback(lambda dummy: self.setParam(C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=name))
d.addCallback(
lambda dummy: self.setParam(
C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=name
)
)
d.addCallback(stopFakeSession)
d.addCallback(lambda dummy: self.auth_sessions.profileDelUnique(name))
return d
@ -534,12 +595,14 @@ class Memory(object):
To be used for direct calls only (not through the bridge).
@return: a Deferred instance
"""
def cleanMemory(dummy):
self.auth_sessions.profileDelUnique(name)
try:
del self._entities_cache[name]
except KeyError:
pass
d = self.params.asyncDeleteProfile(name, force)
d.addCallback(cleanMemory)
return d
@ -567,10 +630,28 @@ class Memory(object):
def addToHistory(self, client, data):
return self.storage.addToHistory(data, client.profile)
def _historyGet(self, from_jid_s, to_jid_s, limit=C.HISTORY_LIMIT_NONE, between=True, filters=None, profile=C.PROF_KEY_NONE):
return self.historyGet(jid.JID(from_jid_s), jid.JID(to_jid_s), limit, between, filters, profile)
def _historyGet(
self,
from_jid_s,
to_jid_s,
limit=C.HISTORY_LIMIT_NONE,
between=True,
filters=None,
profile=C.PROF_KEY_NONE,
):
return self.historyGet(
jid.JID(from_jid_s), jid.JID(to_jid_s), limit, between, filters, profile
)
def historyGet(self, from_jid, to_jid, limit=C.HISTORY_LIMIT_NONE, between=True, filters=None, profile=C.PROF_KEY_NONE):
def historyGet(
self,
from_jid,
to_jid,
limit=C.HISTORY_LIMIT_NONE,
between=True,
filters=None,
profile=C.PROF_KEY_NONE,
):
"""Retrieve messages in history
@param from_jid (JID): source JID (full, or bare for catchall)
@ -586,7 +667,7 @@ class Memory(object):
"""
assert profile != C.PROF_KEY_NONE
if limit == C.HISTORY_LIMIT_DEFAULT:
limit = int(self.getParamA(C.HISTORY_LIMIT, 'General', profile_key=profile))
limit = int(self.getParamA(C.HISTORY_LIMIT, "General", profile_key=profile))
elif limit == C.HISTORY_LIMIT_NONE:
limit = None
if limit == 0:
@ -597,7 +678,7 @@ class Memory(object):
def _getPresenceStatuses(self, profile_key):
ret = self.getPresenceStatuses(profile_key)
return {entity.full():data for entity, data in ret.iteritems()}
return {entity.full(): data for entity, data in ret.iteritems()}
def getPresenceStatuses(self, profile_key):
"""Get all the presence statuses of a profile
@ -617,7 +698,9 @@ class Memory(object):
presence_data = self.getEntityDatum(full_jid, "presence", profile_key)
except KeyError:
continue
entities_presence.setdefault(entity_jid, {})[resource or ''] = presence_data
entities_presence.setdefault(entity_jid, {})[
resource or ""
] = presence_data
return entities_presence
@ -631,7 +714,9 @@ class Memory(object):
@param profile_key: %(doc_profile_key)s
"""
presence_data = PresenceTuple(show, priority, statuses)
self.updateEntityData(entity_jid, "presence", presence_data, profile_key=profile_key)
self.updateEntityData(
entity_jid, "presence", presence_data, profile_key=profile_key
)
if entity_jid.resource and show != C.PRESENCE_UNAVAILABLE:
# If a resource is available, bare jid should not have presence information
try:
@ -657,13 +742,17 @@ class Memory(object):
"""
# FIXME: is there a need to keep cache data for resources which are not connected anymore?
if entity_jid.resource:
raise ValueError("getAllResources must be used with a bare jid (got {})".format(entity_jid))
raise ValueError(
"getAllResources must be used with a bare jid (got {})".format(entity_jid)
)
profile_cache = self._getProfileCache(client)
try:
entity_data = profile_cache[entity_jid.userhostJID()]
except KeyError:
raise exceptions.UnknownEntityError(u"Entity {} not in cache".format(entity_jid))
resources= set(entity_data.keys())
raise exceptions.UnknownEntityError(
u"Entity {} not in cache".format(entity_jid)
)
resources = set(entity_data.keys())
resources.discard(None)
return resources
@ -701,7 +790,9 @@ class Memory(object):
@return (unicode): main resource or None
"""
if entity_jid.resource:
raise ValueError("getMainResource must be used with a bare jid (got {})".format(entity_jid))
raise ValueError(
"getMainResource must be used with a bare jid (got {})".format(entity_jid)
)
try:
if self.host.plugins["XEP-0045"].isJoinedRoom(client, entity_jid):
return None # MUC rooms have no main resource
@ -766,7 +857,9 @@ class Memory(object):
full_jid.resource = resource
yield full_jid
def updateEntityData(self, entity_jid, key, value, silent=False, profile_key=C.PROF_KEY_NONE):
def updateEntityData(
self, entity_jid, key, value, silent=False, profile_key=C.PROF_KEY_NONE
):
"""Set a misc data for an entity
If key was registered with setSignalOnUpdate, a signal will be sent to frontends
@ -780,19 +873,27 @@ class Memory(object):
client = self.host.getClient(profile_key)
profile_cache = self._getProfileCache(client)
if entity_jid in (C.ENTITY_ALL_RESOURCES, C.ENTITY_ALL):
entities = self.getAllEntitiesIter(client, entity_jid==C.ENTITY_ALL)
entities = self.getAllEntitiesIter(client, entity_jid == C.ENTITY_ALL)
else:
entities = (entity_jid,)
for jid_ in entities:
entity_data = profile_cache.setdefault(jid_.userhostJID(),{}).setdefault(jid_.resource, {})
entity_data = profile_cache.setdefault(jid_.userhostJID(), {}).setdefault(
jid_.resource, {}
)
entity_data[key] = value
if key in self._key_signals and not silent:
if not isinstance(value, basestring):
log.error(u"Setting a non string value ({}) for a key ({}) which has a signal flag".format(value, key))
log.error(
u"Setting a non string value ({}) for a key ({}) which has a signal flag".format(
value, key
)
)
else:
self.host.bridge.entityDataUpdated(jid_.full(), key, value, self.getProfileName(profile_key))
self.host.bridge.entityDataUpdated(
jid_.full(), key, value, self.getProfileName(profile_key)
)
def delEntityDatum(self, entity_jid, key, profile_key):
"""Delete a data for an entity
@ -808,7 +909,7 @@ class Memory(object):
client = self.host.getClient(profile_key)
profile_cache = self._getProfileCache(client)
if entity_jid in (C.ENTITY_ALL_RESOURCES, C.ENTITY_ALL):
entities = self.getAllEntitiesIter(client, entity_jid==C.ENTITY_ALL)
entities = self.getAllEntitiesIter(client, entity_jid == C.ENTITY_ALL)
else:
entities = (entity_jid,)
@ -816,17 +917,21 @@ class Memory(object):
try:
entity_data = profile_cache[jid_.userhostJID()][jid_.resource]
except KeyError:
raise exceptions.UnknownEntityError(u"Entity {} not in cache".format(jid_))
raise exceptions.UnknownEntityError(
u"Entity {} not in cache".format(jid_)
)
try:
del entity_data[key]
except KeyError as e:
if entity_jid in (C.ENTITY_ALL_RESOURCES, C.ENTITY_ALL):
continue # we ignore KeyError when deleting keys from several entities
continue # we ignore KeyError when deleting keys from several entities
else:
raise e
def _getEntitiesData(self, entities_jids, keys_list, profile_key):
ret = self.getEntitiesData([jid.JID(jid_) for jid_ in entities_jids], keys_list, profile_key)
ret = self.getEntitiesData(
[jid.JID(jid_) for jid_ in entities_jids], keys_list, profile_key
)
return {jid_.full(): data for jid_, data in ret.iteritems()}
def getEntitiesData(self, entities_jids, keys_list=None, profile_key=C.PROF_KEY_NONE):
@ -843,6 +948,7 @@ class Memory(object):
@raise exceptions.UnknownEntityError: if entity is not in cache
"""
def fillEntityData(entity_cache_data):
entity_data = {}
if keys_list is None:
@ -861,7 +967,9 @@ class Memory(object):
if entities_jids:
for entity in entities_jids:
try:
entity_cache_data = profile_cache[entity.userhostJID()][entity.resource]
entity_cache_data = profile_cache[entity.userhostJID()][
entity.resource
]
except KeyError:
continue
ret_data[entity.full()] = fillEntityData(entity_cache_data, keys_list)
@ -891,7 +999,11 @@ class Memory(object):
try:
entity_data = profile_cache[entity_jid.userhostJID()][entity_jid.resource]
except KeyError:
raise exceptions.UnknownEntityError(u"Entity {} not in cache (was requesting {})".format(entity_jid, keys_list))
raise exceptions.UnknownEntityError(
u"Entity {} not in cache (was requesting {})".format(
entity_jid, keys_list
)
)
if keys_list is None:
return entity_data
@ -910,7 +1022,9 @@ class Memory(object):
"""
return self.getEntityData(entity_jid, (key,), profile_key)[key]
def delEntityCache(self, entity_jid, delete_all_resources=True, profile_key=C.PROF_KEY_NONE):
def delEntityCache(
self, entity_jid, delete_all_resources=True, profile_key=C.PROF_KEY_NONE
):
"""Remove all cached data for entity
@param entity_jid: JID of the entity to delete
@ -928,12 +1042,16 @@ class Memory(object):
try:
del profile_cache[entity_jid]
except KeyError:
raise exceptions.UnknownEntityError(u"Entity {} not in cache".format(entity_jid))
raise exceptions.UnknownEntityError(
u"Entity {} not in cache".format(entity_jid)
)
else:
try:
del profile_cache[entity_jid.userhostJID()][entity_jid.resource]
except KeyError:
raise exceptions.UnknownEntityError(u"Entity {} not in cache".format(entity_jid))
raise exceptions.UnknownEntityError(
u"Entity {} not in cache".format(entity_jid)
)
## Encryption ##
@ -947,9 +1065,14 @@ class Memory(object):
@return: the deferred encrypted value
"""
try:
personal_key = self.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY]
personal_key = self.auth_sessions.profileGetUnique(profile)[
C.MEMORY_CRYPTO_KEY
]
except TypeError:
raise exceptions.InternalError(_('Trying to encrypt a value for %s while the personal key is undefined!') % profile)
raise exceptions.InternalError(
_("Trying to encrypt a value for %s while the personal key is undefined!")
% profile
)
return BlockCipher.encrypt(personal_key, value)
def decryptValue(self, value, profile):
@ -962,9 +1085,14 @@ class Memory(object):
@return: the deferred decrypted value
"""
try:
personal_key = self.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY]
personal_key = self.auth_sessions.profileGetUnique(profile)[
C.MEMORY_CRYPTO_KEY
]
except TypeError:
raise exceptions.InternalError(_('Trying to decrypt a value for %s while the personal key is undefined!') % profile)
raise exceptions.InternalError(
_("Trying to decrypt a value for %s while the personal key is undefined!")
% profile
)
return BlockCipher.decrypt(personal_key, value)
def encryptPersonalData(self, data_key, data_value, crypto_key, profile):
@ -987,8 +1115,10 @@ class Memory(object):
return d.addCallback(cb)
def done(dummy):
log.debug(_(u'Personal data (%(ns)s, %(key)s) has been successfuly encrypted') %
{'ns': C.MEMORY_CRYPTO_NAMESPACE, 'key': data_key})
log.debug(
_(u"Personal data (%(ns)s, %(key)s) has been successfuly encrypted")
% {"ns": C.MEMORY_CRYPTO_NAMESPACE, "key": data_key}
)
d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load()
return d.addCallback(gotIndMemory).addCallback(done)
@ -1014,7 +1144,7 @@ class Memory(object):
"""Called to get a list of currently waiting subscription requests"""
profile = self.getProfileName(profile_key)
if not profile:
log.error(_('Asking waiting subscriptions for a non-existant profile'))
log.error(_("Asking waiting subscriptions for a non-existant profile"))
return {}
if profile not in self.subscriptions:
return {}
@ -1029,28 +1159,59 @@ class Memory(object):
def getParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
return self.params.getParamA(name, category, attr, profile_key=profile_key)
def asyncGetParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
return self.params.asyncGetParamA(name, category, attr, security_limit, profile_key)
def asyncGetParamA(
self,
name,
category,
attr="value",
security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE,
):
return self.params.asyncGetParamA(
name, category, attr, security_limit, profile_key
)
def asyncGetParamsValuesFromCategory(self, category, security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
return self.params.asyncGetParamsValuesFromCategory(category, security_limit, profile_key)
def asyncGetParamsValuesFromCategory(
self, category, security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE
):
return self.params.asyncGetParamsValuesFromCategory(
category, security_limit, profile_key
)
def asyncGetStringParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
return self.params.asyncGetStringParamA(name, category, attr, security_limit, profile_key)
def asyncGetStringParamA(
self,
name,
category,
attr="value",
security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE,
):
return self.params.asyncGetStringParamA(
name, category, attr, security_limit, profile_key
)
def getParamsUI(self, security_limit=C.NO_SECURITY_LIMIT, app='', profile_key=C.PROF_KEY_NONE):
def getParamsUI(
self, security_limit=C.NO_SECURITY_LIMIT, app="", profile_key=C.PROF_KEY_NONE
):
return self.params.getParamsUI(security_limit, app, profile_key)
def getParamsCategories(self):
return self.params.getParamsCategories()
def setParam(self, name, value, category, security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
def setParam(
self,
name,
value,
category,
security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE,
):
return self.params.setParam(name, value, category, security_limit, profile_key)
def updateParams(self, xml):
return self.params.updateParams(xml)
def paramsRegisterApp(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=''):
def paramsRegisterApp(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=""):
return self.params.paramsRegisterApp(xml, security_limit, app)
def setDefault(self, name, category, callback, errback=None):
@ -1073,25 +1234,25 @@ class Memory(object):
if peer_jid is None and perms_to_check is None:
return
peer_jid = peer_jid.userhostJID()
if peer_jid == file_data['owner']:
if peer_jid == file_data["owner"]:
# the owner has all rights
return
if not C.ACCESS_PERMS.issuperset(perms_to_check):
raise exceptions.InternalError(_(u'invalid permission'))
raise exceptions.InternalError(_(u"invalid permission"))
for perm in perms_to_check:
# we check each perm and raise PermissionError as soon as one condition is not valid
# we must never return here, we only return after the loop if nothing was blocking the access
try:
perm_data = file_data[u'access'][perm]
perm_type = perm_data[u'type']
perm_data = file_data[u"access"][perm]
perm_type = perm_data[u"type"]
except KeyError:
raise failure.Failure(exceptions.PermissionError())
if perm_type == C.ACCESS_TYPE_PUBLIC:
continue
elif perm_type == C.ACCESS_TYPE_WHITELIST:
try:
jids = perm_data[u'jids']
jids = perm_data[u"jids"]
except KeyError:
raise failure.Failure(exceptions.PermissionError())
if peer_jid.full() in jids:
@ -1099,7 +1260,9 @@ class Memory(object):
else:
raise failure.Failure(exceptions.PermissionError())
else:
raise exceptions.InternalError(_(u'unknown access type: {type}').format(type=perm_type))
raise exceptions.InternalError(
_(u"unknown access type: {type}").format(type=perm_type)
)
@defer.inlineCallbacks
def checkPermissionToRoot(self, client, file_data, peer_jid, perms_to_check):
@ -1107,17 +1270,21 @@ class Memory(object):
current = file_data
while True:
self.checkFilePermission(current, peer_jid, perms_to_check)
parent = current[u'parent']
parent = current[u"parent"]
if not parent:
break
files_data = yield self.getFile(self, client, peer_jid=None, file_id=parent, perms_to_check=None)
files_data = yield self.getFile(
self, client, peer_jid=None, file_id=parent, perms_to_check=None
)
try:
current = files_data[0]
except IndexError:
raise exceptions.DataError(u'Missing parent')
raise exceptions.DataError(u"Missing parent")
@defer.inlineCallbacks
def _getParentDir(self, client, path, parent, namespace, owner, peer_jid, perms_to_check):
def _getParentDir(
self, client, path, parent, namespace, owner, peer_jid, perms_to_check
):
"""Retrieve parent node from a path, or last existing directory
each directory of the path will be retrieved, until the last existing one
@ -1128,32 +1295,59 @@ class Memory(object):
"""
# if path is set, we have to retrieve parent directory of the file(s) from it
if parent is not None:
raise exceptions.ConflictError(_(u"You can't use path and parent at the same time"))
path_elts = filter(None, path.split(u'/'))
if {u'..', u'.'}.intersection(path_elts):
raise exceptions.ConflictError(
_(u"You can't use path and parent at the same time")
)
path_elts = filter(None, path.split(u"/"))
if {u"..", u"."}.intersection(path_elts):
raise ValueError(_(u'".." or "." can\'t be used in path'))
# we retrieve all directories from path until we get the parent container
# non existing directories will be created
parent = u''
parent = u""
for idx, path_elt in enumerate(path_elts):
directories = yield self.storage.getFiles(client, parent=parent, type_=C.FILE_TYPE_DIRECTORY,
name=path_elt, namespace=namespace, owner=owner)
directories = yield self.storage.getFiles(
client,
parent=parent,
type_=C.FILE_TYPE_DIRECTORY,
name=path_elt,
namespace=namespace,
owner=owner,
)
if not directories:
defer.returnValue((parent, path_elts[idx:]))
# from this point, directories don't exist anymore, we have to create them
elif len(directories) > 1:
raise exceptions.InternalError(_(u"Several directories found, this should not happen"))
raise exceptions.InternalError(
_(u"Several directories found, this should not happen")
)
else:
directory = directories[0]
self.checkFilePermission(directory, peer_jid, perms_to_check)
parent = directory[u'id']
parent = directory[u"id"]
defer.returnValue((parent, []))
@defer.inlineCallbacks
def getFiles(self, client, peer_jid, file_id=None, version=None, parent=None, path=None, type_=None,
file_hash=None, hash_algo=None, name=None, namespace=None, mime_type=None,
owner=None, access=None, projection=None, unique=False, perms_to_check=(C.ACCESS_PERM_READ,)):
def getFiles(
self,
client,
peer_jid,
file_id=None,
version=None,
parent=None,
path=None,
type_=None,
file_hash=None,
hash_algo=None,
name=None,
namespace=None,
mime_type=None,
owner=None,
access=None,
projection=None,
unique=False,
perms_to_check=(C.ACCESS_PERM_READ,),
):
"""retrieve files with with given filters
@param peer_jid(jid.JID, None): jid trying to access the file
@ -1180,12 +1374,16 @@ class Memory(object):
on the path
"""
if peer_jid is None and perms_to_check or perms_to_check is None and peer_jid:
raise exceptions.InternalError('if you want to disable permission check, both peer_jid and perms_to_check must be None')
raise exceptions.InternalError(
"if you want to disable permission check, both peer_jid and perms_to_check must be None"
)
if owner is not None:
owner = owner.userhostJID()
if path is not None:
# permission are checked by _getParentDir
parent, remaining_path_elts = yield self._getParentDir(client, path, parent, namespace, owner, peer_jid, perms_to_check)
parent, remaining_path_elts = yield self._getParentDir(
client, path, parent, namespace, owner, peer_jid, perms_to_check
)
if remaining_path_elts:
# if we have remaining path elements,
# the parent directory is not found
@ -1197,16 +1395,30 @@ class Memory(object):
try:
parent_data = parent_data[0]
except IndexError:
raise exceptions.DataError(u'mising parent')
yield self.checkPermissionToRoot(client, parent_data, peer_jid, perms_to_check)
raise exceptions.DataError(u"mising parent")
yield self.checkPermissionToRoot(
client, parent_data, peer_jid, perms_to_check
)
files = yield self.storage.getFiles(client, file_id=file_id, version=version, parent=parent, type_=type_,
file_hash=file_hash, hash_algo=hash_algo, name=name, namespace=namespace,
mime_type=mime_type, owner=owner, access=access,
projection=projection, unique=unique)
files = yield self.storage.getFiles(
client,
file_id=file_id,
version=version,
parent=parent,
type_=type_,
file_hash=file_hash,
hash_algo=hash_algo,
name=name,
namespace=namespace,
mime_type=mime_type,
owner=owner,
access=access,
projection=projection,
unique=unique,
)
if peer_jid:
# if permission are checked, we must remove all file tha use can't access
#  if permission are checked, we must remove all file tha use can't access
to_remove = []
for file_data in files:
try:
@ -1218,10 +1430,28 @@ class Memory(object):
defer.returnValue(files)
@defer.inlineCallbacks
def setFile(self, client, name, file_id=None, version=u'', parent=None, path=None,
type_=C.FILE_TYPE_FILE, file_hash=None, hash_algo=None, size=None, namespace=None,
mime_type=None, created=None, modified=None, owner=None, access=None, extra=None,
peer_jid = None, perms_to_check=(C.ACCESS_PERM_WRITE,)):
def setFile(
self,
client,
name,
file_id=None,
version=u"",
parent=None,
path=None,
type_=C.FILE_TYPE_FILE,
file_hash=None,
hash_algo=None,
size=None,
namespace=None,
mime_type=None,
created=None,
modified=None,
owner=None,
access=None,
extra=None,
peer_jid=None,
perms_to_check=(C.ACCESS_PERM_WRITE,),
):
"""set a file metadata
@param name(unicode): basename of the file
@ -1258,12 +1488,17 @@ class Memory(object):
if None, permission will no be checked (peer_jid must be None too in this case)
@param profile(unicode): profile owning the file
"""
if '/' in name:
if "/" in name:
raise ValueError('name must not contain a slash ("/")')
if file_id is None:
file_id = shortuuid.uuid()
if file_hash is not None and hash_algo is None or hash_algo is not None and file_hash is None:
raise ValueError('file_hash and hash_algo must be set at the same time')
if (
file_hash is not None
and hash_algo is None
or hash_algo is not None
and file_hash is None
):
raise ValueError("file_hash and hash_algo must be set at the same time")
if mime_type is None:
mime_type, file_encoding = mimetypes.guess_type(name)
if created is None:
@ -1271,31 +1506,56 @@ class Memory(object):
if namespace is not None:
namespace = namespace.strip() or None
if type_ == C.FILE_TYPE_DIRECTORY:
if any(version, file_hash, size, mime_type):
raise ValueError(u"version, file_hash, size and mime_type can't be set for a directory")
if any(version, file_hash, size, mime_type):
raise ValueError(
u"version, file_hash, size and mime_type can't be set for a directory"
)
if owner is not None:
owner = owner.userhostJID()
if path is not None:
# _getParentDir will check permissions if peer_jid is set, so we use owner
parent, remaining_path_elts = yield self._getParentDir(client, path, parent, namespace, owner, owner, perms_to_check)
parent, remaining_path_elts = yield self._getParentDir(
client, path, parent, namespace, owner, owner, perms_to_check
)
# if remaining directories don't exist, we have to create them
for new_dir in remaining_path_elts:
new_dir_id = shortuuid.uuid()
yield self.storage.setFile(client, name=new_dir, file_id=new_dir_id, version=u'', parent=parent,
type_=C.FILE_TYPE_DIRECTORY, namespace=namespace,
created=time.time(),
owner=owner,
access=access, extra={})
yield self.storage.setFile(
client,
name=new_dir,
file_id=new_dir_id,
version=u"",
parent=parent,
type_=C.FILE_TYPE_DIRECTORY,
namespace=namespace,
created=time.time(),
owner=owner,
access=access,
extra={},
)
parent = new_dir_id
elif parent is None:
parent = u''
parent = u""
yield self.storage.setFile(client, file_id=file_id, version=version, parent=parent, type_=type_,
file_hash=file_hash, hash_algo=hash_algo, name=name, size=size,
namespace=namespace, mime_type=mime_type, created=created, modified=modified,
owner=owner,
access=access, extra=extra)
yield self.storage.setFile(
client,
file_id=file_id,
version=version,
parent=parent,
type_=type_,
file_hash=file_hash,
hash_algo=hash_algo,
name=name,
size=size,
namespace=namespace,
mime_type=mime_type,
created=created,
modified=modified,
owner=owner,
access=access,
extra=extra,
)
def fileUpdate(self, file_id, column, update_cb):
"""update a file column taking care of race condition
@ -1318,7 +1578,9 @@ class Memory(object):
@return (bool): True if entity is available
"""
if not entity_jid.resource:
return bool(self.getAvailableResources(client, entity_jid)) # is any resource is available, entity is available
return bool(
self.getAvailableResources(client, entity_jid)
) # is any resource is available, entity is available
try:
presence_data = self.getEntityDatum(entity_jid, "presence", client.profile)
except KeyError:

View file

@ -24,6 +24,7 @@ from sat.core.constants import Const as C
from sat.memory.crypto import BlockCipher, PasswordHasher
from xml.dom import minidom, NotFoundErr
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.internet import defer
from twisted.python.failure import Failure
@ -43,17 +44,18 @@ def createJidElts(jids):
@return (generator[domish.Element]): <jid/> elements
"""
for jid_ in jids:
jid_elt = domish.Element((None, 'jid'))
jid_elt = domish.Element((None, "jid"))
jid_elt.addContent(jid_.full())
yield jid_elt
class Params(object):
"""This class manage parameters with xml"""
### TODO: add desciption in params
#TODO: when priority is changed, a new presence stanza must be emitted
#TODO: int type (Priority should be int instead of string)
# TODO: when priority is changed, a new presence stanza must be emitted
# TODO: int type (Priority should be int instead of string)
default_xml = u"""
<params>
<general>
@ -77,23 +79,23 @@ class Params(object):
</individual>
</params>
""" % {
'category_general': D_("General"),
'category_connection': D_("Connection"),
'history_param': C.HISTORY_LIMIT,
'history_label': D_('Chat history limit'),
'show_offline_contacts': C.SHOW_OFFLINE_CONTACTS,
'show_offline_contacts_label': D_('Show offline contacts'),
'show_empty_groups': C.SHOW_EMPTY_GROUPS,
'show_empty_groups_label': D_('Show empty groups'),
'force_server_param': C.FORCE_SERVER_PARAM,
'force_port_param': C.FORCE_PORT_PARAM,
'new_account_label': D_("Register new account"),
'autoconnect_label': D_('Connect on frontend startup'),
'autodisconnect_label': D_('Disconnect on frontend closure'),
"category_general": D_("General"),
"category_connection": D_("Connection"),
"history_param": C.HISTORY_LIMIT,
"history_label": D_("Chat history limit"),
"show_offline_contacts": C.SHOW_OFFLINE_CONTACTS,
"show_offline_contacts_label": D_("Show offline contacts"),
"show_empty_groups": C.SHOW_EMPTY_GROUPS,
"show_empty_groups_label": D_("Show empty groups"),
"force_server_param": C.FORCE_SERVER_PARAM,
"force_port_param": C.FORCE_PORT_PARAM,
"new_account_label": D_("Register new account"),
"autoconnect_label": D_("Connect on frontend startup"),
"autodisconnect_label": D_("Disconnect on frontend closure"),
}
def load_default_params(self):
self.dom = minidom.parseString(Params.default_xml.encode('utf-8'))
self.dom = minidom.parseString(Params.default_xml.encode("utf-8"))
def _mergeParams(self, source_node, dest_node):
"""Look for every node in source_node and recursively copy them to dest if they don't exists"""
@ -102,8 +104,9 @@ class Params(object):
ret = {}
for child in children:
if child.nodeType == child.ELEMENT_NODE:
ret[(child.tagName, child.getAttribute('name'))] = child
ret[(child.tagName, child.getAttribute("name"))] = child
return ret
source_map = getNodesMap(source_node.childNodes)
dest_map = getNodesMap(dest_node.childNodes)
source_set = set(source_map.keys())
@ -120,7 +123,7 @@ class Params(object):
def load_xml(self, xml_file):
"""Load parameters template from xml file"""
self.dom = minidom.parse(xml_file)
default_dom = minidom.parseString(Params.default_xml.encode('utf-8'))
default_dom = minidom.parseString(Params.default_xml.encode("utf-8"))
self._mergeParams(default_dom.documentElement, self.dom.documentElement)
def loadGenParams(self):
@ -140,7 +143,9 @@ class Params(object):
"""
if cache is None:
self.params[profile] = {}
return self.storage.loadIndParams(self.params[profile] if cache is None else cache, profile)
return self.storage.loadIndParams(
self.params[profile] if cache is None else cache, profile
)
def purgeProfile(self, profile):
"""Remove cache data of a profile
@ -150,12 +155,14 @@ class Params(object):
try:
del self.params[profile]
except KeyError:
log.error(_(u"Trying to purge cache of a profile not in memory: [%s]") % profile)
log.error(
_(u"Trying to purge cache of a profile not in memory: [%s]") % profile
)
def save_xml(self, filename):
"""Save parameters template to xml file"""
with open(filename, 'wb') as xml_file:
xml_file.write(self.dom.toxml('utf-8'))
with open(filename, "wb") as xml_file:
xml_file.write(self.dom.toxml("utf-8"))
def __init__(self, host, storage):
log.debug("Parameters init")
@ -174,7 +181,7 @@ class Params(object):
@return: a Deferred instance
"""
if self.storage.hasProfile(profile):
log.info(_('The profile name already exists'))
log.info(_("The profile name already exists"))
return defer.fail(Failure(exceptions.ConflictError))
if not self.host.trigger.point("ProfileCreation", profile):
return defer.fail(Failure(exceptions.CancelError))
@ -189,7 +196,7 @@ class Params(object):
@return: a Deferred instance
"""
if not self.storage.hasProfile(profile):
log.info(_('Trying to delete an unknown profile'))
log.info(_("Trying to delete an unknown profile"))
return defer.fail(Failure(exceptions.ProfileUnknownError(profile)))
if self.host.isConnected(profile):
if force:
@ -210,22 +217,26 @@ class Params(object):
@raise exceptions.ProfileUnknownError: profile doesn't exists
@raise exceptions.ProfileNotSetError: if C.PROF_KEY_NONE is used
"""
if profile_key == '@DEFAULT@':
default = self.host.memory.memory_data.get('Profile_default')
if profile_key == "@DEFAULT@":
default = self.host.memory.memory_data.get("Profile_default")
if not default:
log.info(_('No default profile, returning first one'))
log.info(_("No default profile, returning first one"))
try:
default = self.host.memory.memory_data['Profile_default'] = self.storage.getProfilesList()[0]
default = self.host.memory.memory_data[
"Profile_default"
] = self.storage.getProfilesList()[0]
except IndexError:
log.info(_('No profile exist yet'))
log.info(_("No profile exist yet"))
raise exceptions.ProfileUnknownError(profile_key)
return default # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists
return (
default
) # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists
elif profile_key == C.PROF_KEY_NONE:
raise exceptions.ProfileNotSetError
elif return_profile_keys and profile_key in [C.PROF_KEY_ALL]:
return profile_key # this value must be managed by the caller
return profile_key # this value must be managed by the caller
if not self.storage.hasProfile(profile_key):
log.error(_(u'Trying to access an unknown profile (%s)') % profile_key)
log.error(_(u"Trying to access an unknown profile (%s)") % profile_key)
raise exceptions.ProfileUnknownError(profile_key)
return profile_key
@ -239,12 +250,12 @@ class Params(object):
"""
for node in parent.childNodes:
if node.nodeName == tag and node.getAttribute("name") == name:
#the node already exists
# the node already exists
return node
#the node is new
# the node is new
return None
def updateParams(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=''):
def updateParams(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=""):
"""import xml in parameters, update if the param already exists
If security_limit is specified and greater than -1, the parameters
@ -254,7 +265,7 @@ class Params(object):
@param app: name of the frontend registering the parameters or empty value
"""
# TODO: should word with domish.Element
src_parent = minidom.parseString(xml.encode('utf-8')).documentElement
src_parent = minidom.parseString(xml.encode("utf-8")).documentElement
def pre_process_app_node(src_parent, security_limit, app):
"""Parameters that are registered from a frontend must be checked"""
@ -264,17 +275,23 @@ class Params(object):
to_remove.append(type_node) # accept individual parameters only
continue
for cat_node in type_node.childNodes:
if cat_node.nodeName != 'category':
if cat_node.nodeName != "category":
to_remove.append(cat_node)
continue
to_remove_count = 0 # count the params to be removed from current category
to_remove_count = (
0
) # count the params to be removed from current category
for node in cat_node.childNodes:
if node.nodeName != "param" or not self.checkSecurityLimit(node, security_limit):
if node.nodeName != "param" or not self.checkSecurityLimit(
node, security_limit
):
to_remove.append(node)
to_remove_count += 1
continue
node.setAttribute('app', app)
if len(cat_node.childNodes) == to_remove_count: # remove empty category
node.setAttribute("app", app)
if (
len(cat_node.childNodes) == to_remove_count
): # remove empty category
for dummy in xrange(0, to_remove_count):
to_remove.pop()
to_remove.append(cat_node)
@ -283,9 +300,11 @@ class Params(object):
def import_node(tgt_parent, src_parent):
for child in src_parent.childNodes:
if child.nodeName == '#text':
if child.nodeName == "#text":
continue
node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name"))
node = self.__get_unique_node(
tgt_parent, child.nodeName, child.getAttribute("name")
)
if not node: # The node is new
tgt_parent.appendChild(child.cloneNode(True))
else:
@ -310,23 +329,35 @@ class Params(object):
@param app: name of the frontend registering the parameters
"""
if not app:
log.warning(_(u"Trying to register frontends parameters with no specified app: aborted"))
log.warning(
_(
u"Trying to register frontends parameters with no specified app: aborted"
)
)
return
if not hasattr(self, "frontends_cache"):
self.frontends_cache = []
if app in self.frontends_cache:
log.debug(_(u"Trying to register twice frontends parameters for %(app)s: aborted" % {"app": app}))
log.debug(
_(
u"Trying to register twice frontends parameters for %(app)s: aborted"
% {"app": app}
)
)
return
self.frontends_cache.append(app)
self.updateParams(xml, security_limit, app)
log.debug(u"Frontends parameters registered for %(app)s" % {'app': app})
log.debug(u"Frontends parameters registered for %(app)s" % {"app": app})
def __default_ok(self, value, name, category):
#FIXME: will not work with individual parameters
# FIXME: will not work with individual parameters
self.setParam(name, value, category)
def __default_ko(self, failure, name, category):
log.error(_(u"Can't determine default value for [%(category)s/%(name)s]: %(reason)s") % {'category': category, 'name': name, 'reason': str(failure.value)})
log.error(
_(u"Can't determine default value for [%(category)s/%(name)s]: %(reason)s")
% {"category": category, "name": name, "reason": str(failure.value)}
)
def setDefault(self, name, category, callback, errback=None):
"""Set default value of parameter
@ -337,19 +368,27 @@ class Params(object):
@param callback: must return a string with the value (use deferred if needed)
@param errback: must manage the error with args failure, name, category
"""
#TODO: send signal param update if value changed
#TODO: manage individual paramaters
log.debug ("setDefault called for %(category)s/%(name)s" % {"category": category, "name": name})
node = self._getParamNode(name, category, '@ALL@')
# TODO: send signal param update if value changed
# TODO: manage individual paramaters
log.debug(
"setDefault called for %(category)s/%(name)s"
% {"category": category, "name": name}
)
node = self._getParamNode(name, category, "@ALL@")
if not node:
log.error(_(u"Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
log.error(
_(
u"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
)
% {"name": name, "category": category}
)
return
if node[1].getAttribute('default_cb') == 'yes':
if node[1].getAttribute("default_cb") == "yes":
# del node[1].attributes['default_cb'] # default_cb is not used anymore as a flag to know if we have to set the default value,
# and we can still use it later e.g. to call a generic setDefault method
# and we can still use it later e.g. to call a generic setDefault method
value = self._getParam(category, name, C.GENERAL)
if value is None: # no value set by the user: we have the default value
log.debug ("Default value to set, using callback")
if value is None: # no value set by the user: we have the default value
log.debug("Default value to set, using callback")
d = defer.maybeDeferred(callback)
d.addCallback(self.__default_ok, name, category)
d.addErrback(errback or self.__default_ko, name, category)
@ -364,30 +403,61 @@ class Params(object):
@param value: user defined value
@return: value (can be str, bool, int, list, None)
"""
if attr == 'value':
value_to_use = value if value is not None else node.getAttribute(attr) # we use value (user defined) if it exist, else we use node's default value
if node.getAttribute('type') == 'bool':
if attr == "value":
value_to_use = (
value if value is not None else node.getAttribute(attr)
) # we use value (user defined) if it exist, else we use node's default value
if node.getAttribute("type") == "bool":
return C.bool(value_to_use)
if node.getAttribute('type') == 'int':
if node.getAttribute("type") == "int":
return int(value_to_use)
elif node.getAttribute('type') == 'list':
if not value_to_use: # no user defined value, take default value from the XML
options = [option for option in node.childNodes if option.nodeName == 'option']
selected = [option for option in options if option.getAttribute('selected') == 'true']
cat, param = node.parentNode.getAttribute('name'), node.getAttribute('name')
elif node.getAttribute("type") == "list":
if (
not value_to_use
): # no user defined value, take default value from the XML
options = [
option
for option in node.childNodes
if option.nodeName == "option"
]
selected = [
option
for option in options
if option.getAttribute("selected") == "true"
]
cat, param = (
node.parentNode.getAttribute("name"),
node.getAttribute("name"),
)
if len(selected) == 1:
value_to_use = selected[0].getAttribute('value')
log.info(_("Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'") %
{'cat': cat, 'param': param, 'value': value_to_use})
value_to_use = selected[0].getAttribute("value")
log.info(
_(
"Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'"
)
% {"cat": cat, "param": param, "value": value_to_use}
)
return value_to_use
if len(selected) == 0:
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has no default option!') % {'cat': cat, 'param': param})
log.error(
_(
u"Parameter (%(cat)s, %(param)s) of type list has no default option!"
)
% {"cat": cat, "param": param}
)
else:
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has more than one default option!') % {'cat': cat, 'param': param})
log.error(
_(
u"Parameter (%(cat)s, %(param)s) of type list has more than one default option!"
)
% {"cat": cat, "param": param}
)
raise exceptions.DataError
elif node.getAttribute('type') == 'jids_list':
elif node.getAttribute("type") == "jids_list":
if value_to_use:
jids = value_to_use.split('\t') # FIXME: it's not good to use tabs as separator !
jids = value_to_use.split(
"\t"
) # FIXME: it's not good to use tabs as separator !
else: # no user defined value, take default value from the XML
jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")]
to_delete = []
@ -395,7 +465,9 @@ class Params(object):
try:
jids[idx] = jid.JID(value)
except (RuntimeError, jid.InvalidFormat, AttributeError):
log.warning(u"Incorrect jid value found in jids list: [{}]".format(value))
log.warning(
u"Incorrect jid value found in jids list: [{}]".format(value)
)
to_delete.append(value)
for value in to_delete:
jids.remove(value)
@ -412,8 +484,10 @@ class Params(object):
@param value: user defined value
@return (unicode, bool, int, list): value to retrieve
"""
if attr == 'value' and node.getAttribute('type') == 'password':
raise exceptions.InternalError('To retrieve password values, use _asyncGetAttr instead of _getAttr')
if attr == "value" and node.getAttribute("type") == "password":
raise exceptions.InternalError(
"To retrieve password values, use _asyncGetAttr instead of _getAttr"
)
return self._getAttr_internal(node, attr, value)
def _asyncGetAttr(self, node, attr, value, profile=None):
@ -428,19 +502,27 @@ class Params(object):
@return (unicode, bool, int, list): Deferred value to retrieve
"""
value = self._getAttr_internal(node, attr, value)
if attr != 'value' or node.getAttribute('type') != 'password':
if attr != "value" or node.getAttribute("type") != "password":
return defer.succeed(value)
param_cat = node.parentNode.getAttribute('name')
param_name = node.getAttribute('name')
param_cat = node.parentNode.getAttribute("name")
param_name = node.getAttribute("name")
if ((param_cat, param_name) == C.PROFILE_PASS_PATH) or not value:
return defer.succeed(value) # profile password and empty passwords are returned "as is"
return defer.succeed(
value
) # profile password and empty passwords are returned "as is"
if not profile:
raise exceptions.ProfileNotSetError('The profile is needed to decrypt a password')
raise exceptions.ProfileNotSetError(
"The profile is needed to decrypt a password"
)
d = self.host.memory.decryptValue(value, profile)
def gotPlainPassword(password):
if password is None: # empty value means empty password, None means decryption failure
raise exceptions.InternalError(_('The stored password could not be decrypted!'))
if (
password is None
): # empty value means empty password, None means decryption failure
raise exceptions.InternalError(
_("The stored password could not be decrypted!")
)
return password
return d.addCallback(gotPlainPassword)
@ -455,9 +537,13 @@ class Params(object):
def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
""" Same as getParamA but for bridge: convert non string value to string """
return self.__type_to_string(self.getParamA(name, category, attr, profile_key=profile_key))
return self.__type_to_string(
self.getParamA(name, category, attr, profile_key=profile_key)
)
def getParamA(self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE):
def getParamA(
self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE
):
"""Helper method to get a specific attribute.
/!\ This method would return encrypted password values,
@ -474,15 +560,22 @@ class Params(object):
# FIXME: security_limit is not managed here !
node = self._getParamNode(name, category)
if not node:
log.error(_(u"Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
log.error(
_(
u"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
)
% {"name": name, "category": category}
)
raise exceptions.NotFound
if attr == 'value' and node[1].getAttribute('type') == 'password':
raise exceptions.InternalError('To retrieve password values, use asyncGetParamA instead of getParamA')
if attr == "value" and node[1].getAttribute("type") == "password":
raise exceptions.InternalError(
"To retrieve password values, use asyncGetParamA instead of getParamA"
)
if node[0] == C.GENERAL:
value = self._getParam(category, name, C.GENERAL)
if value is None and attr=='value' and not use_default:
if value is None and attr == "value" and not use_default:
return value
return self._getAttr(node[1], attr, value)
@ -490,25 +583,39 @@ class Params(object):
profile = self.getProfileName(profile_key)
if not profile:
log.error(_('Requesting a param for an non-existant profile'))
log.error(_("Requesting a param for an non-existant profile"))
raise exceptions.ProfileUnknownError(profile_key)
if profile not in self.params:
log.error(_('Requesting synchronous param for not connected profile'))
log.error(_("Requesting synchronous param for not connected profile"))
raise exceptions.ProfileNotConnected(profile)
if attr == "value":
value = self._getParam(category, name, profile=profile)
if value is None and attr=='value' and not use_default:
if value is None and attr == "value" and not use_default:
return value
return self._getAttr(node[1], attr, value)
def asyncGetStringParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
def asyncGetStringParamA(
self,
name,
category,
attr="value",
security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE,
):
d = self.asyncGetParamA(name, category, attr, security_limit, profile_key)
d.addCallback(self.__type_to_string)
return d
def asyncGetParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
def asyncGetParamA(
self,
name,
category,
attr="value",
security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE,
):
"""Helper method to get a specific attribute.
@param name: name of the parameter
@ -519,12 +626,21 @@ class Params(object):
"""
node = self._getParamNode(name, category)
if not node:
log.error(_(u"Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
log.error(
_(
u"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
)
% {"name": name, "category": category}
)
raise ValueError("Requested param doesn't exist")
if not self.checkSecurityLimit(node[1], security_limit):
log.warning(_(u"Trying to get parameter '%(param)s' in category '%(cat)s' without authorization!!!"
% {'param': name, 'cat': category}))
log.warning(
_(
u"Trying to get parameter '%(param)s' in category '%(cat)s' without authorization!!!"
% {"param": name, "cat": category}
)
)
raise exceptions.PermissionError
if node[0] == C.GENERAL:
@ -535,7 +651,9 @@ class Params(object):
profile = self.getProfileName(profile_key)
if not profile:
raise exceptions.InternalError(_('Requesting a param for a non-existant profile'))
raise exceptions.InternalError(
_("Requesting a param for a non-existant profile")
)
if attr != "value":
return defer.succeed(node[1].getAttribute(attr))
@ -543,9 +661,11 @@ class Params(object):
value = self._getParam(category, name, profile=profile)
return self._asyncGetAttr(node[1], attr, value, profile)
except exceptions.ProfileNotInCacheError:
#We have to ask data to the storage manager
# We have to ask data to the storage manager
d = self.storage.getIndParam(category, name, profile)
return d.addCallback(lambda value: self._asyncGetAttr(node[1], attr, value, profile))
return d.addCallback(
lambda value: self._asyncGetAttr(node[1], attr, value, profile)
)
def asyncGetParamsValuesFromCategory(self, category, security_limit, profile_key):
"""Get all parameters "attribute" for a category
@ -557,7 +677,7 @@ class Params(object):
@param profile_key: %(doc_profile_key)s
@return (dict): key: param name, value: param value (converted to string if needed)
"""
#TODO: manage category of general type (without existant profile)
# TODO: manage category of general type (without existant profile)
profile = self.getProfileName(profile_key)
if not profile:
log.error(_("Asking params for inexistant profile"))
@ -572,11 +692,20 @@ class Params(object):
for category_node in prof_xml.getElementsByTagName("category"):
if category_node.getAttribute("name") == category:
for param_node in category_node.getElementsByTagName("param"):
name = param_node.getAttribute('name')
name = param_node.getAttribute("name")
if not name:
log.warning(u"ignoring attribute without name: {}".format(param_node.toxml()))
log.warning(
u"ignoring attribute without name: {}".format(
param_node.toxml()
)
)
continue
d = self.asyncGetStringParamA(name, category, security_limit=security_limit, profile_key=profile)
d = self.asyncGetStringParamA(
name,
category,
security_limit=security_limit,
profile_key=profile,
)
d.addCallback(setValue, ret, name)
names_d_list.append(d)
break
@ -586,10 +715,12 @@ class Params(object):
dlist.addCallback(lambda dummy: ret)
return ret
d = self._constructProfileXml(security_limit, '', profile)
d = self._constructProfileXml(security_limit, "", profile)
return d.addCallback(returnCategoryXml)
def _getParam(self, category, name, type_=C.INDIVIDUAL, cache=None, profile=C.PROF_KEY_NONE):
def _getParam(
self, category, name, type_=C.INDIVIDUAL, cache=None, profile=C.PROF_KEY_NONE
):
"""Return the param, or None if it doesn't exist
@param category: param category
@ -608,8 +739,10 @@ class Params(object):
raise exceptions.ProfileNotSetError
if profile in self.params:
cache = self.params[profile] # if profile is in main cache, we use it,
# ignoring the temporary cache
elif cache is None: # else we use the temporary cache if it exists, or raise an exception
# ignoring the temporary cache
elif (
cache is None
): # else we use the temporary cache if it exists, or raise an exception
raise exceptions.ProfileNotInCacheError
if (category, name) not in cache:
return None
@ -629,11 +762,13 @@ class Params(object):
def checkNode(node):
"""Check the node against security_limit and app"""
return self.checkSecurityLimit(node, security_limit) and self.checkApp(node, app)
return self.checkSecurityLimit(node, security_limit) and self.checkApp(
node, app
)
def constructProfile(ignore, profile_cache):
# init the result document
prof_xml = minidom.parseString('<params/>')
prof_xml = minidom.parseString("<params/>")
cache = {}
for type_node in self.dom.documentElement.childNodes:
@ -641,9 +776,9 @@ class Params(object):
continue
# we use all params, general and individual
for cat_node in type_node.childNodes:
if cat_node.nodeName != 'category':
if cat_node.nodeName != "category":
continue
category = cat_node.getAttribute('name')
category = cat_node.getAttribute("name")
dest_params = {} # result (merged) params for category
if category not in cache:
# we make a copy for the new xml
@ -655,7 +790,7 @@ class Params(object):
if not checkNode(node):
to_remove.append(node)
continue
dest_params[node.getAttribute('name')] = node
dest_params[node.getAttribute("name")] = node
for node in to_remove:
dest_cat.removeChild(node)
new_node = True
@ -668,7 +803,7 @@ class Params(object):
for param_node in params:
# we have to merge new params (we are parsing individual parameters, we have to add them
# to the previously parsed general ones)
name = param_node.getAttribute('name')
name = param_node.getAttribute("name")
if not checkNode(param_node):
continue
if name not in dest_params:
@ -676,35 +811,53 @@ class Params(object):
dest_params[name] = param_node.cloneNode(True)
dest_cat.appendChild(dest_params[name])
profile_value = self._getParam(category,
name, type_node.nodeName,
cache=profile_cache, profile=profile)
profile_value = self._getParam(
category,
name,
type_node.nodeName,
cache=profile_cache,
profile=profile,
)
if profile_value is not None:
# there is a value for this profile, we must change the default
if dest_params[name].getAttribute('type') == 'list':
for option in dest_params[name].getElementsByTagName("option"):
if option.getAttribute('value') == profile_value:
option.setAttribute('selected', 'true')
if dest_params[name].getAttribute("type") == "list":
for option in dest_params[name].getElementsByTagName(
"option"
):
if option.getAttribute("value") == profile_value:
option.setAttribute("selected", "true")
else:
try:
option.removeAttribute('selected')
option.removeAttribute("selected")
except NotFoundErr:
pass
elif dest_params[name].getAttribute('type') == 'jids_list':
jids = profile_value.split('\t')
for jid_elt in dest_params[name].getElementsByTagName("jid"):
dest_params[name].removeChild(jid_elt) # remove all default
elif dest_params[name].getAttribute("type") == "jids_list":
jids = profile_value.split("\t")
for jid_elt in dest_params[name].getElementsByTagName(
"jid"
):
dest_params[name].removeChild(
jid_elt
) # remove all default
for jid_ in jids: # rebuilt the children with use values
try:
jid.JID(jid_)
except (RuntimeError, jid.InvalidFormat, AttributeError):
log.warning(u"Incorrect jid value found in jids list: [{}]".format(jid_))
except (
RuntimeError,
jid.InvalidFormat,
AttributeError,
):
log.warning(
u"Incorrect jid value found in jids list: [{}]".format(
jid_
)
)
else:
jid_elt = prof_xml.createElement('jid')
jid_elt = prof_xml.createElement("jid")
jid_elt.appendChild(prof_xml.createTextNode(jid_))
dest_params[name].appendChild(jid_elt)
else:
dest_params[name].setAttribute('value', profile_value)
dest_params[name].setAttribute("value", profile_value)
if new_node:
prof_xml.documentElement.appendChild(dest_cat)
@ -721,7 +874,7 @@ class Params(object):
d = defer.succeed(None)
profile_cache = self.params[profile]
else:
#profile is not in cache, we load values in a short time cache
# profile is not in cache, we load values in a short time cache
profile_cache = {}
d = self.loadIndParams(profile, profile_cache)
@ -761,9 +914,11 @@ class Params(object):
def returnXML(prof_xml):
return_xml = prof_xml.toxml()
prof_xml.unlink()
return '\n'.join((line for line in return_xml.split('\n') if line))
return "\n".join((line for line in return_xml.split("\n") if line))
return self._constructProfileXml(security_limit, app, profile).addCallback(returnXML)
return self._constructProfileXml(security_limit, app, profile).addCallback(
returnXML
)
def _getParamNode(self, name, category, type_="@ALL@"): # FIXME: is type_ useful ?
"""Return a node from the param_xml
@ -776,9 +931,14 @@ class Params(object):
@return: a tuple (node type, node) or None if not found"""
for type_node in self.dom.documentElement.childNodes:
if (((type_ == "@ALL@" or type_ == "@GENERAL@") and type_node.nodeName == C.GENERAL)
or ((type_ == "@ALL@" or type_ == "@INDIVIDUAL@") and type_node.nodeName == C.INDIVIDUAL)):
for node in type_node.getElementsByTagName('category'):
if (
(type_ == "@ALL@" or type_ == "@GENERAL@")
and type_node.nodeName == C.GENERAL
) or (
(type_ == "@ALL@" or type_ == "@INDIVIDUAL@")
and type_node.nodeName == C.INDIVIDUAL
):
for node in type_node.getElementsByTagName("category"):
if node.getAttribute("name") == category:
params = node.getElementsByTagName("param")
for param in params:
@ -795,7 +955,14 @@ class Params(object):
categories.append(cat.getAttribute("name"))
return categories
def setParam(self, name, value, category, security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
def setParam(
self,
name,
value,
category,
security_limit=C.NO_SECURITY_LIMIT,
profile_key=C.PROF_KEY_NONE,
):
"""Set a parameter, return None if the parameter is not in param xml.
Parameter of type 'password' that are not the SàT profile password are
@ -813,42 +980,59 @@ class Params(object):
if profile_key != C.PROF_KEY_NONE:
profile = self.getProfileName(profile_key)
if not profile:
log.error(_(u'Trying to set parameter for an unknown profile'))
log.error(_(u"Trying to set parameter for an unknown profile"))
raise exceptions.ProfileUnknownError(profile_key)
node = self._getParamNode(name, category, '@ALL@')
node = self._getParamNode(name, category, "@ALL@")
if not node:
log.error(_(u'Requesting an unknown parameter (%(category)s/%(name)s)')
% {'category': category, 'name': name})
log.error(
_(u"Requesting an unknown parameter (%(category)s/%(name)s)")
% {"category": category, "name": name}
)
return defer.succeed(None)
if not self.checkSecurityLimit(node[1], security_limit):
log.warning(_(u"Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!"
% {'param': name, 'cat': category}))
log.warning(
_(
u"Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!"
% {"param": name, "cat": category}
)
)
return defer.succeed(None)
type_ = node[1].getAttribute("type")
if type_ == 'int':
if type_ == "int":
if not value: # replace with the default value (which might also be '')
value = node[1].getAttribute("value")
else:
try:
int(value)
except ValueError:
log.debug(_(u"Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value"
% {'param': name, 'cat': category}))
log.debug(
_(
u"Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value"
% {"param": name, "cat": category}
)
)
return defer.succeed(None)
if node[1].hasAttribute("constraint"):
constraint = node[1].getAttribute("constraint")
try:
min_, max_ = [int(limit) for limit in constraint.split(";")]
except ValueError:
raise exceptions.InternalError("Invalid integer parameter constraint: %s" % constraint)
raise exceptions.InternalError(
"Invalid integer parameter constraint: %s" % constraint
)
value = str(min(max(int(value), min_), max_))
log.info(_("Setting parameter (%(category)s, %(name)s) = %(value)s") %
{'category': category, 'name': name, 'value': value if type_ != 'password' else '********'})
log.info(
_("Setting parameter (%(category)s, %(name)s) = %(value)s")
% {
"category": category,
"name": name,
"value": value if type_ != "password" else "********",
}
)
if node[0] == C.GENERAL:
self.params_gen[(category, name)] = value
@ -856,7 +1040,9 @@ class Params(object):
for profile in self.storage.getProfilesList():
if self.host.memory.isSessionStarted(profile):
self.host.bridge.paramUpdate(name, value, category, profile)
self.host.trigger.point("paramUpdateTrigger", name, value, category, node[0], profile)
self.host.trigger.point(
"paramUpdateTrigger", name, value, category, node[0], profile
)
return defer.succeed(None)
assert node[0] == C.INDIVIDUAL
@ -867,16 +1053,24 @@ class Params(object):
return defer.succeed(None)
elif type_ == "password":
try:
personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY]
personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[
C.MEMORY_CRYPTO_KEY
]
except TypeError:
raise exceptions.InternalError(_('Trying to encrypt a password while the personal key is undefined!'))
raise exceptions.InternalError(
_("Trying to encrypt a password while the personal key is undefined!")
)
if (category, name) == C.PROFILE_PASS_PATH:
# using 'value' as the encryption key to encrypt another encryption key... could be confusing!
d = self.host.memory.encryptPersonalData(data_key=C.MEMORY_CRYPTO_KEY,
data_value=personal_key,
crypto_key=value,
profile=profile)
d.addCallback(lambda dummy: PasswordHasher.hash(value)) # profile password is hashed (empty value stays empty)
d = self.host.memory.encryptPersonalData(
data_key=C.MEMORY_CRYPTO_KEY,
data_value=personal_key,
crypto_key=value,
profile=profile,
)
d.addCallback(
lambda dummy: PasswordHasher.hash(value)
) # profile password is hashed (empty value stays empty)
elif value: # other non empty passwords are encrypted with the personal key
d = BlockCipher.encrypt(personal_key, value)
else:
@ -888,7 +1082,9 @@ class Params(object):
if self.host.memory.isSessionStarted(profile):
self.params[profile][(category, name)] = value
self.host.bridge.paramUpdate(name, value, category, profile)
self.host.trigger.point("paramUpdateTrigger", name, value, category, node[0], profile)
self.host.trigger.point(
"paramUpdateTrigger", name, value, category, node[0], profile
)
return self.storage.setIndParam(category, name, value, profile)
else:
raise exceptions.ProfileNotConnected
@ -912,10 +1108,15 @@ class Params(object):
"""
ret = {}
for type_node in self.dom.documentElement.childNodes:
if (((node_type == "@ALL@" or node_type == "@GENERAL@") and type_node.nodeName == C.GENERAL) or
((node_type == "@ALL@" or node_type == "@INDIVIDUAL@") and type_node.nodeName == C.INDIVIDUAL)):
for cat_node in type_node.getElementsByTagName('category'):
cat = cat_node.getAttribute('name')
if (
(node_type == "@ALL@" or node_type == "@GENERAL@")
and type_node.nodeName == C.GENERAL
) or (
(node_type == "@ALL@" or node_type == "@INDIVIDUAL@")
and type_node.nodeName == C.INDIVIDUAL
):
for cat_node in type_node.getElementsByTagName("category"):
cat = cat_node.getAttribute("name")
params = cat_node.getElementsByTagName("param")
for param in params:
if param.getAttribute("type") == attr_type: