From b8dc4ea2af56884d0908d1c9261f23c76951c567 Mon Sep 17 00:00:00 2001 From: makefu Date: Tue, 25 Mar 2014 15:35:52 +0100 Subject: add authentication to Reaktor using /query identify configuration contains commands and public_commands --- IRC/reaktor.py | 27 ++++++++++++++++++++++++--- auth.lst | 1 + commands/caps | 3 ++- commands/identify | 22 ++++++++++++++++++++++ config.py | 17 ++++++++++++----- 5 files changed, 61 insertions(+), 9 deletions(-) create mode 100755 auth.lst create mode 100755 commands/identify diff --git a/IRC/reaktor.py b/IRC/reaktor.py index 7ef8d70..e73a3d7 100755 --- a/IRC/reaktor.py +++ b/IRC/reaktor.py @@ -20,6 +20,16 @@ logging.basicConfig(level = logging.DEBUG if getconf('debug') else logging.INFO) restart_timeout = getconf('irc_restart_timeout') or 5 +def is_admin(prefix): + try: + with open(getconf('auth_file')) as f: + for line in f: + if line.strip() == prefix: + return True + except Exception as e: + log.info(e) + return False + class Reaktor(asybot): def __init__(self): asybot.__init__(self, getconf('irc_server'), getconf('irc_port'), getconf('irc_nickname'), getconf('irc_channels'), hammer_interval=getconf('irc_hammer_interval'), alarm_timeout=getconf('irc_alarm_timeout'), kill_timeout=getconf('irc_kill_timeout')) @@ -28,8 +38,16 @@ class Reaktor(asybot): for command in getconf('commands'): y = match(command['pattern'], rest) if y: - self.execute_command(command, y, prefix, params) - break + if not is_admin(prefix): + self.PRIVMSG(params,'unauthorized!') + else: + return self.execute_command(command, y, prefix, params) + + for command in getconf('public_commands'): + y = match(command['pattern'], rest) + if y: + return self.execute_command(command, y, prefix, params) + def execute_command(self, command, match, prefix, target): from os.path import realpath, dirname, join @@ -43,12 +61,16 @@ class Reaktor(asybot): myargv += shlex.split(match.groupdict()['args']) env = {} + env['_prefix'] = prefix env['_from'] = prefix.split('!', 1)[0] + log.debug('self:' +self.nickname) + # when receiving /query, answer to the user, not to self if self.nickname in target: target.remove(self.nickname) target.append(env['_from']) log.debug('target:' +str(target)) + env['config_filename'] = os.path.abspath(config_filename) start = time() try: @@ -84,4 +106,3 @@ if __name__ == "__main__": waiting for %d seconds" % restart_timeout) log.debug("Exception: %s" % str(e)) sleep(restart_timeout) - diff --git a/auth.lst b/auth.lst new file mode 100755 index 0000000..8b13789 --- /dev/null +++ b/auth.lst @@ -0,0 +1 @@ + diff --git a/commands/caps b/commands/caps index c47319f..d024557 100755 --- a/commands/caps +++ b/commands/caps @@ -9,4 +9,5 @@ def load_config(filename): return imp.load_module(modname, file, pathname, description) config = load_config(os.environ['config_filename']) -print(' '.join(filter(None,[ x.get('capname',None) for x in config.commands]))) +print('Private: '+' '.join(filter(None,[ x.get('capname',None) for x in config.commands]))) +print('Public: '+' '.join(filter(None,[ x.get('capname',None) for x in config.public_commands]))) diff --git a/commands/identify b/commands/identify new file mode 100755 index 0000000..c2fb2c5 --- /dev/null +++ b/commands/identify @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import imp +import os,sys + +def load_config(filename): + dirname = os.path.dirname(filename) + modname, ext = os.path.splitext(os.path.basename(filename)) + file, pathname, description = imp.find_module(modname, [ dirname ]) + return imp.load_module(modname, file, pathname, description) + +config = load_config(os.environ['config_filename']) + +with open(config.admin_file) as f: + for line in f: + nick,secret = line.split() + if sys.argv[1] == secret: + print("identified you as %s!"%nick) + with open(config.auth_file,'a+') as g: + g.write(os.environ['_prefix'] +"\n") + sys.exit(0) + +print("unable to identify you, sorry") diff --git a/config.py b/config.py index 928c49d..88ae837 100644 --- a/config.py +++ b/config.py @@ -10,21 +10,21 @@ irc_kill_timeout = 360 irc_nickname = name irc_server = 'irc.freenode.org' irc_port = 6667 -#irc_restart_timeout = 5 +irc_restart_timeout = 5 irc_channels = [ '#krebs' ] - +admin_file='admin.lst' +auth_file='auth.lst' def default_command(cmd): return { 'capname': cmd, 'pattern': '^(?:' + name + '|\\*):\\s*' + cmd + '\\s*(?:\\s+(?P.*))?$', 'argv': [ 'commands/' + cmd ] } -commands = [ +public_commands = [ default_command('caps'), default_command('hello'), - default_command('reload'), default_command('badcommand'), default_command('rev'), default_command('uptime'), @@ -34,5 +34,12 @@ commands = [ 'argv': [ 'commands/respond','You are made of stupid!'] }, # "highlight" { 'pattern': '.*\\b' + name + '\\b.*', - 'argv': [ 'commands/say', 'I\'m famous' ] } + 'argv': [ 'commands/say', 'I\'m famous' ] }, + # identify via direct connect + { 'capname': 'identify', + 'pattern': 'identify' + '\\s*(?:\\s+(?P.*))?$', + 'argv' : [ 'commands/identify' ]} +] +commands = [ + default_command('reload') ] -- cgit v1.2.3