Pull out libusers
This commit is contained in:
16
Makefile
16
Makefile
@@ -3,15 +3,15 @@ CFLAGS=-Wall -Wextra -ggdb
|
|||||||
LIBWS=./vendor/libmicrohttpd/
|
LIBWS=./vendor/libmicrohttpd/
|
||||||
LIBSHA=./vendor/sha256/
|
LIBSHA=./vendor/sha256/
|
||||||
|
|
||||||
SHARED_LIB_FILES=output/libpath.a output/libnoh.a output/libsha.a
|
SHARED_LIB_FILES=output/libpath.a output/libnoh.a output/libusers.a output/libsha.a
|
||||||
SHARED_LIBS=-L./output -lsha -lpath -lnoh
|
SHARED_LIBS=-L./output -lpath -lnoh -lusers -lsha
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
SERVER_LDFLAGS=-lm -luuid -L $(LIBWS)lib -lmicrohttpd $(SHARED_LIBS)
|
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/id_store.c src/library.c \
|
||||||
src/opds_handler.c src/get_handler.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_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
|
userctl: output/userctl
|
||||||
output/userctl: $(USERCTL_SRC) $(SHARED_LIB_FILES) | output
|
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
|
output/libsha.a: $(LIBSHA)sha256.c $(LIBSHA)sha256.h | output
|
||||||
gcc $(CFLAGS) -c -o output/sha.o $(LIBSHA)sha256.c
|
gcc $(CFLAGS) -c -o output/sha.o $(LIBSHA)sha256.c
|
||||||
ar rcsD output/libsha.a output/sha.o
|
ar rcsD output/libsha.a output/sha.o
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
#include <microhttpd.h>
|
#include <microhttpd.h>
|
||||||
#include <sha256.h>
|
|
||||||
#include <uuid/uuid.h>
|
#include <uuid/uuid.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
|
#include "users.h"
|
||||||
#include "noh.h"
|
#include "noh.h"
|
||||||
|
|
||||||
Noh_Arena temp;
|
Noh_Arena temp;
|
||||||
|
|
||||||
#include "users.c"
|
|
||||||
|
|
||||||
#define SV Noh_String_View
|
#define SV Noh_String_View
|
||||||
#define sv noh_sv_from_cstr
|
#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.
|
// Hash the provided password.
|
||||||
hashed = hash_password(provided_username, provided_password);
|
hashed = hash_password(&temp, provided_username, provided_password);
|
||||||
|
|
||||||
// Check the hash.
|
// Check the hash.
|
||||||
if (!noh_sv_eq(noh_sv_from_string(hashed), user->hash)) {
|
if (!noh_sv_eq(noh_sv_from_string(hashed), user->hash)) {
|
||||||
|
|||||||
@@ -2,12 +2,11 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
|
#include "users.h"
|
||||||
#include "noh.h"
|
#include "noh.h"
|
||||||
|
|
||||||
Noh_Arena temp;
|
Noh_Arena temp;
|
||||||
|
|
||||||
#include "users.c"
|
|
||||||
|
|
||||||
#define SV Noh_String_View
|
#define SV Noh_String_View
|
||||||
#define sv noh_sv_from_cstr
|
#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);
|
if (!read_users_file(users_file_path.elems, &users_file, &users)) noh_return_defer(false);
|
||||||
|
|
||||||
// Hash the password.
|
// Hash the password.
|
||||||
hashed = hash_password(username, password);
|
hashed = hash_password(&temp, username, password);
|
||||||
|
|
||||||
// Find the user in the map of users.
|
// Find the user in the map of users.
|
||||||
for (size_t i = 0; i < users.count; i++) {
|
for (size_t i = 0; i < users.count; i++) {
|
||||||
|
|||||||
24
src/users.c
24
src/users.c
@@ -1,17 +1,8 @@
|
|||||||
|
#include "users.h"
|
||||||
|
|
||||||
#include <sha256.h>
|
#include <sha256.h>
|
||||||
|
|
||||||
typedef struct {
|
Noh_String hash_password(Noh_Arena *temp, Noh_String_View username, Noh_String_View password) {
|
||||||
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) {
|
|
||||||
// Build the sha input string.
|
// Build the sha input string.
|
||||||
Noh_String input = {0};
|
Noh_String input = {0};
|
||||||
noh_string_append_sv(&input, username);
|
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);
|
sha256_final(&ctx, hash);
|
||||||
|
|
||||||
// Build the result string.
|
// Build the result string.
|
||||||
noh_arena_save(&temp);
|
noh_arena_save(temp);
|
||||||
Noh_String result = {0};
|
Noh_String result = {0};
|
||||||
for (int i = 0; i < 32; i++) {
|
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;
|
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) {
|
bool read_users_file(char *file_path, Noh_String *users_file, Users *users) {
|
||||||
noh_log(NOH_INFO, "Reading users file: %s", file_path);
|
noh_log(NOH_INFO, "Reading users file: %s", file_path);
|
||||||
if (!noh_file_exists(file_path)) return true;
|
if (!noh_file_exists(file_path)) return true;
|
||||||
|
|||||||
26
src/users.h
Normal file
26
src/users.h
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user