2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on config
|
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
import base64, hashlib, hmac, typing, uuid
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2019-02-05 15:54:20 +00:00
|
|
|
from . import scram
|
2018-07-15 22:56:06 +00:00
|
|
|
|
2019-05-19 10:13:37 +00:00
|
|
|
CAP = utils.irc.Capability("sasl")
|
|
|
|
|
2019-02-14 11:57:53 +00:00
|
|
|
USERPASS_MECHANISMS = [
|
|
|
|
"SCRAM-SHA-512",
|
|
|
|
"SCRAM-SHA-256",
|
|
|
|
"SCRAM-SHA-1",
|
|
|
|
"PLAIN"
|
|
|
|
]
|
2019-10-04 09:25:48 +00:00
|
|
|
ALL_MECHANISMS = USERPASS_MECHANISMS+["EXTERNAL"]
|
2019-02-14 11:57:53 +00:00
|
|
|
|
2019-09-04 13:27:10 +00:00
|
|
|
def _parse(value):
|
|
|
|
mechanism, _, arguments = value.partition(" ")
|
2019-10-04 09:25:48 +00:00
|
|
|
mechanism = mechanism.upper()
|
|
|
|
|
|
|
|
if mechanism in ALL_MECHANISMS:
|
|
|
|
return {"mechanism": mechanism.upper(), "args": arguments}
|
|
|
|
else:
|
|
|
|
raise utils.SettingParseException("Unknown SASL mechanism '%s'"
|
|
|
|
% mechanism)
|
2018-09-09 09:04:21 +00:00
|
|
|
|
2019-10-30 11:20:07 +00:00
|
|
|
SASL_TIMEOUT = 15 # 15 seconds
|
|
|
|
|
2019-09-30 09:41:57 +00:00
|
|
|
HARDFAIL = utils.BoolSetting("sasl-hard-fail",
|
|
|
|
"Set whether a SASL failure should cause a disconnect")
|
|
|
|
|
2019-09-04 13:27:10 +00:00
|
|
|
@utils.export("serverset", utils.FunctionSetting(_parse, "sasl",
|
2019-06-28 22:16:05 +00:00
|
|
|
"Set the sasl username/password for this server",
|
2019-10-07 12:12:17 +00:00
|
|
|
example="PLAIN BitBot:hunter2", format=utils.sensitive_format))
|
2019-09-30 09:41:57 +00:00
|
|
|
@utils.export("serverset", HARDFAIL)
|
|
|
|
@utils.export("botset", HARDFAIL)
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-10-30 11:20:07 +00:00
|
|
|
@utils.hook("new.server")
|
|
|
|
def new_server(self, event):
|
|
|
|
event["server"]._sasl_timeout = None
|
2019-11-08 13:30:08 +00:00
|
|
|
event["server"]._sasl_retry = False
|
2019-10-30 11:20:07 +00:00
|
|
|
|
2019-02-14 11:57:53 +00:00
|
|
|
def _best_userpass_mechanism(self, mechanisms):
|
|
|
|
for potential_mechanism in USERPASS_MECHANISMS:
|
|
|
|
if potential_mechanism in mechanisms:
|
|
|
|
return potential_mechanism
|
|
|
|
|
2019-11-08 13:30:08 +00:00
|
|
|
def _mech_match(self, server, server_mechanisms):
|
|
|
|
our_sasl = server.get_setting("sasl", None)
|
2019-11-08 13:34:05 +00:00
|
|
|
if not our_sasl:
|
|
|
|
return None
|
|
|
|
|
2019-11-08 13:30:08 +00:00
|
|
|
our_mechanism = our_sasl["mechanism"].upper()
|
|
|
|
|
|
|
|
if not server_mechanisms and our_mechanism in ALL_MECHANISMS:
|
|
|
|
return our_mechanism
|
|
|
|
elif our_mechanism in server_mechanisms:
|
|
|
|
return our_mechanism
|
|
|
|
elif our_mechanism == "USERPASS":
|
|
|
|
if server_mechanisms:
|
|
|
|
return self._best_userpass_mechanism(server_mechanisms)
|
|
|
|
else:
|
|
|
|
return USERPASS_MECHANISMS[0]
|
|
|
|
return None
|
|
|
|
|
2019-02-09 02:58:25 +00:00
|
|
|
@utils.hook("received.cap.new")
|
2018-10-03 12:22:37 +00:00
|
|
|
@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"]
|
2019-11-08 13:30:08 +00:00
|
|
|
if has_sasl:
|
|
|
|
server_mechanisms = event["capabilities"]["sasl"]
|
|
|
|
if server_mechanisms:
|
|
|
|
server_mechanisms = server_mechanisms.split(",")
|
2018-09-17 10:49:23 +00:00
|
|
|
else:
|
2019-11-08 13:30:08 +00:00
|
|
|
server_mechanisms = []
|
|
|
|
|
|
|
|
mechanism = self._mech_match(event["server"], server_mechanisms)
|
2019-02-14 11:57:53 +00:00
|
|
|
|
2019-11-08 13:30:08 +00:00
|
|
|
if mechanism:
|
|
|
|
cap = CAP.copy()
|
|
|
|
cap.on_ack(
|
|
|
|
lambda: self._sasl_ack(event["server"], mechanism))
|
|
|
|
return cap
|
|
|
|
|
|
|
|
def _sasl_ack(self, server, mechanism):
|
2019-05-11 17:22:40 +00:00
|
|
|
server.send_authenticate(mechanism)
|
2019-11-08 13:30:08 +00:00
|
|
|
server._sasl_timeout = self.timers.add("sasl-timeout",
|
|
|
|
self._sasl_timeout, SASL_TIMEOUT, server=server)
|
|
|
|
server._sasl_mechanism = mechanism
|
2019-10-30 11:41:49 +00:00
|
|
|
|
2019-05-11 17:22:40 +00:00
|
|
|
server.wait_for_capability("sasl")
|
2018-07-15 22:56:06 +00:00
|
|
|
|
2019-10-30 11:20:07 +00:00
|
|
|
def _sasl_timeout(self, timer):
|
|
|
|
server = timer.kwargs["server"]
|
|
|
|
self._panic(server, "SASL handshake timed out")
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.authenticate")
|
2018-07-15 22:56:06 +00:00
|
|
|
def on_authenticate(self, event):
|
2019-02-05 12:17:25 +00:00
|
|
|
sasl = event["server"].get_setting("sasl")
|
2019-11-08 13:30:08 +00:00
|
|
|
mechanism = event["server"]._sasl_mechanism
|
2019-02-05 12:17:25 +00:00
|
|
|
|
2019-02-06 15:36:59 +00:00
|
|
|
auth_text = None
|
2019-02-05 12:17:25 +00:00
|
|
|
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
|
|
|
|
2019-02-05 12:17:25 +00:00
|
|
|
elif mechanism == "EXTERNAL":
|
|
|
|
if event["message"] != "+":
|
|
|
|
event["server"].send_authenticate("*")
|
|
|
|
else:
|
2018-09-17 10:31:40 +00:00
|
|
|
auth_text = "+"
|
2019-02-05 12:17:25 +00:00
|
|
|
|
|
|
|
elif mechanism.startswith("SCRAM-"):
|
2019-02-06 21:49:44 +00:00
|
|
|
|
2019-02-05 12:17:25 +00:00
|
|
|
if event["message"] == "+":
|
|
|
|
# start SCRAM handshake
|
2019-02-06 21:49:44 +00:00
|
|
|
|
|
|
|
# create SCRAM helper
|
|
|
|
sasl_username, sasl_password = sasl["args"].split(":", 1)
|
2019-02-06 22:28:50 +00:00
|
|
|
algo = mechanism.split("SCRAM-", 1)[1]
|
2019-02-05 15:54:20 +00:00
|
|
|
event["server"]._scram = scram.SCRAM(
|
|
|
|
algo, sasl_username, sasl_password)
|
2019-02-06 21:49:44 +00:00
|
|
|
|
|
|
|
# generate client-first-message
|
2019-02-05 15:54:20 +00:00
|
|
|
auth_text = event["server"]._scram.client_first()
|
2018-09-17 10:31:40 +00:00
|
|
|
else:
|
2019-02-05 15:54:20 +00:00
|
|
|
current_scram = event["server"]._scram
|
2019-02-06 08:50:19 +00:00
|
|
|
data = base64.b64decode(event["message"])
|
2019-02-05 15:54:20 +00:00
|
|
|
if current_scram.state == scram.SCRAMState.ClientFirst:
|
2019-02-06 21:49:44 +00:00
|
|
|
# use server-first-message to generate client-final-message
|
2019-02-06 08:50:19 +00:00
|
|
|
auth_text = current_scram.server_first(data)
|
2019-02-05 15:54:20 +00:00
|
|
|
elif current_scram.state == scram.SCRAMState.ClientFinal:
|
2019-02-06 21:49:44 +00:00
|
|
|
# use server-final-message to check server proof
|
2019-02-06 11:07:50 +00:00
|
|
|
verified = current_scram.server_final(data)
|
2019-02-05 15:54:20 +00:00
|
|
|
del event["server"]._scram
|
2019-02-05 17:03:41 +00:00
|
|
|
|
2019-02-06 11:07:50 +00:00
|
|
|
if verified:
|
|
|
|
auth_text = "+"
|
|
|
|
else:
|
2019-02-06 15:38:59 +00:00
|
|
|
if current_scram.state == scram.SCRAMState.VerifyFailed:
|
2019-02-06 21:49:44 +00:00
|
|
|
# server gave a bad verification so we should panic
|
2019-06-17 13:07:44 +00:00
|
|
|
self._panic(event["server"], "SCRAM VerifyFailed")
|
2019-02-05 17:03:41 +00:00
|
|
|
|
2019-02-05 12:17:25 +00:00
|
|
|
else:
|
|
|
|
raise ValueError("unknown sasl mechanism '%s'" % mechanism)
|
2018-09-17 10:31:40 +00:00
|
|
|
|
2019-02-06 15:37:24 +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-07 15:04:37 +00:00
|
|
|
|
2018-09-17 11:56:41 +00:00
|
|
|
def _end_sasl(self, server):
|
|
|
|
server.capability_done("sasl")
|
2019-10-30 11:20:07 +00:00
|
|
|
if not server._sasl_timeout == None:
|
|
|
|
server._sasl_timeout.cancel()
|
|
|
|
server._sasl_timeout = None
|
2019-02-06 16:25:43 +00:00
|
|
|
|
2019-02-16 15:53:14 +00:00
|
|
|
@utils.hook("received.908")
|
2019-02-14 11:57:53 +00:00
|
|
|
def sasl_mechanisms(self, event):
|
2019-06-22 21:08:39 +00:00
|
|
|
server_mechanisms = event["line"].args[1].split(",")
|
2019-11-08 13:30:08 +00:00
|
|
|
mechanism = self._mech_match(event["server"], server_mechanisms)
|
|
|
|
if mechanism:
|
|
|
|
event["server"]._sasl_mechanism = mechanism
|
|
|
|
event["server"].send_authenticate(mechanism)
|
|
|
|
event["server"]._sasl_retry = True
|
2019-02-14 11:57:53 +00:00
|
|
|
|
2019-02-16 15:53:14 +00:00
|
|
|
@utils.hook("received.903")
|
2018-09-07 15:04:37 +00:00
|
|
|
def sasl_success(self, event):
|
2018-09-17 11:56:41 +00:00
|
|
|
self._end_sasl(event["server"])
|
2019-02-16 15:53:14 +00:00
|
|
|
@utils.hook("received.904")
|
2018-09-17 12:10:22 +00:00
|
|
|
def sasl_failure(self, event):
|
2019-11-08 13:30:08 +00:00
|
|
|
if not event["server"]._sasl_retry:
|
|
|
|
self._panic(event["server"], "ERR_SASLFAIL (%s)" %
|
|
|
|
event["line"].args[1])
|
|
|
|
else:
|
|
|
|
event["server"]._sasl_retry = False
|
2019-05-12 10:48:28 +00:00
|
|
|
|
|
|
|
@utils.hook("received.907")
|
|
|
|
def sasl_already(self, event):
|
|
|
|
self._end_sasl(event["server"])
|
2019-06-17 13:07:44 +00:00
|
|
|
|
|
|
|
def _panic(self, server, message):
|
2019-09-30 09:41:57 +00:00
|
|
|
if server.get_setting("sasl-hard-fail",
|
|
|
|
self.bot.get_setting("sasl-hard-fail", False)):
|
2019-06-17 13:22:08 +00:00
|
|
|
message = "SASL panic for %s: %s" % (str(server), message)
|
2019-10-30 11:03:55 +00:00
|
|
|
self.log.error(message)
|
|
|
|
self.bot.disconnect(server)
|
2019-06-17 13:07:44 +00:00
|
|
|
else:
|
2019-06-17 17:22:12 +00:00
|
|
|
self.log.warn("SASL failure for %s: %s" % (str(server), message))
|
2019-06-17 17:11:02 +00:00
|
|
|
self._end_sasl(server)
|