2018-10-17 17:14:04 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from synapse.util import glob_to_regex
|
|
|
|
|
|
|
|
from ._base import Config, ConfigError
|
|
|
|
|
|
|
|
|
|
|
|
class RoomDirectoryConfig(Config):
|
2019-10-10 10:39:35 +02:00
|
|
|
section = "roomdirectory"
|
|
|
|
|
2019-06-24 12:34:45 +02:00
|
|
|
def read_config(self, config, **kwargs):
|
2019-06-20 11:32:02 +02:00
|
|
|
self.enable_room_list_search = config.get("enable_room_list_search", True)
|
2019-03-19 17:50:51 +01:00
|
|
|
|
2019-02-15 11:53:39 +01:00
|
|
|
alias_creation_rules = config.get("alias_creation_rules")
|
2018-10-17 17:14:04 +02:00
|
|
|
|
2019-02-15 11:53:39 +01:00
|
|
|
if alias_creation_rules is not None:
|
|
|
|
self._alias_creation_rules = [
|
|
|
|
_RoomDirectoryRule("alias_creation_rules", rule)
|
|
|
|
for rule in alias_creation_rules
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
self._alias_creation_rules = [
|
2019-06-20 11:32:02 +02:00
|
|
|
_RoomDirectoryRule("alias_creation_rules", {"action": "allow"})
|
2019-02-15 11:53:39 +01:00
|
|
|
]
|
2018-10-17 17:14:04 +02:00
|
|
|
|
2019-02-15 11:53:39 +01:00
|
|
|
room_list_publication_rules = config.get("room_list_publication_rules")
|
2019-02-14 17:02:23 +01:00
|
|
|
|
2019-02-15 11:53:39 +01:00
|
|
|
if room_list_publication_rules is not None:
|
|
|
|
self._room_list_publication_rules = [
|
|
|
|
_RoomDirectoryRule("room_list_publication_rules", rule)
|
|
|
|
for rule in room_list_publication_rules
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
self._room_list_publication_rules = [
|
2019-06-20 11:32:02 +02:00
|
|
|
_RoomDirectoryRule("room_list_publication_rules", {"action": "allow"})
|
2019-02-15 11:53:39 +01:00
|
|
|
]
|
2019-02-14 17:02:23 +01:00
|
|
|
|
2019-06-22 01:00:20 +02:00
|
|
|
def generate_config_section(self, config_dir_path, server_name, **kwargs):
|
2018-10-17 17:14:04 +02:00
|
|
|
return """
|
2019-03-20 15:24:53 +01:00
|
|
|
# Uncomment to disable searching the public room list. When disabled
|
|
|
|
# blocks searching local and remote room lists for local and remote
|
|
|
|
# users by always returning an empty list for all queries.
|
2019-03-19 17:50:51 +01:00
|
|
|
#
|
2019-03-20 15:24:53 +01:00
|
|
|
#enable_room_list_search: false
|
2019-03-19 17:50:51 +01:00
|
|
|
|
2018-10-17 17:14:04 +02:00
|
|
|
# The `alias_creation` option controls who's allowed to create aliases
|
|
|
|
# on this server.
|
|
|
|
#
|
|
|
|
# The format of this option is a list of rules that contain globs that
|
2019-02-14 17:02:23 +01:00
|
|
|
# match against user_id, room_id and the new alias (fully qualified with
|
|
|
|
# server name). The action in the first rule that matches is taken,
|
|
|
|
# which can currently either be "allow" or "deny".
|
|
|
|
#
|
|
|
|
# Missing user_id/room_id/alias fields default to "*".
|
2018-10-17 17:14:04 +02:00
|
|
|
#
|
2019-02-15 11:17:13 +01:00
|
|
|
# If no rules match the request is denied. An empty list means no one
|
|
|
|
# can create aliases.
|
|
|
|
#
|
|
|
|
# Options for the rules include:
|
|
|
|
#
|
|
|
|
# user_id: Matches against the creator of the alias
|
|
|
|
# alias: Matches against the alias being created
|
|
|
|
# room_id: Matches against the room ID the alias is being pointed at
|
|
|
|
# action: Whether to "allow" or "deny" the request if the rule matches
|
|
|
|
#
|
2019-02-15 11:53:39 +01:00
|
|
|
# The default is:
|
|
|
|
#
|
2019-02-19 14:54:29 +01:00
|
|
|
#alias_creation_rules:
|
|
|
|
# - user_id: "*"
|
|
|
|
# alias: "*"
|
|
|
|
# room_id: "*"
|
|
|
|
# action: allow
|
2019-02-14 17:02:23 +01:00
|
|
|
|
2019-02-14 19:07:24 +01:00
|
|
|
# The `room_list_publication_rules` option controls who can publish and
|
|
|
|
# which rooms can be published in the public room list.
|
2019-02-14 17:02:23 +01:00
|
|
|
#
|
|
|
|
# The format of this option is the same as that for
|
2019-02-14 19:16:32 +01:00
|
|
|
# `alias_creation_rules`.
|
|
|
|
#
|
2019-02-14 19:21:24 +01:00
|
|
|
# If the room has one or more aliases associated with it, only one of
|
|
|
|
# the aliases needs to match the alias rule. If there are no aliases
|
|
|
|
# then only rules with `alias: *` match.
|
2019-02-15 11:17:13 +01:00
|
|
|
#
|
|
|
|
# If no rules match the request is denied. An empty list means no one
|
|
|
|
# can publish rooms.
|
|
|
|
#
|
|
|
|
# Options for the rules include:
|
|
|
|
#
|
2020-10-23 18:38:40 +02:00
|
|
|
# user_id: Matches against the creator of the alias
|
2019-02-15 11:17:13 +01:00
|
|
|
# room_id: Matches against the room ID being published
|
|
|
|
# alias: Matches against any current local or canonical aliases
|
|
|
|
# associated with the room
|
|
|
|
# action: Whether to "allow" or "deny" the request if the rule matches
|
|
|
|
#
|
2019-02-15 11:53:39 +01:00
|
|
|
# The default is:
|
|
|
|
#
|
2019-02-19 14:54:29 +01:00
|
|
|
#room_list_publication_rules:
|
|
|
|
# - user_id: "*"
|
|
|
|
# alias: "*"
|
|
|
|
# room_id: "*"
|
|
|
|
# action: allow
|
2018-10-17 17:14:04 +02:00
|
|
|
"""
|
|
|
|
|
2019-02-14 17:02:23 +01:00
|
|
|
def is_alias_creation_allowed(self, user_id, room_id, alias):
|
2018-10-17 17:14:04 +02:00
|
|
|
"""Checks if the given user is allowed to create the given alias
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str)
|
2019-02-14 17:02:23 +01:00
|
|
|
room_id (str)
|
2018-10-17 17:14:04 +02:00
|
|
|
alias (str)
|
|
|
|
|
|
|
|
Returns:
|
2021-02-12 17:01:48 +01:00
|
|
|
boolean: True if user is allowed to create the alias
|
2018-10-17 17:14:04 +02:00
|
|
|
"""
|
|
|
|
for rule in self._alias_creation_rules:
|
2019-02-14 17:02:23 +01:00
|
|
|
if rule.matches(user_id, room_id, [alias]):
|
|
|
|
return rule.action == "allow"
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_publishing_room_allowed(self, user_id, room_id, aliases):
|
|
|
|
"""Checks if the given user is allowed to publish the room
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str)
|
|
|
|
room_id (str)
|
|
|
|
aliases (list[str]): any local aliases associated with the room
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
boolean: True if user can publish room
|
|
|
|
"""
|
|
|
|
for rule in self._room_list_publication_rules:
|
|
|
|
if rule.matches(user_id, room_id, aliases):
|
2018-10-25 16:25:21 +02:00
|
|
|
return rule.action == "allow"
|
2018-10-17 17:14:04 +02:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-09-04 12:54:56 +02:00
|
|
|
class _RoomDirectoryRule:
|
2019-02-14 17:02:23 +01:00
|
|
|
"""Helper class to test whether a room directory action is allowed, like
|
|
|
|
creating an alias or publishing a room.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, option_name, rule):
|
2019-02-14 19:07:24 +01:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
option_name (str): Name of the config option this rule belongs to
|
|
|
|
rule (dict): The rule as specified in the config
|
|
|
|
"""
|
|
|
|
|
2018-10-17 17:14:04 +02:00
|
|
|
action = rule["action"]
|
2019-02-14 17:02:23 +01:00
|
|
|
user_id = rule.get("user_id", "*")
|
|
|
|
room_id = rule.get("room_id", "*")
|
|
|
|
alias = rule.get("alias", "*")
|
2018-10-17 17:14:04 +02:00
|
|
|
|
2018-10-25 16:25:21 +02:00
|
|
|
if action in ("allow", "deny"):
|
2018-10-17 17:14:04 +02:00
|
|
|
self.action = action
|
|
|
|
else:
|
|
|
|
raise ConfigError(
|
2019-11-21 13:00:14 +01:00
|
|
|
"%s rules can only have action of 'allow' or 'deny'" % (option_name,)
|
2018-10-17 17:14:04 +02:00
|
|
|
)
|
|
|
|
|
2019-02-14 17:02:23 +01:00
|
|
|
self._alias_matches_all = alias == "*"
|
|
|
|
|
2018-10-17 17:14:04 +02:00
|
|
|
try:
|
|
|
|
self._user_id_regex = glob_to_regex(user_id)
|
|
|
|
self._alias_regex = glob_to_regex(alias)
|
2019-02-14 17:02:23 +01:00
|
|
|
self._room_id_regex = glob_to_regex(room_id)
|
2018-10-17 17:14:04 +02:00
|
|
|
except Exception as e:
|
2020-12-08 15:04:35 +01:00
|
|
|
raise ConfigError("Failed to parse glob into regex") from e
|
2018-10-17 17:14:04 +02:00
|
|
|
|
2019-02-14 17:02:23 +01:00
|
|
|
def matches(self, user_id, room_id, aliases):
|
|
|
|
"""Tests if this rule matches the given user_id, room_id and aliases.
|
2018-10-17 17:14:04 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str)
|
2019-02-14 17:02:23 +01:00
|
|
|
room_id (str)
|
|
|
|
aliases (list[str]): The associated aliases to the room. Will be a
|
|
|
|
single element for testing alias creation, and can be empty for
|
|
|
|
testing room publishing.
|
2018-10-17 17:14:04 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
boolean
|
|
|
|
"""
|
|
|
|
|
2018-10-19 11:26:50 +02:00
|
|
|
# Note: The regexes are anchored at both ends
|
|
|
|
if not self._user_id_regex.match(user_id):
|
2018-10-17 17:14:04 +02:00
|
|
|
return False
|
|
|
|
|
2019-02-15 11:17:43 +01:00
|
|
|
if not self._room_id_regex.match(room_id):
|
|
|
|
return False
|
2019-02-14 19:16:32 +01:00
|
|
|
|
2019-02-15 11:17:43 +01:00
|
|
|
# We only have alias checks left, so we can short circuit if the alias
|
|
|
|
# rule matches everything.
|
|
|
|
if self._alias_matches_all:
|
|
|
|
return True
|
2019-02-14 17:02:23 +01:00
|
|
|
|
2019-02-15 11:17:43 +01:00
|
|
|
# If we are not given any aliases then this rule only matches if the
|
|
|
|
# alias glob matches all aliases, which we checked above.
|
|
|
|
if not aliases:
|
2018-10-17 17:14:04 +02:00
|
|
|
return False
|
|
|
|
|
2019-02-15 11:17:43 +01:00
|
|
|
# Otherwise, we just need one alias to match
|
|
|
|
for alias in aliases:
|
|
|
|
if self._alias_regex.match(alias):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|