2015-12-31 01:53:53 +01:00
|
|
|
"""
|
|
|
|
Changehost plugin - automatically changes the hostname of matching users.
|
|
|
|
"""
|
|
|
|
import string
|
|
|
|
|
2019-07-15 00:12:29 +02:00
|
|
|
from pylinkirc import conf, utils, world
|
|
|
|
from pylinkirc.coremods import permissions
|
|
|
|
from pylinkirc.log import log
|
|
|
|
|
2015-12-31 01:53:53 +01:00
|
|
|
# Characters allowed in a hostname.
|
2015-12-31 02:08:38 +01:00
|
|
|
allowed_chars = string.ascii_letters + '-./:' + string.digits
|
2015-12-31 01:53:53 +01:00
|
|
|
|
2016-03-05 19:11:47 +01:00
|
|
|
def _changehost(irc, target, args):
|
2016-07-28 02:02:04 +02:00
|
|
|
changehost_conf = conf.conf.get("changehost")
|
2015-12-31 01:53:53 +01:00
|
|
|
|
2016-11-20 21:04:30 +01:00
|
|
|
if target not in irc.users:
|
|
|
|
return
|
2017-06-30 08:01:39 +02:00
|
|
|
elif irc.is_internal_client(target):
|
2016-11-20 21:04:30 +01:00
|
|
|
log.debug('(%s) Skipping changehost on internal client %s', irc.name, target)
|
|
|
|
return
|
|
|
|
|
2015-12-31 01:53:53 +01:00
|
|
|
if not changehost_conf:
|
|
|
|
log.warning("(%s) Missing 'changehost:' configuration block; "
|
|
|
|
"Changehost will not function correctly!", irc.name)
|
|
|
|
return
|
|
|
|
elif irc.name not in changehost_conf.get('enabled_nets'):
|
|
|
|
# We're not enabled on the network, break.
|
|
|
|
return
|
|
|
|
|
2016-11-20 21:34:11 +01:00
|
|
|
match_ip = changehost_conf.get('match_ip', False)
|
|
|
|
match_realhosts = changehost_conf.get('match_realhosts', False)
|
|
|
|
|
2015-12-31 01:53:53 +01:00
|
|
|
changehost_hosts = changehost_conf.get('hosts')
|
|
|
|
if not changehost_hosts:
|
|
|
|
log.warning("(%s) No hosts were defined in changehost::hosts; "
|
|
|
|
"Changehost will not function correctly!", irc.name)
|
|
|
|
return
|
|
|
|
|
2016-08-03 07:56:59 +02:00
|
|
|
args = args.copy()
|
2019-04-10 04:01:35 +02:00
|
|
|
|
|
|
|
# $host is explicitly forbidden by default because it can cause recursive
|
|
|
|
# loops when IP or real host masks are used to match a target. vHost
|
|
|
|
# updates do not affect these fields, so any further host application will
|
|
|
|
# cause the vHost to grow rapidly in size.
|
|
|
|
# That said, it is possible to get away with this expansion if you're
|
|
|
|
# careful enough, and that's why this hidden option exists.
|
2016-08-03 07:56:59 +02:00
|
|
|
if not changehost_conf.get('force_host_expansion'):
|
|
|
|
del args['host']
|
|
|
|
|
|
|
|
log.debug('(%s) Changehost args: %s', irc.name, args)
|
|
|
|
|
2015-12-31 01:53:53 +01:00
|
|
|
for host_glob, host_template in changehost_hosts.items():
|
2016-11-20 21:34:11 +01:00
|
|
|
log.debug('(%s) Changehost: checking mask %s', irc.name, host_glob)
|
2017-06-30 08:01:39 +02:00
|
|
|
if irc.match_host(host_glob, target, ip=match_ip, realhost=match_realhosts):
|
2016-11-20 21:34:11 +01:00
|
|
|
log.debug('(%s) Changehost matched mask %s', irc.name, host_glob)
|
2015-12-31 01:53:53 +01:00
|
|
|
# This uses template strings for simple substitution:
|
|
|
|
# https://docs.python.org/3/library/string.html#template-strings
|
|
|
|
template = string.Template(host_template)
|
|
|
|
|
|
|
|
# Substitute using the fields provided the hook data. This means
|
|
|
|
# that the following variables are available for substitution:
|
2016-08-03 07:56:59 +02:00
|
|
|
# $uid, $ts, $nick, $realhost, $ident, and $ip.
|
2016-08-03 08:04:09 +02:00
|
|
|
try:
|
|
|
|
new_host = template.substitute(args)
|
|
|
|
except KeyError as e:
|
|
|
|
log.warning('(%s) Bad expansion %s in template %s' % (irc.name, e, host_template))
|
|
|
|
continue
|
2015-12-31 01:53:53 +01:00
|
|
|
|
|
|
|
# Replace characters that are not allowed in hosts with "-".
|
|
|
|
for char in new_host:
|
|
|
|
if char not in allowed_chars:
|
|
|
|
new_host = new_host.replace(char, '-')
|
|
|
|
|
2019-04-10 04:01:35 +02:00
|
|
|
# Only send a host change if something has changed
|
|
|
|
if new_host != irc.users[target].host:
|
|
|
|
irc.update_client(target, 'HOST', new_host)
|
2015-12-31 01:53:53 +01:00
|
|
|
|
|
|
|
# Only operate on the first match.
|
|
|
|
break
|
|
|
|
|
2016-03-05 19:11:47 +01:00
|
|
|
def handle_uid(irc, sender, command, args):
|
|
|
|
"""
|
|
|
|
Changehost listener for new connections.
|
|
|
|
"""
|
|
|
|
|
|
|
|
target = args['uid']
|
|
|
|
_changehost(irc, target, args)
|
|
|
|
|
2015-12-31 01:53:53 +01:00
|
|
|
utils.add_hook(handle_uid, 'UID')
|
2016-03-05 19:11:47 +01:00
|
|
|
|
2016-11-20 20:53:55 +01:00
|
|
|
def handle_chghost(irc, sender, command, args):
|
|
|
|
"""
|
|
|
|
Handles incoming CHGHOST requests for optional host-change enforcement.
|
|
|
|
"""
|
|
|
|
changehost_conf = conf.conf.get("changehost")
|
|
|
|
if not changehost_conf:
|
|
|
|
return
|
|
|
|
|
|
|
|
target = args['target']
|
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
if (not irc.is_internal_client(sender)) and (not irc.is_internal_server(sender)):
|
2016-11-20 20:53:55 +01:00
|
|
|
if irc.name in changehost_conf.get('enforced_nets', []):
|
|
|
|
log.debug('(%s) Enforce for network is on, re-checking host for target %s/%s',
|
2017-06-30 08:01:39 +02:00
|
|
|
irc.name, target, irc.get_friendly_name(target))
|
2016-11-20 21:33:58 +01:00
|
|
|
|
|
|
|
for ex in changehost_conf.get("enforce_exceptions", []):
|
2017-06-30 08:01:39 +02:00
|
|
|
if irc.match_host(ex, target):
|
2016-11-20 21:33:58 +01:00
|
|
|
log.debug('(%s) Skipping host change for target %s; they are exempted by mask %s',
|
|
|
|
irc.name, target, ex)
|
|
|
|
return
|
|
|
|
|
2018-05-26 11:12:38 +02:00
|
|
|
userobj = irc.users.get(target)
|
|
|
|
if userobj:
|
|
|
|
_changehost(irc, target, userobj.get_fields())
|
2016-11-20 20:53:55 +01:00
|
|
|
|
|
|
|
utils.add_hook(handle_chghost, 'CHGHOST')
|
|
|
|
|
2016-03-05 19:11:47 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def applyhosts(irc, sender, args):
|
|
|
|
"""[<network>]
|
|
|
|
|
|
|
|
Applies all configured hosts for users on the given network, or the current network if none is specified."""
|
|
|
|
|
2017-08-02 16:24:23 +02:00
|
|
|
permissions.check_permissions(irc, sender, ['changehost.applyhosts'])
|
2016-12-10 07:07:27 +01:00
|
|
|
|
2016-03-05 19:11:47 +01:00
|
|
|
try: # Try to get network from the command line.
|
|
|
|
network = world.networkobjects[args[0]]
|
|
|
|
except IndexError: # No network was given
|
|
|
|
network = irc
|
|
|
|
except KeyError: # Unknown network
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error("Unknown network '%s'." % network)
|
2016-03-05 19:11:47 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
for user, userdata in network.users.copy().items():
|
|
|
|
_changehost(network, user, userdata.__dict__)
|
|
|
|
|
|
|
|
irc.reply("Done.")
|