2020-07-02 09:41:18 -05:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2017-06-27 06:37:25 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-10-11 12:16:53 -05:00
|
|
|
package routing
|
2017-06-27 06:37:25 -05:00
|
|
|
|
|
|
|
import (
|
2018-08-08 10:17:09 -05:00
|
|
|
"fmt"
|
2017-07-07 08:11:32 -05:00
|
|
|
"net/http"
|
|
|
|
|
2017-06-27 06:37:25 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2021-11-24 04:45:23 -06:00
|
|
|
federationAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2018-08-08 10:17:09 -05:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-07-02 09:41:18 -05:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2017-06-27 06:37:25 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2019-10-01 11:09:47 -05:00
|
|
|
type roomDirectoryResponse struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
Servers []string `json:"servers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *roomDirectoryResponse) fillServers(servers []gomatrixserverlib.ServerName) {
|
|
|
|
r.Servers = make([]string, len(servers))
|
|
|
|
for i, s := range servers {
|
|
|
|
r.Servers[i] = string(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 06:37:25 -05:00
|
|
|
// DirectoryRoom looks up a room alias
|
|
|
|
func DirectoryRoom(
|
|
|
|
req *http.Request,
|
|
|
|
roomAlias string,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
2020-08-10 08:18:04 -05:00
|
|
|
cfg *config.ClientAPI,
|
2022-05-05 07:17:38 -05:00
|
|
|
rsAPI roomserverAPI.ClientRoomserverAPI,
|
|
|
|
fedSenderAPI federationAPI.ClientFederationAPI,
|
2017-06-27 06:37:25 -05:00
|
|
|
) util.JSONResponse {
|
2017-07-07 08:11:32 -05:00
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
2017-06-27 06:37:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-06-27 06:37:25 -05:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:09:47 -05:00
|
|
|
var res roomDirectoryResponse
|
2017-07-28 05:31:43 -05:00
|
|
|
|
2019-10-01 11:09:47 -05:00
|
|
|
// Query the roomserver API to check if the alias exists locally.
|
2021-03-03 11:00:31 -06:00
|
|
|
queryReq := &roomserverAPI.GetRoomIDForAliasRequest{
|
|
|
|
Alias: roomAlias,
|
|
|
|
IncludeAppservices: true,
|
|
|
|
}
|
|
|
|
queryRes := &roomserverAPI.GetRoomIDForAliasResponse{}
|
|
|
|
if err = rsAPI.GetRoomIDForAlias(req.Context(), queryReq, queryRes); err != nil {
|
2020-03-02 10:20:44 -06:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("rsAPI.GetRoomIDForAlias failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2019-10-01 11:09:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
res.RoomID = queryRes.RoomID
|
|
|
|
|
|
|
|
if res.RoomID == "" {
|
|
|
|
// If we don't know it locally, do a federation query.
|
|
|
|
// But don't send the query to ourselves.
|
2022-10-26 06:59:19 -05:00
|
|
|
if !cfg.Matrix.IsLocalServerName(domain) {
|
2019-10-01 11:09:47 -05:00
|
|
|
fedRes, fedErr := federation.LookupRoomAlias(req.Context(), domain, roomAlias)
|
|
|
|
if fedErr != nil {
|
2018-08-08 10:17:09 -05:00
|
|
|
// TODO: Return 502 if the remote server errored.
|
|
|
|
// TODO: Return 504 if the remote server timed out.
|
2020-06-02 03:34:36 -05:00
|
|
|
util.GetLogger(req.Context()).WithError(fedErr).Error("federation.LookupRoomAlias failed")
|
2020-03-02 10:20:44 -06:00
|
|
|
return jsonerror.InternalServerError()
|
2018-08-08 10:17:09 -05:00
|
|
|
}
|
2019-10-01 11:09:47 -05:00
|
|
|
res.RoomID = fedRes.RoomID
|
|
|
|
res.fillServers(fedRes.Servers)
|
2018-08-08 10:17:09 -05:00
|
|
|
}
|
2019-10-01 11:09:47 -05:00
|
|
|
|
|
|
|
if res.RoomID == "" {
|
2018-08-08 10:17:09 -05:00
|
|
|
return util.JSONResponse{
|
2019-10-01 11:09:47 -05:00
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: jsonerror.NotFound(
|
|
|
|
fmt.Sprintf("Room alias %s not found", roomAlias),
|
|
|
|
),
|
2017-06-27 06:37:25 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-01 11:09:47 -05:00
|
|
|
} else {
|
2021-11-24 04:45:23 -06:00
|
|
|
joinedHostsReq := federationAPI.QueryJoinedHostServerNamesInRoomRequest{RoomID: res.RoomID}
|
|
|
|
var joinedHostsRes federationAPI.QueryJoinedHostServerNamesInRoomResponse
|
2019-10-01 11:09:47 -05:00
|
|
|
if err = fedSenderAPI.QueryJoinedHostServerNamesInRoom(req.Context(), &joinedHostsReq, &joinedHostsRes); err != nil {
|
2020-03-02 10:20:44 -06:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("fedSenderAPI.QueryJoinedHostServerNamesInRoom failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2019-10-01 11:09:47 -05:00
|
|
|
}
|
|
|
|
res.fillServers(joinedHostsRes.ServerNames)
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2019-10-01 11:09:47 -05:00
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: res,
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLocalAlias implements PUT /directory/room/{roomAlias}
|
|
|
|
func SetLocalAlias(
|
|
|
|
req *http.Request,
|
2021-09-08 11:31:03 -05:00
|
|
|
device *userapi.Device,
|
2017-07-28 05:31:43 -05:00
|
|
|
alias string,
|
2020-08-10 08:18:04 -05:00
|
|
|
cfg *config.ClientAPI,
|
2022-05-05 07:17:38 -05:00
|
|
|
rsAPI roomserverAPI.ClientRoomserverAPI,
|
2017-07-28 05:31:43 -05:00
|
|
|
) util.JSONResponse {
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', alias)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 06:59:19 -05:00
|
|
|
if !cfg.Matrix.IsLocalServerName(domain) {
|
2017-07-28 05:31:43 -05:00
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusForbidden,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: jsonerror.Forbidden("Alias must be on local homeserver"),
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 06:37:25 -05:00
|
|
|
|
2018-06-29 06:09:00 -05:00
|
|
|
// Check that the alias does not fall within an exclusive namespace of an
|
|
|
|
// application service
|
2018-07-06 05:29:15 -05:00
|
|
|
// TODO: This code should eventually be refactored with:
|
|
|
|
// 1. The new method for checking for things matching an AS's namespace
|
|
|
|
// 2. Using an overall Regex object for all AS's just like we did for usernames
|
2022-03-07 11:14:08 -06:00
|
|
|
reqUserID, _, err := gomatrixserverlib.SplitID('@', device.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("User ID must be in the form '@localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
2018-06-29 06:09:00 -05:00
|
|
|
for _, appservice := range cfg.Derived.ApplicationServices {
|
2019-07-12 08:36:17 -05:00
|
|
|
// Don't prevent AS from creating aliases in its own namespace
|
|
|
|
// Note that Dendrite uses SenderLocalpart as UserID for AS users
|
2022-03-07 11:14:08 -06:00
|
|
|
if reqUserID != appservice.SenderLocalpart {
|
2019-07-12 08:36:17 -05:00
|
|
|
if aliasNamespaces, ok := appservice.NamespaceMap["aliases"]; ok {
|
|
|
|
for _, namespace := range aliasNamespaces {
|
|
|
|
if namespace.Exclusive && namespace.RegexpObject.MatchString(alias) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.ASExclusive("Alias is reserved by an application service"),
|
|
|
|
}
|
2018-06-29 06:09:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 05:31:43 -05:00
|
|
|
var r struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
2018-08-08 10:17:09 -05:00
|
|
|
queryReq := roomserverAPI.SetRoomAliasRequest{
|
2017-07-28 05:31:43 -05:00
|
|
|
UserID: device.UserID,
|
|
|
|
RoomID: r.RoomID,
|
|
|
|
Alias: alias,
|
|
|
|
}
|
2018-08-08 10:17:09 -05:00
|
|
|
var queryRes roomserverAPI.SetRoomAliasResponse
|
2021-07-21 10:53:50 -05:00
|
|
|
if err := rsAPI.SetRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
2020-03-02 10:20:44 -06:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("aliasAPI.SetRoomAlias failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if queryRes.AliasExists {
|
2017-06-27 06:37:25 -05:00
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusConflict,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: jsonerror.Unknown("The alias " + alias + " already exists."),
|
2017-06-27 06:37:25 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 05:31:43 -05:00
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLocalAlias implements DELETE /directory/room/{roomAlias}
|
|
|
|
func RemoveLocalAlias(
|
|
|
|
req *http.Request,
|
2021-09-08 11:31:03 -05:00
|
|
|
device *userapi.Device,
|
2017-07-28 05:31:43 -05:00
|
|
|
alias string,
|
2022-05-05 07:17:38 -05:00
|
|
|
rsAPI roomserverAPI.ClientRoomserverAPI,
|
2017-07-28 05:31:43 -05:00
|
|
|
) util.JSONResponse {
|
2021-07-21 10:53:50 -05:00
|
|
|
queryReq := roomserverAPI.RemoveRoomAliasRequest{
|
|
|
|
Alias: alias,
|
|
|
|
UserID: device.UserID,
|
2019-08-06 22:00:58 -05:00
|
|
|
}
|
2021-07-21 10:53:50 -05:00
|
|
|
var queryRes roomserverAPI.RemoveRoomAliasResponse
|
|
|
|
if err := rsAPI.RemoveRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("aliasAPI.RemoveRoomAlias failed")
|
2020-03-02 10:20:44 -06:00
|
|
|
return jsonerror.InternalServerError()
|
2019-08-06 22:00:58 -05:00
|
|
|
}
|
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
if !queryRes.Found {
|
2019-08-06 22:00:58 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
2021-07-21 10:53:50 -05:00
|
|
|
JSON: jsonerror.NotFound("The alias does not exist."),
|
2019-08-06 22:00:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
if !queryRes.Removed {
|
2019-08-06 22:00:58 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2021-07-21 10:53:50 -05:00
|
|
|
JSON: jsonerror.Forbidden("You do not have permission to remove this alias."),
|
2019-08-06 22:00:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 05:31:43 -05:00
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
2017-06-27 06:37:25 -05:00
|
|
|
}
|
2020-07-02 09:41:18 -05:00
|
|
|
|
|
|
|
type roomVisibility struct {
|
|
|
|
Visibility string `json:"visibility"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVisibility implements GET /directory/list/room/{roomID}
|
|
|
|
func GetVisibility(
|
2022-05-05 07:17:38 -05:00
|
|
|
req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI,
|
2020-07-02 09:41:18 -05:00
|
|
|
roomID string,
|
|
|
|
) util.JSONResponse {
|
|
|
|
var res roomserverAPI.QueryPublishedRoomsResponse
|
|
|
|
err := rsAPI.QueryPublishedRooms(req.Context(), &roomserverAPI.QueryPublishedRoomsRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
}, &res)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("QueryPublishedRooms failed")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
|
|
|
|
var v roomVisibility
|
|
|
|
if len(res.RoomIDs) == 1 {
|
|
|
|
v.Visibility = gomatrixserverlib.Public
|
|
|
|
} else {
|
|
|
|
v.Visibility = "private"
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVisibility implements PUT /directory/list/room/{roomID}
|
|
|
|
// TODO: Allow admin users to edit the room visibility
|
|
|
|
func SetVisibility(
|
2022-05-05 07:17:38 -05:00
|
|
|
req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI, dev *userapi.Device,
|
2020-07-02 09:41:18 -05:00
|
|
|
roomID string,
|
|
|
|
) util.JSONResponse {
|
2020-09-04 06:30:56 -05:00
|
|
|
resErr := checkMemberInRoom(req.Context(), rsAPI, dev.UserID, roomID)
|
2020-07-02 09:41:18 -05:00
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
|
|
|
queryEventsReq := roomserverAPI.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateToFetch: []gomatrixserverlib.StateKeyTuple{{
|
|
|
|
EventType: gomatrixserverlib.MRoomPowerLevels,
|
|
|
|
StateKey: "",
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
var queryEventsRes roomserverAPI.QueryLatestEventsAndStateResponse
|
|
|
|
err := rsAPI.QueryLatestEventsAndState(req.Context(), &queryEventsReq, &queryEventsRes)
|
|
|
|
if err != nil || len(queryEventsRes.StateEvents) == 0 {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("could not query events from room")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
// NOTSPEC: Check if the user's power is greater than power required to change m.room.canonical_alias event
|
2020-07-02 09:41:18 -05:00
|
|
|
power, _ := gomatrixserverlib.NewPowerLevelContentFromEvent(queryEventsRes.StateEvents[0].Event)
|
2021-07-21 10:53:50 -05:00
|
|
|
if power.UserLevel(dev.UserID) < power.EventLevel(gomatrixserverlib.MRoomCanonicalAlias, true) {
|
2020-07-02 09:41:18 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: jsonerror.Forbidden("userID doesn't have power level to change visibility"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var v roomVisibility
|
|
|
|
if reqErr := httputil.UnmarshalJSONRequest(req, &v); reqErr != nil {
|
|
|
|
return *reqErr
|
|
|
|
}
|
|
|
|
|
|
|
|
var publishRes roomserverAPI.PerformPublishResponse
|
2022-08-11 09:29:33 -05:00
|
|
|
if err := rsAPI.PerformPublish(req.Context(), &roomserverAPI.PerformPublishRequest{
|
2020-07-02 09:41:18 -05:00
|
|
|
RoomID: roomID,
|
|
|
|
Visibility: v.Visibility,
|
2022-08-11 09:29:33 -05:00
|
|
|
}, &publishRes); err != nil {
|
|
|
|
return jsonerror.InternalAPIError(req.Context(), err)
|
|
|
|
}
|
2020-07-02 09:41:18 -05:00
|
|
|
if publishRes.Error != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(publishRes.Error).Error("PerformPublish failed")
|
|
|
|
return publishRes.Error.JSONResponse()
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|