From 5dbaf3c223e00bf6c8084d6969dd66dddaf5d080 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Wed, 15 Mar 2023 13:36:49 +0000 Subject: [PATCH] Apply #65, remove printf() in Db, and make tt show login messages. --- TODO.txt | 4 ++-- src/Db.c | 1 - src/Routes/RouteLogin.c | 9 +++++++++ src/User.c | 17 ++++++++++++++++- src/include/User.h | 3 +++ tools/bin/tt | 8 +++++--- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/TODO.txt b/TODO.txt index 95377ba..bc716e6 100644 --- a/TODO.txt +++ b/TODO.txt @@ -55,8 +55,8 @@ Milestone: v0.3.0 - Ensure that registration tokens can be used even if registration is disabled. [~] 4: Account management - [ ] Deactivate - [ ] Make sure UserLogin() fails if user is deactivated. + [~] Deactivate + [x] Make sure UserLogin() fails if user is deactivated. [ ] Change password [x] Whoami [ ] 9: User Data diff --git a/src/Db.c b/src/Db.c index 7a880a3..b922767 100644 --- a/src/Db.c +++ b/src/Db.c @@ -460,7 +460,6 @@ DbLockFromArr(Db * db, Array * args) /* Lock the file on the disk */ if (fcntl(fileno(fp), F_SETLK, &lock) < 0) { - printf("fcntl() failed on %s (%s)\n", file, strerror(errno)); fclose(fp); ref = NULL; goto finish; diff --git a/src/Routes/RouteLogin.c b/src/Routes/RouteLogin.c index ec442fd..5351c39 100644 --- a/src/Routes/RouteLogin.c +++ b/src/Routes/RouteLogin.c @@ -242,6 +242,15 @@ ROUTE_IMPL(RouteLogin, args) break; } + if (UserDeactivated(user)) + { + UserUnlock(user); + + HttpResponseStatus(args->context, HTTP_FORBIDDEN); + response = MatrixErrorCreate(M_USER_DEACTIVATED); + break; + } + loginInfo = UserLogin(user, password, deviceId, initialDeviceDisplayName, refreshToken); diff --git a/src/User.c b/src/User.c index 3e9a946..2ec453d 100644 --- a/src/User.c +++ b/src/User.c @@ -246,7 +246,7 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName, return NULL; } - if (!UserCheckPassword(user, password)) + if (!UserCheckPassword(user, password) || UserDeactivated(user)) { return NULL; } @@ -428,6 +428,21 @@ UserDeactivate(User * user) return 1; } +int +UserDeactivated(User *user) +{ + HashMap *json; + + if (!user) + { + return 1; + } + + json = DbJson(user->ref); + + return JsonValueAsBoolean(HashMapGet(json, "deactivated")); +} + HashMap * UserGetDevices(User * user) { diff --git a/src/include/User.h b/src/include/User.h index 7ee6a86..d14f7d9 100644 --- a/src/include/User.h +++ b/src/include/User.h @@ -84,6 +84,9 @@ extern int extern int UserDeactivate(User *); +extern int + UserDeactivated(User *); + extern HashMap * UserGetDevices(User *); diff --git a/tools/bin/tt b/tools/bin/tt index 1a5bec2..2af9ffa 100755 --- a/tools/bin/tt +++ b/tools/bin/tt @@ -26,7 +26,7 @@ if [ "$user_available" = "true" ]; then fi # Log in -ACCESS_TOKEN=$(( +RESPONSE=$(( printf '{' printf ' "identifier": {' printf ' "type": "m.id.user",' @@ -35,10 +35,12 @@ ACCESS_TOKEN=$(( printf ' "type": "m.login.password",' printf ' "password": %s' "$PASSWORD" printf '}' -) | http -X POST -d @- "$BASE/_matrix/client/v3/login" | json -s "access_token->@decode") +) | http -X POST -d @- "$BASE/_matrix/client/v3/login") + +ACCESS_TOKEN=$(echo "$RESPONSE" | json -s "access_token->@decode") if [ -z "$ACCESS_TOKEN" ]; then - echo "Unable to log in." + echo "$RESPONSE" | json exit 1 fi