[MOD/WIP] Start doing checks on room alias

NOTE: Currently UNTESTED.
This commit is contained in:
lda 2023-11-29 12:57:42 +01:00
parent 8eab884289
commit 78daf86eb3
3 changed files with 44 additions and 7 deletions

View file

@ -267,6 +267,8 @@ ParseCommonID(char *str, CommonID *id)
switch (sigil)
{
case '#': /* It seems like the localpart should be the same as the
user's: everything, except ':'. */
case '@':
if (!ParseUserLocalpart(&str, &id->local))
{

View file

@ -27,6 +27,9 @@
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Str.h>
#include <Config.h>
#include <Parser.h>
#include <User.h>
ROUTE_IMPL(RouteAliasDirectory, path, argp)
@ -43,15 +46,33 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
JsonValue *val;
char *token;
char *msg;
User *user = NULL;
/* TODO: Return HTTP 400 M_INVALID_PARAM if alias is invalid */
CommonID commonID;
Config *config;
commonID.sigil = '\0';
commonID.local = NULL;
commonID.server.hostname = NULL;
commonID.server.port = NULL;
config = ConfigLock(db);
if (!ParseCommonID(alias, &commonID) || commonID.sigil != '#')
{
msg = "Invalid room alias.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_PARAM, msg);
goto finish;
}
ref = DbLock(db, 1, "aliases");
if (!ref && !(ref = DbCreate(db, 1, "aliases")))
{
msg = "Unable to access alias database.",
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
response = MatrixErrorCreate(M_UNKNOWN, "Unable to access alias database.");
response = MatrixErrorCreate(M_UNKNOWN, msg);
goto finish;
}
@ -69,8 +90,9 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
}
else
{
msg = "There is no mapped room ID for this room alias.";
HttpResponseStatus(args->context, HTTP_NOT_FOUND);
response = MatrixErrorCreate(M_NOT_FOUND, "There is no mapped room ID for this room alias.");
response = MatrixErrorCreate(M_NOT_FOUND, msg);
}
break;
case HTTP_PUT:
@ -93,8 +115,15 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
{
HashMap *newAlias;
/* TODO: Validate alias domain and make sure it matches
* server name and is well formed. */
/* Check for server name.
* TODO: Take the port into account, that might need a
* refactor for it to use a ServerPart */
if (!StrEquals(commonID.server.hostname, config->serverName))
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_PARAM, "Invalid servername");
goto finish;
}
if (JsonGet(aliases, 2, "alias", alias))
{
@ -156,6 +185,8 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
}
finish:
CommonIDFree(commonID);
ConfigUnlock(config);
UserUnlock(user);
DbUnlock(db, ref);
JsonFree(request);

View file

@ -44,8 +44,8 @@ typedef struct ServerPart {
char *port;
} ServerPart;
/**
* A common identifier in the form '&local[:server]', where
* & determines the *type* of the identifier.
* A common identifier in the form '&local[:server]', where & determines the
* *type* of the identifier.
*/
typedef struct CommonID {
char sigil;
@ -59,5 +59,9 @@ typedef struct CommonID {
*/
extern int ParseCommonID(char *, CommonID *);
/**
* Frees a CommonID's values. Note that it doesn't free the CommonID itself. */
extern void CommonIDFree(CommonID id);
#endif /* TELODENDRIA_PARSER_H */