From 05f318233b8431b912659f02543d899829ead36c Mon Sep 17 00:00:00 2001 From: ThoNohT Date: Wed, 17 Jun 2026 17:34:07 +0200 Subject: [PATCH] Pull out libusers --- Makefile | 16 ++++++++++------ src/server.c | 5 ++--- src/userctl.c | 5 ++--- src/users.c | 24 ++++++------------------ src/users.h | 26 ++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 src/users.h diff --git a/Makefile b/Makefile index 28ad7c9..f19e81f 100644 --- a/Makefile +++ b/Makefile @@ -3,15 +3,15 @@ CFLAGS=-Wall -Wextra -ggdb LIBWS=./vendor/libmicrohttpd/ LIBSHA=./vendor/sha256/ -SHARED_LIB_FILES=output/libpath.a output/libnoh.a output/libsha.a -SHARED_LIBS=-L./output -lsha -lpath -lnoh +SHARED_LIB_FILES=output/libpath.a output/libnoh.a output/libusers.a output/libsha.a +SHARED_LIBS=-L./output -lpath -lnoh -lusers -lsha ################################################################################ SERVER_LDFLAGS=-lm -luuid -L $(LIBWS)lib -lmicrohttpd $(SHARED_LIBS) -SERVER_INCL_FLAGS=-I$(LIBWS)include -I$(LIBSHA) +SERVER_INCL_FLAGS=-I$(LIBWS)include -SERVER_SRC=src/server.c src/users.c \ +SERVER_SRC=src/server.c \ src/id_store.c src/library.c \ src/opds_handler.c src/get_handler.c @@ -22,9 +22,9 @@ output/server: $(SERVER_SRC) $(SHARED_LIB_FILES) | output ################################################################################ USERCTL_LDFLAGS=-lm $(SHARED_LIBS) -USERCTL_INCL_FLAGS=-I$(LIBSHA) +USERCTL_INCL_FLAGS= -USERCTL_SRC=src/userctl.c src/users.c +USERCTL_SRC=src/userctl.c userctl: output/userctl output/userctl: $(USERCTL_SRC) $(SHARED_LIB_FILES) | output @@ -32,6 +32,10 @@ output/userctl: $(USERCTL_SRC) $(SHARED_LIB_FILES) | output ################################################################################ +output/libusers.a: src/users.c src/users.h output/libsha.a | output + gcc $(CFLAGS) -x c -c -o output/users.o src/users.c -L ./output -lnoh -lsha -I$(LIBSHA) + ar rcsD output/libusers.a output/users.o + output/libsha.a: $(LIBSHA)sha256.c $(LIBSHA)sha256.h | output gcc $(CFLAGS) -c -o output/sha.o $(LIBSHA)sha256.c ar rcsD output/libsha.a output/sha.o diff --git a/src/server.c b/src/server.c index ace1aa5..493524f 100644 --- a/src/server.c +++ b/src/server.c @@ -1,14 +1,13 @@ #include -#include #include #include #include "path.h" +#include "users.h" #include "noh.h" Noh_Arena temp; -#include "users.c" #define SV Noh_String_View #define sv noh_sv_from_cstr @@ -67,7 +66,7 @@ static bool authorize_user(struct MHD_Connection *connection, SV *username, Sett } // Hash the provided password. - hashed = hash_password(provided_username, provided_password); + hashed = hash_password(&temp, provided_username, provided_password); // Check the hash. if (!noh_sv_eq(noh_sv_from_string(hashed), user->hash)) { diff --git a/src/userctl.c b/src/userctl.c index e3bd125..6fbbea3 100644 --- a/src/userctl.c +++ b/src/userctl.c @@ -2,12 +2,11 @@ #include #include "path.h" +#include "users.h" #include "noh.h" Noh_Arena temp; -#include "users.c" - #define SV Noh_String_View #define sv noh_sv_from_cstr @@ -96,7 +95,7 @@ bool register_user(Noh_String_View library_path, Noh_String_View username, Noh_S if (!read_users_file(users_file_path.elems, &users_file, &users)) noh_return_defer(false); // Hash the password. - hashed = hash_password(username, password); + hashed = hash_password(&temp, username, password); // Find the user in the map of users. for (size_t i = 0; i < users.count; i++) { diff --git a/src/users.c b/src/users.c index 51f94f1..52f4d25 100644 --- a/src/users.c +++ b/src/users.c @@ -1,17 +1,8 @@ +#include "users.h" + #include -typedef struct { - Noh_String_View username; - Noh_String_View hash; -} User; - -typedef struct { - User *elems; - size_t count; - size_t capacity; -} Users; - -Noh_String hash_password(Noh_String_View username, Noh_String_View password) { +Noh_String hash_password(Noh_Arena *temp, Noh_String_View username, Noh_String_View password) { // Build the sha input string. Noh_String input = {0}; noh_string_append_sv(&input, username); @@ -26,19 +17,16 @@ Noh_String hash_password(Noh_String_View username, Noh_String_View password) { sha256_final(&ctx, hash); // Build the result string. - noh_arena_save(&temp); + noh_arena_save(temp); Noh_String result = {0}; for (int i = 0; i < 32; i++) { - noh_string_append_cstr(&result, noh_arena_sprintf(&temp, "%02x", hash[i])); + noh_string_append_cstr(&result, noh_arena_sprintf(temp, "%02x", hash[i])); } - noh_arena_reset(&temp); + noh_arena_reset(temp); return result; } -// Reads the users file into users_file. Free this string when the data is no longer needed. -// Note that the string needs to be initialized and empty before calling this function. -// The users struct will contain string views referencing the data in this string. bool read_users_file(char *file_path, Noh_String *users_file, Users *users) { noh_log(NOH_INFO, "Reading users file: %s", file_path); if (!noh_file_exists(file_path)) return true; diff --git a/src/users.h b/src/users.h new file mode 100644 index 0000000..59f383b --- /dev/null +++ b/src/users.h @@ -0,0 +1,26 @@ +#ifndef USERS_H +#define USERS_H + +#include "noh.h" + +typedef struct { + Noh_String_View username; + Noh_String_View hash; +} User; + +typedef struct { + User *elems; + size_t count; + size_t capacity; +} Users; + +Noh_String hash_password(Noh_Arena *temp, Noh_String_View username, Noh_String_View password); + +// Reads the users file into users_file. Free this string when the data is no longer needed. +// Note that the string needs to be initialized and empty before calling this function. +// The users struct will contain string views referencing the data in this string. +bool read_users_file(char *file_path, Noh_String *users_file, Users *users); + +bool write_users_file(char *file_path, Users *users, User *user_to_skip); + +#endif // USERS_H