From bd2bd55e89f5018a4559f6e59fd48da1b2b4295f Mon Sep 17 00:00:00 2001 From: James Lu Date: Thu, 20 Aug 2015 22:13:28 -0700 Subject: [PATCH 1/6] relay: fix mode relaying when sender is a client but isn't in any shared channels --- plugins/relay.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/relay.py b/plugins/relay.py index 5f2e03d..5b1d641 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -538,10 +538,9 @@ def relayModes(irc, remoteirc, sender, channel, modes=None): # Don't send anything if there are no supported modes left after filtering. if supported_modes: # Check if the sender is a user; remember servers are allowed to set modes too. - if sender in irc.users: - u = getRemoteUser(irc, remoteirc, sender, spawnIfMissing=False) - if u: - remoteirc.proto.modeClient(remoteirc, u, remotechan, supported_modes) + u = getRemoteUser(irc, remoteirc, sender, spawnIfMissing=False) + if u: + remoteirc.proto.modeClient(remoteirc, u, remotechan, supported_modes) else: remoteirc.proto.modeServer(remoteirc, remoteirc.sid, remotechan, supported_modes) From c1d98838d7497a9d8ddcff365934d9213f628e00 Mon Sep 17 00:00:00 2001 From: James Lu Date: Fri, 21 Aug 2015 00:39:58 -0700 Subject: [PATCH 2/6] plugins/admin: allow sending KICK from servers (#90) --- plugins/admin.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/admin.py b/plugins/admin.py index 57a2b3d..a834222 100644 --- a/plugins/admin.py +++ b/plugins/admin.py @@ -144,12 +144,15 @@ def kick(irc, source, args): except IndexError: utils.msg(irc, source, "Error: not enough arguments. Needs 3-4: source nick, channel, target, reason (optional).") return - u = utils.nickToUid(irc, nick) + u = utils.nickToUid(irc, nick) or nick targetu = utils.nickToUid(irc, target) if not utils.isChannel(channel): utils.msg(irc, source, "Error: Invalid channel name %r." % channel) return - irc.proto.kickClient(irc, u, channel, targetu, reason) + if utils.isInternalServer(irc, u): + irc.proto.kickServer(irc, u, channel, targetu, reason) + else: + irc.proto.kickClient(irc, u, channel, targetu, reason) irc.callHooks([u, 'PYLINK_ADMIN_KICK', {'channel': channel, 'target': targetu, 'text': reason, 'parse_as': 'KICK'}]) @utils.add_cmd From 1e95f4b3df87d731b64492659a899ea24b1b627f Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 22 Aug 2015 21:00:29 -0700 Subject: [PATCH 3/6] main: Fix ImportError logging if a required library, and not the actual module is missing The old message showed "the plugin/file could not be found" even if it's a library that couldn't be imported. "No module named X" is too broad to check for here! --- main.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index f649ff2..776cc72 100755 --- a/main.py +++ b/main.py @@ -278,10 +278,10 @@ if __name__ == '__main__': pl = imp.load_source(plugin, moduleinfo[1]) utils.plugins.append(pl) except ImportError as e: - if str(e).startswith('No module named'): - log.error('Failed to load plugin %r: the plugin could not be found.', plugin) + if str(e) == ('No module named %r' % plugin): + log.error('Failed to load plugin %r: The plugin could not be found.', plugin) else: - log.error('Failed to load plugin %r: import error %s', plugin, str(e)) + log.error('Failed to load plugin %r: ImportError: %s', plugin, str(e)) else: if hasattr(pl, 'main'): log.debug('Calling main() function of plugin %r', pl) @@ -293,10 +293,10 @@ if __name__ == '__main__': moduleinfo = imp.find_module(protoname, protocols_folder) proto = imp.load_source(protoname, moduleinfo[1]) except ImportError as e: - if str(e).startswith('No module named'): - log.critical('Failed to load protocol module %r: the file could not be found.', protoname) + if str(e) == ('No module named %r' % protoname): + log.critical('Failed to load protocol module %r: The file could not be found.', protoname) else: - log.critical('Failed to load protocol module: import error %s', protoname, str(e)) + log.critical('Failed to load protocol module: ImportError: %s', protoname, str(e)) sys.exit(2) else: utils.networkobjects[network] = Irc(network, proto, conf.conf) From 20474dabac1b4a0acca9dec9f6485dd8258e19de Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 22 Aug 2015 20:51:50 -0700 Subject: [PATCH 4/6] relay: fix many bugs with nick collision handling, and add flood prevention for fixing nicks Closes #85. Nick collisions caused by internal clients are handled fine now, including the following cases: - Abusing spawnClient to create a user with the same nick as a relay client, but with a lower TS. - When both an UID (e.g. 42XAAAAAA) and a tagged UID (_42XAAAAAA) exist on the same network (the two will both try to take the same nick of _42XAAAAAA/net over the relay). The case where changing NICK from a long cut-off nick to another long cut-off nick is also mitigated. somelongnick/net won't show nick changes to somelongnic//net if the old and new nicks have give the same normalized relay nick. This introduces a new dependency, expiringdict, from https://pypi.python.org/pypi/expiringdict/1.1.2, which is used as a cache for flood prevention. --- README.md | 3 ++- plugins/relay.py | 54 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 33d4788..a4216fa 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Dependencies currently include: * Python 3.4+ * PyYAML (`pip install pyyaml` or `apt-get install python3-yaml`) +* *For the relay plugin only*: expiringdict (`pip install expiringdict`/`apt-get install python3-expiringdict`) #### Supported IRCds @@ -23,7 +24,7 @@ Dependencies currently include: ### Installation -1) Rename `config.yml.example` to `config.yml` and configure your instance there. Not all options are properly implemented yet, and the configuration schema isn't finalized yet - this means your configuration may break in an update! +1) Rename `config.yml.example` to `config.yml` and configure your instance there. Not all options are properly implemented yet, and the configuration schema isn't finalized yet - this means that your configuration may break in an update! 2) Run `main.py` from the command line. diff --git a/plugins/relay.py b/plugins/relay.py index 5b1d641..f96712f 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -8,6 +8,8 @@ import threading import string from collections import defaultdict +from expiringdict import ExpiringDict + import utils from log import log from conf import confname @@ -19,6 +21,7 @@ dbname += '.db' relayusers = defaultdict(dict) spawnlocks = defaultdict(threading.Lock) +savecache = ExpiringDict(max_len=5, max_age_seconds=10) def relayWhoisHandlers(irc, target): user = irc.users[target] @@ -31,41 +34,46 @@ def relayWhoisHandlers(irc, target): remotenick)] utils.whois_handlers.append(relayWhoisHandlers) -def normalizeNick(irc, netname, nick, separator=None, oldnick=''): +def normalizeNick(irc, netname, nick, separator=None, uid=''): separator = separator or irc.serverdata.get('separator') or "/" log.debug('(%s) normalizeNick: using %r as separator.', irc.name, separator) orig_nick = nick protoname = irc.proto.__name__ maxnicklen = irc.maxnicklen if not protoname.startswith(('insp', 'unreal')): - # Charybdis doesn't allow / in usernames, and will quit with - # a protocol violation if there is one. + # Charybdis doesn't allow / in usernames, and will SQUIT with + # a protocol violation if it sees one. separator = separator.replace('/', '|') nick = nick.replace('/', '|') if nick.startswith(tuple(string.digits)): # On TS6 IRCds, nicks that start with 0-9 are only allowed if # they match the UID of the originating server. Otherwise, you'll - # get nasty protocol violations! + # get nasty protocol violation SQUITs! nick = '_' + nick tagnicks = True suffix = separator + netname nick = nick[:maxnicklen] - # Maximum allowed length of a nickname. + # Maximum allowed length of a nickname, minus the obligatory /network tag. allowedlength = maxnicklen - len(suffix) - # If a nick is too long, the real nick portion must be cut off, but the - # /network suffix must remain the same. + # If a nick is too long, the real nick portion will be cut off, but the + # /network suffix MUST remain the same. nick = nick[:allowedlength] nick += suffix - # FIXME: factorize - while utils.nickToUid(irc, nick) or utils.nickToUid(irc, oldnick) and not \ - isRelayClient(irc, utils.nickToUid(irc, nick)): - # The nick we want exists? Darn, create another one then, but only if - # the target isn't an internal client! - # Increase the separator length by 1 if the user was already tagged, - # but couldn't be created due to a nick conflict. - # This can happen when someone steals a relay user's nick. + + # The nick we want exists? Darn, create another one then. + # Increase the separator length by 1 if the user was already tagged, + # but couldn't be created due to a nick conflict. + # This can happen when someone steals a relay user's nick. + + # However, if the user is changing from, say, a long, cut-off nick to another long, + # cut-off nick, we don't need to check for duplicates and tag the nick twice. + + # somecutoffnick/net would otherwise be erroneous NICK'ed to somecutoffnic//net, + # even though there would be no collision because the old and new nicks are from + # the same client. + while utils.nickToUid(irc, nick) and utils.nickToUid(irc, nick) != uid: new_sep = separator + separator[-1] log.debug('(%s) normalizeNick: nick %r is in use; using %r as new_sep.', irc.name, nick, new_sep) nick = normalizeNick(irc, netname, orig_nick, separator=new_sep) @@ -271,7 +279,7 @@ utils.add_hook(handle_squit, 'SQUIT') def handle_nick(irc, numeric, command, args): for netname, user in relayusers[(irc.name, numeric)].items(): remoteirc = utils.networkobjects[netname] - newnick = normalizeNick(remoteirc, irc.name, args['newnick']) + newnick = normalizeNick(remoteirc, irc.name, args['newnick'], uid=user) if remoteirc.users[user].nick != newnick: remoteirc.proto.nickClient(remoteirc, user, newnick) utils.add_hook(handle_nick, 'NICK') @@ -912,8 +920,18 @@ def handle_save(irc, numeric, command, args): remotenet, remoteuser = realuser remoteirc = utils.networkobjects[remotenet] nick = remoteirc.users[remoteuser].nick - newnick = normalizeNick(irc, remotenet, nick, oldnick=args['oldnick']) - irc.proto.nickClient(irc, target, newnick) + # Limit how many times we can attempt to fix our nick, to prevent + # floods and such. + if savecache.setdefault(target, 0) <= 5: + newnick = normalizeNick(irc, remotenet, nick) + log.info('(%s) SAVE received for relay client %r (%s), fixing nick to %s', + irc.name, target, nick, newnick) + irc.proto.nickClient(irc, target, newnick) + else: + log.warning('(%s) SAVE received for relay client %r (%s), not ' + 'fixing nick again due to 5 failed attempts in ' + 'the last 10 seconds!', irc.name, target, nick) + savecache[target] += 1 else: # Somebody else on the network (not a PyLink client) had a nick collision; # relay this as a nick change appropriately. From 713ba1469b264ed8f6ebdfa8179c7a63503dcfa6 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 22 Aug 2015 21:43:25 -0700 Subject: [PATCH 5/6] relay: log blocked KILLs/KICKs (Closes #95) --- plugins/relay.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plugins/relay.py b/plugins/relay.py index f96712f..0fef139 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -404,9 +404,16 @@ def handle_kick(irc, source, command, args): # Join the kicked client back with its respective modes. irc.proto.sjoinServer(irc, irc.sid, channel, [(modes, target)]) if kicker in irc.users: + log.info('(%s) Blocked KICK (reason %r) from %s to relay client %s/%s on %s.', + irc.name, args['text'], irc.users[source].nick, + remoteirc.users[real_target].nick, remoteirc.name, channel) utils.msg(irc, kicker, "This channel is claimed; your kick to " "%s has been blocked because you are not " "(half)opped." % channel, notice=True) + else: + log.info('(%s) Blocked KICK (reason %r) from server %s to relay client %s/%s on %s.', + irc.name, args['text'], irc.servers[source].name, + remoteirc.users[real_target].nick, remoteirc.name, channel) return if not real_target: @@ -634,10 +641,17 @@ def handle_kill(irc, numeric, command, args): client = getRemoteUser(remoteirc, irc, realuser[1]) irc.proto.sjoinServer(irc, irc.sid, localchan, [(modes, client)]) if userdata and numeric in irc.users: + log.info('(%s) Blocked KILL (reason %r) from %s to relay client %s/%s.', + irc.name, args['text'], irc.users[numeric].nick, + remoteirc.users[realuser[1]].nick, realuser[0]) utils.msg(irc, numeric, "Your kill to %s has been blocked " "because PyLink does not allow killing" " users over the relay at this time." % \ userdata.nick, notice=True) + else: + log.info('(%s) Blocked KILL (reason %r) from server %s to relay client %s/%s.', + irc.name, args['text'], irc.servers[numeric].name, + remoteirc.users[realuser[1]].nick, realuser[0]) # Target user was local. else: # IMPORTANT: some IRCds (charybdis) don't send explicit QUIT messages From 8f09c356723c5a1c5ddebd7e21a488a39164f9ca Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 23 Aug 2015 21:15:49 -0700 Subject: [PATCH 6/6] Revert "example conf: remove unused sidrange option" This reverts commit d533ce3d2847a648c8dcdd1fb5d42428cc550155. --- config.yml.example | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config.yml.example b/config.yml.example index c091cce..390c5c8 100644 --- a/config.yml.example +++ b/config.yml.example @@ -31,6 +31,12 @@ servers: # The first char must be a digit [0-9], and the remaining two chars may be letters [A-Z] or digits. sid: "0AL" + # SID range - the range of SIDs PyLink is allowed to use to generate server IDs. On TS6, + # this should be a combination of digits, letters, and #'s. Each # denotes a range (0-9A-Z) + # of characters that can be used by PyLink. You will want to make sure no other servers + # are using this range. There must be at least one # in the entry. + sidrange: "8##" + # Autojoin channels channels: ["#pylink"]