2022-04-28 16:02:30 +01:00
|
|
|
// Copyright 2022 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.
|
|
|
|
|
|
|
|
package perform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-29 15:29:39 +01:00
|
|
|
"database/sql"
|
2022-04-28 16:02:30 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/query"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2023-04-27 12:54:20 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2022-04-28 16:02:30 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-28 16:46:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-01-19 20:02:32 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-04-28 16:02:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Admin struct {
|
|
|
|
DB storage.Database
|
|
|
|
Cfg *config.RoomServer
|
|
|
|
Queryer *query.Queryer
|
|
|
|
Inputer *input.Inputer
|
2022-06-29 15:29:39 +01:00
|
|
|
Leaver *Leaver
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
// PerformAdminEvacuateRoom will remove all local users from the given room.
|
2022-04-28 16:02:30 +01:00
|
|
|
func (r *Admin) PerformAdminEvacuateRoom(
|
|
|
|
ctx context.Context,
|
2023-04-28 16:46:01 +01:00
|
|
|
roomID string,
|
|
|
|
) (affected []string, err error) {
|
|
|
|
roomInfo, err := r.DB.RoomInfo(ctx, roomID)
|
2022-04-28 16:02:30 +01:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
2022-08-01 15:29:19 +01:00
|
|
|
if roomInfo == nil || roomInfo.IsStub() {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, eventutil.ErrRoomNoExists
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
memberNIDs, err := r.DB.GetMembershipEventNIDsForRoom(ctx, roomInfo.RoomNID, true, true)
|
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 16:06:47 +00:00
|
|
|
memberEvents, err := r.DB.Events(ctx, roomInfo, memberNIDs)
|
2022-04-28 16:02:30 +01:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inputEvents := make([]api.InputRoomEvent, 0, len(memberEvents))
|
2023-04-28 16:46:01 +01:00
|
|
|
affected = make([]string, 0, len(memberEvents))
|
2022-04-28 16:02:30 +01:00
|
|
|
latestReq := &api.QueryLatestEventsAndStateRequest{
|
2023-04-28 16:46:01 +01:00
|
|
|
RoomID: roomID,
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
latestRes := &api.QueryLatestEventsAndStateResponse{}
|
|
|
|
if err = r.Queryer.QueryLatestEventsAndState(ctx, latestReq, latestRes); err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
prevEvents := latestRes.LatestEvents
|
2023-04-28 16:46:01 +01:00
|
|
|
var senderDomain spec.ServerName
|
|
|
|
var eventsNeeded gomatrixserverlib.StateNeeded
|
|
|
|
var identity *fclient.SigningIdentity
|
|
|
|
var event *types.HeaderedEvent
|
2022-04-28 16:02:30 +01:00
|
|
|
for _, memberEvent := range memberEvents {
|
|
|
|
if memberEvent.StateKey() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var memberContent gomatrixserverlib.MemberContent
|
|
|
|
if err = json.Unmarshal(memberEvent.Content(), &memberContent); err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
2023-04-19 15:50:33 +01:00
|
|
|
memberContent.Membership = spec.Leave
|
2022-04-28 16:02:30 +01:00
|
|
|
|
|
|
|
stateKey := *memberEvent.StateKey()
|
|
|
|
fledglingEvent := &gomatrixserverlib.EventBuilder{
|
2023-04-28 16:46:01 +01:00
|
|
|
RoomID: roomID,
|
2023-04-19 15:50:33 +01:00
|
|
|
Type: spec.MRoomMember,
|
2022-04-28 16:02:30 +01:00
|
|
|
StateKey: &stateKey,
|
|
|
|
Sender: stateKey,
|
|
|
|
PrevEvents: prevEvents,
|
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
_, senderDomain, err = gomatrixserverlib.SplitID('@', fledglingEvent.Sender)
|
2022-10-26 12:59:19 +01:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:02:30 +01:00
|
|
|
if fledglingEvent.Content, err = json.Marshal(memberContent); err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
eventsNeeded, err = gomatrixserverlib.StateNeededForEventBuilder(fledglingEvent)
|
2022-04-28 16:02:30 +01:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
identity, err = r.Cfg.Matrix.SigningIdentityFor(senderDomain)
|
2022-11-15 15:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
event, err = eventutil.BuildEvent(ctx, fledglingEvent, r.Cfg.Matrix, identity, time.Now(), &eventsNeeded, latestRes)
|
2022-04-28 16:02:30 +01:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inputEvents = append(inputEvents, api.InputRoomEvent{
|
|
|
|
Kind: api.KindNew,
|
|
|
|
Event: event,
|
2022-10-26 12:59:19 +01:00
|
|
|
Origin: senderDomain,
|
|
|
|
SendAsServer: string(senderDomain),
|
2022-04-28 16:02:30 +01:00
|
|
|
})
|
2023-04-28 16:46:01 +01:00
|
|
|
affected = append(affected, stateKey)
|
2022-04-28 16:02:30 +01:00
|
|
|
prevEvents = []gomatrixserverlib.EventReference{
|
|
|
|
event.EventReference(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inputReq := &api.InputRoomEventsRequest{
|
|
|
|
InputRoomEvents: inputEvents,
|
|
|
|
Asynchronous: true,
|
|
|
|
}
|
|
|
|
inputRes := &api.InputRoomEventsResponse{}
|
2023-04-28 16:46:01 +01:00
|
|
|
err = r.Inputer.InputRoomEvents(ctx, inputReq, inputRes)
|
|
|
|
return affected, err
|
2022-04-28 16:02:30 +01:00
|
|
|
}
|
2022-06-29 15:29:39 +01:00
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
// PerformAdminEvacuateUser will remove the given user from all rooms.
|
2022-06-29 15:29:39 +01:00
|
|
|
func (r *Admin) PerformAdminEvacuateUser(
|
|
|
|
ctx context.Context,
|
2023-04-28 16:46:01 +01:00
|
|
|
userID string,
|
|
|
|
) (affected []string, err error) {
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', userID)
|
2022-06-29 15:29:39 +01:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-06-29 15:29:39 +01:00
|
|
|
}
|
2022-10-26 12:59:19 +01:00
|
|
|
if !r.Cfg.Matrix.IsLocalServerName(domain) {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, fmt.Errorf("can only evacuate local users using this endpoint")
|
2022-06-29 15:29:39 +01:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
roomIDs, err := r.DB.GetRoomsByMembership(ctx, userID, spec.Join)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-06-29 15:29:39 +01:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
inviteRoomIDs, err := r.DB.GetRoomsByMembership(ctx, userID, spec.Invite)
|
2022-06-29 15:29:39 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-06-29 15:29:39 +01:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
allRooms := append(roomIDs, inviteRoomIDs...)
|
|
|
|
affected = make([]string, 0, len(allRooms))
|
|
|
|
for _, roomID := range allRooms {
|
2022-06-29 15:29:39 +01:00
|
|
|
leaveReq := &api.PerformLeaveRequest{
|
|
|
|
RoomID: roomID,
|
2023-04-28 16:46:01 +01:00
|
|
|
UserID: userID,
|
2022-06-29 15:29:39 +01:00
|
|
|
}
|
|
|
|
leaveRes := &api.PerformLeaveResponse{}
|
|
|
|
outputEvents, err := r.Leaver.PerformLeave(ctx, leaveReq, leaveRes)
|
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-06-29 15:29:39 +01:00
|
|
|
}
|
2023-04-28 16:46:01 +01:00
|
|
|
affected = append(affected, roomID)
|
2022-06-29 15:29:39 +01:00
|
|
|
if len(outputEvents) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-01 10:54:07 +01:00
|
|
|
if err := r.Inputer.OutputProducer.ProduceRoomEvents(roomID, outputEvents); err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return nil, err
|
2022-06-29 15:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-28 16:46:01 +01:00
|
|
|
return affected, nil
|
2022-06-29 15:29:39 +01:00
|
|
|
}
|
2022-10-31 09:13:28 +00:00
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
// PerformAdminPurgeRoom removes all traces for the given room from the database.
|
2023-01-19 20:02:32 +00:00
|
|
|
func (r *Admin) PerformAdminPurgeRoom(
|
|
|
|
ctx context.Context,
|
2023-04-28 16:46:01 +01:00
|
|
|
roomID string,
|
2023-01-19 20:02:32 +00:00
|
|
|
) error {
|
|
|
|
// Validate we actually got a room ID and nothing else
|
2023-04-28 16:46:01 +01:00
|
|
|
if _, _, err := gomatrixserverlib.SplitID('!', roomID); err != nil {
|
|
|
|
return err
|
2023-01-19 20:02:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
// Evacuate the room before purging it from the database
|
|
|
|
if _, err := r.PerformAdminEvacuateRoom(ctx, roomID); err != nil {
|
|
|
|
logrus.WithField("room_id", roomID).WithError(err).Warn("Failed to evacuate room before purging")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.WithField("room_id", roomID).Warn("Purging room from roomserver")
|
|
|
|
if err := r.DB.PurgeRoom(ctx, roomID); err != nil {
|
|
|
|
logrus.WithField("room_id", roomID).WithError(err).Warn("Failed to purge room from roomserver")
|
|
|
|
return err
|
2023-01-19 20:02:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
logrus.WithField("room_id", roomID).Warn("Room purged from roomserver")
|
2023-01-19 20:02:32 +00:00
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
return r.Inputer.OutputProducer.ProduceRoomEvents(roomID, []api.OutputEvent{
|
2023-01-19 20:02:32 +00:00
|
|
|
{
|
|
|
|
Type: api.OutputTypePurgeRoom,
|
|
|
|
PurgeRoom: &api.OutputPurgeRoom{
|
2023-04-28 16:46:01 +01:00
|
|
|
RoomID: roomID,
|
2023-01-19 20:02:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-31 09:13:28 +00:00
|
|
|
func (r *Admin) PerformAdminDownloadState(
|
|
|
|
ctx context.Context,
|
2023-04-28 16:46:01 +01:00
|
|
|
roomID, userID string, serverName spec.ServerName,
|
2022-10-31 09:13:28 +00:00
|
|
|
) error {
|
2023-04-28 16:46:01 +01:00
|
|
|
_, senderDomain, err := r.Cfg.Matrix.SplitLocalID('@', userID)
|
2022-11-15 15:05:23 +00:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return err
|
2022-11-15 15:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
roomInfo, err := r.DB.RoomInfo(ctx, roomID)
|
2022-10-31 09:13:28 +00:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return err
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if roomInfo == nil || roomInfo.IsStub() {
|
2023-04-28 16:46:01 +01:00
|
|
|
return eventutil.ErrRoomNoExists
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fwdExtremities, _, depth, err := r.DB.LatestEventIDs(ctx, roomInfo.RoomNID)
|
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return err
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
authEventMap := map[string]*gomatrixserverlib.Event{}
|
|
|
|
stateEventMap := map[string]*gomatrixserverlib.Event{}
|
|
|
|
|
|
|
|
for _, fwdExtremity := range fwdExtremities {
|
2023-04-06 09:55:01 +01:00
|
|
|
var state gomatrixserverlib.StateResponse
|
2023-04-28 16:46:01 +01:00
|
|
|
state, err = r.Inputer.FSAPI.LookupState(ctx, r.Inputer.ServerName, serverName, roomID, fwdExtremity.EventID, roomInfo.RoomVersion)
|
2022-10-31 09:13:28 +00:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return fmt.Errorf("r.Inputer.FSAPI.LookupState (%q): %s", fwdExtremity.EventID, err)
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
for _, authEvent := range state.GetAuthEvents().UntrustedEvents(roomInfo.RoomVersion) {
|
2023-04-27 16:35:19 +01:00
|
|
|
if err = gomatrixserverlib.VerifyEventSignatures(ctx, authEvent, r.Inputer.KeyRing); err != nil {
|
2022-10-31 09:13:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
authEventMap[authEvent.EventID()] = authEvent
|
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
for _, stateEvent := range state.GetStateEvents().UntrustedEvents(roomInfo.RoomVersion) {
|
2023-04-27 16:35:19 +01:00
|
|
|
if err = gomatrixserverlib.VerifyEventSignatures(ctx, stateEvent, r.Inputer.KeyRing); err != nil {
|
2022-10-31 09:13:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
stateEventMap[stateEvent.EventID()] = stateEvent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
authEvents := make([]*types.HeaderedEvent, 0, len(authEventMap))
|
|
|
|
stateEvents := make([]*types.HeaderedEvent, 0, len(stateEventMap))
|
2022-10-31 09:13:28 +00:00
|
|
|
stateIDs := make([]string, 0, len(stateEventMap))
|
|
|
|
|
|
|
|
for _, authEvent := range authEventMap {
|
2023-04-27 12:54:20 +01:00
|
|
|
authEvents = append(authEvents, &types.HeaderedEvent{Event: authEvent})
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
for _, stateEvent := range stateEventMap {
|
2023-04-27 12:54:20 +01:00
|
|
|
stateEvents = append(stateEvents, &types.HeaderedEvent{Event: stateEvent})
|
2022-10-31 09:13:28 +00:00
|
|
|
stateIDs = append(stateIDs, stateEvent.EventID())
|
|
|
|
}
|
|
|
|
|
|
|
|
builder := &gomatrixserverlib.EventBuilder{
|
|
|
|
Type: "org.matrix.dendrite.state_download",
|
2023-04-28 16:46:01 +01:00
|
|
|
Sender: userID,
|
|
|
|
RoomID: roomID,
|
2023-04-19 15:50:33 +01:00
|
|
|
Content: spec.RawJSON("{}"),
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
|
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return fmt.Errorf("gomatrixserverlib.StateNeededForEventBuilder: %w", err)
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
queryRes := &api.QueryLatestEventsAndStateResponse{
|
|
|
|
RoomExists: true,
|
|
|
|
RoomVersion: roomInfo.RoomVersion,
|
|
|
|
LatestEvents: fwdExtremities,
|
|
|
|
StateEvents: stateEvents,
|
|
|
|
Depth: depth,
|
|
|
|
}
|
|
|
|
|
2022-11-15 15:05:23 +00:00
|
|
|
identity, err := r.Cfg.Matrix.SigningIdentityFor(senderDomain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ev, err := eventutil.BuildEvent(ctx, builder, r.Cfg.Matrix, identity, time.Now(), &eventsNeeded, queryRes)
|
2022-10-31 09:13:28 +00:00
|
|
|
if err != nil {
|
2023-04-28 16:46:01 +01:00
|
|
|
return fmt.Errorf("eventutil.BuildEvent: %w", err)
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inputReq := &api.InputRoomEventsRequest{
|
|
|
|
Asynchronous: false,
|
|
|
|
}
|
|
|
|
inputRes := &api.InputRoomEventsResponse{}
|
|
|
|
|
|
|
|
for _, authEvent := range append(authEvents, stateEvents...) {
|
|
|
|
inputReq.InputRoomEvents = append(inputReq.InputRoomEvents, api.InputRoomEvent{
|
|
|
|
Kind: api.KindOutlier,
|
|
|
|
Event: authEvent,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
inputReq.InputRoomEvents = append(inputReq.InputRoomEvents, api.InputRoomEvent{
|
|
|
|
Kind: api.KindNew,
|
|
|
|
Event: ev,
|
|
|
|
Origin: r.Cfg.Matrix.ServerName,
|
|
|
|
HasState: true,
|
|
|
|
StateEventIDs: stateIDs,
|
|
|
|
SendAsServer: string(r.Cfg.Matrix.ServerName),
|
|
|
|
})
|
|
|
|
|
2023-04-28 16:46:01 +01:00
|
|
|
if err = r.Inputer.InputRoomEvents(ctx, inputReq, inputRes); err != nil {
|
|
|
|
return fmt.Errorf("r.Inputer.InputRoomEvents: %w", err)
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if inputRes.ErrMsg != "" {
|
2023-04-28 16:46:01 +01:00
|
|
|
return inputRes.Err()
|
2022-10-31 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|