2017-12-19 11:00:44 -06: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 05:02:48 -06:00
|
|
|
"fmt"
|
2017-12-19 11:00:44 -06:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2018-02-08 05:02:48 -06:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2017-12-19 11:00:44 -06:00
|
|
|
|
2018-06-18 04:43:15 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-05-30 07:43:13 -05:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2017-12-19 11:00:44 -06:00
|
|
|
)
|
|
|
|
|
2020-08-10 08:18:04 -05:00
|
|
|
type AppServiceAPI struct {
|
|
|
|
Matrix *Global `yaml:"-"`
|
|
|
|
Derived *Derived `yaml:"-"` // TODO: Nuke Derived from orbit
|
|
|
|
|
2020-08-13 06:16:37 -05:00
|
|
|
InternalAPI InternalAPIOptions `yaml:"internal_api"`
|
2020-08-10 08:18:04 -05:00
|
|
|
|
|
|
|
Database DatabaseOptions `yaml:"database"`
|
|
|
|
|
2021-03-05 04:40:27 -06:00
|
|
|
// DisableTLSValidation disables the validation of X.509 TLS certs
|
|
|
|
// on appservice endpoints. This is not recommended in production!
|
|
|
|
DisableTLSValidation bool `yaml:"disable_tls_validation"`
|
|
|
|
|
2020-08-10 08:18:04 -05:00
|
|
|
ConfigFiles []string `yaml:"config_files"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AppServiceAPI) Defaults() {
|
2020-08-13 06:16:37 -05:00
|
|
|
c.InternalAPI.Listen = "http://localhost:7777"
|
|
|
|
c.InternalAPI.Connect = "http://localhost:7777"
|
2021-03-08 07:18:29 -06:00
|
|
|
c.Database.Defaults(10)
|
2020-08-10 08:18:04 -05:00
|
|
|
c.Database.ConnectionString = "file:appservice.db"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AppServiceAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
|
2020-08-13 06:16:37 -05: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 08:18:04 -05:00
|
|
|
checkNotEmpty(configErrs, "app_service_api.database.connection_string", string(c.Database.ConnectionString))
|
|
|
|
}
|
|
|
|
|
2017-12-19 11:00:44 -06: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 04:43:15 -05: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 05:02:48 -06:00
|
|
|
// Regex object representing our pattern. Saves having to recompile every time
|
|
|
|
RegexpObject *regexp.Regexp
|
2017-12-19 11:00:44 -06: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 07:43:13 -05:00
|
|
|
// Information about an application service's namespaces. Key is either
|
|
|
|
// "users", "aliases" or "rooms"
|
2018-02-08 05:02:48 -06:00
|
|
|
NamespaceMap map[string][]ApplicationServiceNamespace `yaml:"namespaces"`
|
2018-06-18 04:43:15 -05: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 11:00:44 -06:00
|
|
|
}
|
|
|
|
|
2018-07-16 07:30:04 -05: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 09:31:27 -05: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 07:30:04 -05: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 09:36:04 -05:00
|
|
|
// loadAppServices iterates through all application service config files
|
2018-02-08 05:02:48 -06:00
|
|
|
// and loads their data into the config object for later access.
|
2020-08-10 08:18:04 -05:00
|
|
|
func loadAppServices(config *AppServiceAPI, derived *Derived) error {
|
|
|
|
for _, configPath := range config.ConfigFiles {
|
2018-06-18 04:43:15 -05:00
|
|
|
// Create a new application service with default options
|
|
|
|
appservice := ApplicationService{
|
|
|
|
RateLimited: true,
|
|
|
|
}
|
2017-12-19 11:00:44 -06: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 08:18:04 -05:00
|
|
|
derived.ApplicationServices = append(
|
|
|
|
derived.ApplicationServices, appservice,
|
2018-05-30 07:43:13 -05:00
|
|
|
)
|
2017-12-19 11:00:44 -06:00
|
|
|
}
|
|
|
|
|
2018-02-08 05:02:48 -06:00
|
|
|
// Check for any errors in the loaded application services
|
2020-08-10 08:18:04 -05:00
|
|
|
return checkErrors(config, derived)
|
2018-02-08 05:02:48 -06: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.
|
2021-03-05 08:57:42 -06:00
|
|
|
func setupRegexps(asAPI *AppServiceAPI, derived *Derived) (err error) {
|
2018-02-08 05:02:48 -06:00
|
|
|
// Combine all exclusive namespaces for later string checking
|
2018-05-30 07:43:13 -05:00
|
|
|
var exclusiveUsernameStrings, exclusiveAliasStrings []string
|
2018-02-08 05:02:48 -06:00
|
|
|
|
|
|
|
// If an application service's regex is marked as exclusive, add
|
2018-05-30 07:43:13 -05: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 08:18:04 -05:00
|
|
|
for _, appservice := range derived.ApplicationServices {
|
2021-03-05 08:57:42 -06:00
|
|
|
// The sender_localpart can be considered an exclusive regex for a single user, so let's do that
|
|
|
|
// to simplify the code
|
|
|
|
var senderUserIDSlice = []string{fmt.Sprintf("@%s:%s", appservice.SenderLocalpart, asAPI.Matrix.ServerName)}
|
|
|
|
usersSlice, found := appservice.NamespaceMap["users"]
|
|
|
|
if !found {
|
|
|
|
usersSlice = []ApplicationServiceNamespace{}
|
|
|
|
appservice.NamespaceMap["users"] = usersSlice
|
|
|
|
}
|
|
|
|
appendExclusiveNamespaceRegexs(&senderUserIDSlice, usersSlice)
|
|
|
|
|
2018-02-08 05:02:48 -06: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 07:43:13 -05: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 05:02:48 -06:00
|
|
|
exclusiveUsernames := strings.Join(exclusiveUsernameStrings, "|")
|
2018-05-30 07:43:13 -05:00
|
|
|
exclusiveAliases := strings.Join(exclusiveAliasStrings, "|")
|
2018-02-08 05:02:48 -06:00
|
|
|
|
2018-05-30 07:43:13 -05:00
|
|
|
// If there are no exclusive regexes, compile string so that it will not match
|
|
|
|
// any valid usernames/aliases/roomIDs
|
2018-02-27 05:42:10 -06:00
|
|
|
if exclusiveUsernames == "" {
|
|
|
|
exclusiveUsernames = "^$"
|
|
|
|
}
|
2018-05-30 07:43:13 -05:00
|
|
|
if exclusiveAliases == "" {
|
|
|
|
exclusiveAliases = "^$"
|
|
|
|
}
|
2018-02-27 05:42:10 -06:00
|
|
|
|
2018-05-30 07:43:13 -05:00
|
|
|
// Store compiled Regex
|
2020-08-10 08:18:04 -05:00
|
|
|
if derived.ExclusiveApplicationServicesUsernameRegexp, err = regexp.Compile(exclusiveUsernames); err != nil {
|
2018-05-30 07:43:13 -05:00
|
|
|
return err
|
|
|
|
}
|
2020-08-10 08:18:04 -05:00
|
|
|
if derived.ExclusiveApplicationServicesAliasRegexp, err = regexp.Compile(exclusiveAliases); err != nil {
|
2018-05-30 07:43:13 -05:00
|
|
|
return err
|
|
|
|
}
|
2018-02-08 05:02:48 -06:00
|
|
|
|
2018-05-30 07:43:13 -05:00
|
|
|
return nil
|
2018-02-08 05:02:48 -06:00
|
|
|
}
|
|
|
|
|
2018-05-30 07:43:13 -05:00
|
|
|
// appendExclusiveNamespaceRegexs takes a slice of strings and a slice of
|
2018-02-08 05:02:48 -06: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 07:43:13 -05:00
|
|
|
for index, namespace := range namespaces {
|
2018-02-08 05:02:48 -06: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 07:43:13 -05:00
|
|
|
namespaces[index].RegexpObject, _ = regexp.Compile(namespace.Regex)
|
2018-02-08 05:02:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkErrors checks for any configuration errors amongst the loaded
|
|
|
|
// application services according to the application service spec.
|
2020-08-10 08:18:04 -05:00
|
|
|
func checkErrors(config *AppServiceAPI, derived *Derived) (err error) {
|
2018-02-08 05:02:48 -06:00
|
|
|
var idMap = make(map[string]bool)
|
|
|
|
var tokenMap = make(map[string]bool)
|
|
|
|
|
2018-06-18 04:43:15 -05:00
|
|
|
// Compile regexp object for checking groupIDs
|
|
|
|
groupIDRegexp := regexp.MustCompile(`\+.*:.*`)
|
|
|
|
|
2018-05-30 07:43:13 -05:00
|
|
|
// Check each application service for any config errors
|
2020-08-10 08:18:04 -05:00
|
|
|
for _, appservice := range derived.ApplicationServices {
|
2018-06-18 04:43:15 -05: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 10:16:01 -05:00
|
|
|
// Check if the url has trailing /'s. If so, remove them
|
|
|
|
appservice.URL = strings.TrimRight(appservice.URL, "/")
|
|
|
|
|
2018-05-30 07:43:13 -05:00
|
|
|
// Check if we've already seen this ID. No two application services
|
|
|
|
// can have the same ID or token.
|
2018-02-08 05:02:48 -06:00
|
|
|
if idMap[appservice.ID] {
|
2020-08-10 08:18:04 -05:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-07-05 11:34:59 -05:00
|
|
|
"Application service ID %s must be unique", appservice.ID,
|
2018-04-10 07:21:20 -05:00
|
|
|
)})
|
2018-02-08 05:02:48 -06:00
|
|
|
}
|
2018-05-30 07:43:13 -05:00
|
|
|
// Check if we've already seen this token
|
2018-02-08 05:02:48 -06:00
|
|
|
if tokenMap[appservice.ASToken] {
|
2020-08-10 08:18:04 -05:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-07-05 11:34:59 -05:00
|
|
|
"Application service Token %s must be unique", appservice.ASToken,
|
2018-04-10 07:21:20 -05:00
|
|
|
)})
|
2018-02-08 05:02:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the id/token to their respective maps if we haven't already
|
|
|
|
// seen them.
|
|
|
|
idMap[appservice.ID] = true
|
2018-06-01 10:34:52 -05:00
|
|
|
tokenMap[appservice.ASToken] = true
|
2018-05-30 07:43:13 -05:00
|
|
|
|
2018-06-18 04:43:15 -05: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 05:02:48 -06:00
|
|
|
}
|
|
|
|
|
2020-08-10 08:18:04 -05:00
|
|
|
return setupRegexps(config, derived)
|
2018-06-18 04:43:15 -05: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 05:02:48 -06:00
|
|
|
// Check that namespace(s) are valid regex
|
2018-06-18 04:43:15 -05:00
|
|
|
if !IsValidRegex(namespace.Regex) {
|
2020-08-10 08:18:04 -05:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-06-18 04:43:15 -05: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 08:18:04 -05:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-06-18 04:43:15 -05:00
|
|
|
"Invalid user group_id field for application service %s.",
|
|
|
|
appservice.ID,
|
|
|
|
)})
|
2018-02-08 05:02:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 04:43:15 -05:00
|
|
|
return nil
|
2017-12-19 11:00:44 -06:00
|
|
|
}
|
2018-02-08 05:02:48 -06: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
|
|
|
|
}
|