2020-09-02 16:13:15 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-09-02 12:47:31 +00:00
|
|
|
package perform
|
2020-05-04 12:53:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-24 08:59:59 +00:00
|
|
|
"errors"
|
2020-05-04 12:53:47 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
fsAPI "github.com/matrix-org/dendrite/federationsender/api"
|
2020-06-12 13:55:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2020-05-04 12:53:47 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 12:47:31 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2020-09-02 16:13:15 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
2020-09-02 12:47:31 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-05-04 12:53:47 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-09-02 12:47:31 +00:00
|
|
|
type Joiner struct {
|
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
Cfg *config.RoomServer
|
|
|
|
FSAPI fsAPI.FederationSenderInternalAPI
|
|
|
|
DB storage.Database
|
|
|
|
|
2020-09-02 16:13:15 +00:00
|
|
|
Inputer *input.Inputer
|
2020-09-02 12:47:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:06:14 +00:00
|
|
|
// PerformJoin handles joining matrix rooms, including over federation by talking to the federationsender.
|
2020-09-02 12:47:31 +00:00
|
|
|
func (r *Joiner) PerformJoin(
|
2020-05-04 12:53:47 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformJoinRequest,
|
|
|
|
res *api.PerformJoinResponse,
|
2020-06-24 14:06:14 +00:00
|
|
|
) {
|
2020-11-19 11:34:59 +00:00
|
|
|
roomID, joinedVia, err := r.performJoin(ctx, req)
|
2020-06-24 14:06:14 +00:00
|
|
|
if err != nil {
|
|
|
|
perr, ok := err.(*api.PerformError)
|
|
|
|
if ok {
|
|
|
|
res.Error = perr
|
|
|
|
} else {
|
|
|
|
res.Error = &api.PerformError{
|
|
|
|
Msg: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.RoomID = roomID
|
2020-11-19 11:34:59 +00:00
|
|
|
res.JoinedVia = joinedVia
|
2020-06-24 14:06:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 12:47:31 +00:00
|
|
|
func (r *Joiner) performJoin(
|
2020-06-24 14:06:14 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformJoinRequest,
|
2020-11-19 11:34:59 +00:00
|
|
|
) (string, gomatrixserverlib.ServerName, error) {
|
2020-05-04 12:53:47 +00:00
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", &api.PerformError{
|
2020-06-24 14:06:14 +00:00
|
|
|
Code: api.PerformErrorBadRequest,
|
|
|
|
Msg: fmt.Sprintf("Supplied user ID %q in incorrect format", req.UserID),
|
|
|
|
}
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
if domain != r.Cfg.Matrix.ServerName {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", &api.PerformError{
|
2020-06-24 14:06:14 +00:00
|
|
|
Code: api.PerformErrorBadRequest,
|
|
|
|
Msg: fmt.Sprintf("User %q does not belong to this homeserver", req.UserID),
|
|
|
|
}
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.RoomIDOrAlias, "!") {
|
2020-06-24 14:06:14 +00:00
|
|
|
return r.performJoinRoomByID(ctx, req)
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.RoomIDOrAlias, "#") {
|
2020-06-24 14:06:14 +00:00
|
|
|
return r.performJoinRoomByAlias(ctx, req)
|
|
|
|
}
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", &api.PerformError{
|
2020-06-24 14:06:14 +00:00
|
|
|
Code: api.PerformErrorBadRequest,
|
|
|
|
Msg: fmt.Sprintf("Room ID or alias %q is invalid", req.RoomIDOrAlias),
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 12:47:31 +00:00
|
|
|
func (r *Joiner) performJoinRoomByAlias(
|
2020-05-04 12:53:47 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformJoinRequest,
|
2020-11-19 11:34:59 +00:00
|
|
|
) (string, gomatrixserverlib.ServerName, error) {
|
2020-05-04 12:53:47 +00:00
|
|
|
// Get the domain part of the room alias.
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', req.RoomIDOrAlias)
|
|
|
|
if err != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", fmt.Errorf("Alias %q is not in the correct format", req.RoomIDOrAlias)
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
req.ServerNames = append(req.ServerNames, domain)
|
|
|
|
|
|
|
|
// Check if this alias matches our own server configuration. If it
|
|
|
|
// doesn't then we'll need to try a federated join.
|
|
|
|
var roomID string
|
|
|
|
if domain != r.Cfg.Matrix.ServerName {
|
|
|
|
// The alias isn't owned by us, so we will need to try joining using
|
|
|
|
// a remote server.
|
|
|
|
dirReq := fsAPI.PerformDirectoryLookupRequest{
|
|
|
|
RoomAlias: req.RoomIDOrAlias, // the room alias to lookup
|
|
|
|
ServerName: domain, // the server to ask
|
|
|
|
}
|
|
|
|
dirRes := fsAPI.PerformDirectoryLookupResponse{}
|
2020-09-02 12:47:31 +00:00
|
|
|
err = r.FSAPI.PerformDirectoryLookup(ctx, &dirReq, &dirRes)
|
2020-05-04 12:53:47 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("error looking up alias %q", req.RoomIDOrAlias)
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", fmt.Errorf("Looking up alias %q over federation failed: %w", req.RoomIDOrAlias, err)
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
roomID = dirRes.RoomID
|
|
|
|
req.ServerNames = append(req.ServerNames, dirRes.ServerNames...)
|
|
|
|
} else {
|
|
|
|
// Otherwise, look up if we know this room alias locally.
|
|
|
|
roomID, err = r.DB.GetRoomIDForAlias(ctx, req.RoomIDOrAlias)
|
|
|
|
if err != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", fmt.Errorf("Lookup room alias %q failed: %w", req.RoomIDOrAlias, err)
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the room ID is empty then we failed to look up the alias.
|
|
|
|
if roomID == "" {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", fmt.Errorf("Alias %q not found", req.RoomIDOrAlias)
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we do, then pluck out the room ID and continue the join.
|
|
|
|
req.RoomIDOrAlias = roomID
|
2020-06-24 14:06:14 +00:00
|
|
|
return r.performJoinRoomByID(ctx, req)
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Break this function up a bit
|
|
|
|
// nolint:gocyclo
|
2020-09-02 12:47:31 +00:00
|
|
|
func (r *Joiner) performJoinRoomByID(
|
2020-05-04 12:53:47 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformJoinRequest,
|
2020-11-19 11:34:59 +00:00
|
|
|
) (string, gomatrixserverlib.ServerName, error) {
|
2020-05-04 12:53:47 +00:00
|
|
|
// Get the domain part of the room ID.
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('!', req.RoomIDOrAlias)
|
|
|
|
if err != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", &api.PerformError{
|
2020-06-24 14:06:14 +00:00
|
|
|
Code: api.PerformErrorBadRequest,
|
|
|
|
Msg: fmt.Sprintf("Room ID %q is invalid: %s", req.RoomIDOrAlias, err),
|
|
|
|
}
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
2020-07-03 09:25:26 +00:00
|
|
|
|
|
|
|
// If the server name in the room ID isn't ours then it's a
|
|
|
|
// possible candidate for finding the room via federation. Add
|
|
|
|
// it to the list of servers to try.
|
|
|
|
if domain != r.Cfg.Matrix.ServerName {
|
|
|
|
req.ServerNames = append(req.ServerNames, domain)
|
|
|
|
}
|
2020-05-04 12:53:47 +00:00
|
|
|
|
|
|
|
// Prepare the template for the join event.
|
|
|
|
userID := req.UserID
|
|
|
|
eb := gomatrixserverlib.EventBuilder{
|
|
|
|
Type: gomatrixserverlib.MRoomMember,
|
|
|
|
Sender: userID,
|
|
|
|
StateKey: &userID,
|
|
|
|
RoomID: req.RoomIDOrAlias,
|
|
|
|
Redacts: "",
|
|
|
|
}
|
|
|
|
if err = eb.SetUnsigned(struct{}{}); err != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", fmt.Errorf("eb.SetUnsigned: %w", err)
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// It is possible for the request to include some "content" for the
|
|
|
|
// event. We'll always overwrite the "membership" key, but the rest,
|
|
|
|
// like "display_name" or "avatar_url", will be kept if supplied.
|
|
|
|
if req.Content == nil {
|
|
|
|
req.Content = map[string]interface{}{}
|
|
|
|
}
|
2020-05-04 17:34:09 +00:00
|
|
|
req.Content["membership"] = gomatrixserverlib.Join
|
2020-05-04 12:53:47 +00:00
|
|
|
if err = eb.SetContent(req.Content); err != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", fmt.Errorf("eb.SetContent: %w", err)
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 10:17:46 +00:00
|
|
|
// Force a federated join if we aren't in the room and we've been
|
|
|
|
// given some server names to try joining by.
|
2020-09-02 12:47:31 +00:00
|
|
|
serverInRoom, _ := helpers.IsServerCurrentlyInRoom(ctx, r.DB, r.ServerName, req.RoomIDOrAlias)
|
2020-09-15 10:17:46 +00:00
|
|
|
forceFederatedJoin := len(req.ServerNames) > 0 && !serverInRoom
|
|
|
|
|
|
|
|
// Force a federated join if we're dealing with a pending invite
|
|
|
|
// and we aren't in the room.
|
2020-09-02 12:47:31 +00:00
|
|
|
isInvitePending, inviteSender, _, err := helpers.IsInvitePending(ctx, r.DB, req.RoomIDOrAlias, req.UserID)
|
2020-09-15 10:17:46 +00:00
|
|
|
if err == nil && isInvitePending {
|
2020-05-26 13:41:16 +00:00
|
|
|
_, inviterDomain, ierr := gomatrixserverlib.SplitID('@', inviteSender)
|
|
|
|
if ierr != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", fmt.Errorf("gomatrixserverlib.SplitID: %w", err)
|
2020-05-14 13:58:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 10:17:46 +00:00
|
|
|
// If we were invited by someone from another server then we can
|
|
|
|
// assume they are in the room so we can join via them.
|
2020-05-26 13:41:16 +00:00
|
|
|
if inviterDomain != r.Cfg.Matrix.ServerName {
|
|
|
|
req.ServerNames = append(req.ServerNames, inviterDomain)
|
2020-09-15 10:17:46 +00:00
|
|
|
forceFederatedJoin = true
|
2020-05-26 13:41:16 +00:00
|
|
|
}
|
2020-05-14 13:58:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 10:17:46 +00:00
|
|
|
// If we should do a forced federated join then do that.
|
2020-11-19 11:34:59 +00:00
|
|
|
var joinedVia gomatrixserverlib.ServerName
|
2020-09-15 10:17:46 +00:00
|
|
|
if forceFederatedJoin {
|
2020-11-19 11:34:59 +00:00
|
|
|
joinedVia, err = r.performFederatedJoinRoomByID(ctx, req)
|
|
|
|
return req.RoomIDOrAlias, joinedVia, err
|
2020-09-15 10:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 12:53:47 +00:00
|
|
|
// Try to construct an actual join event from the template.
|
|
|
|
// If this succeeds then it is a sign that the room already exists
|
|
|
|
// locally on the homeserver.
|
|
|
|
// TODO: Check what happens if the room exists on the server
|
|
|
|
// but everyone has since left. I suspect it does the wrong thing.
|
2020-09-02 16:13:15 +00:00
|
|
|
event, buildRes, err := buildEvent(ctx, r.DB, r.Cfg.Matrix, &eb)
|
2020-05-04 12:53:47 +00:00
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
// The room join is local. Send the new join event into the
|
|
|
|
// roomserver. First of all check that the user isn't already
|
|
|
|
// a member of the room.
|
|
|
|
alreadyJoined := false
|
|
|
|
for _, se := range buildRes.StateEvents {
|
2020-08-25 20:04:35 +00:00
|
|
|
if !se.StateKeyEquals(userID) {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-04 12:53:47 +00:00
|
|
|
if membership, merr := se.Membership(); merr == nil {
|
2020-08-25 20:04:35 +00:00
|
|
|
alreadyJoined = (membership == gomatrixserverlib.Join)
|
|
|
|
break
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't already joined the room then send an event
|
|
|
|
// into the room changing our membership status.
|
|
|
|
if !alreadyJoined {
|
|
|
|
inputReq := api.InputRoomEventsRequest{
|
|
|
|
InputRoomEvents: []api.InputRoomEvent{
|
2020-05-04 17:34:09 +00:00
|
|
|
{
|
2020-05-04 12:53:47 +00:00
|
|
|
Kind: api.KindNew,
|
|
|
|
Event: event.Headered(buildRes.RoomVersion),
|
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
|
|
|
SendAsServer: string(r.Cfg.Matrix.ServerName),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
inputRes := api.InputRoomEventsResponse{}
|
2020-09-16 12:00:52 +00:00
|
|
|
r.Inputer.InputRoomEvents(ctx, &inputReq, &inputRes)
|
|
|
|
if err = inputRes.Err(); err != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", &api.PerformError{
|
2020-09-29 12:40:29 +00:00
|
|
|
Code: api.PerformErrorNotAllowed,
|
|
|
|
Msg: fmt.Sprintf("InputRoomEvents auth failed: %s", err),
|
2020-06-24 08:59:59 +00:00
|
|
|
}
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:55:57 +00:00
|
|
|
case eventutil.ErrRoomNoExists:
|
2020-07-03 09:25:26 +00:00
|
|
|
// The room doesn't exist locally. If the room ID looks like it should
|
|
|
|
// be ours then this probably means that we've nuked our database at
|
|
|
|
// some point.
|
2020-05-04 12:53:47 +00:00
|
|
|
if domain == r.Cfg.Matrix.ServerName {
|
2020-07-03 09:25:26 +00:00
|
|
|
// If there are no more server names to try then give up here.
|
|
|
|
// Otherwise we'll try a federated join as normal, since it's quite
|
|
|
|
// possible that the room still exists on other servers.
|
|
|
|
if len(req.ServerNames) == 0 {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", &api.PerformError{
|
2020-07-03 09:25:26 +00:00
|
|
|
Code: api.PerformErrorNoRoom,
|
|
|
|
Msg: fmt.Sprintf("Room ID %q does not exist", req.RoomIDOrAlias),
|
|
|
|
}
|
2020-06-24 14:06:14 +00:00
|
|
|
}
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 13:58:47 +00:00
|
|
|
// Perform a federated room join.
|
2020-11-19 11:34:59 +00:00
|
|
|
joinedVia, err = r.performFederatedJoinRoomByID(ctx, req)
|
|
|
|
return req.RoomIDOrAlias, joinedVia, err
|
2020-05-04 12:53:47 +00:00
|
|
|
|
|
|
|
default:
|
2020-05-14 13:58:47 +00:00
|
|
|
// Something else went wrong.
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", "", fmt.Errorf("Error joining local room: %q", err)
|
2020-05-14 13:58:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:06:14 +00:00
|
|
|
// By this point, if req.RoomIDOrAlias contained an alias, then
|
|
|
|
// it will have been overwritten with a room ID by performJoinRoomByAlias.
|
|
|
|
// We should now include this in the response so that the CS API can
|
|
|
|
// return the right room ID.
|
2020-11-19 11:34:59 +00:00
|
|
|
return req.RoomIDOrAlias, r.Cfg.Matrix.ServerName, nil
|
2020-05-14 13:58:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 12:47:31 +00:00
|
|
|
func (r *Joiner) performFederatedJoinRoomByID(
|
2020-05-14 13:58:47 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformJoinRequest,
|
2020-11-19 11:34:59 +00:00
|
|
|
) (gomatrixserverlib.ServerName, error) {
|
2020-05-14 13:58:47 +00:00
|
|
|
// Try joining by all of the supplied server names.
|
|
|
|
fedReq := fsAPI.PerformJoinRequest{
|
|
|
|
RoomID: req.RoomIDOrAlias, // the room ID to try and join
|
|
|
|
UserID: req.UserID, // the user ID joining the room
|
|
|
|
ServerNames: req.ServerNames, // the server to try joining with
|
|
|
|
Content: req.Content, // the membership event content
|
|
|
|
}
|
|
|
|
fedRes := fsAPI.PerformJoinResponse{}
|
2020-09-02 12:47:31 +00:00
|
|
|
r.FSAPI.PerformJoin(ctx, &fedReq, &fedRes)
|
2020-06-25 14:04:48 +00:00
|
|
|
if fedRes.LastError != nil {
|
2020-11-19 11:34:59 +00:00
|
|
|
return "", &api.PerformError{
|
2020-06-25 14:04:48 +00:00
|
|
|
Code: api.PerformErrRemote,
|
|
|
|
Msg: fedRes.LastError.Message,
|
|
|
|
RemoteCode: fedRes.LastError.Code,
|
|
|
|
}
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
2020-11-19 11:34:59 +00:00
|
|
|
return fedRes.JoinedVia, nil
|
2020-05-04 12:53:47 +00:00
|
|
|
}
|
2020-09-02 16:13:15 +00:00
|
|
|
|
|
|
|
func buildEvent(
|
|
|
|
ctx context.Context, db storage.Database, cfg *config.Global, builder *gomatrixserverlib.EventBuilder,
|
|
|
|
) (*gomatrixserverlib.HeaderedEvent, *api.QueryLatestEventsAndStateResponse, error) {
|
|
|
|
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("gomatrixserverlib.StateNeededForEventBuilder: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(eventsNeeded.Tuples()) == 0 {
|
|
|
|
return nil, nil, errors.New("expecting state tuples for event builder, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
var queryRes api.QueryLatestEventsAndStateResponse
|
|
|
|
err = helpers.QueryLatestEventsAndState(ctx, db, &api.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: builder.RoomID,
|
|
|
|
StateToFetch: eventsNeeded.Tuples(),
|
|
|
|
}, &queryRes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("QueryLatestEventsAndState: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ev, err := eventutil.BuildEvent(ctx, builder, cfg, time.Now(), &eventsNeeded, &queryRes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return ev, &queryRes, nil
|
|
|
|
}
|