From 03f6db22b15cb0e38fee00b5a6e0b29dac9090e6 Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Wed, 29 Apr 2026 18:47:28 +0300 Subject: [PATCH] updates CMake configuration for SQLite; sqlite: open or create table function. --- CMakeLists.txt | 3 + src/CMakeLists.txt | 3 +- src/db/CMakeLists.txt | 22 ++++++ src/db/db.c | 5 ++ src/{ => db}/db.h | 2 +- src/{db.c => db/db_postgresql.c} | 8 +- src/db/db_sqlite.c | 123 +++++++++++++++++++++++++++++++ src/file_p.c | 2 +- src/main.c | 3 +- src/session.c | 2 +- src/user.c | 2 +- 11 files changed, 164 insertions(+), 11 deletions(-) create mode 100644 src/db/CMakeLists.txt create mode 100644 src/db/db.c rename src/{ => db}/db.h (98%) rename src/{db.c => db/db_postgresql.c} (98%) create mode 100644 src/db/db_sqlite.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c327553..fb08caf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,9 @@ project(ZBBSServer set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +# Uncomment if you need to use PostgresQL +# add_compile_definitions(ZBBSServer USE_POSTGRESQL) + if( (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c66fa67..8bfa26b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,11 +10,12 @@ target_sources(zbbs-server ) add_subdirectory(libs) +add_subdirectory(db) target_link_libraries(zbbs-server PRIVATE + db murmur3 - PostgreSQL::PostgreSQL OpenSSL::Crypto ) diff --git a/src/db/CMakeLists.txt b/src/db/CMakeLists.txt new file mode 100644 index 0000000..aac8e10 --- /dev/null +++ b/src/db/CMakeLists.txt @@ -0,0 +1,22 @@ +add_library(db STATIC) + +if (USE_POSTGRESQL) + target_link_libraries(db + PRIVATE + PostgreSQL::PostgreSQL + ) +else() + target_link_libraries(db + PRIVATE + sqlite + ) +endif() + +target_sources(db + PRIVATE + "db.c" + PUBLIC + FILE_SET HEADERS + FILES + "db.h" +) \ No newline at end of file diff --git a/src/db/db.c b/src/db/db.c new file mode 100644 index 0000000..f93a553 --- /dev/null +++ b/src/db/db.c @@ -0,0 +1,5 @@ +#ifdef USE_POSTGRESQL +#include "db_postgresql.c" +#else +#include "db_sqlite.c" +#endif \ No newline at end of file diff --git a/src/db.h b/src/db/db.h similarity index 98% rename from src/db.h rename to src/db/db.h index b704b38..4781c7d 100644 --- a/src/db.h +++ b/src/db/db.h @@ -3,7 +3,7 @@ #ifndef DB_H #define DB_H -#include "main.h" +#include "../main.h" #include /* Auth */ diff --git a/src/db.c b/src/db/db_postgresql.c similarity index 98% rename from src/db.c rename to src/db/db_postgresql.c index 31a0bf2..821b593 100644 --- a/src/db.c +++ b/src/db/db_postgresql.c @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: MIT */ /* Copyright (c) 2026 Oleksandr Zhylin */ -#include "db.h" -#include "main.h" +#include +#include "../main.h" #include #include #include @@ -24,9 +24,7 @@ static PGconn *conn = NULL; static PGresult *res = NULL; -// #define USR_NOT_FOUND "User with such credentials is not found!" - -static void clearRes() { +static void clearRes(void) { PQclear(res); res = NULL; } diff --git a/src/db/db_sqlite.c b/src/db/db_sqlite.c new file mode 100644 index 0000000..4acbf1d --- /dev/null +++ b/src/db/db_sqlite.c @@ -0,0 +1,123 @@ +/* SPDX-License-Identifier: MIT */ +/* Copyright (c) 2026 Oleksandr Zhylin */ + +#include "../main.h" +#include +#include +#include +#include +#include +#include + +#define UNUSED(x) (void)(x) +#define Q_LEN 128 +#define BIN 1 +#define TEXT 0 +#define ENCR_SIZE 2048 + +static sqlite3 *db; + +void print_err(char **err) { + if (*err != NULL) { + printf("%s\n", *err); + sqlite3_free(*err); + *err = NULL; + } +} + +int check_and_create_tables() { + int res = 0; + char *err = NULL; + + res = sqlite3_exec(db, + "CREATE TABLE IF NOT EXISTS 'users' (" + " 'id' SERIAL," + " 'username' VARCHAR(50) NOT NULL," + " 'password' CHARACTER(64) NOT NULL," + " 'email' VARCHAR(255) NOT NULL," + " 'created_at' TIMESTAMP NOT NULL," + " 'last_login' TIMESTAMP NULL," + " 'privileges' SMALLINT NOT NULL," + " CONSTRAINT 'users_pkey' PRIMARY KEY ('id')," + " CONSTRAINT 'users_email_key' UNIQUE ('email')," + " CONSTRAINT 'users_username_key' UNIQUE ('username')" + "); ", + NULL, NULL, &err); + + print_err(&err); + res = sqlite3_exec(db, + "CREATE UNIQUE INDEX IF NOT EXISTS 'username_idx'" + " ON 'users' (" + " 'username' ASC" + " );", + NULL, NULL, &err); + print_err(&err); + res = sqlite3_exec(db, + "CREATE UNIQUE INDEX IF NOT EXISTS 'email_idx' " + "ON 'users' (" + "'email' ASC" + ");", + NULL, NULL, &err); + print_err(&err); + res = sqlite3_exec(db, + "CREATE TABLE IF NOT EXISTS 'files' (" + " 'id' SERIAL," + " 'user_id' INTEGER NULL," + " 'name' VARCHAR(255) NOT NULL," + " 'size' BIGINT NOT NULL," + " 'created_at' DATE NOT NULL," + " 'description' TEXT NULL," + " 'permissions' INTEGER NULL," + " 'hash' INTEGER NULL," + " CONSTRAINT 'files_pkey' PRIMARY KEY ('id')," + " CONSTRAINT 'fk_user' FOREIGN KEY ('user_id')" + " REFERENCES 'users' ('id') ON DELETE NO ACTION" + " ON UPDATE NO ACTION" + ");", + NULL, NULL, &err); + print_err(&err); + return res; +} + +int init_db_connection(void) { + int res = sqlite3_open("db.sql", &db); + char *err = NULL; + if (!res) { + res = check_and_create_tables(); + } + + return 0; +} + +void SHA256_raw_to_string(const unsigned char *passHashed, char *restrict out) { + int i; + for (i = 0; i < 4; i++) { + uint64_t *num_pointer = (uint64_t *)(passHashed + i * 8); + sprintf(out + 16 * i, "%016lx", htobe64(*num_pointer)); + } +} + +void string_to_SHA256(const char *str, char *restrict out) { + unsigned char md[SHA256_DIGEST_LENGTH]; + unsigned char *passHashed = SHA256((unsigned char *)str, strlen(str), md); + SHA256_raw_to_string(passHashed, out); +} + +int32_t db_user_auth(i_auth_t *c, o_auth_t *r) { return 0; } + +int32_t db_user_create(i_db_user_create *args) { return 0; } + +int32_t db_save_file(session *s) { return 0; } + +// TODO: get file +s_file_t *db_get_file(i_get_file_db *arg) { + s_file_t *sf = NULL; + + return sf; +} + +uint64_t db_get_files_data(i_get_files_db *arg, fl_t **fl_start, + uint64_t *full_count) { + fl_t *fl_current = NULL; + return 0; +} \ No newline at end of file diff --git a/src/file_p.c b/src/file_p.c index 5abf9fb..af3b037 100644 --- a/src/file_p.c +++ b/src/file_p.c @@ -2,7 +2,7 @@ /* Copyright (c) 2026 Oleksandr Zhylin */ #include "file_p.h" -#include "db.h" +#include #include "main.h" #include "session.h" #include diff --git a/src/main.c b/src/main.c index 93df41d..93b3343 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,7 @@ #include "main.h" #include "server.h" -#include "db.h" +#include #include /* *** MAIN *** */ @@ -12,6 +12,7 @@ int main(int argc, char *argv[]) { prepare_start(argc, argv); server_data.welcome_message = get_welcome_mes(); init_db_connection(); + return 0; server_data.ls = start_server(); /* TODO: display server port */ printf("Server is started!\n"); diff --git a/src/session.c b/src/session.c index 445243a..5505e92 100644 --- a/src/session.c +++ b/src/session.c @@ -3,7 +3,7 @@ #include "session.h" #include "client.h" -#include "db.h" +#include #include "file_p.h" #include "main.h" #include "user.h" diff --git a/src/user.c b/src/user.c index 844631f..b07796d 100644 --- a/src/user.c +++ b/src/user.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: MIT */ /* Copyright (c) 2026 Oleksandr Zhylin */ -#include "db.h" +#include #include "main.h" #include "session.h" #include