2019-12-01 09:48:03 +00:00
|
|
|
#--depends-on commands
|
|
|
|
|
2019-05-30 16:25:24 +00:00
|
|
|
import base64, binascii, os
|
2016-04-04 17:42:37 +00:00
|
|
|
import scrypt
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-04-04 17:42:37 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
HOSTMASKS_SETTING = "hostmask-account"
|
|
|
|
NO_PERMISSION = "You do not have permission to do that"
|
2018-08-28 17:16:19 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-11-22 11:48:29 +00:00
|
|
|
def on_load(self):
|
|
|
|
self.exports.add("is-identified", self._is_identified)
|
|
|
|
self.exports.add("account-name", self._account_name)
|
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
@utils.hook("new.server")
|
|
|
|
def new_server(self, event):
|
2019-11-30 19:32:38 +00:00
|
|
|
event["server"]._hostmasks = {}
|
2016-04-04 17:42:37 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
for account, user_hostmasks in event["server"].get_all_user_settings(
|
|
|
|
HOSTMASKS_SETTING):
|
|
|
|
for hostmask in user_hostmasks:
|
2019-11-30 19:32:38 +00:00
|
|
|
self._add_hostmask(event["server"], hostmask, account)
|
|
|
|
|
|
|
|
def _add_hostmask(self, server, hostmask, account):
|
|
|
|
server._hostmasks[hostmask] = (
|
2019-11-30 20:30:54 +00:00
|
|
|
utils.irc.hostmask_parse(hostmask), account)
|
2019-11-30 19:32:38 +00:00
|
|
|
def _remove_hostmask(self, server, hostmask):
|
|
|
|
if hostmask in server._hostmasks:
|
|
|
|
del server._hostmasks[hostmask]
|
2016-04-06 17:23:02 +00:00
|
|
|
|
2016-04-04 17:42:37 +00:00
|
|
|
def _make_salt(self):
|
2019-05-30 16:25:24 +00:00
|
|
|
return base64.b64encode(os.urandom(64)).decode("utf8")
|
|
|
|
|
2019-04-15 13:52:08 +00:00
|
|
|
def _random_password(self):
|
2019-05-30 16:25:24 +00:00
|
|
|
return binascii.hexlify(os.urandom(32)).decode("utf8")
|
2016-04-06 17:23:02 +00:00
|
|
|
|
2016-04-04 17:42:37 +00:00
|
|
|
def _make_hash(self, password, salt=None):
|
|
|
|
salt = salt or self._make_salt()
|
|
|
|
hash = base64.b64encode(scrypt.hash(password, salt)).decode("utf8")
|
|
|
|
return hash, salt
|
2016-04-06 17:23:02 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
def _get_hash(self, server, account):
|
|
|
|
hash, salt = server.get_user(account).get_setting("authentication",
|
|
|
|
(None, None))
|
|
|
|
return hash, salt
|
2016-04-06 17:23:02 +00:00
|
|
|
|
2019-11-21 16:31:23 +00:00
|
|
|
def _master_password(self):
|
|
|
|
master_password = self._random_password()
|
|
|
|
hash, salt = self._make_hash(master_password)
|
|
|
|
self.bot.set_setting("master-password", [hash, salt])
|
|
|
|
return master_password
|
|
|
|
@utils.hook("control.master-password")
|
|
|
|
def command_line(self, event):
|
|
|
|
master_password = self._master_password()
|
|
|
|
return "One-time master password: %s" % master_password
|
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
def _has_identified(self, server, user, account):
|
|
|
|
user._id_override = server.get_user_id(account)
|
|
|
|
def _is_identified(self, user):
|
|
|
|
return not user._id_override == None
|
|
|
|
def _signout(self, user):
|
|
|
|
user._id_override = None
|
|
|
|
|
|
|
|
def _find_hostmask(self, server, user):
|
|
|
|
user_hostmask = user.hostmask()
|
2019-11-30 19:32:38 +00:00
|
|
|
for hostmask, (hostmask_pattern, account) in server._hostmasks.items():
|
|
|
|
if utils.irc.hostmask_match(user_hostmask, hostmask_pattern):
|
|
|
|
return (hostmask, account)
|
2019-11-21 15:49:08 +00:00
|
|
|
def _specific_hostmask(self, server, hostmask, account):
|
|
|
|
for user in server.users.values():
|
|
|
|
if utils.irc.hostmask_match(user.hostmask(), hostmask):
|
|
|
|
if account == None:
|
|
|
|
user._hostmask_account = None
|
|
|
|
self._signout(user)
|
|
|
|
else:
|
|
|
|
user._hostmask_account = (hostmask, account)
|
|
|
|
self._has_identified(server, user, account)
|
2016-04-06 17:23:02 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
def _account_name(self, user):
|
|
|
|
if not user.account == None:
|
|
|
|
return user.account
|
|
|
|
elif not user._account_override == None:
|
|
|
|
return user._account_override
|
|
|
|
elif not user._hostmask_account == None:
|
|
|
|
return user._hostmask_account[1]
|
2019-04-15 13:52:08 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
@utils.hook("new.user")
|
|
|
|
def new_user(self, event):
|
|
|
|
event["user"]._hostmask_account = None
|
|
|
|
event["user"]._account_override = None
|
2019-11-21 16:31:23 +00:00
|
|
|
event["user"]._master_admin = False
|
2019-11-21 15:49:08 +00:00
|
|
|
|
|
|
|
def _set_hostmask(self, server, user):
|
|
|
|
account = self._find_hostmask(server, user)
|
|
|
|
if not account == None:
|
|
|
|
hostmask, account = account
|
|
|
|
user._hostmask_account = (hostmask, account)
|
|
|
|
self._has_identified(server, user, account)
|
|
|
|
|
|
|
|
@utils.hook("received.chghost")
|
|
|
|
@utils.hook("received.nick")
|
2019-11-26 18:42:27 +00:00
|
|
|
@utils.hook("received.who")
|
2019-11-26 18:45:13 +00:00
|
|
|
@utils.hook("received.whox")
|
2019-11-30 19:13:03 +00:00
|
|
|
@utils.hook("received.message.private")
|
2019-11-21 15:49:08 +00:00
|
|
|
def chghost(self, event):
|
|
|
|
if not self._is_identified(event["user"]):
|
|
|
|
self._set_hostmask(event["server"], event["user"])
|
|
|
|
@utils.hook("received.whox")
|
|
|
|
@utils.hook("received.account")
|
|
|
|
@utils.hook("received.account.login")
|
|
|
|
@utils.hook("received.account.logout")
|
|
|
|
@utils.hook("received.join")
|
|
|
|
def check_account(self, event):
|
|
|
|
if not self._is_identified(event["user"]):
|
|
|
|
if event["user"].account:
|
|
|
|
self._has_identified(event["server"], event["user"],
|
|
|
|
event["user"].account)
|
|
|
|
else:
|
|
|
|
self._set_hostmask(event["server"], event["user"])
|
2019-04-15 13:52:08 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
def _get_permissions(self, user):
|
|
|
|
if self._is_identified(user):
|
|
|
|
return user.get_setting("permissions", [])
|
|
|
|
return []
|
2019-04-15 13:52:08 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
def _has_permission(self, user, permission):
|
2019-11-21 16:31:23 +00:00
|
|
|
if user._master_admin:
|
|
|
|
return True
|
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
permissions = self._get_permissions(user)
|
|
|
|
if permission in permissions:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
permission_parts = permission.split(".")
|
|
|
|
for user_permission in permissions:
|
|
|
|
user_permission_parts = user_permission.split(".")
|
|
|
|
for i, part in enumerate(permission_parts):
|
|
|
|
last = i==(len(permission_parts)-1)
|
|
|
|
user_last = i==(len(user_permission_parts)-1)
|
|
|
|
if not permission_parts[i] == user_permission_parts[i]:
|
2019-11-21 16:36:06 +00:00
|
|
|
if user_permission_parts[i] == "*" and user_last:
|
2019-11-21 15:49:08 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if last and user_last:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-11-21 16:31:23 +00:00
|
|
|
@utils.hook("received.command.masterlogin")
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("private_only", True)
|
|
|
|
def master_login(self, event):
|
|
|
|
saved_hash, saved_salt = self.bot.get_setting("master-password",
|
|
|
|
(None, None))
|
|
|
|
if saved_hash and saved_salt:
|
|
|
|
given_hash, _ = self._make_hash(event["args"], saved_salt)
|
|
|
|
if utils.security.constant_time_compare(given_hash, saved_hash):
|
|
|
|
self.bot.del_setting("master-password")
|
|
|
|
event["user"]._master_admin = True
|
|
|
|
event["stdout"].write("Master login successful")
|
|
|
|
return
|
|
|
|
event["stderr"].write("Master login failed")
|
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
@utils.hook("received.command.mypermissions")
|
|
|
|
@utils.kwarg("authenticated", True)
|
|
|
|
def my_permissions(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-11-21 15:49:08 +00:00
|
|
|
:help: Show your permissions
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-11-21 15:49:08 +00:00
|
|
|
permissions = event["user"].get_setting("permissions", [])
|
|
|
|
event["stdout"].write("Your permissions: %s" % ", ".join(permissions))
|
|
|
|
|
|
|
|
|
|
|
|
@utils.hook("received.command.register", private_only=True, min_args=1)
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("private_only", True)
|
|
|
|
@utils.kwarg("help", "Register your nickname")
|
|
|
|
@utils.kwarg("usage", "<password>")
|
|
|
|
def register(self, event):
|
|
|
|
hash, salt = self._get_hash(event["server"], event["user"].nickname)
|
|
|
|
if not hash and not salt:
|
|
|
|
password = event["args"]
|
|
|
|
hash, salt = self._make_hash(password)
|
|
|
|
event["user"].set_setting("authentication", [hash, salt])
|
2019-11-21 16:53:27 +00:00
|
|
|
|
|
|
|
event["user"]._account_override = event["user"].nickname
|
2019-11-21 15:49:08 +00:00
|
|
|
self._has_identified(event["server"], event["user"],
|
|
|
|
event["user"].nickname)
|
2019-11-21 16:53:27 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
event["stdout"].write("Nickname registered successfully")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("This nickname is already registered")
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
@utils.hook("received.command.identify", private_only=True, min_args=1)
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("private_only", True)
|
|
|
|
@utils.kwarg("help", "Identify for your current nickname")
|
|
|
|
@utils.kwarg("usage", "[account] <password>")
|
|
|
|
def identify(self, event):
|
2016-04-04 17:48:39 +00:00
|
|
|
if not event["user"].channels:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("You must share at least one channel "
|
2016-04-04 17:48:39 +00:00
|
|
|
"with me before you can identify")
|
2018-09-05 10:58:10 +00:00
|
|
|
|
2019-11-21 15:49:08 +00:00
|
|
|
if not self._is_identified(event["user"]):
|
2018-09-29 09:51:16 +00:00
|
|
|
if len(event["args_split"]) > 1:
|
|
|
|
account = event["args_split"][0]
|
|
|
|
password = " ".join(event["args_split"][1:])
|
|
|
|
else:
|
|
|
|
account = event["user"].nickname
|
|
|
|
password = event["args"]
|
|
|
|
|
2018-09-05 10:58:10 +00:00
|
|
|
hash, salt = self._get_hash(event["server"], account)
|
2016-04-04 17:42:37 +00:00
|
|
|
if hash and salt:
|
|
|
|
attempt, _ = self._make_hash(password, salt)
|
2019-02-12 11:59:38 +00:00
|
|
|
if utils.security.constant_time_compare(attempt, hash):
|
2019-11-21 15:49:08 +00:00
|
|
|
event["user"]._account_override = account
|
|
|
|
self._has_identified(event["server"], event["user"], account)
|
|
|
|
|
2016-04-04 17:42:37 +00:00
|
|
|
event["stdout"].write("Correct password, you have "
|
2019-11-21 15:49:08 +00:00
|
|
|
"been identified as %s." % account)
|
2018-10-11 10:20:53 +00:00
|
|
|
self.events.on("internal.identified").call(
|
|
|
|
user=event["user"])
|
2016-04-04 17:42:37 +00:00
|
|
|
else:
|
2018-09-05 10:58:10 +00:00
|
|
|
event["stderr"].write("Incorrect password for '%s'" %
|
|
|
|
account)
|
2016-04-04 17:42:37 +00:00
|
|
|
else:
|
2018-09-05 10:58:10 +00:00
|
|
|
event["stderr"].write("Account '%s' is not registered" %
|
|
|
|
account)
|
2016-04-04 17:42:37 +00:00
|
|
|
else:
|
2019-11-21 15:49:08 +00:00
|
|
|
event["stderr"].write("You are already identified as %s" %
|
|
|
|
self._account_name(event["user"]))
|
|
|
|
|
|
|
|
@utils.hook("received.command.permission")
|
|
|
|
@utils.kwarg("min_args", 2)
|
|
|
|
@utils.kwarg("usage", "list <account>")
|
|
|
|
@utils.kwarg("usage", "clear <account>")
|
|
|
|
@utils.kwarg("usage", "add <account> <permission>")
|
|
|
|
@utils.kwarg("usage", "remove <account> <permission>")
|
|
|
|
@utils.kwarg("permission", "permissions.change")
|
|
|
|
def permission(self, event):
|
|
|
|
subcommand = event["args_split"][0].lower()
|
|
|
|
account = event["args_split"][1]
|
|
|
|
target_user = event["server"].get_user(account)
|
|
|
|
|
|
|
|
if subcommand == "list":
|
|
|
|
event["stdout"].write("Permissions for %s: %s" % (
|
|
|
|
account, ", ".join(self._get_permissions(target_user))))
|
|
|
|
elif subcommand == "clear":
|
|
|
|
if not self._get_permissions(target_user):
|
|
|
|
raise utils.EventError("%s has no permissions" % account)
|
|
|
|
target_user.del_setting("permissions")
|
|
|
|
event["stdout"].write("Cleared permissions for %s" % account)
|
2016-04-04 17:42:37 +00:00
|
|
|
else:
|
2019-11-21 15:49:08 +00:00
|
|
|
permissions = event["args_split"][2:]
|
|
|
|
if not permissions:
|
|
|
|
raise utils.EventError("Please provide at least 1 permission")
|
|
|
|
user_permissions = self._get_permissions(target_user)
|
|
|
|
|
|
|
|
if subcommand == "add":
|
|
|
|
new = list(set(permissions)-set(user_permissions))
|
|
|
|
if not new:
|
|
|
|
raise utils.EventError("No new permissions to give")
|
|
|
|
target_user.set_setting("permissions", user_permissions+new)
|
|
|
|
event["stdout"].write("Gave %s new permissions: %s" %
|
|
|
|
(account, ", ".join(new)))
|
|
|
|
elif subcommand == "remove":
|
|
|
|
permissions_set = set(permissions)
|
|
|
|
user_permissions_set = set(user_permissions)
|
|
|
|
removed = list(user_permissions_set&permissions_set)
|
|
|
|
if not (user_permissions_set & permissions_set):
|
|
|
|
raise utils.EventError("New permissions to remove")
|
|
|
|
change = list(user_permissions_set - permissions_set)
|
|
|
|
|
|
|
|
if not change:
|
|
|
|
target_user.del_setting("permissions")
|
|
|
|
else:
|
|
|
|
target_user.set_setting("permissions", change)
|
|
|
|
event["stdout"].write("Removed permissions from %s: %s" %
|
|
|
|
(account, ", ".join(change)))
|
|
|
|
else:
|
|
|
|
raise utils.EventError("Unknown subcommand %s" % subcommand)
|
|
|
|
|
|
|
|
@utils.hook("received.command.hostmask")
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("authenticated", True)
|
|
|
|
@utils.kwarg("usage", "list")
|
|
|
|
@utils.kwarg("usage", "add [hostmask]")
|
|
|
|
@utils.kwarg("usage", "remove [hostmask]")
|
|
|
|
def hostmask(self, event):
|
|
|
|
subcommand = event["args_split"][0].lower()
|
|
|
|
hostmasks = event["user"].get_setting(HOSTMASKS_SETTING, [])
|
|
|
|
|
|
|
|
if subcommand == "list":
|
|
|
|
event["stdout"].write("Your hostmasks: %s" % ", ".join(hostmasks))
|
2018-09-03 10:30:54 +00:00
|
|
|
else:
|
2019-11-21 15:49:08 +00:00
|
|
|
if event["args_split"][1:]:
|
|
|
|
hostmask = event["args_split"][1]
|
2019-01-29 07:14:53 +00:00
|
|
|
else:
|
2019-11-21 15:49:08 +00:00
|
|
|
hostmask = "*!%s" % event["user"].userhost()
|
|
|
|
account = self._account_name(event["user"])
|
|
|
|
|
|
|
|
if subcommand == "add":
|
|
|
|
if hostmask in hostmasks:
|
|
|
|
raise utils.EventError(
|
|
|
|
"Hostmask %s is already on your account" % hostmask)
|
|
|
|
hostmasks.append(hostmask)
|
|
|
|
event["user"].set_setting(HOSTMASKS_SETTING, hostmasks)
|
|
|
|
|
|
|
|
self._specific_hostmask(event["server"], hostmask, account)
|
2019-11-30 19:32:38 +00:00
|
|
|
self._add_hostmask(event["server"], hostmask, account)
|
2019-11-21 15:49:08 +00:00
|
|
|
|
|
|
|
event["stdout"].write("Added %s to your hostmasks" % hostmask)
|
|
|
|
elif subcommand == "remove":
|
|
|
|
if not hostmask in hostmasks:
|
|
|
|
raise utils.EventError("Hostmask %s is not on your account"
|
|
|
|
% hostmask)
|
|
|
|
while hostmask in hostmasks:
|
|
|
|
hostmasks.remove(hostmask)
|
|
|
|
event["user"].set_setting(HOSTMASKS_SETTING, hostmasks)
|
|
|
|
|
|
|
|
self._specific_hostmask(event["server"], hostmask, None)
|
2019-11-30 19:32:38 +00:00
|
|
|
self._remove_hostmask(event["server"], hostmask)
|
2019-11-21 15:49:08 +00:00
|
|
|
|
|
|
|
event["stdout"].write("Removed %s from your hostmasks"
|
|
|
|
% hostmask)
|
2019-01-29 07:14:53 +00:00
|
|
|
else:
|
2019-11-21 15:49:08 +00:00
|
|
|
raise utils.EventError("Unknown subcommand %s" % subcommand)
|
2016-05-17 13:50:48 +00:00
|
|
|
|
2019-11-26 15:32:24 +00:00
|
|
|
def _assert(self, allowed):
|
|
|
|
if allowed:
|
|
|
|
return utils.consts.PERMISSION_FORCE_SUCCESS, None
|
|
|
|
else:
|
|
|
|
return utils.consts.PERMISSION_ERROR, NO_PERMISSION
|
|
|
|
|
2019-06-14 11:01:55 +00:00
|
|
|
@utils.hook("preprocess.command")
|
|
|
|
def preprocess_command(self, event):
|
2019-11-21 15:49:08 +00:00
|
|
|
allowed = None
|
2019-06-14 11:01:55 +00:00
|
|
|
permission = event["hook"].get_kwarg("permission", None)
|
2019-06-26 09:59:52 +00:00
|
|
|
authenticated = event["hook"].get_kwarg("authenticated", False)
|
2019-11-21 15:49:08 +00:00
|
|
|
if not permission == None:
|
|
|
|
allowed = self._has_permission(event["user"], permission)
|
2019-11-21 16:19:43 +00:00
|
|
|
elif authenticated:
|
2019-11-21 15:49:08 +00:00
|
|
|
allowed = self._is_identified(event["user"])
|
2019-11-26 15:32:24 +00:00
|
|
|
else:
|
|
|
|
return
|
2018-08-28 15:53:21 +00:00
|
|
|
|
2019-11-26 15:32:24 +00:00
|
|
|
return self._assert(allowed)
|
|
|
|
|
|
|
|
@utils.hook("check.command.permission")
|
|
|
|
def check_permission(self, event):
|
|
|
|
return self._assert(
|
|
|
|
self._has_permission(event["user"], event["request_args"][0]))
|
|
|
|
@utils.hook("check.command.authenticated")
|
|
|
|
def check_authenticated(self, event):
|
|
|
|
return self._assert(self._is_identified(event["user"]))
|