Finish user creation
authorChip Black <bytex64@bytex64.net>
Tue, 21 Dec 2010 11:01:27 +0000 (05:01 -0600)
committerChip Black <bytex64@bytex64.net>
Tue, 21 Dec 2010 11:01:27 +0000 (05:01 -0600)
Makefile
auth.c [new file with mode: 0644]
auth.h [new file with mode: 0644]
http_blerg.c

index 709b6b9..761b2bb 100644 (file)
--- 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 (file)
index 0000000..6ce78b2
--- /dev/null
+++ b/auth.c
@@ -0,0 +1,40 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdio.h>
+#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 (file)
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
index f8513c5..6a6de82 100644 (file)
@@ -4,6 +4,7 @@
 #include <microhttpd.h>
 #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 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
@@ -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);