Add cookie auth to /put
[blerg.git] / http / http_blerg.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <microhttpd.h>
5 #include <yajl/yajl_gen.h>
6 #include "database.h"
7 #include "tags.h"
8 #include "auth.h"
9 #include "canned_responses.h"
10 #include "app.h"
11 #include "config.h"
12
13 yajl_gen_config yajl_c = { 0, 0 };
14
15 struct auth_state {
16         struct MHD_PostProcessor *pp;
17         char username[33];
18         char password[33];
19 };
20
21 struct put_state {
22         struct MHD_PostProcessor *pp;
23         char username[33];
24         char *data;
25         int data_size;
26 };
27
28 struct get_state {
29         struct blerg *b;
30         yajl_gen g;
31         unsigned int yoff;
32         uint64_t *entries;
33         uint64_t i;
34         int done;
35 };
36
37 struct tag_state {
38         yajl_gen g;
39         unsigned int yoff;
40         struct tag *results;
41         uint64_t i;
42         int done;
43 };
44
45 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
46         struct get_state *gs = cls;
47         const unsigned char *ybuf;
48         char *data;
49         char number[21];
50         unsigned int len;
51
52         if (gs->yoff > 0) {
53                 yajl_gen_get_buf(gs->g, &ybuf, &len);
54                 size_t bytes_remaining = len - gs->yoff;
55                 if (bytes_remaining > max) {
56                         memcpy(buf, ybuf + gs->yoff, max);
57                         gs->yoff += max;
58                         return max;
59                 } else {
60                         memcpy(buf, ybuf + gs->yoff, bytes_remaining);
61                         gs->yoff = 0;
62                         yajl_gen_clear(gs->g);
63                         return bytes_remaining;
64                 }
65         }
66
67         if (gs->done)
68                 return -1;
69
70         if (pos == 0) { /* Start iterating */
71                 yajl_gen_array_open(gs->g);
72         }
73
74         /* Snarf one record */
75         json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]);
76
77         if (gs->i == 0) {
78                 yajl_gen_array_close(gs->g);
79                 gs->done = 1;
80         }
81         gs->i--;
82
83
84         yajl_gen_get_buf(gs->g, &ybuf, &len);
85         if (len > max) {
86                 memcpy(buf, ybuf, max);
87                 gs->yoff = max;
88                 return max;
89         } else {
90                 memcpy(buf, ybuf, len);
91                 yajl_gen_clear(gs->g);
92                 return len;
93         }
94 }
95
96 void GET_generate_list_free(void *cls) {
97         struct get_state *gs = cls;
98
99         blerg_close(gs->b);
100         yajl_gen_free(gs->g);
101         free(gs->entries);
102         free(gs);
103 }
104
105 ssize_t GET_generate_taglist(void *cls, uint64_t pos, char *buf, size_t max) {
106         struct tag_state *ts = cls;
107         struct blerg *b;
108         const unsigned char *ybuf;
109         unsigned int len;
110
111         if (ts->yoff > 0) {
112                 yajl_gen_get_buf(ts->g, &ybuf, &len);
113                 size_t bytes_remaining = len - ts->yoff;
114                 if (bytes_remaining > max) {
115                         memcpy(buf, ybuf + ts->yoff, max);
116                         ts->yoff += max;
117                         return max;
118                 } else {
119                         memcpy(buf, ybuf + ts->yoff, bytes_remaining);
120                         ts->yoff = 0;
121                         yajl_gen_clear(ts->g);
122                         return bytes_remaining;
123                 }
124         }
125
126         if (ts->done)
127                 return -1;
128
129         if (pos == 0) { /* Start iterating */
130                 yajl_gen_array_open(ts->g);
131         }
132
133         /* Snarf one record */
134         b = blerg_open(ts->results[ts->i].author);
135         if (b != NULL) {
136                 json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record);
137                 blerg_close(b);
138         }
139
140         if (ts->i == 0) {
141                 yajl_gen_array_close(ts->g);
142                 ts->done = 1;
143         }
144
145         ts->i--;
146
147         yajl_gen_get_buf(ts->g, &ybuf, &len);
148         if (len > max) {
149                 memcpy(buf, ybuf, max);
150                 ts->yoff = max;
151                 return max;
152         } else {
153                 memcpy(buf, ybuf, len);
154                 yajl_gen_clear(ts->g);
155                 return len;
156         }
157 }
158
159 void GET_generate_taglist_free(void *cls) {
160         struct tag_state *ts = cls;
161
162         yajl_gen_free(ts->g);
163         free(ts->results);
164         free(ts);
165 }
166
167 int POST_auth_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) {
168         struct auth_state *as = cls;
169
170         if (strncmp(key, "username", 9) == 0) {
171                 if (size > 32) size = 32;
172                 memcpy(as->username, data, size);
173                 as->username[size] = 0;
174         } else if (strncmp(key, "password", 9) == 0) {
175                 if (size > 32) size = 32;
176                 memcpy(as->password, data, size);
177                 as->password[size] = 0;
178         }
179
180         return MHD_YES;
181 }
182
183 int POST_put_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) {
184         struct put_state *ps = cls;
185
186         if (strncmp(key, "data", 5) == 0) {
187                 if (ps->data == NULL) {
188                         ps->data_size = size;
189                         ps->data = malloc(size);
190                 } else {
191                         if (ps->data_size + size > MAX_RECORD_SIZE) {
192                                 size = MAX_RECORD_SIZE - ps->data_size;
193                         }
194                         ps->data_size += size;
195                         ps->data = realloc(ps->data, ps->data_size);
196                 }
197                 memcpy(ps->data + off, data, size);
198                 if (ps->data_size == MAX_RECORD_SIZE)
199                         return MHD_NO;
200         } else if (strncmp(key, "username", 9) == 0) {
201                 if (size > 32) size = 32;
202                 memcpy(ps->username, data, size);
203                 ps->username[size] = 0;
204         }
205
206         return MHD_YES;
207 }
208
209 struct MHD_Response *create_response_for_range(struct blerg *b, uint64_t from, uint64_t to) {
210         struct MHD_Response *response;
211         struct get_state *gs = malloc(sizeof(struct get_state));
212         gs->b = b;
213
214         uint64_t record_count = blerg_get_record_count(b);
215
216         if (from > to || from >= record_count || to >= record_count || to - from > 99) {
217                 blerg_close(b);
218                 free(gs);
219                 return NULL;
220         }
221
222         gs->entries = make_sequential_list(from, to);
223         gs->i = to - from;
224
225         gs->g = yajl_gen_alloc(&yajl_c, NULL);
226         gs->yoff = gs->done = 0;
227
228         response = MHD_create_response_from_callback(-1, 262144, &GET_generate_list, gs, &GET_generate_list_free);
229
230         return response;
231 }
232
233 struct MHD_Response *create_tag_response(struct tag *results, uint64_t len) {
234         struct tag_state *ts = malloc(sizeof(struct tag_state));
235         ts->g = yajl_gen_alloc(&yajl_c, NULL);
236         ts->results = results;
237         ts->i = len - 1;
238         ts->yoff = ts->done = 0;
239
240         return MHD_create_response_from_callback(-1, 262144, &GET_generate_taglist, ts, &GET_generate_taglist_free);
241 }
242
243 static int
244 ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
245           const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
246         struct MHD_Response *response;
247         int ret, len;
248         struct url_info info;
249         char *data;
250
251         if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
252                 if (*ptr == NULL) {
253                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
254                                 return respond_405(connection);
255
256                         *ptr = (void *) 1;
257                         return MHD_YES;
258                 }
259
260                 if (url[4] != '/')
261                         return respond_404(connection);
262
263                 ret = parse_url_info(url + 5, &info);
264                 if ((ret & URL_INFO_AUTHOR) == 0)
265                         return respond_404(connection);
266
267                 if (!blerg_exists(info.author))
268                         return respond_404(connection);
269
270                 *ptr == NULL;
271
272                 struct blerg *b = blerg_open(info.author);
273
274                 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
275                         response = create_response_for_range(b, info.record, info.record_to);
276                 } else if (ret & URL_INFO_RECORD) {
277                         ret = blerg_fetch(b, info.record, &data, &len);
278                         blerg_close(b);
279
280                         if (ret == 0)
281                                 return respond_404(connection);
282                         response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
283                 } else {
284                         uint64_t record_count, from, to;
285                         record_count = blerg_get_record_count(b);
286                         if (record_count == 0) {
287                                 blerg_close(b);
288                                 response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
289                         } else {
290                                 to = record_count - 1;
291                                 from = (record_count > 50 ? to - 49 : 0);
292                                 response = create_response_for_range(b, from, to);
293                         }
294                 }
295
296                 if (response == NULL) {
297                         blerg_close(b);
298                         return respond_JSON_Failure(connection);
299                 }
300
301                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
302                 MHD_destroy_response(response);
303                 return ret;
304         } else if (strncmp(url, "/tag", 4) == 0 && strlen(url) > 4) {
305                 if (*ptr == NULL) {
306                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
307                                 return respond_405(connection);
308
309                         *ptr = (void *) 1;
310                         return MHD_YES;
311                 }
312
313                 if (url[4] != '/')
314                         return respond_404(connection);
315
316                 ret = parse_url_info(url + 5, &info);
317                 if ((ret & URL_INFO_AUTHOR) == 0)
318                         return respond_404(connection);
319
320                 if (!tag_exists(info.author))
321                         return respond_404(connection);
322
323                 int recs = 50;
324                 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
325
326                 if (recs == 0) {
327                         response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO);
328                 } else {
329                         response = create_tag_response(taglist, recs);
330                 }
331
332                 if (response == NULL)
333                         return respond_JSON_Failure(connection);
334
335                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
336                 MHD_destroy_response(response);
337
338                 return ret;
339         } else if (strncmp(url, "/put", 4) == 0) {
340                 struct put_state *ps = (struct put_state *) *ptr;
341                 char *username;
342                 char password[33];
343
344                 if (*ptr == NULL) {
345                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
346                                 return respond_405(connection);
347
348                         if (url[4] == '/')
349                                 return respond_404(connection);
350
351                         *ptr = (void *) 1;
352
353                         struct put_state *ps = malloc(sizeof(struct put_state));
354                         ps->data = NULL;
355                         ps->data_size = 0;
356                         ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
357                         ps->username[0] = 0;
358                         *ptr = ps;
359                         return MHD_YES;
360                 }
361
362                 if (*upload_data_size) {
363                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
364                         *upload_data_size = 0;
365                         return MHD_YES;
366                 }
367
368                 if (ps->data == NULL || ps->data_size == 0 || ps->username[0] == 0)
369                         return respond_JSON_Failure(connection);
370
371                 const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
372                 if (!auth_check_token(ps->username, given_token))
373                         return respond_JSON_Failure(connection);
374
375                 struct blerg *b = blerg_open(ps->username);
376                 if (b == NULL)
377                         return respond_JSON_Failure(connection);
378                 if (blerg_store(b, ps->data, ps->data_size) == -1) {
379                         blerg_close(b);
380                         return respond_JSON_Failure(connection);
381                 }
382                 blerg_close(b);
383
384                 MHD_destroy_post_processor(ps->pp);
385                 free(ps->data);
386                 free(ps);
387                 *ptr = NULL;
388
389                 return respond_JSON_Success(connection);
390         } else if (strncmp(url, "/info", 5) == 0) {
391                 if (*ptr == NULL) {
392                         *ptr = (void *) 1;
393
394                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
395                                 return respond_405(connection);
396                         return MHD_YES;
397                 }
398
399
400                 if (url[5] != '/')
401                         return respond_404(connection);
402
403                 ret = parse_url_info(url + 6, &info);
404                 if ((ret & URL_INFO_AUTHOR) == 0)
405                         return respond_404(connection);
406
407                 if (!blerg_exists(info.author))
408                         return respond_404(connection);
409
410                 *ptr == NULL;
411
412                 struct blerg *b = blerg_open(info.author);
413                 uint64_t record_count = blerg_get_record_count(b);
414                 blerg_close(b);
415
416                 char number[21];
417                 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
418                 yajl_gen_map_open(g);
419                 yajl_gen_string(g, "record_count", 12);
420                 snprintf(number, 21, "%llu", record_count);
421                 yajl_gen_string(g, number, strlen(number));
422                 yajl_gen_map_close(g);
423
424                 const unsigned char *ybuf;
425                 yajl_gen_get_buf(g, &ybuf, &len);
426
427                 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
428                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
429                 MHD_destroy_response(response);
430
431                 yajl_gen_free(g);
432
433                 return ret;
434         } else if (strncmp(url, "/create", 8) == 0) {
435                 struct auth_state *as = (struct auth_state *) *ptr;
436
437                 if (as == NULL) {
438                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
439                                 return respond_405(connection);
440
441                         struct auth_state *as = malloc(sizeof(struct auth_state));
442                         as->username[0] = as->password[0] = 0;
443                         as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
444                         *ptr = as;
445                         return MHD_YES;
446                 }
447
448                 if (*upload_data_size) {
449                         MHD_post_process(as->pp, upload_data, *upload_data_size);
450                         *upload_data_size = 0;
451                         return MHD_YES;
452                 }
453
454                 if (as->username[0] == 0 || as->password[0] == 0)
455                         return respond_JSON_Failure(connection);
456
457                 if (blerg_exists(as->username))
458                         return respond_JSON_Failure(connection);
459
460                 struct blerg *b = blerg_open(as->username);
461                 blerg_close(b);
462                 auth_set_password(as->username, as->password);
463
464                 MHD_destroy_post_processor(as->pp);
465                 free(as);
466                 *ptr = NULL;
467
468                 return respond_JSON_Success(connection);
469         } else if (strncmp(url, "/login", 7) == 0) {
470                 struct auth_state *as = (struct auth_state *) *ptr;
471
472                 if (as == NULL) {
473                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
474                                 return respond_405(connection);
475
476                         struct auth_state *as = malloc(sizeof(struct auth_state));
477                         as->username[0] = as->password[0] = 0;
478                         as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
479                         *ptr = as;
480                         return MHD_YES;
481                 }
482
483                 if (*upload_data_size) {
484                         MHD_post_process(as->pp, upload_data, *upload_data_size);
485                         *upload_data_size = 0;
486                         return MHD_YES;
487                 }
488
489                 if (as->username[0] == 0 || as->password[0] == 0)
490                         return respond_JSON_Failure(connection);
491
492                 if (!auth_login(as->username, as->password))
493                         return respond_JSON_Failure(connection);
494
495                 response = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
496
497                 char *token = auth_get_token(as->username);
498                 data = malloc(512);
499                 snprintf(data, 512, "auth=%s", token);
500                 MHD_add_response_header(response, "Set-Cookie", data);
501                 free(token);
502                 free(data);
503
504                 MHD_destroy_post_processor(as->pp);
505                 free(as);
506                 *ptr = NULL;
507
508                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
509                 MHD_destroy_response(response);
510
511                 return ret;
512         } else if (strncmp(url, "/logout", 8) == 0) {
513                 struct auth_state *as = (struct auth_state *) *ptr;
514
515                 if (as == NULL) {
516                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
517                                 return respond_405(connection);
518
519                         struct auth_state *as = malloc(sizeof(struct auth_state));
520                         as->username[0] = as->password[0] = 0;
521                         as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
522                         *ptr = as;
523                         return MHD_YES;
524                 }
525
526                 if (*upload_data_size) {
527                         MHD_post_process(as->pp, upload_data, *upload_data_size);
528                         *upload_data_size = 0;
529                         return MHD_YES;
530                 }
531
532                 const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
533                 if (given_token != NULL && auth_check_token(as->username, given_token)) {
534                         auth_logout(as->username);
535                         return respond_JSON_Success(connection);
536                 } else {
537                         return respond_JSON_Failure(connection);
538                 }
539         } else {
540                 return respond_404(connection);
541         }
542 }
543
544
545 int main(int argc, char *argv[]) {
546         struct MHD_Daemon *daemon;
547         fd_set rs, ws, es;
548         int max;
549
550         init_responses();
551
552         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
553         if (daemon == NULL) {
554                 fprintf(stderr, "Could not start web server\n");
555                 return 1;
556         }
557
558         while (1) {
559                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
560                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
561                         fprintf(stderr, "Fatal error getting fd sets\n");
562                         break;
563                 }
564                 select(max + 1, &rs, &ws, &es, NULL);
565                 MHD_run(daemon);
566         }
567         MHD_stop_daemon(daemon);
568         return 0;
569 }