Finish user creation
[blerg.git] / http_blerg.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <microhttpd.h>
5 #include "database.h"
6 #include "tags.h"
7 #include "auth.h"
8
9 #define URL_INFO_AUTHOR 0x1
10 #define URL_INFO_RECORD 0x2
11 #define DERP "DERP DERP DERP"
12
13 struct create_state {
14         struct MHD_PostProcessor *pp;
15         char username[33];
16         char password[33];
17 };
18
19 struct MHD_Response *response_404;
20 struct MHD_Response *response_501;
21 struct MHD_Response *response_JSON_Success;
22 struct MHD_Response *response_JSON_Failure;
23
24 void init_responses() {
25 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
26         response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
27
28 #define CONTENT_501 "<html><head><title>501 Not Implemented</title></head><body><h1>501 Not Implemented</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
29         response_501 = MHD_create_response_from_data(strlen(CONTENT_501), CONTENT_501, MHD_NO, MHD_NO);
30
31 #define JSON_SUCCESS "{status: \"success\"}"
32         response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
33
34 #define JSON_FAILURE "{status: \"failure\"}"
35         response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
36 }
37
38 int respond_404(struct MHD_Connection *connection) {
39         return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
40 }
41
42 int respond_501(struct MHD_Connection *connection) {
43         return MHD_queue_response(connection, MHD_HTTP_NOT_IMPLEMENTED, response_501);
44 }
45
46 int respond_JSON_Success(struct MHD_Connection *connection) {
47         return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
48 }
49
50 int respond_JSON_Failure(struct MHD_Connection *connection) {
51         return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
52 }
53
54 int parse_url_info(const char *url, char *author, uint64_t *record) {
55         const char *c;
56         int ret = 0;
57         int len;
58
59         c = strchr(url, '/');
60         if (c == NULL) {
61                 len = strlen(url);
62         } else {
63                 len = c - url;
64         }
65         if (len == 0)
66                 return 0;
67         memcpy(author, url, len);
68         author[len] = 0;
69         ret |= URL_INFO_AUTHOR;
70
71         if (c != NULL && c[1] != 0) {
72                 *record = strtoull(c + 1, NULL, 10);
73                 ret |= URL_INFO_RECORD;
74         }
75
76         return ret;
77 }
78
79 int POST_create_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) {
80         struct create_state *cs = cls;
81
82         if (strncmp(key, "username", 8) == 0) {
83                 if (size > 32) size = 32;
84                 memcpy(cs->username, data, size);
85                 cs->username[size] = 0;
86         } else if (strncmp(key, "password", 8) == 0) {
87                 if (size > 32) size = 32;
88                 memcpy(cs->password, data, size);
89                 cs->password[size] = 0;
90         }
91 }
92
93 static int
94 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
95           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
96         struct MHD_Response *response;
97         int ret, len;
98         char author[33];
99         uint64_t record;
100         char *data;
101
102         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
103                 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
104                         return MHD_NO;
105                 if (url[4] != '/')
106                         return respond_404(connection);
107                 ret = parse_url_info(url + 5, author, &record);
108                 if ((ret & URL_INFO_AUTHOR) == 0)
109                         return respond_404(connection);
110
111                 if (!blerg_exists(author))
112                         return respond_404(connection);
113
114                 if (ret & URL_INFO_RECORD) {
115                         if (*ptr == NULL) {
116                                 *ptr = (void *) 1;
117                                 return MHD_YES;
118                         } else {
119                                 *ptr == NULL;
120
121                                 struct blerg *b = blerg_open(author);
122                                 ret = blerg_fetch(b, record, &data, &len);
123                                 blerg_close(b);
124
125                                 if (ret == 0) {
126                                         return respond_404(connection);
127                                 } else {
128                                         response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
129                                         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
130                                 }
131                                 MHD_destroy_response(response);
132                                 return ret;
133                         }
134                 } else {
135                         if (*ptr == NULL) {
136                                 *ptr = (void*) 1;
137                                 return MHD_YES;
138                         } else {
139                                 *ptr == NULL;
140                                 response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
141                                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
142                                 MHD_destroy_response(response);
143                                 return ret;
144                         }
145                 }
146         } else if (strncmp(url, "/put", 4) == 0) {
147                 char *username;
148                 const char *password = "testpass";
149                 const char *realm = "Blerg Post";
150
151 #define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
152 #define DENIED "DENIED, MOTHERFUCKER"
153 #define PAGE "DERP DERP AUTHENTICATED DERP"
154
155                 if (*ptr == NULL) {
156                         *ptr = (void *) 1;
157
158                         username = MHD_digest_auth_get_username(connection);
159                         if (username == NULL) {
160                                 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);  
161                                 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO);    
162                                 MHD_destroy_response(response);  
163                                 return ret;
164                         }
165
166                         printf("username: %s\n", username);
167
168                         ret = MHD_digest_auth_check(connection, realm, username, password, 300);
169                         free(username);
170
171                         if (ret == MHD_INVALID_NONCE || ret == MHD_NO) {
172                                 response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);  
173                                 ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response,
174                                                                    (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);       
175                                 MHD_destroy_response(response);  
176                                 return ret;
177                         }
178                 }
179
180                 *ptr = NULL;
181
182                 if (url[4] != '/')
183                         return respond_404(connection);
184                 ret = parse_url_info(url + 5, author, &record);
185                 if ((ret & URL_INFO_AUTHOR) == 0)
186                         return respond_404(connection);
187
188                 response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO);
189                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);  
190                 MHD_destroy_response(response);
191                 return ret;
192         } else if (strncmp(url, "/create", 8) == 0) {
193                 struct create_state *cs = (struct create_state *) *ptr;
194
195                 if (cs == NULL) {
196                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
197                                 return respond_501(connection);
198
199                         struct create_state *cs = malloc(sizeof(struct create_state));
200                         cs->username[0] = cs->password[0] = 0;
201                         cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
202                         *ptr = cs;
203                         return MHD_YES;
204                 }
205
206                 if (*upload_data_size) {
207                         MHD_post_process(cs->pp, upload_data, *upload_data_size);
208                         *upload_data_size = 0;
209                         return MHD_YES;
210                 } else {
211                         if (cs->username[0] == 0 || cs->password[0] == 0)
212                                 return respond_JSON_Failure(connection);
213
214                         if (blerg_exists(cs->username))
215                                 return respond_JSON_Failure(connection);
216
217                         struct blerg *b = blerg_open(cs->username);
218                         blerg_close(b);
219                         auth_set_password(cs->username, cs->password);
220
221                         MHD_destroy_post_processor(cs->pp);
222                         free(cs);
223                         return respond_JSON_Success(connection);
224                 }
225         } else {
226                 return respond_404(connection);
227         }
228 }
229
230
231 int main(int argc, char *argv[]) {
232         struct MHD_Daemon *daemon;
233         fd_set rs, ws, es;
234         int max;
235
236         init_responses();
237
238         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
239         if (daemon == NULL) {
240                 fprintf(stderr, "Could not start web server\n");
241                 return 1;
242         }
243
244         while (1) {
245                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
246                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
247                         fprintf(stderr, "Fatal error getting fd sets\n");
248                         break;
249                 }
250                 select(max + 1, &rs, &ws, &es, NULL);
251                 MHD_run(daemon);
252         }
253         MHD_stop_daemon(daemon);
254         return 0;
255 }