bitbot-3.11-fork/modules/sasl/__init__.py

108 lines
4.1 KiB
Python
Raw Normal View History

import base64, hashlib, hmac, uuid
from src import ModuleManager, utils
from . import scram
2018-07-15 22:56:06 +00:00
def _validate(self, s):
mechanism, _, arguments = s.partition(" ")
return {"mechanism": mechanism, "args": arguments}
def _scram_nonce():
return str(uuid.uuid4().hex)
def _scram_escape(s):
return s.replace("=", "=3D").replace(",", "=2C")
def _scram_unescape(s):
return s.replace("=3D", "=").replace("=2C", ",")
def _scram_xor(s1, s2):
return bytes(a ^ b for a, b in zip(s1, s2))
@utils.export("serverset", {"setting": "sasl",
"help": "Set the sasl username/password for this server",
"validate": _validate})
class Module(ModuleManager.BaseModule):
@utils.hook("received.cap.ls")
2018-07-15 22:56:06 +00:00
def on_cap(self, event):
2018-09-07 14:51:41 +00:00
has_sasl = "sasl" in event["capabilities"]
our_sasl = event["server"].get_setting("sasl", None)
2018-09-07 14:51:41 +00:00
do_sasl = False
if has_sasl and our_sasl:
if not event["capabilities"]["sasl"] == None:
our_mechanism = our_sasl["mechanism"].upper()
do_sasl = our_mechanism in event["capabilities"
]["sasl"].split(",")
else:
do_sasl = True
if do_sasl:
event["server"].queue_capability("sasl")
2018-09-07 14:51:41 +00:00
@utils.hook("received.cap.ack")
def on_cap_ack(self, event):
if "sasl" in event["capabilities"]:
2018-09-17 10:31:40 +00:00
sasl = event["server"].get_setting("sasl")
event["server"].send_authenticate(sasl["mechanism"].upper())
event["server"].wait_for_capability("sasl")
2018-07-15 22:56:06 +00:00
@utils.hook("received.authenticate")
2018-07-15 22:56:06 +00:00
def on_authenticate(self, event):
sasl = event["server"].get_setting("sasl")
mechanism = sasl["mechanism"].upper()
auth_text = None
if mechanism == "PLAIN":
if event["message"] != "+":
event["server"].send_authenticate("*")
else:
sasl_username, sasl_password = sasl["args"].split(":", 1)
auth_text = ("%s\0%s\0%s" % (
sasl_username, sasl_username, sasl_password)).encode("utf8")
2018-09-17 10:31:40 +00:00
elif mechanism == "EXTERNAL":
if event["message"] != "+":
event["server"].send_authenticate("*")
else:
2018-09-17 10:31:40 +00:00
auth_text = "+"
elif mechanism.startswith("SCRAM-"):
algo = mechanism.split("SCRAM-", 1)[1].replace("-", "")
sasl_username, sasl_password = sasl["args"].split(":", 1)
if event["message"] == "+":
# start SCRAM handshake
event["server"]._scram = scram.SCRAM(
algo, sasl_username, sasl_password)
auth_text = event["server"]._scram.client_first()
2018-09-17 10:31:40 +00:00
else:
current_scram = event["server"]._scram
data = base64.b64decode(event["message"])
if current_scram.state == scram.SCRAMState.ClientFirst:
auth_text = current_scram.server_first(data)
elif current_scram.state == scram.SCRAMState.ClientFinal:
verified = current_scram.server_final(data)
del event["server"]._scram
if verified:
auth_text = "+"
else:
if current_scram.state == scram.SCRAMState.VerifyFailed:
event["server"].disconnect()
raise ValueError("Server SCRAM verification failed")
self._end_sasl(event["server"])
else:
raise ValueError("unknown sasl mechanism '%s'" % mechanism)
2018-09-17 10:31:40 +00:00
if not auth_text == None:
if not auth_text == "+":
auth_text = base64.b64encode(auth_text)
auth_text = auth_text.decode("utf8")
event["server"].send_authenticate(auth_text)
2018-09-17 11:56:41 +00:00
def _end_sasl(self, server):
server.capability_done("sasl")
@utils.hook("received.numeric.903")
def sasl_success(self, event):
2018-09-17 11:56:41 +00:00
self._end_sasl(event["server"])
@utils.hook("received.numeric.904")
def sasl_failure(self, event):
2018-09-17 11:56:41 +00:00
self._end_sasl(event["server"])