From e24c6b3ef31e6a7a65502efaf1218916968f6604 Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Thu, 19 Feb 2026 14:46:08 +0200 Subject: [PATCH] improves registration --- TODO.md | 2 +- src/db.c | 33 +++++++++++++++++++++++++++++++-- src/user.c | 18 +++++++++++++----- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/TODO.md b/TODO.md index 9803114..99f5b21 100644 --- a/TODO.md +++ b/TODO.md @@ -7,7 +7,7 @@ - [x] Get files in FS by their hash - [x] Registration - [x] Encrypt passwords in DB -- [ ] Registration: Error when username or email is already used +- [x] Registration: Error when username or email is already used - [ ] Check correctness of email - [ ] Message to admin - [x] Advanced folder structure in the storage diff --git a/src/db.c b/src/db.c index 3f68fcc..4dcb2e6 100644 --- a/src/db.c +++ b/src/db.c @@ -147,22 +147,51 @@ int32_t db_user_auth(i_auth_t *c, o_auth_t *r) { int32_t db_user_create(i_db_user_create *args) { const char *paramValues[3]; + int32_t paramLengths[3]; char passHashed[SHA256_DIGEST_LENGTH * 2]; paramValues[0] = args->uname; paramValues[2] = args->email; + + paramLengths[0] = strlen(args->uname); + paramLengths[2] = strlen(args->email); uint32_t ret_value; string_to_SHA256(args->pass, passHashed); paramValues[1] = passHashed; + paramLengths[1] = 64; + + /* Check username */ + res = PQexecParams(conn, "SELECT id FROM users WHERE username=$1", 1, NULL, + paramValues, paramLengths, NULL, TEXT); + + if (PQresultStatus(res) != PGRES_TUPLES_OK && !PQntuples(res)) + return exit_query_2(-1); + + if (PQntuples(res) > 0) { + return exit_query_2(-2); + } + + /* Check email */ + res = PQexecParams(conn, "SELECT id FROM users WHERE email=$1", 1, NULL, + paramValues+2, paramLengths+2, NULL, TEXT); + + if (PQresultStatus(res) != PGRES_TUPLES_OK && !PQntuples(res)) + return exit_query_2(-1); + + if (PQntuples(res) > 0) { + return exit_query_2(-3); + } + + clearRes(); res = PQexecParams(conn, "INSERT INTO users (username, password, email, " "privileges, created_at, last_login)" " VALUES ($1, $2, $3, 1, NOW(), NOW()) RETURNING id", - 3, NULL, paramValues, NULL, NULL, TEXT); + 3, NULL, paramValues, paramLengths, NULL, TEXT); if (PQresultStatus(res) != PGRES_TUPLES_OK && !PQntuples(res)) - return exit_query(-1); + return exit_query(-4); ret_value = atoi(PQgetvalue(res, 0, 0)); clearRes(); diff --git a/src/user.c b/src/user.c index 55b0cd0..47cf595 100644 --- a/src/user.c +++ b/src/user.c @@ -9,16 +9,23 @@ int32_t create_user(session *sess, char *line) { i_db_user_create p; uint32_t privileges; - uint32_t res; + int32_t res; if (sscanf(line, "register %s %s", p.uname, p.pass) == 2) { + sprintf(p.email, "%s@mail.net", p.uname); /* TODO: return back email */ res = db_user_create(&p); - if (res) { + if (res > 0) { sess->state = OP_WAIT; sess->uname = malloc(strlen(p.uname)); strcpy(sess->uname, p.uname); sess->uid = res; - sess->privileges = 1; // by default - session_send_string(sess, "ok"); + sess->privileges = 1; /* by default */ + session_send_string(sess, "ok\n"); + } else if (res == -2) { + session_send_string(sess, "User with such name already exists\n"); + } else if (res == -3) { + session_send_string(sess, "User with such email already exists\n"); + } else { + session_send_string(sess, "Error creating the user\n"); } return res; }; @@ -85,7 +92,8 @@ int process_error(session *sess) { return 1; break; case LOGIN: - session_send_string(sess, "Can't find the user with such credentials\04\n"); + session_send_string(sess, + "Can't find the user with such credentials\04\n"); sess->state = OP_LOGIN_USR; sess->reason = NO_REASON; session_send_string(sess, "login_again> ");