From c4025f5d80dc8f3f15ee6814ac689b9eee1dba10 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Tue, 21 Dec 2010 05:01:27 -0600 Subject: [PATCH] Finish user creation --- Makefile | 2 +- auth.c | 40 ++++++++++++++++++++++++++++++++++++++++ auth.h | 7 +++++++ http_blerg.c | 18 ++++++++++++++---- 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 auth.c create mode 100644 auth.h diff --git a/Makefile b/Makefile index 709b6b9..761b2bb 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ LIBS = targets = blerg.a blergtool http_blerg blerg_a_objects = database.o tags.o blergtool_objects = blergtool.o blerg.a -http_blerg_objects = http_blerg.o blerg.a +http_blerg_objects = http_blerg.o auth.o blerg.a all: $(targets) diff --git a/auth.c b/auth.c new file mode 100644 index 0000000..6ce78b2 --- /dev/null +++ b/auth.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include "config.h" +#include "auth.h" + +int auth_set_password(const char *username, const char *password) { + char filename[512]; + int fd; + + if (!blerg_exists(username)) + return 0; + + snprintf(filename, 512, "%s/%s/password", DATA_PATH, username); + fd = open(filename, O_WRONLY | O_CREAT, 0600); + write(fd, password, strlen(password)); + close(fd); + + return 1; +} + +int auth_check_password(const char *username, const char *password) { + char filename[512]; + char epw[33]; + int fd, len; + + sprintf(filename, "%s/%s/password", DATA_PATH, username); + fd = open(filename, O_RDONLY); + if (fd == -1) + return 0; + len = read(fd, password, 32); + close(fd); + + if (strncmp(password, epw, 32) == 0) + return 1; + else + return 0; +} diff --git a/auth.h b/auth.h new file mode 100644 index 0000000..4d551f6 --- /dev/null +++ b/auth.h @@ -0,0 +1,7 @@ +#ifndef _AUTH_H +#define _AUTH_H + +int auth_set_password(const char *, const char *); +int auth_check_password(const char *, const char *); + +#endif //_AUTH_H diff --git a/http_blerg.c b/http_blerg.c index f8513c5..6a6de82 100644 --- a/http_blerg.c +++ b/http_blerg.c @@ -4,6 +4,7 @@ #include #include "database.h" #include "tags.h" +#include "auth.h" #define URL_INFO_AUTHOR 0x1 #define URL_INFO_RECORD 0x2 @@ -18,6 +19,7 @@ struct create_state { struct MHD_Response *response_404; struct MHD_Response *response_501; struct MHD_Response *response_JSON_Success; +struct MHD_Response *response_JSON_Failure; void init_responses() { #define CONTENT_404 "404 Not Found

404 Not Found

I couldn't find that." @@ -28,6 +30,9 @@ void init_responses() { #define JSON_SUCCESS "{status: \"success\"}" response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO); + +#define JSON_FAILURE "{status: \"failure\"}" + response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO); } int respond_404(struct MHD_Connection *connection) { @@ -42,6 +47,10 @@ int respond_JSON_Success(struct MHD_Connection *connection) { return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success); } +int respond_JSON_Failure(struct MHD_Connection *connection) { + return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure); +} + int parse_url_info(const char *url, char *author, uint64_t *record) { const char *c; int ret = 0; @@ -199,14 +208,15 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c *upload_data_size = 0; return MHD_YES; } else { - printf("username: %s, password: %s\n", cs->username, cs->password); - if (cs->username[0] == 0 || cs->password[0] == 0) - return MHD_NO; // TODO: Give a better response + return respond_JSON_Failure(connection); + + if (blerg_exists(cs->username)) + return respond_JSON_Failure(connection); struct blerg *b = blerg_open(cs->username); blerg_close(b); - /* auth_set_password(cs->username, cs->password); */ + auth_set_password(cs->username, cs->password); MHD_destroy_post_processor(cs->pp); free(cs); -- 2.25.1