2022-04-29 09:10:08 +01:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2023-01-19 20:02:32 +00:00
|
|
|
"context"
|
2022-08-12 12:00:07 +01:00
|
|
|
"encoding/json"
|
2022-09-30 09:32:31 +01:00
|
|
|
"fmt"
|
2022-04-29 09:10:08 +01:00
|
|
|
"net/http"
|
2022-09-27 17:06:49 +01:00
|
|
|
"time"
|
2022-04-29 09:10:08 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-12-22 10:54:03 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2022-09-30 09:32:31 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-04-29 09:10:08 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2022-09-30 09:32:31 +01:00
|
|
|
"github.com/matrix-org/dendrite/keyserver/api"
|
2022-04-29 09:10:08 +01:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2022-08-12 12:00:07 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-09-27 17:06:49 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2022-04-29 09:10:08 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
)
|
|
|
|
|
2022-08-12 12:00:07 +01:00
|
|
|
func AdminEvacuateRoom(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
2022-04-29 09:10:08 +01:00
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
roomID, ok := vars["roomID"]
|
|
|
|
if !ok {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting room ID."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res := &roomserverAPI.PerformAdminEvacuateRoomResponse{}
|
2022-08-11 15:29:33 +01:00
|
|
|
if err := rsAPI.PerformAdminEvacuateRoom(
|
2022-04-29 09:10:08 +01:00
|
|
|
req.Context(),
|
|
|
|
&roomserverAPI.PerformAdminEvacuateRoomRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
},
|
|
|
|
res,
|
2022-08-11 15:29:33 +01:00
|
|
|
); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2022-04-29 09:10:08 +01:00
|
|
|
if err := res.Error; err != nil {
|
|
|
|
return err.JSONResponse()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: map[string]interface{}{
|
|
|
|
"affected": res.Affected,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 15:29:39 +01:00
|
|
|
|
2022-08-12 12:00:07 +01:00
|
|
|
func AdminEvacuateUser(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
2022-06-29 15:29:39 +01:00
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
userID, ok := vars["userID"]
|
|
|
|
if !ok {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting user ID."),
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 12:00:07 +01:00
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if err != nil {
|
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2022-10-26 12:59:19 +01:00
|
|
|
if !cfg.Matrix.IsLocalServerName(domain) {
|
2022-08-12 12:00:07 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("User ID must belong to this server."),
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 15:29:39 +01:00
|
|
|
res := &roomserverAPI.PerformAdminEvacuateUserResponse{}
|
2022-08-11 15:29:33 +01:00
|
|
|
if err := rsAPI.PerformAdminEvacuateUser(
|
2022-06-29 15:29:39 +01:00
|
|
|
req.Context(),
|
|
|
|
&roomserverAPI.PerformAdminEvacuateUserRequest{
|
|
|
|
UserID: userID,
|
|
|
|
},
|
|
|
|
res,
|
2022-08-11 15:29:33 +01:00
|
|
|
); err != nil {
|
|
|
|
return jsonerror.InternalAPIError(req.Context(), err)
|
|
|
|
}
|
2022-06-29 15:29:39 +01:00
|
|
|
if err := res.Error; err != nil {
|
|
|
|
return err.JSONResponse()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: map[string]interface{}{
|
|
|
|
"affected": res.Affected,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 12:00:07 +01:00
|
|
|
|
2023-01-19 20:02:32 +00:00
|
|
|
func AdminPurgeRoom(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
roomID, ok := vars["roomID"]
|
|
|
|
if !ok {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting room ID."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res := &roomserverAPI.PerformAdminPurgeRoomResponse{}
|
|
|
|
if err := rsAPI.PerformAdminPurgeRoom(
|
|
|
|
context.Background(),
|
|
|
|
&roomserverAPI.PerformAdminPurgeRoomRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
},
|
|
|
|
res,
|
|
|
|
); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := res.Error; err != nil {
|
|
|
|
return err.JSONResponse()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: res,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 12:00:07 +01:00
|
|
|
func AdminResetPassword(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, userAPI userapi.ClientUserAPI) util.JSONResponse {
|
2022-12-22 10:54:03 +00:00
|
|
|
if req.Body == nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown("Missing request body"),
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 12:00:07 +01:00
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2022-12-22 10:54:03 +00:00
|
|
|
var localpart string
|
|
|
|
userID := vars["userID"]
|
|
|
|
localpart, serverName, err := cfg.Matrix.SplitLocalID('@', userID)
|
|
|
|
if err != nil {
|
2022-08-12 12:00:07 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2022-12-22 10:54:03 +00:00
|
|
|
JSON: jsonerror.InvalidArgumentValue(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accAvailableResp := &userapi.QueryAccountAvailabilityResponse{}
|
|
|
|
if err = userAPI.QueryAccountAvailability(req.Context(), &userapi.QueryAccountAvailabilityRequest{
|
|
|
|
Localpart: localpart,
|
|
|
|
ServerName: serverName,
|
|
|
|
}, accAvailableResp); err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: jsonerror.InternalAPIError(req.Context(), err),
|
2022-08-12 12:00:07 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-22 10:54:03 +00:00
|
|
|
if accAvailableResp.Available {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: jsonerror.Unknown("User does not exist"),
|
|
|
|
}
|
2022-11-11 16:41:37 +00:00
|
|
|
}
|
2022-08-12 12:00:07 +01:00
|
|
|
request := struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}{}
|
2022-12-23 13:11:11 +00:00
|
|
|
if err = json.NewDecoder(req.Body).Decode(&request); err != nil {
|
2022-08-12 12:00:07 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown("Failed to decode request body: " + err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if request.Password == "" {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting non-empty password."),
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 10:54:03 +00:00
|
|
|
|
2022-12-23 13:11:11 +00:00
|
|
|
if err = internal.ValidatePassword(request.Password); err != nil {
|
|
|
|
return *internal.PasswordResponse(err)
|
2022-12-22 10:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 12:00:07 +01:00
|
|
|
updateReq := &userapi.PerformPasswordUpdateRequest{
|
|
|
|
Localpart: localpart,
|
2022-11-11 16:41:37 +00:00
|
|
|
ServerName: serverName,
|
2022-08-12 12:00:07 +01:00
|
|
|
Password: request.Password,
|
|
|
|
LogoutDevices: true,
|
|
|
|
}
|
|
|
|
updateRes := &userapi.PerformPasswordUpdateResponse{}
|
|
|
|
if err := userAPI.PerformPasswordUpdate(req.Context(), updateReq, updateRes); err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown("Failed to perform password update: " + err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct {
|
|
|
|
Updated bool `json:"password_updated"`
|
|
|
|
}{
|
|
|
|
Updated: updateRes.PasswordUpdated,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 17:06:49 +01:00
|
|
|
|
|
|
|
func AdminReindex(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, natsClient *nats.Conn) util.JSONResponse {
|
|
|
|
_, err := natsClient.RequestMsg(nats.NewMsg(cfg.Matrix.JetStream.Prefixed(jetstream.InputFulltextReindex)), time.Second*10)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("failed to publish nats message")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 09:32:31 +01:00
|
|
|
|
|
|
|
func AdminMarkAsStale(req *http.Request, cfg *config.ClientAPI, keyAPI api.ClientKeyAPI) util.JSONResponse {
|
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
userID := vars["userID"]
|
|
|
|
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if err != nil {
|
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2022-10-26 12:59:19 +01:00
|
|
|
if cfg.Matrix.IsLocalServerName(domain) {
|
2022-09-30 09:32:31 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidParam("Can not mark local device list as stale"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = keyAPI.PerformMarkAsStaleIfNeeded(req.Context(), &api.PerformMarkAsStaleRequest{
|
|
|
|
UserID: userID,
|
|
|
|
Domain: domain,
|
|
|
|
}, &struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to mark device list as stale: %s", err)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
2022-10-31 09:13:28 +00:00
|
|
|
|
|
|
|
func AdminDownloadState(req *http.Request, cfg *config.ClientAPI, device *userapi.Device, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
|
|
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
roomID, ok := vars["roomID"]
|
|
|
|
if !ok {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting room ID."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serverName, ok := vars["serverName"]
|
|
|
|
if !ok {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingArgument("Expecting remote server name."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res := &roomserverAPI.PerformAdminDownloadStateResponse{}
|
|
|
|
if err := rsAPI.PerformAdminDownloadState(
|
|
|
|
req.Context(),
|
|
|
|
&roomserverAPI.PerformAdminDownloadStateRequest{
|
|
|
|
UserID: device.UserID,
|
|
|
|
RoomID: roomID,
|
|
|
|
ServerName: gomatrixserverlib.ServerName(serverName),
|
|
|
|
},
|
|
|
|
res,
|
|
|
|
); err != nil {
|
|
|
|
return jsonerror.InternalAPIError(req.Context(), err)
|
|
|
|
}
|
|
|
|
if err := res.Error; err != nil {
|
|
|
|
return err.JSONResponse()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
}
|