--- /dev/null
+#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;
+}
#include <microhttpd.h>
#include "database.h"
#include "tags.h"
+#include "auth.h"
#define URL_INFO_AUTHOR 0x1
#define URL_INFO_RECORD 0x2
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>"
#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) {
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;
*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);