2017-12-19 18:00:44 +01:00
|
|
|
// Copyright 2017 Andrew Morgan <andrew@amorgan.xyz>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-02-08 12:02:48 +01:00
|
|
|
"fmt"
|
2017-12-19 18:00:44 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2018-02-08 12:02:48 +01:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2017-12-19 18:00:44 +01:00
|
|
|
|
2018-06-18 11:43:15 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-05-30 14:43:13 +02:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2017-12-19 18:00:44 +01:00
|
|
|
)
|
|
|
|
|
2020-08-10 15:18:04 +02:00
|
|
|
type AppServiceAPI struct {
|
|
|
|
Matrix *Global `yaml:"-"`
|
|
|
|
Derived *Derived `yaml:"-"` // TODO: Nuke Derived from orbit
|
|
|
|
|
2020-08-13 13:16:37 +02:00
|
|
|
InternalAPI InternalAPIOptions `yaml:"internal_api"`
|
2020-08-10 15:18:04 +02:00
|
|
|
|
|
|
|
Database DatabaseOptions `yaml:"database"`
|
|
|
|
|
|
|
|
ConfigFiles []string `yaml:"config_files"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AppServiceAPI) Defaults() {
|
2020-08-13 13:16:37 +02:00
|
|
|
c.InternalAPI.Listen = "http://localhost:7777"
|
|
|
|
c.InternalAPI.Connect = "http://localhost:7777"
|
2020-08-10 15:18:04 +02:00
|
|
|
c.Database.Defaults()
|
|
|
|
c.Database.ConnectionString = "file:appservice.db"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AppServiceAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
|
2020-08-13 13:16:37 +02:00
|
|
|
checkURL(configErrs, "app_service_api.internal_api.listen", string(c.InternalAPI.Listen))
|
|
|
|
checkURL(configErrs, "app_service_api.internal_api.bind", string(c.InternalAPI.Connect))
|
2020-08-10 15:18:04 +02:00
|
|
|
checkNotEmpty(configErrs, "app_service_api.database.connection_string", string(c.Database.ConnectionString))
|
|
|
|
}
|
|
|
|
|
2017-12-19 18:00:44 +01:00
|
|
|
// ApplicationServiceNamespace is the namespace that a specific application
|
|
|
|
// service has management over.
|
|
|
|
type ApplicationServiceNamespace struct {
|
|
|
|
// Whether or not the namespace is managed solely by this application service
|
|
|
|
Exclusive bool `yaml:"exclusive"`
|
|
|
|
// A regex pattern that represents the namespace
|
|
|
|
Regex string `yaml:"regex"`
|
2018-06-18 11:43:15 +02:00
|
|
|
// The ID of an existing group that all users of this application service will
|
|
|
|
// be added to. This field is only relevant to the `users` namespace.
|
|
|
|
// Note that users who are joined to this group through an application service
|
|
|
|
// are not to be listed when querying for the group's members, however the
|
|
|
|
// group should be listed when querying an application service user's groups.
|
|
|
|
// This is to prevent making spamming all users of an application service
|
|
|
|
// trivial.
|
|
|
|
GroupID string `yaml:"group_id"`
|
2018-02-08 12:02:48 +01:00
|
|
|
// Regex object representing our pattern. Saves having to recompile every time
|
|
|
|
RegexpObject *regexp.Regexp
|
2017-12-19 18:00:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplicationService represents a Matrix application service.
|
|
|
|
// https://matrix.org/docs/spec/application_service/unstable.html
|
|
|
|
type ApplicationService struct {
|
|
|
|
// User-defined, unique, persistent ID of the application service
|
|
|
|
ID string `yaml:"id"`
|
|
|
|
// Base URL of the application service
|
|
|
|
URL string `yaml:"url"`
|
|
|
|
// Application service token provided in requests to a homeserver
|
|
|
|
ASToken string `yaml:"as_token"`
|
|
|
|
// Homeserver token provided in requests to an application service
|
|
|
|
HSToken string `yaml:"hs_token"`
|
|
|
|
// Localpart of application service user
|
|
|
|
SenderLocalpart string `yaml:"sender_localpart"`
|
2018-05-30 14:43:13 +02:00
|
|
|
// Information about an application service's namespaces. Key is either
|
|
|
|
// "users", "aliases" or "rooms"
|
2018-02-08 12:02:48 +01:00
|
|
|
NamespaceMap map[string][]ApplicationServiceNamespace `yaml:"namespaces"`
|
2018-06-18 11:43:15 +02:00
|
|
|
// Whether rate limiting is applied to each application service user
|
|
|
|
RateLimited bool `yaml:"rate_limited"`
|
|
|
|
// Any custom protocols that this application service provides (e.g. IRC)
|
|
|
|
Protocols []string `yaml:"protocols"`
|
2017-12-19 18:00:44 +01:00
|
|
|
}
|
|
|
|
|
2018-07-16 14:30:04 +02:00
|
|
|
// IsInterestedInRoomID returns a bool on whether an application service's
|
|
|
|
// namespace includes the given room ID
|
|
|
|
func (a *ApplicationService) IsInterestedInRoomID(
|
|
|
|
roomID string,
|
|
|
|
) bool {
|
|
|
|
if namespaceSlice, ok := a.NamespaceMap["rooms"]; ok {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if namespace.RegexpObject.MatchString(roomID) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInterestedInUserID returns a bool on whether an application service's
|
|
|
|
// namespace includes the given user ID
|
|
|
|
func (a *ApplicationService) IsInterestedInUserID(
|
|
|
|
userID string,
|
|
|
|
) bool {
|
|
|
|
if namespaceSlice, ok := a.NamespaceMap["users"]; ok {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if namespace.RegexpObject.MatchString(userID) {
|
2020-04-14 16:31:27 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// OwnsNamespaceCoveringUserId returns a bool on whether an application service's
|
|
|
|
// namespace is exclusive and includes the given user ID
|
|
|
|
func (a *ApplicationService) OwnsNamespaceCoveringUserId(
|
|
|
|
userID string,
|
|
|
|
) bool {
|
|
|
|
if namespaceSlice, ok := a.NamespaceMap["users"]; ok {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if namespace.Exclusive && namespace.RegexpObject.MatchString(userID) {
|
2018-07-16 14:30:04 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInterestedInRoomAlias returns a bool on whether an application service's
|
|
|
|
// namespace includes the given room alias
|
|
|
|
func (a *ApplicationService) IsInterestedInRoomAlias(
|
|
|
|
roomAlias string,
|
|
|
|
) bool {
|
|
|
|
if namespaceSlice, ok := a.NamespaceMap["aliases"]; ok {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if namespace.RegexpObject.MatchString(roomAlias) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:36:04 +02:00
|
|
|
// loadAppServices iterates through all application service config files
|
2018-02-08 12:02:48 +01:00
|
|
|
// and loads their data into the config object for later access.
|
2020-08-10 15:18:04 +02:00
|
|
|
func loadAppServices(config *AppServiceAPI, derived *Derived) error {
|
|
|
|
for _, configPath := range config.ConfigFiles {
|
2018-06-18 11:43:15 +02:00
|
|
|
// Create a new application service with default options
|
|
|
|
appservice := ApplicationService{
|
|
|
|
RateLimited: true,
|
|
|
|
}
|
2017-12-19 18:00:44 +01:00
|
|
|
|
|
|
|
// Create an absolute path from a potentially relative path
|
|
|
|
absPath, err := filepath.Abs(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the application service's config file
|
|
|
|
configData, err := ioutil.ReadFile(absPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the config data into our struct
|
|
|
|
if err = yaml.UnmarshalStrict(configData, &appservice); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the parsed application service to the global config
|
2020-08-10 15:18:04 +02:00
|
|
|
derived.ApplicationServices = append(
|
|
|
|
derived.ApplicationServices, appservice,
|
2018-05-30 14:43:13 +02:00
|
|
|
)
|
2017-12-19 18:00:44 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 12:02:48 +01:00
|
|
|
// Check for any errors in the loaded application services
|
2020-08-10 15:18:04 +02:00
|
|
|
return checkErrors(config, derived)
|
2018-02-08 12:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// setupRegexps will create regex objects for exclusive and non-exclusive
|
|
|
|
// usernames, aliases and rooms of all application services, so that other
|
|
|
|
// methods can quickly check if a particular string matches any of them.
|
2020-08-10 15:18:04 +02:00
|
|
|
func setupRegexps(_ *AppServiceAPI, derived *Derived) (err error) {
|
2018-02-08 12:02:48 +01:00
|
|
|
// Combine all exclusive namespaces for later string checking
|
2018-05-30 14:43:13 +02:00
|
|
|
var exclusiveUsernameStrings, exclusiveAliasStrings []string
|
2018-02-08 12:02:48 +01:00
|
|
|
|
|
|
|
// If an application service's regex is marked as exclusive, add
|
2018-05-30 14:43:13 +02:00
|
|
|
// its contents to the overall exlusive regex string. Room regex
|
|
|
|
// not necessary as we aren't denying exclusive room ID creation
|
2020-08-10 15:18:04 +02:00
|
|
|
for _, appservice := range derived.ApplicationServices {
|
2018-02-08 12:02:48 +01:00
|
|
|
for key, namespaceSlice := range appservice.NamespaceMap {
|
|
|
|
switch key {
|
|
|
|
case "users":
|
|
|
|
appendExclusiveNamespaceRegexs(&exclusiveUsernameStrings, namespaceSlice)
|
|
|
|
case "aliases":
|
|
|
|
appendExclusiveNamespaceRegexs(&exclusiveAliasStrings, namespaceSlice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join the regexes together into one big regex.
|
|
|
|
// i.e. "app1.*", "app2.*" -> "(app1.*)|(app2.*)"
|
2018-05-30 14:43:13 +02:00
|
|
|
// Later we can check if a username or alias matches any exclusive regex and
|
|
|
|
// deny access if it isn't from an application service
|
2018-02-08 12:02:48 +01:00
|
|
|
exclusiveUsernames := strings.Join(exclusiveUsernameStrings, "|")
|
2018-05-30 14:43:13 +02:00
|
|
|
exclusiveAliases := strings.Join(exclusiveAliasStrings, "|")
|
2018-02-08 12:02:48 +01:00
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// If there are no exclusive regexes, compile string so that it will not match
|
|
|
|
// any valid usernames/aliases/roomIDs
|
2018-02-27 12:42:10 +01:00
|
|
|
if exclusiveUsernames == "" {
|
|
|
|
exclusiveUsernames = "^$"
|
|
|
|
}
|
2018-05-30 14:43:13 +02:00
|
|
|
if exclusiveAliases == "" {
|
|
|
|
exclusiveAliases = "^$"
|
|
|
|
}
|
2018-02-27 12:42:10 +01:00
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// Store compiled Regex
|
2020-08-10 15:18:04 +02:00
|
|
|
if derived.ExclusiveApplicationServicesUsernameRegexp, err = regexp.Compile(exclusiveUsernames); err != nil {
|
2018-05-30 14:43:13 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-08-10 15:18:04 +02:00
|
|
|
if derived.ExclusiveApplicationServicesAliasRegexp, err = regexp.Compile(exclusiveAliases); err != nil {
|
2018-05-30 14:43:13 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-02-08 12:02:48 +01:00
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
return nil
|
2018-02-08 12:02:48 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// appendExclusiveNamespaceRegexs takes a slice of strings and a slice of
|
2018-02-08 12:02:48 +01:00
|
|
|
// namespaces and will append the regexes of only the exclusive namespaces
|
|
|
|
// into the string slice
|
|
|
|
func appendExclusiveNamespaceRegexs(
|
|
|
|
exclusiveStrings *[]string, namespaces []ApplicationServiceNamespace,
|
|
|
|
) {
|
2018-05-30 14:43:13 +02:00
|
|
|
for index, namespace := range namespaces {
|
2018-02-08 12:02:48 +01:00
|
|
|
if namespace.Exclusive {
|
|
|
|
// We append parenthesis to later separate each regex when we compile
|
|
|
|
// i.e. "app1.*", "app2.*" -> "(app1.*)|(app2.*)"
|
|
|
|
*exclusiveStrings = append(*exclusiveStrings, "("+namespace.Regex+")")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile this regex into a Regexp object for later use
|
2018-05-30 14:43:13 +02:00
|
|
|
namespaces[index].RegexpObject, _ = regexp.Compile(namespace.Regex)
|
2018-02-08 12:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkErrors checks for any configuration errors amongst the loaded
|
|
|
|
// application services according to the application service spec.
|
2020-08-10 15:18:04 +02:00
|
|
|
func checkErrors(config *AppServiceAPI, derived *Derived) (err error) {
|
2018-02-08 12:02:48 +01:00
|
|
|
var idMap = make(map[string]bool)
|
|
|
|
var tokenMap = make(map[string]bool)
|
|
|
|
|
2018-06-18 11:43:15 +02:00
|
|
|
// Compile regexp object for checking groupIDs
|
|
|
|
groupIDRegexp := regexp.MustCompile(`\+.*:.*`)
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// Check each application service for any config errors
|
2020-08-10 15:18:04 +02:00
|
|
|
for _, appservice := range derived.ApplicationServices {
|
2018-06-18 11:43:15 +02:00
|
|
|
// Namespace-related checks
|
|
|
|
for key, namespaceSlice := range appservice.NamespaceMap {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if err := validateNamespace(&appservice, key, &namespace, groupIDRegexp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:16:01 +02:00
|
|
|
// Check if the url has trailing /'s. If so, remove them
|
|
|
|
appservice.URL = strings.TrimRight(appservice.URL, "/")
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// Check if we've already seen this ID. No two application services
|
|
|
|
// can have the same ID or token.
|
2018-02-08 12:02:48 +01:00
|
|
|
if idMap[appservice.ID] {
|
2020-08-10 15:18:04 +02:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-07-05 18:34:59 +02:00
|
|
|
"Application service ID %s must be unique", appservice.ID,
|
2018-04-10 14:21:20 +02:00
|
|
|
)})
|
2018-02-08 12:02:48 +01:00
|
|
|
}
|
2018-05-30 14:43:13 +02:00
|
|
|
// Check if we've already seen this token
|
2018-02-08 12:02:48 +01:00
|
|
|
if tokenMap[appservice.ASToken] {
|
2020-08-10 15:18:04 +02:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-07-05 18:34:59 +02:00
|
|
|
"Application service Token %s must be unique", appservice.ASToken,
|
2018-04-10 14:21:20 +02:00
|
|
|
)})
|
2018-02-08 12:02:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the id/token to their respective maps if we haven't already
|
|
|
|
// seen them.
|
|
|
|
idMap[appservice.ID] = true
|
2018-06-01 17:34:52 +02:00
|
|
|
tokenMap[appservice.ASToken] = true
|
2018-05-30 14:43:13 +02:00
|
|
|
|
2018-06-18 11:43:15 +02:00
|
|
|
// TODO: Remove once rate_limited is implemented
|
|
|
|
if appservice.RateLimited {
|
|
|
|
log.Warn("WARNING: Application service option rate_limited is currently unimplemented")
|
|
|
|
}
|
|
|
|
// TODO: Remove once protocols is implemented
|
|
|
|
if len(appservice.Protocols) > 0 {
|
|
|
|
log.Warn("WARNING: Application service option protocols is currently unimplemented")
|
|
|
|
}
|
2018-02-08 12:02:48 +01:00
|
|
|
}
|
|
|
|
|
2020-08-10 15:18:04 +02:00
|
|
|
return setupRegexps(config, derived)
|
2018-06-18 11:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validateNamespace returns nil or an error based on whether a given
|
|
|
|
// application service namespace is valid. A namespace is valid if it has the
|
|
|
|
// required fields, and its regex is correct.
|
|
|
|
func validateNamespace(
|
|
|
|
appservice *ApplicationService,
|
|
|
|
key string,
|
|
|
|
namespace *ApplicationServiceNamespace,
|
|
|
|
groupIDRegexp *regexp.Regexp,
|
|
|
|
) error {
|
2018-02-08 12:02:48 +01:00
|
|
|
// Check that namespace(s) are valid regex
|
2018-06-18 11:43:15 +02:00
|
|
|
if !IsValidRegex(namespace.Regex) {
|
2020-08-10 15:18:04 +02:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-06-18 11:43:15 +02:00
|
|
|
"Invalid regex string for Application Service %s", appservice.ID,
|
|
|
|
)})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if GroupID for the users namespace is in the correct format
|
|
|
|
if key == "users" && namespace.GroupID != "" {
|
|
|
|
// TODO: Remove once group_id is implemented
|
|
|
|
log.Warn("WARNING: Application service option group_id is currently unimplemented")
|
|
|
|
|
|
|
|
correctFormat := groupIDRegexp.MatchString(namespace.GroupID)
|
|
|
|
if !correctFormat {
|
2020-08-10 15:18:04 +02:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-06-18 11:43:15 +02:00
|
|
|
"Invalid user group_id field for application service %s.",
|
|
|
|
appservice.ID,
|
|
|
|
)})
|
2018-02-08 12:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 11:43:15 +02:00
|
|
|
return nil
|
2017-12-19 18:00:44 +01:00
|
|
|
}
|
2018-02-08 12:02:48 +01:00
|
|
|
|
|
|
|
// IsValidRegex returns true or false based on whether the
|
|
|
|
// given string is valid regex or not
|
|
|
|
func IsValidRegex(regexString string) bool {
|
|
|
|
_, err := regexp.Compile(regexString)
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|