From d14db43e9b8cf0075895819c3a4308b7efcc59fa Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Wed, 22 Oct 2025 16:46:36 +0700 Subject: [PATCH] updated file database added get_all_users get_user_by_id get_user_by_username get_user_field_by_id --- CMakeLists.txt | 5 +++++ Makefile | 5 +++++ meson.build | 5 +++++ meson_options.txt | 1 + src/database.c | 3 +++ src/database.h | 4 ++++ src/db_get_all_users.c | 9 ++++++++- src/db_get_user_by_id.c | 31 +++++++++++++++++++++++++++++-- src/db_get_user_by_username.c | 32 ++++++++++++++++++++++++++++++-- src/db_get_user_field_by_id.c | 30 +++++++++++++++++++++++++++++- src/file.c | 21 +++++++++++++++++++-- src/file.h | 3 ++- users_db.json | 18 ++++++++++++++++++ 13 files changed, 158 insertions(+), 9 deletions(-) create mode 100644 users_db.json diff --git a/CMakeLists.txt b/CMakeLists.txt index c7f4435..2320bb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ include_directories(./src/lib/ ${SQLite3_INCLUDE_DIRS}) FILE(GLOB_RECURSE SOURCES src/*.c) option(USE_SQLITE3 "Enable SQLite3 support" OFF) +option(USE_WERROR "Enable SQLite3 support" OFF) if(USE_SQLITE3) find_package(SQLite3 REQUIRED) @@ -18,6 +19,10 @@ if(USE_SQLITE3) add_definitions(-DUSE_SQLITE3) endif() +if(USE_WERROR) + add_definitions(-Werror) +endif() + add_executable( auth_service ${SOURCES} diff --git a/Makefile b/Makefile index d2dfa18..eeb2ead 100644 --- a/Makefile +++ b/Makefile @@ -9,12 +9,17 @@ BUILD_DIR=./build CROSS_COMPILE = LIBS = -lpthread -lcrypto -lssl USE_SQLITE3 ?= 0 +WERROR ?= 0 ifeq ($(USE_SQLITE3), 1) CFLAGS += -DUSE_SQLITE3 LIBS += -lsqlite3 endif +ifeq ($(WERROR), 1) + CFLAGS += -Werror +endif + all: $(TARGET) $(TARGET): $(OBJS) diff --git a/meson.build b/meson.build index 6c91083..b51d336 100644 --- a/meson.build +++ b/meson.build @@ -6,6 +6,7 @@ extra_c_link_args = ['-Wl,--gc-sections'] sqlite3_dep = dependency('sqlite3', required: false) use_sqlite3 = get_option('use_sqlite3') +use_werror = get_option('use_werror') if use_sqlite3 if not sqlite3_dep.found() @@ -15,6 +16,10 @@ if use_sqlite3 add_project_arguments('-DUSE_SQLITE3', language: 'c') endif +if use_werror + add_project_arguments('-Werror', language: 'c') +endif + executable( 'auth_service', 'src/main.c', diff --git a/meson_options.txt b/meson_options.txt index ddd46af..54d2bc5 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1 +1,2 @@ option('use_sqlite3', type: 'boolean', value: false, description: 'Enable SQLite3 support') +option('use_werror', type: 'boolean', value: false, description: 'Build with warning as errors') diff --git a/src/database.c b/src/database.c index 11ebe10..570de7d 100644 --- a/src/database.c +++ b/src/database.c @@ -10,6 +10,9 @@ #ifdef USE_SQLITE3 sqlite3 *db = nullptr; pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER; +#else +FILE *users_file = nullptr; +pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER; #endif void init_db() { diff --git a/src/database.h b/src/database.h index 5011be9..1513eac 100644 --- a/src/database.h +++ b/src/database.h @@ -6,6 +6,10 @@ #include extern sqlite3 *db; extern pthread_mutex_t db_mutex; +#else +#include +extern FILE *users_file; +extern pthread_mutex_t users_file_mutex; #endif void init_db(); diff --git a/src/db_get_all_users.c b/src/db_get_all_users.c index c51c9bd..fd3bc27 100644 --- a/src/db_get_all_users.c +++ b/src/db_get_all_users.c @@ -4,6 +4,10 @@ #include "sql.h" #include "user.h" +#ifndef USE_SQLITE3 +#include "file.h" +#endif + char *db_get_all_users() { #ifdef USE_SQLITE3 pthread_mutex_lock(&db_mutex); @@ -99,6 +103,9 @@ char *db_get_all_users() { pthread_mutex_unlock(&db_mutex); return result; #else - return ""; + pthread_mutex_lock(&users_file_mutex); + char *data = read_file("users_db.json"); + pthread_mutex_unlock(&users_file_mutex); + return data; #endif } diff --git a/src/db_get_user_by_id.c b/src/db_get_user_by_id.c index 6a82392..5c7448e 100644 --- a/src/db_get_user_by_id.c +++ b/src/db_get_user_by_id.c @@ -2,6 +2,10 @@ #include "sql.h" #include "user.h" +#ifndef USE_SQLITE3 +#include "file.h" +#endif + User *db_get_user_by_id(const int id) { #ifdef USE_SQLITE3 User *user = nullptr; @@ -43,7 +47,30 @@ User *db_get_user_by_id(const int id) { pthread_mutex_unlock(&db_mutex); return user; #else - User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false); - return asdf; + pthread_mutex_lock(&users_file_mutex); + char *data = read_file("users_db.json"); + cJSON *users = cJSON_Parse(data); + cJSON *user = nullptr; + User *found = nullptr; + cJSON_ArrayForEach(user, users) { + cJSON *user_id = cJSON_GetObjectItem(user, "id"); + cJSON *user_username = cJSON_GetObjectItem(user, "username"); + cJSON *user_password = cJSON_GetObjectItem(user, "password"); + cJSON *user_email = cJSON_GetObjectItem(user, "email"); + cJSON *user_group = cJSON_GetObjectItem(user, "group"); + cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser"); + if (user_id->valueint == id) { + found = new_user(user_id->valueint, user_username->valuestring, + user_password->valuestring, user_email->valuestring, + user_group->valuestring, user_is_superuser->valueint); + break; + } + } + if (found == nullptr) { + pthread_mutex_unlock(&users_file_mutex); + return nullptr; + } + pthread_mutex_unlock(&users_file_mutex); + return found; #endif } diff --git a/src/db_get_user_by_username.c b/src/db_get_user_by_username.c index 740f184..8063c8f 100644 --- a/src/db_get_user_by_username.c +++ b/src/db_get_user_by_username.c @@ -1,7 +1,12 @@ +#include "cJSON.h" #include "database.h" #include "sql.h" #include "user.h" +#ifndef USE_SQLITE3 +#include "file.h" +#endif + User *db_get_user_by_username(const char *username) { #ifdef USE_SQLITE3 User *user = nullptr; @@ -43,7 +48,30 @@ User *db_get_user_by_username(const char *username) { pthread_mutex_unlock(&db_mutex); return user; #else - User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false); - return asdf; + pthread_mutex_lock(&users_file_mutex); + char *data = read_file("users_db.json"); + cJSON *users = cJSON_Parse(data); + cJSON *user = nullptr; + User *found = nullptr; + cJSON_ArrayForEach(user, users) { + cJSON *user_id = cJSON_GetObjectItem(user, "id"); + cJSON *user_username = cJSON_GetObjectItem(user, "username"); + cJSON *user_password = cJSON_GetObjectItem(user, "password"); + cJSON *user_email = cJSON_GetObjectItem(user, "email"); + cJSON *user_group = cJSON_GetObjectItem(user, "group"); + cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser"); + if (!strcmp(user_username->valuestring, username)) { + found = new_user(user_id->valueint, user_username->valuestring, + user_password->valuestring, user_email->valuestring, + user_group->valuestring, user_is_superuser->valueint); + break; + } + } + if (found == nullptr) { + pthread_mutex_unlock(&users_file_mutex); + return nullptr; + } + pthread_mutex_unlock(&users_file_mutex); + return found; #endif } diff --git a/src/db_get_user_field_by_id.c b/src/db_get_user_field_by_id.c index 5ac0cd4..d1e57d9 100644 --- a/src/db_get_user_field_by_id.c +++ b/src/db_get_user_field_by_id.c @@ -4,6 +4,10 @@ #include "sql.h" #include "user.h" +#ifndef USE_SQLITE3 +#include "file.h" +#endif + char *db_get_user_field_by_id(const int id, const char *field) { #ifdef USE_SQLITE3 char *password = nullptr; @@ -45,6 +49,30 @@ char *db_get_user_field_by_id(const int id, const char *field) { pthread_mutex_unlock(&db_mutex); return password; #else - return ""; + pthread_mutex_lock(&users_file_mutex); + char *data = read_file("users_db.json"); + cJSON *users = cJSON_Parse(data); + cJSON *user = nullptr; + User *found = nullptr; + cJSON_ArrayForEach(user, users) { + cJSON *user_id = cJSON_GetObjectItem(user, "id"); + cJSON *user_username = cJSON_GetObjectItem(user, "username"); + cJSON *user_password = cJSON_GetObjectItem(user, "password"); + cJSON *user_email = cJSON_GetObjectItem(user, "email"); + cJSON *user_group = cJSON_GetObjectItem(user, "group"); + cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser"); + if (user_id->valueint == id) { + found = new_user(user_id->valueint, user_username->valuestring, + user_password->valuestring, user_email->valuestring, + user_group->valuestring, user_is_superuser->valueint); + break; + } + } + if (found == nullptr) { + pthread_mutex_unlock(&users_file_mutex); + return nullptr; + } + pthread_mutex_unlock(&users_file_mutex); + return found->password; #endif } diff --git a/src/file.c b/src/file.c index 5287cb5..c0488c8 100644 --- a/src/file.c +++ b/src/file.c @@ -2,6 +2,7 @@ #include #include +#include char *read_file(const char *path) { FILE *fp = fopen(path, "rb"); @@ -14,7 +15,7 @@ char *read_file(const char *path) { long file_size = ftell(fp); rewind(fp); - char *buffer = (char *)malloc(file_size + 1); + char *buffer = (char *) malloc(file_size + 1); if (!buffer) { perror("Error allocating memory"); fclose(fp); @@ -32,4 +33,20 @@ char *read_file(const char *path) { buffer[file_size] = '\0'; fclose(fp); return buffer; -} \ No newline at end of file +} + +char *write_file(const char *path, const char *data) { + FILE *fp = fopen(path, "w"); + if (fp == NULL) { + printf("%s\n", path); + perror("Error opening file"); + return nullptr; + } + if (fprintf(fp, "%s", data) < 0) { + perror("Error write file"); + return nullptr; + } + fclose(fp); + char *ret = strdup(data); + return ret; +} diff --git a/src/file.h b/src/file.h index 7503f09..9ac6d5d 100644 --- a/src/file.h +++ b/src/file.h @@ -1,3 +1,4 @@ #pragma once -char *read_file(const char *path); \ No newline at end of file +char *read_file(const char *path); +char *write_file(const char *path, const char *data); diff --git a/users_db.json b/users_db.json new file mode 100644 index 0000000..58db746 --- /dev/null +++ b/users_db.json @@ -0,0 +1,18 @@ +[ + { + "id": 0, + "username": "tola", + "email": "mail@example.com", + "password": "e0c7bfa5b8e4ae0cecfc1f14093a71e05c27abd93a3850fc35f008c3baf866b4", + "group": "root", + "is_superuser": true + }, + { + "id": 1, + "username": "lisa", + "email": "lisa@example.com", + "password": "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b", + "group": "users", + "is_superuser": false + } +]