Finish authenticated API endpoints on cgi_blerg
[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                 if (*ptr == NULL) {
342                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
343                                 return respond_405(connection);
344
345                         if (url[4] == '/')
346                                 return respond_404(connection);
347
348                         *ptr = (void *) 1;
349
350                         struct put_state *ps = malloc(sizeof(struct put_state));
351                         ps->data = NULL;
352                         ps->data_size = 0;
353                         ps->pp = MHD_create_post_processor(connection, 16384, &POST_put_iterator, ps);
354                         ps->username[0] = 0;
355                         *ptr = ps;
356                         return MHD_YES;
357                 }
358
359                 if (*upload_data_size) {
360                         MHD_post_process(ps->pp, upload_data, *upload_data_size);
361                         *upload_data_size = 0;
362                         return MHD_YES;
363                 }
364
365                 if (ps->data == NULL || ps->data_size == 0 || ps->username[0] == 0)
366                         return respond_JSON_Failure(connection);
367
368                 const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
369                 if (!auth_check_token(ps->username, given_token))
370                         return respond_JSON_Failure(connection);
371
372                 struct blerg *b = blerg_open(ps->username);
373                 if (b == NULL)
374                         return respond_JSON_Failure(connection);
375                 ret = blerg_store(b, ps->data, ps->data_size);
376                 blerg_close(b);
377                 if (ret == -1)
378                         return respond_JSON_Failure(connection);
379
380                 MHD_destroy_post_processor(ps->pp);
381                 free(ps->data);
382                 free(ps);
383                 *ptr = NULL;
384
385                 return respond_JSON_Success(connection);
386         } else if (strncmp(url, "/info", 5) == 0) {
387                 if (*ptr == NULL) {
388                         *ptr = (void *) 1;
389
390                         if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
391                                 return respond_405(connection);
392                         return MHD_YES;
393                 }
394
395
396                 if (url[5] != '/')
397                         return respond_404(connection);
398
399                 ret = parse_url_info(url + 6, &info);
400                 if ((ret & URL_INFO_AUTHOR) == 0)
401                         return respond_404(connection);
402
403                 if (!blerg_exists(info.author))
404                         return respond_404(connection);
405
406                 *ptr == NULL;
407
408                 struct blerg *b = blerg_open(info.author);
409                 uint64_t record_count = blerg_get_record_count(b);
410                 blerg_close(b);
411
412                 char number[21];
413                 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
414                 yajl_gen_map_open(g);
415                 yajl_gen_string(g, "record_count", 12);
416                 snprintf(number, 21, "%llu", record_count);
417                 yajl_gen_string(g, number, strlen(number));
418                 yajl_gen_map_close(g);
419
420                 const unsigned char *ybuf;
421                 yajl_gen_get_buf(g, &ybuf, &len);
422
423                 response = MHD_create_response_from_data(len, (void *)ybuf, MHD_NO, MHD_YES);
424                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
425                 MHD_destroy_response(response);
426
427                 yajl_gen_free(g);
428
429                 return ret;
430         } else if (strncmp(url, "/create", 8) == 0) {
431                 struct auth_state *as = (struct auth_state *) *ptr;
432
433                 if (as == NULL) {
434                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
435                                 return respond_405(connection);
436
437                         struct auth_state *as = malloc(sizeof(struct auth_state));
438                         as->username[0] = as->password[0] = 0;
439                         as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
440                         *ptr = as;
441                         return MHD_YES;
442                 }
443
444                 if (*upload_data_size) {
445                         MHD_post_process(as->pp, upload_data, *upload_data_size);
446                         *upload_data_size = 0;
447                         return MHD_YES;
448                 }
449
450                 if (as->username[0] == 0 || as->password[0] == 0)
451                         return respond_JSON_Failure(connection);
452
453                 if (blerg_exists(as->username))
454                         return respond_JSON_Failure(connection);
455
456                 struct blerg *b = blerg_open(as->username);
457                 blerg_close(b);
458                 auth_set_password(as->username, as->password);
459
460                 MHD_destroy_post_processor(as->pp);
461                 free(as);
462                 *ptr = NULL;
463
464                 return respond_JSON_Success(connection);
465         } else if (strncmp(url, "/login", 7) == 0) {
466                 struct auth_state *as = (struct auth_state *) *ptr;
467
468                 if (as == NULL) {
469                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
470                                 return respond_405(connection);
471
472                         struct auth_state *as = malloc(sizeof(struct auth_state));
473                         as->username[0] = as->password[0] = 0;
474                         as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
475                         *ptr = as;
476                         return MHD_YES;
477                 }
478
479                 if (*upload_data_size) {
480                         MHD_post_process(as->pp, upload_data, *upload_data_size);
481                         *upload_data_size = 0;
482                         return MHD_YES;
483                 }
484
485                 if (as->username[0] == 0 || as->password[0] == 0)
486                         return respond_JSON_Failure(connection);
487
488                 if (!auth_login(as->username, as->password))
489                         return respond_JSON_Failure(connection);
490
491                 response = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
492
493                 char *token = auth_get_token(as->username);
494                 data = malloc(512);
495                 snprintf(data, 512, "auth=%s", token);
496                 MHD_add_response_header(response, "Set-Cookie", data);
497                 free(token);
498                 free(data);
499
500                 MHD_destroy_post_processor(as->pp);
501                 free(as);
502                 *ptr = NULL;
503
504                 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
505                 MHD_destroy_response(response);
506
507                 return ret;
508         } else if (strncmp(url, "/logout", 8) == 0) {
509                 struct auth_state *as = (struct auth_state *) *ptr;
510
511                 if (as == NULL) {
512                         if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
513                                 return respond_405(connection);
514
515                         struct auth_state *as = malloc(sizeof(struct auth_state));
516                         as->username[0] = as->password[0] = 0;
517                         as->pp = MHD_create_post_processor(connection, 1024, &POST_auth_iterator, as);
518                         *ptr = as;
519                         return MHD_YES;
520                 }
521
522                 if (*upload_data_size) {
523                         MHD_post_process(as->pp, upload_data, *upload_data_size);
524                         *upload_data_size = 0;
525                         return MHD_YES;
526                 }
527
528                 const char *given_token = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "auth");
529                 if (auth_check_token(as->username, given_token)) {
530                         auth_logout(as->username);
531                         return respond_JSON_Success(connection);
532                 } else {
533                         return respond_JSON_Failure(connection);
534                 }
535         } else {
536                 return respond_404(connection);
537         }
538 }
539
540
541 int main(int argc, char *argv[]) {
542         struct MHD_Daemon *daemon;
543         fd_set rs, ws, es;
544         int max;
545
546         init_responses();
547
548         daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
549         if (daemon == NULL) {
550                 fprintf(stderr, "Could not start web server\n");
551                 return 1;
552         }
553
554         while (1) {
555                 FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
556                 if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
557                         fprintf(stderr, "Fatal error getting fd sets\n");
558                         break;
559                 }
560                 select(max + 1, &rs, &ws, &es, NULL);
561                 MHD_run(daemon);
562         }
563         MHD_stop_daemon(daemon);
564         return 0;
565 }