2018-07-21 13:19:01 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# XEP-0258: Security Labels in XMPP
|
|
|
|
|
|
|
|
import nbxmpp
|
|
|
|
|
|
|
|
from gajim.common import app
|
|
|
|
from gajim.common.nec import NetworkIncomingEvent
|
2019-03-09 23:16:27 +01:00
|
|
|
from gajim.common.modules.base import BaseModule
|
2018-07-21 13:19:01 +02:00
|
|
|
|
|
|
|
|
2019-03-09 23:16:27 +01:00
|
|
|
class SecLabels(BaseModule):
|
2018-07-21 13:19:01 +02:00
|
|
|
def __init__(self, con):
|
2019-03-09 23:16:27 +01:00
|
|
|
BaseModule.__init__(self, con)
|
2018-07-21 13:19:01 +02:00
|
|
|
|
|
|
|
self._catalogs = {}
|
|
|
|
self.supported = False
|
|
|
|
|
2018-09-11 22:25:55 +02:00
|
|
|
def pass_disco(self, from_, _identities, features, _data, _node):
|
2018-07-22 12:18:24 +02:00
|
|
|
if nbxmpp.NS_SECLABEL not in features:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.supported = True
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Discovered security labels: %s', from_)
|
2018-07-22 12:18:24 +02:00
|
|
|
|
2018-07-21 13:19:01 +02:00
|
|
|
def request_catalog(self, jid):
|
|
|
|
server = app.get_jid_from_account(self._account).split("@")[1]
|
|
|
|
iq = nbxmpp.Iq(typ='get', to=server)
|
|
|
|
iq.addChild(name='catalog',
|
|
|
|
namespace=nbxmpp.NS_SECLABEL_CATALOG,
|
2018-09-30 00:15:00 +02:00
|
|
|
attrs={'to': jid})
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Request catalog: server: %s, to: %s', server, jid)
|
2018-07-21 13:19:01 +02:00
|
|
|
self._con.connection.SendAndCallForResponse(
|
|
|
|
iq, self._catalog_received)
|
|
|
|
|
|
|
|
def _catalog_received(self, stanza):
|
|
|
|
if not nbxmpp.isResultNode(stanza):
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Error: %s', stanza.getError())
|
2018-07-21 13:19:01 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
query = stanza.getTag('catalog', namespace=nbxmpp.NS_SECLABEL_CATALOG)
|
|
|
|
to = query.getAttr('to')
|
|
|
|
items = query.getTags('item')
|
|
|
|
|
|
|
|
labels = {}
|
|
|
|
label_list = []
|
|
|
|
default = None
|
|
|
|
for item in items:
|
|
|
|
label = item.getAttr('selector')
|
|
|
|
labels[label] = item.getTag('securitylabel')
|
|
|
|
label_list.append(label)
|
|
|
|
if item.getAttr('default') == 'true':
|
|
|
|
default = label
|
|
|
|
|
|
|
|
catalog = (labels, label_list, default)
|
|
|
|
self._catalogs[to] = catalog
|
|
|
|
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Received catalog: %s', to)
|
|
|
|
self._log.debug(catalog)
|
2018-09-29 18:29:59 +02:00
|
|
|
|
2018-07-21 13:19:01 +02:00
|
|
|
app.nec.push_incoming_event(SecLabelCatalog(
|
2018-09-30 00:15:00 +02:00
|
|
|
None, account=self._account, jid=to, catalog=catalog))
|
2018-07-21 13:19:01 +02:00
|
|
|
|
|
|
|
def get_catalog(self, jid):
|
|
|
|
return self._catalogs.get(jid)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_securitylabel(stanza):
|
|
|
|
seclabel = stanza.getTag('securitylabel', namespace=nbxmpp.NS_SECLABEL)
|
|
|
|
if seclabel is None:
|
|
|
|
return None
|
|
|
|
return seclabel.getTag('displaymarking')
|
|
|
|
|
|
|
|
|
|
|
|
class SecLabelCatalog(NetworkIncomingEvent):
|
|
|
|
name = 'sec-catalog-received'
|
|
|
|
|
|
|
|
|
|
|
|
def get_instance(*args, **kwargs):
|
|
|
|
return SecLabels(*args, **kwargs), 'SecLabels'
|