2016-08-25 09:45:05 +02:00
|
|
|
"""
|
|
|
|
permissions.py - Permissions Abstraction for PyLink IRC Services.
|
|
|
|
"""
|
|
|
|
|
2019-07-15 00:12:29 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
from pylinkirc import conf, utils
|
|
|
|
from pylinkirc.log import log
|
2016-08-25 09:45:05 +02:00
|
|
|
|
2020-06-19 00:47:20 +02:00
|
|
|
__all__ = ['default_permissions', 'add_default_permissions',
|
|
|
|
'remove_default_permissions', 'check_permissions']
|
|
|
|
|
2016-08-25 09:45:05 +02:00
|
|
|
# Global variables: these store mappings of hostmasks/exttargets to lists of permissions each target has.
|
|
|
|
default_permissions = defaultdict(set)
|
|
|
|
|
|
|
|
|
2017-08-02 16:24:23 +02:00
|
|
|
def add_default_permissions(perms):
|
2016-08-25 09:45:05 +02:00
|
|
|
"""Adds default permissions to the index."""
|
2017-10-22 09:08:16 +02:00
|
|
|
global default_permissions
|
|
|
|
for target, permlist in perms.items():
|
|
|
|
default_permissions[target] |= set(permlist)
|
2017-08-02 16:24:23 +02:00
|
|
|
addDefaultPermissions = add_default_permissions
|
2016-08-25 09:45:05 +02:00
|
|
|
|
2017-08-02 16:24:23 +02:00
|
|
|
def remove_default_permissions(perms):
|
2016-08-25 09:45:05 +02:00
|
|
|
"""Remove default permissions from the index."""
|
2017-10-22 09:08:16 +02:00
|
|
|
global default_permissions
|
|
|
|
for target, permlist in perms.items():
|
|
|
|
default_permissions[target] -= set(permlist)
|
2017-08-02 16:24:23 +02:00
|
|
|
removeDefaultPermissions = remove_default_permissions
|
2016-08-25 09:45:05 +02:00
|
|
|
|
2017-08-02 16:24:23 +02:00
|
|
|
def check_permissions(irc, uid, perms, also_show=[]):
|
2016-08-25 09:45:05 +02:00
|
|
|
"""
|
|
|
|
Checks permissions of the caller. If the caller has any of the permissions listed in perms,
|
|
|
|
this function returns True. Otherwise, NotAuthorizedError is raised.
|
|
|
|
"""
|
2016-12-10 05:51:04 +01:00
|
|
|
# For old (< 1.1 login blocks):
|
2016-08-31 22:32:21 +02:00
|
|
|
# If the user is logged in, they automatically have all permissions.
|
2019-06-26 22:17:00 +02:00
|
|
|
olduser = conf.conf['login'].get('user')
|
|
|
|
if olduser and irc.match_host('$pylinkacc:%s' % olduser, uid):
|
2016-12-10 05:51:04 +01:00
|
|
|
log.debug('permissions: overriding permissions check for old-style admin user %s',
|
2017-06-30 08:01:39 +02:00
|
|
|
irc.get_hostmask(uid))
|
2016-08-31 22:32:21 +02:00
|
|
|
return True
|
|
|
|
|
2017-10-22 09:08:16 +02:00
|
|
|
permissions = defaultdict(set)
|
|
|
|
# Enumerate the configured permissions list.
|
|
|
|
for k, v in (conf.conf.get('permissions') or {}).items():
|
|
|
|
permissions[k] |= set(v)
|
|
|
|
|
|
|
|
# Merge in default permissions if enabled.
|
|
|
|
if conf.conf.get('permissions_merge_defaults', True):
|
|
|
|
for k, v in default_permissions.items():
|
|
|
|
permissions[k] |= v
|
|
|
|
|
|
|
|
for host, permlist in permissions.items():
|
2016-08-25 09:56:13 +02:00
|
|
|
log.debug('permissions: permlist for %s: %s', host, permlist)
|
2017-06-30 08:01:39 +02:00
|
|
|
if irc.match_host(host, uid):
|
2016-08-25 09:45:05 +02:00
|
|
|
# Now, iterate over all the perms we are looking for.
|
2016-08-25 09:56:13 +02:00
|
|
|
for perm in permlist:
|
2017-06-30 08:01:39 +02:00
|
|
|
# Use irc.match_host to expand globs in an IRC-case insensitive and wildcard
|
2016-08-25 09:45:05 +02:00
|
|
|
# friendly way. e.g. 'xyz.*.#Channel\' will match 'xyz.manage.#channel|' on IRCds
|
|
|
|
# using the RFC1459 casemapping.
|
2016-08-25 09:56:13 +02:00
|
|
|
log.debug('permissions: checking if %s glob matches anything in %s', perm, permlist)
|
2017-06-30 08:01:39 +02:00
|
|
|
if any(irc.match_host(perm, p) for p in perms):
|
2016-08-25 09:45:05 +02:00
|
|
|
return True
|
|
|
|
raise utils.NotAuthorizedError("You are missing one of the following permissions: %s" %
|
2016-08-25 20:41:37 +02:00
|
|
|
(', '.join(perms+also_show)))
|
2017-08-02 16:24:23 +02:00
|
|
|
checkPermissions = check_permissions
|