userscript: make "No. 123" links smarter
[rb-79.git] / rb79-server.c
blob4c6e4412ed59c09bb4eab51eb89bf7bc21613f9a
1 /*
2 * Copyright (c) 2017, De Rais <derais@cock.li>
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all
7 * copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
18 #include <limits.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
27 #include <fcgiapp.h>
29 #include "macros.h"
30 #include "rb79.h"
32 #include "config.h"
34 const char *program_name = "rb79-server";
36 /* Print out a page saying the request was malformed (400) */
37 static int report_bad_request(FCGX_Request *r, const char *reason)
39 if (!reason) {
40 reason = "That's not a real request. That's all we know.";
43 FCGX_FPrintF(r->out, BAD_REQUEST_FMT, reason);
45 return 0;
48 /* Print out a page saying they failed the CAPTCHA (403) */
49 static int report_bad_challenge(FCGX_Request *r)
51 FCGX_FPrintF(r->out, BAD_CHALLENGE_FMT);
53 return 0;
56 /* Print out a BANNED page (403) */
57 static int report_ban(FCGX_Request *r, char *ban_until, char *ban_reason)
59 FCGX_FPrintF(r->out, BAN_FMT, UBSAFES(ban_until), UBSAFES(ban_reason));
61 return 0;
64 /* Print out a BAD METHOD page (405) */
65 static int report_bad_method(FCGX_Request *r)
67 FCGX_FPrintF(r->out, BAD_METHOD_FMT);
69 return 0;
72 /* Print out a FILE TOO LARGE page (413) */
73 static int report_too_large(FCGX_Request *r, const char *large_thing)
75 FCGX_FPrintF(r->out, TOO_LARGE_FMT, UBSAFES(large_thing));
77 return 0;
80 /* Print out a page saying they're posting too fast (429) */
81 static int report_cooldown(FCGX_Request *r, char *cooldown_length)
83 FCGX_FPrintF(r->out, COOLDOWN_FMT, UBSAFES(cooldown_length));
85 return 0;
88 /* Print out an INTERNAL ERROR page (500) */
89 static int report_internal_error(FCGX_Request *r)
91 FCGX_FPrintF(r->out, INTERNAL_ERROR_FMT);
93 return 0;
96 /* Print out a POST SUCCESSFUL page (200) */
97 static int report_post_successful(FCGX_Request *r, const char *buf)
99 FCGX_FPrintF(r->out, POST_SUCCESSFUL_FMT, buf);
101 return 0;
104 /* Make sure every board has a page (really only for brand-new boards) */
105 static int board_pages_init(struct configuration *conf)
107 int ret = -1;
108 uintmax_t *thread_ids = 0;
109 size_t thread_ids_num = 0;
110 size_t board_pages_num = 0;
112 for (size_t j = 0; j < conf->boards_num; ++j) {
113 free(thread_ids);
114 thread_ids = 0;
116 if (lock_acquire(j) < 0) {
117 goto done;
120 if (db_cull_and_report_threads(j, &thread_ids, &thread_ids_num,
121 &board_pages_num) < 0) {
122 goto done;
125 if (wt_write_board(j, thread_ids, thread_ids_num,
126 board_pages_num) < 0) {
127 goto done;
130 lock_release(j);
133 ret = 0;
134 done:
135 free(thread_ids);
137 return ret;
140 /* Free what needs to be freed */
141 static void clean_post_cmd(struct post_cmd *p)
143 if (!p) {
144 return;
147 free(p->raw.action);
148 free(p->raw.board);
149 free(p->raw.thread);
150 free(p->raw.post);
151 free(p->raw.name);
152 free(p->raw.email);
153 free(p->raw.tripcode);
154 free(p->raw.subject);
155 free(p->raw.comment);
156 free(p->raw.file_name);
157 free(p->raw.file_contents);
158 free(p->raw.challenge_id);
159 free(p->raw.challenge_response);
160 free(p->prepared.name);
161 free(p->prepared.email);
162 free(p->prepared.tripcode);
163 free(p->prepared.subject);
164 free(p->prepared.comment);
165 free(p->prepared.ext);
166 free(p->prepared.file_name);
167 free(p->prepared.system_full_path);
168 free(p->prepared.system_thumb_path);
169 free(p->prepared.file_info);
170 free(p->scannable_comment);
171 free(p->position_map);
172 *p = (struct post_cmd) { 0 };
175 /* The bulk of work for processing a post */
176 static void handle_op_or_reply(struct configuration *conf, FCGX_Request *r,
177 struct post_cmd *pc, const char *ip, size_t
178 parent_thread)
180 char *buf = 0;
181 char *abs_file_path = 0;
182 size_t len = 0;
183 int our_fault = 0;
184 uintmax_t real_thread = 0;
185 int cooldown = 0;
186 int thread_dne = 0;
187 int thread_full = 0;
188 int thread_closed = 0;
189 const struct filetype *f;
190 size_t board_pages_num = 0;
191 uintmax_t *thread_ids = 0;
192 size_t thread_ids_num = 0;
193 uint_fast8_t need_to_unlock = 0;
195 if (!parent_thread &&
196 (!pc->raw.file_contents ||
197 !pc->raw.file_contents_len)) {
198 LOG("New thread, yet no file (400)");
199 report_bad_request(r, "New threads must have a file");
200 goto done;
203 /* pc comes in with a bunch of these lens set not-as-desired */
204 if (pc->raw.file_name_len > conf->max_text_len) {
205 LOG("File name length (%zu) larger than max (%zu) (413)",
206 pc->raw.file_name_len, conf->max_text_len);
207 report_too_large(r, "Filename");
208 goto done;
211 if (pc->raw.subject_len > conf->max_text_len) {
212 LOG("Subject length (%zu) larger than max (%zu) (413)",
213 pc->raw.subject_len, conf->max_text_len);
214 report_too_large(r, "Subject text");
215 goto done;
218 if (pc->raw.email_len > conf->max_text_len) {
219 LOG("Email length (%zu) larger than max (%zu) (413)",
220 pc->raw.email_len, conf->max_text_len);
221 report_too_large(r, "Email address");
222 goto done;
225 if (pc->raw.comment_len > conf->max_text_len) {
226 LOG("Comment length (%zu) larger than max (%zu) (413)",
227 pc->raw.comment_len, conf->max_text_len);
228 report_too_large(r, "Comment text");
229 goto done;
232 if (pc->raw.file_contents_len > conf->max_file_size) {
233 LOG("File size (%zu) larger than max (%zu) (413)",
234 pc->raw.file_contents_len, conf->max_file_size);
235 report_too_large(r, "File size");
236 goto done;
239 if (sf_check_mime_type(pc->raw.file_contents, pc->raw.file_contents_len,
240 &f) < 0) {
241 LOG("Bad MIME check (400)");
242 report_bad_request(r, "Unsupported file type");
243 goto done;
246 /* Calculate tripcodes before HTML-escaping everything */
247 if (tripcodes_calculate(pc) < 0) {
248 LOG("Error in tripcodes_calculate (500)");
249 report_internal_error(r);
250 goto done;
253 /* HTML-escape, wordfilter, linkify */
254 if (st_sanitize_text(pc, &our_fault) < 0) {
255 if (our_fault) {
256 LOG("Error in st_sanitize_text (500)");
257 report_internal_error(r);
258 goto done;
261 LOG("Bad text (400)");
262 report_bad_request(r, "Disallowed text");
263 goto done;
266 cooldown = pc->prepared.comment_len ?
267 conf->boards[pc->board_idx].text_cooldown :
268 conf->boards[pc->board_idx].blank_cooldown;
271 * From now on, everything must be under lock, since we
272 * could be touching the filesystem. Strictly, we don't
273 * need to worry about locking for db-only operations, so
274 * this could be delayed a bit.
276 if (lock_acquire(pc->board_idx) < 0) {
277 LOG("Error in lock_acquire (500)");
278 report_internal_error(r);
279 goto done;
282 need_to_unlock = 1;
284 if (db_insert_post(ip, parent_thread, cooldown, pc, &thread_dne,
285 &thread_closed, &thread_full, &pc->prepared.id) <
286 0) {
287 LOG("Error in insert_post (500)");
288 report_internal_error(r);
289 goto done;
292 LOG("Post %zu on board /%s/", pc->prepared.id,
293 conf->boards[pc->board_idx].name);
295 if (thread_dne) {
296 LOG("Thread %zu does not exist (400)", (size_t) 0);
297 report_bad_request(r, "Thread does not exist");
298 goto done;
301 if (thread_full) {
302 LOG("Thread %zu is full (400)", (size_t) 0);
303 report_bad_request(r, "Thread is full");
304 goto done;
307 if (thread_closed) {
308 LOG("Thread %zu is closed (400)", (size_t) 0);
309 report_bad_request(r, "Thread is closed");
310 goto done;
313 /* Make thumbnails and insert them */
314 if (f) {
315 if (sf_install_files(pc->board_idx, pc->raw.file_contents,
316 pc->raw.file_contents_len,
317 &pc->prepared.now, f, &abs_file_path,
318 &pc->prepared.system_full_path,
319 &pc->prepared.system_full_path_len,
320 &pc->prepared.system_thumb_path,
321 &pc->prepared.system_thumb_path_len,
322 &our_fault) < 0) {
323 if (our_fault) {
324 LOG("Error in sf_install_files (500)");
325 report_internal_error(r);
326 goto done;
329 LOG("Couldn't install files (400)");
330 report_bad_request(r, "Bad file upload");
331 goto done;
334 /* ... and now that they're inserted, describe them ... */
335 if (sf_describe_file(f->mime_type, abs_file_path,
336 &pc->prepared.file_info,
337 &pc->prepared.file_info_len) < 0) {
338 LOG("Error in sf_describe_file (500)");
339 report_internal_error(r);
340 goto done;
343 /* ... and alert the db about that description. */
344 if (db_update_file_info(pc->board_idx, pc->prepared.id,
345 pc->prepared.file_info,
346 pc->prepared.file_info_len,
347 pc->prepared.system_full_path,
348 pc->prepared.system_full_path_len,
349 pc->prepared.system_thumb_path,
350 pc->prepared.system_thumb_path_len) <
351 0) {
352 LOG("Error in db_update_post_description (500)");
353 report_internal_error(r);
354 goto done;
359 * We're about ready to write out the threads, boards, etc.
360 * Therefore, we must now check for thread culling, and
361 * also calculate how many board pages we need.
363 if (db_cull_and_report_threads(pc->board_idx, &thread_ids,
364 &thread_ids_num, &board_pages_num) < 0) {
365 LOG("Error in db_cull_and_report_threads (500)");
366 report_internal_error(r);
367 goto done;
370 real_thread = parent_thread ? parent_thread : pc->prepared.id;
372 if (wt_write_thread(pc->board_idx, real_thread) < 0) {
373 LOG("Error in wt_write_thread (500)");
374 report_internal_error(r);
375 goto done;
378 if (wt_write_board(pc->board_idx, thread_ids, thread_ids_num,
379 board_pages_num) < 0) {
380 LOG("Error in wt_write_board (500)");
381 report_internal_error(r);
382 goto done;
385 len = snprintf(0, 0, "/%s/res/%s", pc->raw.board, pc->raw.thread);
387 if (len + 1 < len) {
388 ERROR_MESSAGE("overflow");
389 report_internal_error(r);
390 goto done;
393 if (!(buf = malloc(len + 1))) {
394 PERROR_MESSAGE("malloc");
395 report_internal_error(r);
396 goto done;
399 if (pc->raw.email &&
400 !strcmp(pc->raw.email, "noko")) {
401 sprintf(buf, "/%s/res/%s", pc->raw.board, pc->raw.thread);
402 } else {
403 sprintf(buf, "/%s", pc->raw.board);
406 report_post_successful(r, buf);
407 done:
409 if (need_to_unlock) {
410 lock_release(pc->board_idx);
413 free(buf);
414 free(abs_file_path);
415 free(thread_ids);
418 /* Rebuild every thread and every board */
419 static void handle_rebuild (struct configuration *conf, FCGX_Request *r)
421 uint_fast8_t had_errors = util_rebuild(conf);
423 FCGX_FPrintF(r->out, "Status: 200\r\nContent-type: text/plain\r\n\r\n"
424 "Rebuild complete%s\n", had_errors ?
425 " with errors" : "");
427 return;
430 /* Figure out what they want us to do */
431 static void handle(struct configuration *conf, FCGX_Request *r)
433 char *p = 0;
434 char *content_type = 0;
435 char *content_len_str = 0;
436 size_t content_len = 0;
437 char *buf_main = 0;
438 size_t buf_main_len = 0;
439 const char *content_type_prefix = "Content-Type: ";
440 struct post_cmd post_cmd = { 0 };
441 const char *ip_raw = FCGX_GetParam("REMOTE_ADDR", r->envp);
442 char *ip = 0;
443 char *ban_reason = 0;
444 char *ban_until = 0;
445 char *cooldown_length = 0;
446 uint_fast8_t found_idx = 0;
448 /* In case someone is trying for a time GET, prioritize that */
449 time(&post_cmd.prepared.now);
450 LOG("-----------------------------------------");
451 LOG("Handling post at %zu from %s", (size_t) post_cmd.prepared.now,
452 UBSAFES(ip_raw));
454 if (!ip_raw) {
455 LOG("Couldn't get REMOTE_ADDR (500)");
456 report_internal_error(r);
457 goto done;
460 if (util_normalize_ip(ip_raw, &ip) < 0) {
461 LOG("Couldn't normalize ip (500)");
462 report_internal_error(r);
463 goto done;
466 /* You can only POST to /action */
467 if (!(p = FCGX_GetParam("REQUEST_METHOD", r->envp))) {
468 LOG("Couldn't get request method (500)");
469 report_internal_error(r);
470 goto done;
473 if (strcmp(p, "POST")) {
474 LOG("request method was not POST (405)");
475 report_bad_method(r);
476 goto done;
479 /* We have to somehow feed this into multipart */
480 if (!(content_type = FCGX_GetParam("CONTENT_TYPE", r->envp))) {
481 LOG("Can't get CONTENT_TYPE (500)");
482 report_internal_error(r);
483 goto done;
486 if (!(content_len_str = FCGX_GetParam("CONTENT_LENGTH", r->envp))) {
487 LOG("Can't get CONTENT_LENGTH (500)");
488 report_internal_error(r);
489 goto done;
492 content_len = (size_t) strtoll(content_len_str, 0, 0);
494 if (content_len > max_form_data_size) {
495 LOG("Buffer would have exceeded %zuB (413)",
496 max_form_data_size);
497 report_too_large(r, "Total POST");
498 goto done;
501 buf_main_len = strlen(content_type_prefix) + strlen(content_type) +
502 strlen("\r\n\r\n") + content_len;
504 if (buf_main_len + 1 < buf_main_len) {
505 ERROR_MESSAGE("overflow");
506 goto done;
509 if (!(buf_main = malloc(buf_main_len + 1))) {
510 PERROR_MESSAGE("malloc");
511 goto done;
514 size_t offset = sprintf(buf_main, "%s%s\r\n\r\n", content_type_prefix,
515 content_type);
517 /* Try and swallow this thing into a buffer */
518 FCGX_GetStr(buf_main + offset, content_len, r->in);
520 /* Okay, we've got it in the buffer */
521 if (multipart_decompose(buf_main, buf_main_len, &post_cmd) < 0) {
522 LOG("Decoding message failed, returning (400)");
523 report_bad_request(r, "Invalid multipart/form-data");
524 goto done;
527 /* Now we can check what they actually wanted us to DO */
528 if (!post_cmd.raw.action) {
529 LOG("No action specified (400)");
530 report_bad_request(r, "You have to give action=something");
531 goto done;
532 } else if (!(strcmp(post_cmd.raw.action, "reply"))) {
533 post_cmd.action_id = REPLY;
534 } else if (!(strcmp(post_cmd.raw.action, "newthread"))) {
535 post_cmd.action_id = NEWTHREAD;
536 } else if (!(strcmp(post_cmd.raw.action, "rebuild"))) {
537 post_cmd.action_id = REBUILD;
540 if (post_cmd.raw.thread) {
541 post_cmd.thread_id = strtoll(post_cmd.raw.thread, 0, 0);
544 if (post_cmd.action_id == NONE) {
545 LOG("Invalid action \"%s\" (400)", post_cmd.raw.action);
546 report_bad_request(r, "That's not a valid action");
547 goto done;
551 * XXX: the idea is to only accept REBUILD commmands from
552 * the local machine. Is this necessary and sufficient in
553 * the world of ipv6?
555 if (post_cmd.action_id == REBUILD) {
556 /* Note that the IP is normalized so we can sort it */
557 if (strcmp(ip, "127.000.000.001") &&
558 strcmp(ip, "000.000.000.000") &&
559 strcmp(ip, "0000:0000:0000:0000:0000:0000:0000:0001")) {
560 LOG("REBUILD requested from invalid ip %s", ip);
561 report_bad_request(r, "You can(not) rebuild");
562 goto done;
565 goto take_action;
568 /* And we can find where they wanted to do it */
569 found_idx = 0;
571 if (!post_cmd.raw.board) {
572 LOG("No board specified (400)");
573 report_bad_request(r, "You have to give board=something");
574 goto done;
577 if (post_cmd.action_id == REPLY &&
578 !post_cmd.thread_id) {
579 LOG("Reply, yet no thread (400)");
580 report_bad_request(r, "You have to give thread=something");
581 goto done;
584 for (size_t j = 0; j < conf->boards_num; ++j) {
585 if (!strcmp(post_cmd.raw.board, conf->boards[j].name)) {
586 post_cmd.board_idx = j;
587 found_idx = 1;
588 break;
592 if (!found_idx) {
593 LOG("Invalid board \"%s\" (400)", post_cmd.raw.board);
594 report_bad_request(r, "That's not a valid board");
595 goto done;
598 int is_banned = 0;
600 if (db_check_bans(ip, post_cmd.board_idx, post_cmd.prepared.now,
601 &is_banned, &ban_until, &ban_reason) < 0) {
602 LOG("Couldn't determine ban status (500)");
603 report_internal_error(r);
604 goto done;
607 if (is_banned) {
608 /* This should give HTTP 403 */
609 LOG("Ban detected (until=\"%s\", reason=\"%s\") (403)",
610 ban_until, ban_reason);
611 report_ban(r, ban_until, ban_reason);
612 goto done;
615 if (post_cmd.action_id == REPLY ||
616 post_cmd.action_id == NEWTHREAD) {
617 int is_cooled = 0;
619 if (db_check_cooldowns(ip, post_cmd.board_idx,
620 post_cmd.prepared.now, &is_cooled,
621 &cooldown_length) < 0) {
622 LOG("Couldn't determine cooldown status (500)");
623 report_internal_error(r);
624 goto done;
627 if (is_cooled) {
628 /* This should give HTTP 429 */
629 LOG("Cooldown triggered (length=\"%s\") (429)",
630 cooldown_length);
631 report_cooldown(r, cooldown_length);
632 goto done;
635 int correct_challenge = 0;
637 if (!post_cmd.raw.challenge_id) {
638 LOG("No challenge id given (403)");
639 report_bad_challenge(r);
640 goto done;
643 char *e = 0;
644 size_t challenge_idx = (size_t) strtoll(
645 post_cmd.raw.challenge_id, &e, 0);
647 if (e &&
648 *e) {
649 challenge_idx = conf->challenges_num;
652 if (challenge_idx >= conf->challenges_num) {
653 LOG("Bad challenge id \"%s\" given (403)",
654 post_cmd.raw.challenge_id);
655 report_bad_challenge(r);
656 goto done;
659 if (!post_cmd.raw.challenge_response) {
660 LOG("No challenge response given (403)");
661 report_bad_challenge(r);
662 goto done;
665 for (size_t j = 0; j < NUM_CHALLENGE_ANSWERS; ++j) {
666 if (!conf->challenges[challenge_idx].answers[j]) {
667 continue;
670 if (!strcasecmp(post_cmd.raw.challenge_response,
671 conf->challenges[challenge_idx].answers[
672 j])) {
673 correct_challenge = 1;
677 if (!correct_challenge) {
678 LOG("Incorrect response \"%s\" to challenge %s (403)",
679 post_cmd.raw.challenge_response,
680 post_cmd.raw.challenge_id);
681 report_bad_challenge(r);
682 goto done;
686 take_action:
688 /* Now we split into specific actions */
689 switch (post_cmd.action_id) {
690 case REPLY:
691 LOG("reply to /%s/%ju", UBSAFES(post_cmd.raw.board),
692 post_cmd.thread_id);
693 handle_op_or_reply(conf, r, &post_cmd, ip, post_cmd.thread_id);
694 break;
695 case NEWTHREAD:
696 LOG("newthread on /%s/", UBSAFES(post_cmd.raw.board));
697 handle_op_or_reply(conf, r, &post_cmd, ip, 0);
698 break;
699 case REBUILD:
700 LOG("rebuild");
701 handle_rebuild(conf, r);
702 break;
703 case NONE:
704 ERROR_MESSAGE("Impossible");
705 report_internal_error(r);
706 break;
709 done:
710 clean_post_cmd(&post_cmd);
711 free(buf_main);
712 free(ban_reason);
713 free(ban_until);
714 free(cooldown_length);
717 /* Do the thing */
718 int main(void)
720 int ret = 1;
721 FCGX_Request r = { 0 };
722 struct configuration conf = { 0 };
724 setlocale(LC_ALL, "");
726 /* tedu@ is probably laughing at me right now. Hi! */
727 srand(time(0));
728 conf = (struct configuration) {
729 /* */
730 .static_www_folder = static_www_folder, /* */
731 .work_path = work_path, /* */
732 .trip_salt = trip_salt, /* */
733 .trip_salt_len = strlen(trip_salt), /* */
734 .boards = boards, /* */
735 .boards_num = NUM_OF(boards), /* */
736 .max_form_data_size = max_form_data_size, /* */
737 .max_file_size = max_file_size, /* */
738 .max_text_len = max_text_len, /* */
739 .filetypes = filetypes, /* */
740 .filetypes_num = NUM_OF(filetypes), /* */
741 .file_description_prog = file_description_prog, /* */
742 .headers = headers, /* */
743 .headers_num = NUM_OF(headers), /* */
744 .challenges = challenges, /* */
745 .challenges_num = NUM_OF(challenges), /* */
746 .wordfilter_inputs = wordfilter_inputs, /* */
747 .wordfilter_inputs_num = NUM_OF(wordfilter_inputs), /* */
750 if (preconditions_check(&conf) < 0) {
751 goto done;
754 if (board_pages_init(&conf) < 0) {
755 goto done;
758 FCGX_Init();
759 FCGX_InitRequest(&r, 0, 0);
761 while (FCGX_Accept_r(&r) == 0) {
762 handle(&conf, &r);
763 FCGX_Finish_r(&r);
766 ret = 0;
767 done:
768 clean_dbs();
769 clean_locks();
770 clean_multipart();
771 clean_sanitize_comment();
772 clean_sanitize_file();
773 clean_tripcodes();
774 clean_write_thread();
776 return ret;