2020-09-02 11:13:15 -05: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 07:47:31 -05:00
|
|
|
package perform
|
2020-05-04 07:53:47 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-28 13:29:49 -05:00
|
|
|
"crypto/ed25519"
|
2022-12-22 06:05:59 -06:00
|
|
|
"database/sql"
|
2020-06-24 03:59:59 -05:00
|
|
|
"errors"
|
2020-05-04 07:53:47 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-03-24 05:25:24 -05:00
|
|
|
"github.com/getsentry/sentry-go"
|
2022-10-07 09:00:12 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-06-28 13:29:49 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 09:50:33 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-06-14 09:23:46 -05:00
|
|
|
"github.com/matrix-org/util"
|
2022-10-07 09:00:12 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
fsAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2020-06-12 08:55:57 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2022-05-25 04:05:30 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2021-03-03 11:00:31 -06:00
|
|
|
rsAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 07:47:31 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2020-09-02 11:13:15 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
2021-07-09 11:49:59 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/query"
|
2020-09-02 07:47:31 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2023-04-27 06:54:20 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-05-04 07:53:47 -05:00
|
|
|
)
|
|
|
|
|
2020-09-02 07:47:31 -05:00
|
|
|
type Joiner struct {
|
2022-11-15 09:05:23 -06:00
|
|
|
Cfg *config.RoomServer
|
|
|
|
FSAPI fsAPI.RoomserverFederationAPI
|
|
|
|
RSAPI rsAPI.RoomserverInternalAPI
|
|
|
|
DB storage.Database
|
2020-09-02 07:47:31 -05:00
|
|
|
|
2020-09-02 11:13:15 -05:00
|
|
|
Inputer *input.Inputer
|
2021-07-09 11:49:59 -05:00
|
|
|
Queryer *query.Queryer
|
2020-09-02 07:47:31 -05:00
|
|
|
}
|
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
// PerformJoin handles joining matrix rooms, including over federation by talking to the federationapi.
|
2020-09-02 07:47:31 -05:00
|
|
|
func (r *Joiner) PerformJoin(
|
2020-05-04 07:53:47 -05:00
|
|
|
ctx context.Context,
|
2021-09-08 11:31:03 -05:00
|
|
|
req *rsAPI.PerformJoinRequest,
|
2023-04-28 10:46:01 -05:00
|
|
|
) (roomID string, joinedVia spec.ServerName, err error) {
|
2022-01-31 10:01:54 -06:00
|
|
|
logger := logrus.WithContext(ctx).WithFields(logrus.Fields{
|
|
|
|
"room_id": req.RoomIDOrAlias,
|
|
|
|
"user_id": req.UserID,
|
|
|
|
"servers": req.ServerNames,
|
|
|
|
})
|
|
|
|
logger.Info("User requested to room join")
|
2023-04-28 10:46:01 -05:00
|
|
|
roomID, joinedVia, err = r.performJoin(context.Background(), req)
|
2020-06-24 09:06:14 -05:00
|
|
|
if err != nil {
|
2022-01-31 10:01:54 -06:00
|
|
|
logger.WithError(err).Error("Failed to join room")
|
2021-03-24 05:25:24 -05:00
|
|
|
sentry.CaptureException(err)
|
2023-04-28 10:46:01 -05:00
|
|
|
return "", "", err
|
2020-06-24 09:06:14 -05:00
|
|
|
}
|
2022-01-31 10:01:54 -06:00
|
|
|
logger.Info("User joined room successfully")
|
2023-04-28 10:46:01 -05:00
|
|
|
|
|
|
|
return roomID, joinedVia, nil
|
2020-06-24 09:06:14 -05:00
|
|
|
}
|
|
|
|
|
2020-09-02 07:47:31 -05:00
|
|
|
func (r *Joiner) performJoin(
|
2020-06-24 09:06:14 -05:00
|
|
|
ctx context.Context,
|
2021-09-08 11:31:03 -05:00
|
|
|
req *rsAPI.PerformJoinRequest,
|
2023-04-19 09:50:33 -05:00
|
|
|
) (string, spec.ServerName, error) {
|
2020-05-04 07:53:47 -05:00
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
2023-04-28 10:46:01 -05:00
|
|
|
return "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("supplied user ID %q in incorrect format", req.UserID)}
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
2022-10-26 06:59:19 -05:00
|
|
|
if !r.Cfg.Matrix.IsLocalServerName(domain) {
|
2023-04-28 10:46:01 -05:00
|
|
|
return "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("user %q does not belong to this homeserver", req.UserID)}
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.RoomIDOrAlias, "!") {
|
2020-06-24 09:06:14 -05:00
|
|
|
return r.performJoinRoomByID(ctx, req)
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.RoomIDOrAlias, "#") {
|
2020-06-24 09:06:14 -05:00
|
|
|
return r.performJoinRoomByAlias(ctx, req)
|
|
|
|
}
|
2023-04-28 10:46:01 -05:00
|
|
|
return "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("room ID or alias %q is invalid", req.RoomIDOrAlias)}
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
|
2020-09-02 07:47:31 -05:00
|
|
|
func (r *Joiner) performJoinRoomByAlias(
|
2020-05-04 07:53:47 -05:00
|
|
|
ctx context.Context,
|
2021-09-08 11:31:03 -05:00
|
|
|
req *rsAPI.PerformJoinRequest,
|
2023-04-19 09:50:33 -05:00
|
|
|
) (string, spec.ServerName, error) {
|
2020-05-04 07:53:47 -05:00
|
|
|
// Get the domain part of the room alias.
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', req.RoomIDOrAlias)
|
|
|
|
if err != nil {
|
2021-09-08 11:31:03 -05:00
|
|
|
return "", "", fmt.Errorf("alias %q is not in the correct format", req.RoomIDOrAlias)
|
2020-05-04 07:53:47 -05: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
|
2022-10-26 06:59:19 -05:00
|
|
|
if !r.Cfg.Matrix.IsLocalServerName(domain) {
|
2020-05-04 07:53:47 -05:00
|
|
|
// 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 07:47:31 -05:00
|
|
|
err = r.FSAPI.PerformDirectoryLookup(ctx, &dirReq, &dirRes)
|
2020-05-04 07:53:47 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("error looking up alias %q", req.RoomIDOrAlias)
|
2021-09-08 11:31:03 -05:00
|
|
|
return "", "", fmt.Errorf("looking up alias %q over federation failed: %w", req.RoomIDOrAlias, err)
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
roomID = dirRes.RoomID
|
|
|
|
req.ServerNames = append(req.ServerNames, dirRes.ServerNames...)
|
|
|
|
} else {
|
2021-03-03 11:00:31 -06:00
|
|
|
var getRoomReq = rsAPI.GetRoomIDForAliasRequest{
|
|
|
|
Alias: req.RoomIDOrAlias,
|
|
|
|
IncludeAppservices: true,
|
|
|
|
}
|
|
|
|
var getRoomRes = rsAPI.GetRoomIDForAliasResponse{}
|
2020-05-04 07:53:47 -05:00
|
|
|
// Otherwise, look up if we know this room alias locally.
|
2021-03-03 11:00:31 -06:00
|
|
|
err = r.RSAPI.GetRoomIDForAlias(ctx, &getRoomReq, &getRoomRes)
|
2020-05-04 07:53:47 -05:00
|
|
|
if err != nil {
|
2021-09-08 11:31:03 -05:00
|
|
|
return "", "", fmt.Errorf("lookup room alias %q failed: %w", req.RoomIDOrAlias, err)
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
2021-03-03 11:00:31 -06:00
|
|
|
roomID = getRoomRes.RoomID
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the room ID is empty then we failed to look up the alias.
|
|
|
|
if roomID == "" {
|
2021-09-08 11:31:03 -05:00
|
|
|
return "", "", fmt.Errorf("alias %q not found", req.RoomIDOrAlias)
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we do, then pluck out the room ID and continue the join.
|
|
|
|
req.RoomIDOrAlias = roomID
|
2020-06-24 09:06:14 -05:00
|
|
|
return r.performJoinRoomByID(ctx, req)
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
|
2023-05-16 19:33:27 -05:00
|
|
|
// TODO: Break this function up a bit & move to GMSL
|
2022-05-25 04:05:30 -05:00
|
|
|
// nolint:gocyclo
|
2020-09-02 07:47:31 -05:00
|
|
|
func (r *Joiner) performJoinRoomByID(
|
2020-05-04 07:53:47 -05:00
|
|
|
ctx context.Context,
|
2021-09-08 11:31:03 -05:00
|
|
|
req *rsAPI.PerformJoinRequest,
|
2023-04-19 09:50:33 -05:00
|
|
|
) (string, spec.ServerName, error) {
|
2021-03-08 12:16:28 -06:00
|
|
|
// The original client request ?server_name=... may include this HS so filter that out so we
|
|
|
|
// don't attempt to make_join with ourselves
|
|
|
|
for i := 0; i < len(req.ServerNames); i++ {
|
2022-10-26 06:59:19 -05:00
|
|
|
if r.Cfg.Matrix.IsLocalServerName(req.ServerNames[i]) {
|
2021-03-08 12:16:28 -06:00
|
|
|
// delete this entry
|
|
|
|
req.ServerNames = append(req.ServerNames[:i], req.ServerNames[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:53:47 -05:00
|
|
|
// Get the domain part of the room ID.
|
2023-06-12 06:19:25 -05:00
|
|
|
roomID, err := spec.NewRoomID(req.RoomIDOrAlias)
|
2020-05-04 07:53:47 -05:00
|
|
|
if err != nil {
|
2023-04-28 10:46:01 -05:00
|
|
|
return "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("room ID %q is invalid: %w", req.RoomIDOrAlias, err)}
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
2020-07-03 04:25:26 -05: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.
|
2023-06-12 06:19:25 -05:00
|
|
|
if !r.Cfg.Matrix.IsLocalServerName(roomID.Domain()) {
|
|
|
|
req.ServerNames = append(req.ServerNames, roomID.Domain())
|
2020-07-03 04:25:26 -05:00
|
|
|
}
|
2020-05-04 07:53:47 -05:00
|
|
|
|
2020-09-15 05:17:46 -05:00
|
|
|
// Force a federated join if we aren't in the room and we've been
|
|
|
|
// given some server names to try joining by.
|
2021-09-08 11:31:03 -05:00
|
|
|
inRoomReq := &rsAPI.QueryServerJoinedToRoomRequest{
|
2021-07-09 11:49:59 -05:00
|
|
|
RoomID: req.RoomIDOrAlias,
|
|
|
|
}
|
2021-09-08 11:31:03 -05:00
|
|
|
inRoomRes := &rsAPI.QueryServerJoinedToRoomResponse{}
|
2021-07-09 11:49:59 -05:00
|
|
|
if err = r.Queryer.QueryServerJoinedToRoom(ctx, inRoomReq, inRoomRes); err != nil {
|
|
|
|
return "", "", fmt.Errorf("r.Queryer.QueryServerJoinedToRoom: %w", err)
|
|
|
|
}
|
|
|
|
serverInRoom := inRoomRes.IsInRoom
|
2020-09-15 05:17:46 -05:00
|
|
|
forceFederatedJoin := len(req.ServerNames) > 0 && !serverInRoom
|
|
|
|
|
2023-06-14 09:23:46 -05:00
|
|
|
userID, err := spec.NewUserID(req.UserID, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("user ID %q is invalid: %w", req.UserID, err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the room NID for the supplied room ID.
|
|
|
|
var senderID spec.SenderID
|
|
|
|
checkInvitePending := false
|
|
|
|
info, err := r.DB.RoomInfo(ctx, req.RoomIDOrAlias)
|
|
|
|
if err == nil && info != nil {
|
|
|
|
switch info.RoomVersion {
|
|
|
|
case gomatrixserverlib.RoomVersionPseudoIDs:
|
2023-08-02 05:12:14 -05:00
|
|
|
senderIDPtr, queryErr := r.Queryer.QuerySenderIDForUser(ctx, *roomID, *userID)
|
|
|
|
if queryErr == nil {
|
2023-06-14 09:23:46 -05:00
|
|
|
checkInvitePending = true
|
2023-06-28 13:29:49 -05:00
|
|
|
}
|
2023-08-02 05:12:14 -05:00
|
|
|
if senderIDPtr == nil {
|
2023-06-14 09:23:46 -05:00
|
|
|
// create user room key if needed
|
|
|
|
key, keyErr := r.RSAPI.GetOrCreateUserRoomPrivateKey(ctx, *userID, *roomID)
|
|
|
|
if keyErr != nil {
|
|
|
|
util.GetLogger(ctx).WithError(keyErr).Error("GetOrCreateUserRoomPrivateKey failed")
|
|
|
|
return "", "", fmt.Errorf("GetOrCreateUserRoomPrivateKey failed: %w", keyErr)
|
|
|
|
}
|
2023-06-28 13:29:49 -05:00
|
|
|
senderID = spec.SenderIDFromPseudoIDKey(key)
|
2023-08-02 05:12:14 -05:00
|
|
|
} else {
|
|
|
|
senderID = *senderIDPtr
|
2023-06-14 09:23:46 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
checkInvitePending = true
|
|
|
|
senderID = spec.SenderID(userID.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userDomain := userID.Domain()
|
|
|
|
|
2020-09-15 05:17:46 -05:00
|
|
|
// Force a federated join if we're dealing with a pending invite
|
|
|
|
// and we aren't in the room.
|
2023-06-14 09:23:46 -05:00
|
|
|
if checkInvitePending {
|
|
|
|
isInvitePending, inviteSender, _, inviteEvent, inviteErr := helpers.IsInvitePending(ctx, r.DB, req.RoomIDOrAlias, senderID)
|
|
|
|
if inviteErr == nil && !serverInRoom && isInvitePending {
|
|
|
|
inviter, queryErr := r.RSAPI.QueryUserIDForSender(ctx, *roomID, inviteSender)
|
|
|
|
if queryErr != nil {
|
|
|
|
return "", "", fmt.Errorf("r.RSAPI.QueryUserIDForSender: %w", queryErr)
|
|
|
|
}
|
2020-05-14 08:58:47 -05:00
|
|
|
|
2023-06-14 09:23:46 -05: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.
|
|
|
|
if inviter != nil && !r.Cfg.Matrix.IsLocalServerName(inviter.Domain()) {
|
|
|
|
req.ServerNames = append(req.ServerNames, inviter.Domain())
|
|
|
|
forceFederatedJoin = true
|
|
|
|
memberEvent := gjson.Parse(string(inviteEvent.JSON()))
|
|
|
|
// only set unsigned if we've got a content.membership, which we _should_
|
|
|
|
if memberEvent.Get("content.membership").Exists() {
|
|
|
|
req.Unsigned = map[string]interface{}{
|
|
|
|
"prev_sender": memberEvent.Get("sender").Str,
|
|
|
|
"prev_content": map[string]interface{}{
|
|
|
|
"is_direct": memberEvent.Get("content.is_direct").Bool(),
|
|
|
|
"membership": memberEvent.Get("content.membership").Str,
|
|
|
|
},
|
|
|
|
}
|
2022-10-07 09:00:12 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 08:41:16 -05:00
|
|
|
}
|
2020-05-14 08:58:47 -05:00
|
|
|
}
|
|
|
|
|
2022-12-22 06:05:59 -06:00
|
|
|
// If a guest is trying to join a room, check that the room has a m.room.guest_access event
|
|
|
|
if req.IsGuest {
|
2023-04-27 06:54:20 -05:00
|
|
|
var guestAccessEvent *types.HeaderedEvent
|
2022-12-22 06:05:59 -06:00
|
|
|
guestAccess := "forbidden"
|
2023-04-19 09:50:33 -05:00
|
|
|
guestAccessEvent, err = r.DB.GetStateEvent(ctx, req.RoomIDOrAlias, spec.MRoomGuestAccess, "")
|
2022-12-22 06:05:59 -06:00
|
|
|
if (err != nil && !errors.Is(err, sql.ErrNoRows)) || guestAccessEvent == nil {
|
|
|
|
logrus.WithError(err).Warn("unable to get m.room.guest_access event, defaulting to 'forbidden'")
|
|
|
|
}
|
|
|
|
if guestAccessEvent != nil {
|
|
|
|
guestAccess = gjson.GetBytes(guestAccessEvent.Content(), "guest_access").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Servers MUST only allow guest users to join rooms if the m.room.guest_access state event
|
|
|
|
// is present on the room and has the guest_access value can_join.
|
|
|
|
if guestAccess != "can_join" {
|
2023-04-28 10:46:01 -05:00
|
|
|
return "", "", rsAPI.ErrNotAllowed{Err: fmt.Errorf("guest access is forbidden")}
|
2022-12-22 06:05:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 05:17:46 -05:00
|
|
|
// If we should do a forced federated join then do that.
|
2023-04-19 09:50:33 -05:00
|
|
|
var joinedVia spec.ServerName
|
2020-09-15 05:17:46 -05:00
|
|
|
if forceFederatedJoin {
|
2020-11-19 05:34:59 -06:00
|
|
|
joinedVia, err = r.performFederatedJoinRoomByID(ctx, req)
|
|
|
|
return req.RoomIDOrAlias, joinedVia, err
|
2020-09-15 05:17:46 -05:00
|
|
|
}
|
|
|
|
|
2020-05-04 07:53:47 -05: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.
|
2023-04-14 10:03:07 -05:00
|
|
|
|
|
|
|
var buildRes rsAPI.QueryLatestEventsAndStateResponse
|
2023-07-21 11:08:40 -05:00
|
|
|
identity := r.Cfg.Matrix.SigningIdentity
|
2023-06-14 09:23:46 -05:00
|
|
|
|
2023-06-28 13:29:49 -05:00
|
|
|
// at this point we know we have an existing room
|
|
|
|
if inRoomRes.RoomVersion == gomatrixserverlib.RoomVersionPseudoIDs {
|
|
|
|
var pseudoIDKey ed25519.PrivateKey
|
|
|
|
pseudoIDKey, err = r.RSAPI.GetOrCreateUserRoomPrivateKey(ctx, *userID, *roomID)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("GetOrCreateUserRoomPrivateKey failed")
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping := &gomatrixserverlib.MXIDMapping{
|
|
|
|
UserRoomKey: spec.SenderIDFromPseudoIDKey(pseudoIDKey),
|
|
|
|
UserID: userID.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the mapping with the server identity
|
|
|
|
if err = mapping.Sign(identity.ServerName, identity.KeyID, identity.PrivateKey); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
req.Content["mxid_mapping"] = mapping
|
|
|
|
|
|
|
|
// sign the event with the pseudo ID key
|
|
|
|
identity = fclient.SigningIdentity{
|
2023-07-06 10:15:24 -05:00
|
|
|
ServerName: spec.ServerName(spec.SenderIDFromPseudoIDKey(pseudoIDKey)),
|
2023-06-28 13:29:49 -05:00
|
|
|
KeyID: "ed25519:1",
|
|
|
|
PrivateKey: pseudoIDKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 09:23:46 -05:00
|
|
|
senderIDString := string(senderID)
|
|
|
|
|
|
|
|
// Prepare the template for the join event.
|
|
|
|
proto := gomatrixserverlib.ProtoEvent{
|
|
|
|
Type: spec.MRoomMember,
|
|
|
|
SenderID: senderIDString,
|
|
|
|
StateKey: &senderIDString,
|
|
|
|
RoomID: req.RoomIDOrAlias,
|
|
|
|
Redacts: "",
|
|
|
|
}
|
|
|
|
if err = proto.SetUnsigned(struct{}{}); err != nil {
|
|
|
|
return "", "", fmt.Errorf("eb.SetUnsigned: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{}{}
|
|
|
|
}
|
|
|
|
req.Content["membership"] = spec.Join
|
|
|
|
if authorisedVia, aerr := r.populateAuthorisedViaUserForRestrictedJoin(ctx, req, senderID); aerr != nil {
|
|
|
|
return "", "", aerr
|
|
|
|
} else if authorisedVia != "" {
|
|
|
|
req.Content["join_authorised_via_users_server"] = authorisedVia
|
|
|
|
}
|
|
|
|
if err = proto.SetContent(req.Content); err != nil {
|
|
|
|
return "", "", fmt.Errorf("eb.SetContent: %w", err)
|
|
|
|
}
|
2023-06-28 13:29:49 -05:00
|
|
|
event, err := eventutil.QueryAndBuildEvent(ctx, &proto, &identity, time.Now(), r.RSAPI, &buildRes)
|
2020-05-04 07:53:47 -05:00
|
|
|
|
2023-05-16 19:33:27 -05:00
|
|
|
switch err.(type) {
|
2020-05-04 07:53:47 -05:00
|
|
|
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
|
2022-07-22 08:44:04 -05:00
|
|
|
// a member of the room. This is best-effort (as in we won't
|
|
|
|
// fail if we can't find the existing membership) because there
|
|
|
|
// is really no harm in just sending another membership event.
|
|
|
|
membershipRes := &api.QueryMembershipForUserResponse{}
|
2023-06-12 06:19:25 -05:00
|
|
|
_ = r.Queryer.QueryMembershipForSenderID(ctx, *roomID, senderID, membershipRes)
|
2020-05-04 07:53:47 -05:00
|
|
|
|
|
|
|
// If we haven't already joined the room then send an event
|
|
|
|
// into the room changing our membership status.
|
2022-07-22 08:44:04 -05:00
|
|
|
if !membershipRes.RoomExists || !membershipRes.IsInRoom {
|
2021-09-08 11:31:03 -05:00
|
|
|
inputReq := rsAPI.InputRoomEventsRequest{
|
|
|
|
InputRoomEvents: []rsAPI.InputRoomEvent{
|
2020-05-04 12:34:09 -05:00
|
|
|
{
|
2021-09-08 11:31:03 -05:00
|
|
|
Kind: rsAPI.KindNew,
|
2023-04-27 06:54:20 -05:00
|
|
|
Event: event,
|
2022-10-26 06:59:19 -05:00
|
|
|
SendAsServer: string(userDomain),
|
2020-05-04 07:53:47 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-09-08 11:31:03 -05:00
|
|
|
inputRes := rsAPI.InputRoomEventsResponse{}
|
2023-05-09 17:46:49 -05:00
|
|
|
r.Inputer.InputRoomEvents(ctx, &inputReq, &inputRes)
|
2020-09-16 07:00:52 -05:00
|
|
|
if err = inputRes.Err(); err != nil {
|
2023-04-28 10:46:01 -05:00
|
|
|
return "", "", rsAPI.ErrNotAllowed{Err: err}
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 08:55:57 -05:00
|
|
|
case eventutil.ErrRoomNoExists:
|
2020-07-03 04:25:26 -05: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.
|
2023-06-12 06:19:25 -05:00
|
|
|
if r.Cfg.Matrix.IsLocalServerName(roomID.Domain()) {
|
2020-07-03 04:25:26 -05: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 {
|
2023-05-16 19:33:27 -05:00
|
|
|
return "", "", eventutil.ErrRoomNoExists{}
|
2020-06-24 09:06:14 -05:00
|
|
|
}
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
|
|
|
|
2020-05-14 08:58:47 -05:00
|
|
|
// Perform a federated room join.
|
2020-11-19 05:34:59 -06:00
|
|
|
joinedVia, err = r.performFederatedJoinRoomByID(ctx, req)
|
|
|
|
return req.RoomIDOrAlias, joinedVia, err
|
2020-05-04 07:53:47 -05:00
|
|
|
|
|
|
|
default:
|
2020-05-14 08:58:47 -05:00
|
|
|
// Something else went wrong.
|
2021-09-08 11:31:03 -05:00
|
|
|
return "", "", fmt.Errorf("error joining local room: %q", err)
|
2020-05-14 08:58:47 -05:00
|
|
|
}
|
|
|
|
|
2020-06-24 09:06:14 -05: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.
|
2022-10-26 06:59:19 -05:00
|
|
|
return req.RoomIDOrAlias, userDomain, nil
|
2020-05-14 08:58:47 -05:00
|
|
|
}
|
|
|
|
|
2023-10-12 18:41:45 -05:00
|
|
|
func (r *Joiner) PerformSendJoinCryptoIDs(
|
|
|
|
ctx context.Context,
|
|
|
|
req *rsAPI.PerformJoinRequestCryptoIDs,
|
|
|
|
) error {
|
|
|
|
logger := logrus.WithContext(ctx).WithFields(logrus.Fields{
|
|
|
|
"room_id": req.RoomID,
|
|
|
|
"user_id": req.UserID,
|
|
|
|
"servers": req.ServerNames,
|
|
|
|
})
|
|
|
|
logger.Info("performing send join")
|
|
|
|
res := fsAPI.PerformJoinResponse{}
|
|
|
|
r.FSAPI.PerformSendJoin(ctx, &fsAPI.PerformSendJoinRequestCryptoIDs{
|
|
|
|
RoomID: req.RoomID,
|
|
|
|
UserID: req.UserID,
|
|
|
|
ServerNames: req.ServerNames,
|
|
|
|
Unsigned: req.Unsigned,
|
|
|
|
Event: req.JoinEvent,
|
|
|
|
}, &res)
|
|
|
|
if res.LastError != nil {
|
|
|
|
return res.LastError
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PerformJoin handles joining matrix rooms, including over federation by talking to the federationapi.
|
|
|
|
func (r *Joiner) PerformJoinCryptoIDs(
|
|
|
|
ctx context.Context,
|
|
|
|
req *rsAPI.PerformJoinRequest,
|
|
|
|
) (joinEvent gomatrixserverlib.PDU, roomID string, version gomatrixserverlib.RoomVersion, serverName spec.ServerName, err error) {
|
|
|
|
logger := logrus.WithContext(ctx).WithFields(logrus.Fields{
|
|
|
|
"room_id": req.RoomIDOrAlias,
|
|
|
|
"user_id": req.UserID,
|
|
|
|
"servers": req.ServerNames,
|
|
|
|
})
|
|
|
|
logger.Info("User requested to room join")
|
|
|
|
join, roomID, version, serverName, err := r.makeJoinEvent(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("Failed to make join room event")
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
return nil, "", "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return join, roomID, version, serverName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Joiner) makeJoinEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
req *rsAPI.PerformJoinRequest,
|
|
|
|
) (gomatrixserverlib.PDU, string, gomatrixserverlib.RoomVersion, spec.ServerName, error) {
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("supplied user ID %q in incorrect format", req.UserID)}
|
|
|
|
}
|
|
|
|
if !r.Cfg.Matrix.IsLocalServerName(domain) {
|
|
|
|
return nil, "", "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("user %q does not belong to this homeserver", req.UserID)}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.RoomIDOrAlias, "!") {
|
|
|
|
return r.performJoinRoomByIDCryptoIDs(ctx, req)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.RoomIDOrAlias, "#") {
|
|
|
|
return r.performJoinRoomByAliasCryptoIDs(ctx, req)
|
|
|
|
}
|
|
|
|
return nil, "", "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("room ID or alias %q is invalid", req.RoomIDOrAlias)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Joiner) performJoinRoomByAliasCryptoIDs(
|
|
|
|
ctx context.Context,
|
|
|
|
req *rsAPI.PerformJoinRequest,
|
|
|
|
) (gomatrixserverlib.PDU, string, gomatrixserverlib.RoomVersion, spec.ServerName, error) {
|
|
|
|
// Get the domain part of the room alias.
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', req.RoomIDOrAlias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", "", "", fmt.Errorf("alias %q is not in the correct format", req.RoomIDOrAlias)
|
|
|
|
}
|
|
|
|
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 !r.Cfg.Matrix.IsLocalServerName(domain) {
|
|
|
|
// 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{}
|
|
|
|
err = r.FSAPI.PerformDirectoryLookup(ctx, &dirReq, &dirRes)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("error looking up alias %q", req.RoomIDOrAlias)
|
|
|
|
return nil, "", "", "", fmt.Errorf("looking up alias %q over federation failed: %w", req.RoomIDOrAlias, err)
|
|
|
|
}
|
|
|
|
roomID = dirRes.RoomID
|
|
|
|
req.ServerNames = append(req.ServerNames, dirRes.ServerNames...)
|
|
|
|
} else {
|
|
|
|
var getRoomReq = rsAPI.GetRoomIDForAliasRequest{
|
|
|
|
Alias: req.RoomIDOrAlias,
|
|
|
|
IncludeAppservices: true,
|
|
|
|
}
|
|
|
|
var getRoomRes = rsAPI.GetRoomIDForAliasResponse{}
|
|
|
|
// Otherwise, look up if we know this room alias locally.
|
|
|
|
err = r.RSAPI.GetRoomIDForAlias(ctx, &getRoomReq, &getRoomRes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", "", "", fmt.Errorf("lookup room alias %q failed: %w", req.RoomIDOrAlias, err)
|
|
|
|
}
|
|
|
|
roomID = getRoomRes.RoomID
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the room ID is empty then we failed to look up the alias.
|
|
|
|
if roomID == "" {
|
|
|
|
return nil, "", "", "", fmt.Errorf("alias %q not found", req.RoomIDOrAlias)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we do, then pluck out the room ID and continue the join.
|
|
|
|
req.RoomIDOrAlias = roomID
|
|
|
|
return r.performJoinRoomByIDCryptoIDs(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Break this function up a bit & move to GMSL
|
|
|
|
// nolint:gocyclo
|
|
|
|
func (r *Joiner) performJoinRoomByIDCryptoIDs(
|
|
|
|
ctx context.Context,
|
|
|
|
req *rsAPI.PerformJoinRequest,
|
|
|
|
) (gomatrixserverlib.PDU, string, gomatrixserverlib.RoomVersion, spec.ServerName, error) {
|
|
|
|
// The original client request ?server_name=... may include this HS so filter that out so we
|
|
|
|
// don't attempt to make_join with ourselves
|
|
|
|
for i := 0; i < len(req.ServerNames); i++ {
|
|
|
|
if r.Cfg.Matrix.IsLocalServerName(req.ServerNames[i]) {
|
|
|
|
// delete this entry
|
|
|
|
req.ServerNames = append(req.ServerNames[:i], req.ServerNames[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the domain part of the room ID.
|
|
|
|
roomID, err := spec.NewRoomID(req.RoomIDOrAlias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("room ID %q is invalid: %w", req.RoomIDOrAlias, err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 !r.Cfg.Matrix.IsLocalServerName(roomID.Domain()) {
|
|
|
|
req.ServerNames = append(req.ServerNames, roomID.Domain())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force a federated join if we aren't in the room and we've been
|
|
|
|
// given some server names to try joining by.
|
|
|
|
inRoomReq := &rsAPI.QueryServerJoinedToRoomRequest{
|
|
|
|
RoomID: req.RoomIDOrAlias,
|
|
|
|
}
|
|
|
|
inRoomRes := &rsAPI.QueryServerJoinedToRoomResponse{}
|
|
|
|
if err = r.Queryer.QueryServerJoinedToRoom(ctx, inRoomReq, inRoomRes); err != nil {
|
|
|
|
return nil, "", "", "", fmt.Errorf("r.Queryer.QueryServerJoinedToRoom: %w", err)
|
|
|
|
}
|
|
|
|
serverInRoom := inRoomRes.IsInRoom
|
|
|
|
forceFederatedJoin := len(req.ServerNames) > 0 && !serverInRoom
|
|
|
|
|
|
|
|
userID, err := spec.NewUserID(req.UserID, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", "", "", rsAPI.ErrInvalidID{Err: fmt.Errorf("user ID %q is invalid: %w", req.UserID, err)}
|
|
|
|
}
|
|
|
|
|
2023-11-27 14:10:07 -06:00
|
|
|
//TODO: CryptoIDs - what is provided & calculated senderIDs don't match?
|
|
|
|
|
2023-10-12 18:41:45 -05:00
|
|
|
// Look up the room NID for the supplied room ID.
|
|
|
|
var senderID spec.SenderID
|
|
|
|
checkInvitePending := false
|
|
|
|
info, err := r.DB.RoomInfo(ctx, req.RoomIDOrAlias)
|
|
|
|
if err == nil && info != nil {
|
|
|
|
switch info.RoomVersion {
|
2023-11-17 18:34:01 -06:00
|
|
|
case gomatrixserverlib.RoomVersionCryptoIDs:
|
2023-10-12 18:41:45 -05:00
|
|
|
senderIDPtr, queryErr := r.Queryer.QuerySenderIDForUser(ctx, *roomID, *userID)
|
|
|
|
if queryErr == nil {
|
|
|
|
checkInvitePending = true
|
|
|
|
}
|
|
|
|
if senderIDPtr == nil {
|
2023-11-27 14:10:07 -06:00
|
|
|
senderID = req.SenderID
|
2023-10-12 18:41:45 -05:00
|
|
|
} else {
|
|
|
|
senderID = *senderIDPtr
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
checkInvitePending = true
|
|
|
|
senderID = spec.SenderID(userID.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force a federated join if we're dealing with a pending invite
|
|
|
|
// and we aren't in the room.
|
|
|
|
if checkInvitePending {
|
|
|
|
isInvitePending, inviteSender, _, inviteEvent, inviteErr := helpers.IsInvitePending(ctx, r.DB, req.RoomIDOrAlias, senderID)
|
|
|
|
if inviteErr == nil && !serverInRoom && isInvitePending {
|
|
|
|
inviter, queryErr := r.RSAPI.QueryUserIDForSender(ctx, *roomID, inviteSender)
|
|
|
|
if queryErr != nil {
|
|
|
|
return nil, "", "", "", fmt.Errorf("r.RSAPI.QueryUserIDForSender: %w", queryErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were invited by someone from another server then we can
|
|
|
|
// assume they are in the room so we can join via them.
|
|
|
|
if inviter != nil && !r.Cfg.Matrix.IsLocalServerName(inviter.Domain()) {
|
|
|
|
req.ServerNames = append(req.ServerNames, inviter.Domain())
|
|
|
|
forceFederatedJoin = true
|
|
|
|
memberEvent := gjson.Parse(string(inviteEvent.JSON()))
|
|
|
|
// only set unsigned if we've got a content.membership, which we _should_
|
|
|
|
if memberEvent.Get("content.membership").Exists() {
|
|
|
|
req.Unsigned = map[string]interface{}{
|
|
|
|
"prev_sender": memberEvent.Get("sender").Str,
|
|
|
|
"prev_content": map[string]interface{}{
|
|
|
|
"is_direct": memberEvent.Get("content.is_direct").Bool(),
|
|
|
|
"membership": memberEvent.Get("content.membership").Str,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a guest is trying to join a room, check that the room has a m.room.guest_access event
|
|
|
|
if req.IsGuest {
|
|
|
|
var guestAccessEvent *types.HeaderedEvent
|
|
|
|
guestAccess := "forbidden"
|
|
|
|
guestAccessEvent, err = r.DB.GetStateEvent(ctx, req.RoomIDOrAlias, spec.MRoomGuestAccess, "")
|
|
|
|
if (err != nil && !errors.Is(err, sql.ErrNoRows)) || guestAccessEvent == nil {
|
|
|
|
logrus.WithError(err).Warn("unable to get m.room.guest_access event, defaulting to 'forbidden'")
|
|
|
|
}
|
|
|
|
if guestAccessEvent != nil {
|
|
|
|
guestAccess = gjson.GetBytes(guestAccessEvent.Content(), "guest_access").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Servers MUST only allow guest users to join rooms if the m.room.guest_access state event
|
|
|
|
// is present on the room and has the guest_access value can_join.
|
|
|
|
if guestAccess != "can_join" {
|
|
|
|
return nil, "", "", "", rsAPI.ErrNotAllowed{Err: fmt.Errorf("guest access is forbidden")}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we should do a forced federated join then do that.
|
|
|
|
if forceFederatedJoin {
|
|
|
|
joinEvent, version, serverName, err := r.performFederatedMakeJoinByIDCryptoIDs(ctx, req)
|
|
|
|
return joinEvent, req.RoomIDOrAlias, version, serverName, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
var buildRes rsAPI.QueryLatestEventsAndStateResponse
|
|
|
|
identity := r.Cfg.Matrix.SigningIdentity
|
|
|
|
|
|
|
|
// at this point we know we have an existing room
|
2023-11-17 18:34:01 -06:00
|
|
|
if inRoomRes.RoomVersion == gomatrixserverlib.RoomVersionCryptoIDs {
|
2023-10-12 18:41:45 -05:00
|
|
|
mapping := &gomatrixserverlib.MXIDMapping{
|
2023-11-06 17:54:29 -06:00
|
|
|
UserRoomKey: senderID,
|
2023-10-12 18:41:45 -05:00
|
|
|
UserID: userID.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the mapping with the server identity
|
|
|
|
if err = mapping.Sign(identity.ServerName, identity.KeyID, identity.PrivateKey); err != nil {
|
|
|
|
return nil, "", "", "", err
|
|
|
|
}
|
|
|
|
req.Content["mxid_mapping"] = mapping
|
|
|
|
|
|
|
|
// sign the event with the pseudo ID key
|
|
|
|
identity = fclient.SigningIdentity{
|
2023-11-06 17:54:29 -06:00
|
|
|
ServerName: spec.ServerName(senderID),
|
2023-10-12 18:41:45 -05:00
|
|
|
KeyID: "ed25519:1",
|
2023-11-06 17:54:29 -06:00
|
|
|
PrivateKey: nil,
|
2023-10-12 18:41:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
senderIDString := string(senderID)
|
|
|
|
|
|
|
|
// Prepare the template for the join event.
|
|
|
|
proto := gomatrixserverlib.ProtoEvent{
|
|
|
|
Type: spec.MRoomMember,
|
|
|
|
SenderID: senderIDString,
|
|
|
|
StateKey: &senderIDString,
|
|
|
|
RoomID: req.RoomIDOrAlias,
|
|
|
|
Redacts: "",
|
|
|
|
}
|
|
|
|
if err = proto.SetUnsigned(struct{}{}); err != nil {
|
|
|
|
return nil, "", "", "", fmt.Errorf("eb.SetUnsigned: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{}{}
|
|
|
|
}
|
|
|
|
req.Content["membership"] = spec.Join
|
|
|
|
if authorisedVia, aerr := r.populateAuthorisedViaUserForRestrictedJoin(ctx, req, senderID); aerr != nil {
|
|
|
|
return nil, "", "", "", aerr
|
|
|
|
} else if authorisedVia != "" {
|
|
|
|
req.Content["join_authorised_via_users_server"] = authorisedVia
|
|
|
|
}
|
|
|
|
if err = proto.SetContent(req.Content); err != nil {
|
|
|
|
return nil, "", "", "", fmt.Errorf("eb.SetContent: %w", err)
|
|
|
|
}
|
|
|
|
joinEvent, err := eventutil.QueryAndBuildEvent(ctx, &proto, &identity, time.Now(), r.RSAPI, &buildRes)
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
|
|
|
// Do nothing
|
|
|
|
|
|
|
|
case eventutil.ErrRoomNoExists:
|
|
|
|
// 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.
|
|
|
|
if r.Cfg.Matrix.IsLocalServerName(roomID.Domain()) {
|
|
|
|
// 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 {
|
|
|
|
return nil, "", "", "", eventutil.ErrRoomNoExists{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a federated room join.
|
|
|
|
joinEvent, version, serverName, err := r.performFederatedMakeJoinByIDCryptoIDs(ctx, req)
|
|
|
|
return joinEvent, req.RoomIDOrAlias, version, serverName, err
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Something else went wrong.
|
|
|
|
return nil, "", "", "", fmt.Errorf("error joining local room: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
return joinEvent, req.RoomIDOrAlias, inRoomRes.RoomVersion, userID.Domain(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Joiner) performFederatedMakeJoinByIDCryptoIDs(
|
|
|
|
ctx context.Context,
|
|
|
|
req *rsAPI.PerformJoinRequest,
|
|
|
|
) (gomatrixserverlib.PDU, gomatrixserverlib.RoomVersion, spec.ServerName, error) {
|
|
|
|
// 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
|
|
|
|
Unsigned: req.Unsigned, // the unsigned event content, if any
|
|
|
|
}
|
|
|
|
joinEvent, version, serverName, err := r.FSAPI.PerformMakeJoin(ctx, &fedReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", "", err
|
|
|
|
}
|
|
|
|
return joinEvent, version, serverName, nil
|
|
|
|
}
|
|
|
|
|
2020-09-02 07:47:31 -05:00
|
|
|
func (r *Joiner) performFederatedJoinRoomByID(
|
2020-05-14 08:58:47 -05:00
|
|
|
ctx context.Context,
|
2021-09-08 11:31:03 -05:00
|
|
|
req *rsAPI.PerformJoinRequest,
|
2023-04-19 09:50:33 -05:00
|
|
|
) (spec.ServerName, error) {
|
2020-05-14 08:58:47 -05: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
|
2022-10-07 09:00:12 -05:00
|
|
|
Unsigned: req.Unsigned, // the unsigned event content, if any
|
2020-05-14 08:58:47 -05:00
|
|
|
}
|
|
|
|
fedRes := fsAPI.PerformJoinResponse{}
|
2020-09-02 07:47:31 -05:00
|
|
|
r.FSAPI.PerformJoin(ctx, &fedReq, &fedRes)
|
2020-06-25 09:04:48 -05:00
|
|
|
if fedRes.LastError != nil {
|
2023-04-28 10:46:01 -05:00
|
|
|
return "", fedRes.LastError
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
2020-11-19 05:34:59 -06:00
|
|
|
return fedRes.JoinedVia, nil
|
2020-05-04 07:53:47 -05:00
|
|
|
}
|
2020-09-02 11:13:15 -05:00
|
|
|
|
2022-05-25 04:05:30 -05:00
|
|
|
func (r *Joiner) populateAuthorisedViaUserForRestrictedJoin(
|
|
|
|
ctx context.Context,
|
|
|
|
joinReq *rsAPI.PerformJoinRequest,
|
2023-06-12 06:19:25 -05:00
|
|
|
senderID spec.SenderID,
|
2022-05-25 04:05:30 -05:00
|
|
|
) (string, error) {
|
2023-06-06 08:16:55 -05:00
|
|
|
roomID, err := spec.NewRoomID(joinReq.RoomIDOrAlias)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-05-25 04:05:30 -05:00
|
|
|
}
|
2023-06-06 08:16:55 -05:00
|
|
|
|
2023-06-12 06:19:25 -05:00
|
|
|
return r.Queryer.QueryRestrictedJoinAllowed(ctx, *roomID, senderID)
|
2022-05-25 04:05:30 -05:00
|
|
|
}
|