From 7bf6bc9385adff81f0f66962630bdd60572d5c57 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Fri, 9 Oct 2020 18:15:36 +0200 Subject: cleanup --- .gitignore | 1 + bcrypt-test.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index cf17be8..b64b38c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ lua-bcrypt *.o bcrypt-test tags +.tags user.db diff --git a/bcrypt-test.c b/bcrypt-test.c index 2bbdfe7..1df296d 100644 --- a/bcrypt-test.c +++ b/bcrypt-test.c @@ -8,6 +8,7 @@ #define USER_DATABASE "./user.db" #define SECURITY_LEVEL 12 +#define SEP '|' static bool valid_char(int ch) @@ -44,7 +45,7 @@ create_user(const char *username, const char *password) return false; } - fprintf(db, "%s|%s\n", username, hash); + fprintf(db, "%s%c%s\n", username, SEP, hash); fclose(db); return true; @@ -53,13 +54,13 @@ create_user(const char *username, const char *password) /** * Check is a valid username/password exists in the user-database * - * \param[in] username - * \param[in] password + * username A username + * password A strong password * - * \return true, if the user was successfully authenticated, otherwise false + * @return true, if the user was successfully authenticated, otherwise false */ bool -check_user(const char *username, const char *password) +check_user(const char *const username, const char *const password) { FILE *db = fopen(USER_DATABASE, "r"); if ( db == NULL ) { @@ -77,7 +78,7 @@ check_user(const char *username, const char *password) continue; } - if ( line[username_length] != '|' ) { + if ( line[username_length] != SEP ) { continue; } @@ -94,6 +95,45 @@ check_user(const char *username, const char *password) return found; } +/** + * Check if a valid username already exists in the user-database + * + * \param[in] username to be checked + * + * \return true, if the user was successfully authenticated, otherwise false + */ +bool +check_user_if_exists(const char *username) +{ + FILE *db = fopen(USER_DATABASE, "r"); + if ( db == NULL ) { + return false; + } + + const size_t username_length = strlen(username); + char line[256]; + + bool found = false; + while ( !found && fgets(line, sizeof line, db) ) { + line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) + + if ( strncmp(line, username, username_length) != 0 ) { + continue; + } + + if ( line[username_length] != '|' ) { + continue; + } + + // User successfully authenticated! + found = true; + } + + fclose(db); + + return found; +} + int main(void) { @@ -103,3 +143,6 @@ main(void) return EXIT_SUCCESS; } + + + -- cgit v1.3