[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) switch (sigil)
{ {
case '#': /* It seems like the localpart should be the same as the
user's: everything, except ':'. */
case '@': case '@':
if (!ParseUserLocalpart(&str, &id->local)) if (!ParseUserLocalpart(&str, &id->local))
{ {

View file

@ -27,6 +27,9 @@
#include <Cytoplasm/Json.h> #include <Cytoplasm/Json.h>
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <Config.h>
#include <Parser.h>
#include <User.h> #include <User.h>
ROUTE_IMPL(RouteAliasDirectory, path, argp) ROUTE_IMPL(RouteAliasDirectory, path, argp)
@ -43,15 +46,33 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
JsonValue *val; JsonValue *val;
char *token; char *token;
char *msg;
User *user = NULL; 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"); ref = DbLock(db, 1, "aliases");
if (!ref && !(ref = DbCreate(db, 1, "aliases"))) if (!ref && !(ref = DbCreate(db, 1, "aliases")))
{ {
msg = "Unable to access alias database.",
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
response = MatrixErrorCreate(M_UNKNOWN, "Unable to access alias database."); response = MatrixErrorCreate(M_UNKNOWN, msg);
goto finish; goto finish;
} }
@ -69,8 +90,9 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
} }
else else
{ {
msg = "There is no mapped room ID for this room alias.";
HttpResponseStatus(args->context, HTTP_NOT_FOUND); 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; break;
case HTTP_PUT: case HTTP_PUT:
@ -93,8 +115,15 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
{ {
HashMap *newAlias; HashMap *newAlias;
/* TODO: Validate alias domain and make sure it matches /* Check for server name.
* server name and is well formed. */ * 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)) if (JsonGet(aliases, 2, "alias", alias))
{ {
@ -156,6 +185,8 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
} }
finish: finish:
CommonIDFree(commonID);
ConfigUnlock(config);
UserUnlock(user); UserUnlock(user);
DbUnlock(db, ref); DbUnlock(db, ref);
JsonFree(request); JsonFree(request);

View file

@ -44,8 +44,8 @@ typedef struct ServerPart {
char *port; char *port;
} ServerPart; } ServerPart;
/** /**
* A common identifier in the form '&local[:server]', where * A common identifier in the form '&local[:server]', where & determines the
* & determines the *type* of the identifier. * *type* of the identifier.
*/ */
typedef struct CommonID { typedef struct CommonID {
char sigil; char sigil;
@ -59,5 +59,9 @@ typedef struct CommonID {
*/ */
extern int ParseCommonID(char *, 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 */ #endif /* TELODENDRIA_PARSER_H */