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
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.
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
)
40 reason
= "That's not a real request. That's all we know.";
43 FCGX_FPrintF(r
->out
, BAD_REQUEST_FMT
, reason
);
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
);
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
));
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
);
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
));
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
));
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
);
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
);
104 /* Make sure every board has a page (really only for brand-new boards) */
105 static int board_pages_init(struct configuration
*conf
)
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
) {
116 if (lock_acquire(j
) < 0) {
120 if (db_cull_and_report_threads(j
, &thread_ids
, &thread_ids_num
,
121 &board_pages_num
) < 0) {
125 if (wt_write_board(j
, thread_ids
, thread_ids_num
,
126 board_pages_num
) < 0) {
140 /* Free what needs to be freed */
141 static void clean_post_cmd(struct post_cmd
*p
)
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
181 char *abs_file_path
= 0;
184 uintmax_t real_thread
= 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");
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");
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");
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");
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");
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");
239 if (sf_check_mime_type(pc
->raw
.file_contents
, pc
->raw
.file_contents_len
,
241 LOG("Bad MIME check (400)");
242 report_bad_request(r
, "Unsupported file type");
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
);
253 /* HTML-escape, wordfilter, linkify */
254 if (st_sanitize_text(pc
, &our_fault
) < 0) {
256 LOG("Error in st_sanitize_text (500)");
257 report_internal_error(r
);
261 LOG("Bad text (400)");
262 report_bad_request(r
, "Disallowed text");
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
);
284 if (db_insert_post(ip
, parent_thread
, cooldown
, pc
, &thread_dne
,
285 &thread_closed
, &thread_full
, &pc
->prepared
.id
) <
287 LOG("Error in insert_post (500)");
288 report_internal_error(r
);
292 LOG("Post %zu on board /%s/", pc
->prepared
.id
,
293 conf
->boards
[pc
->board_idx
].name
);
296 LOG("Thread %zu does not exist (400)", (size_t) 0);
297 report_bad_request(r
, "Thread does not exist");
302 LOG("Thread %zu is full (400)", (size_t) 0);
303 report_bad_request(r
, "Thread is full");
308 LOG("Thread %zu is closed (400)", (size_t) 0);
309 report_bad_request(r
, "Thread is closed");
313 /* Make thumbnails and insert them */
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
,
324 LOG("Error in sf_install_files (500)");
325 report_internal_error(r
);
329 LOG("Couldn't install files (400)");
330 report_bad_request(r
, "Bad file upload");
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
);
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
) <
352 LOG("Error in db_update_post_description (500)");
353 report_internal_error(r
);
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
);
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
);
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
);
385 len
= snprintf(0, 0, "/%s/res/%s", pc
->raw
.board
, pc
->raw
.thread
);
388 ERROR_MESSAGE("overflow");
389 report_internal_error(r
);
393 if (!(buf
= malloc(len
+ 1))) {
394 PERROR_MESSAGE("malloc");
395 report_internal_error(r
);
400 !strcmp(pc
->raw
.email
, "noko")) {
401 sprintf(buf
, "/%s/res/%s", pc
->raw
.board
, pc
->raw
.thread
);
403 sprintf(buf
, "/%s", pc
->raw
.board
);
406 report_post_successful(r
, buf
);
409 if (need_to_unlock
) {
410 lock_release(pc
->board_idx
);
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" : "");
430 /* Figure out what they want us to do */
431 static void handle(struct configuration
*conf
, FCGX_Request
*r
)
434 char *content_type
= 0;
435 char *content_len_str
= 0;
436 size_t content_len
= 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
);
443 char *ban_reason
= 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
,
455 LOG("Couldn't get REMOTE_ADDR (500)");
456 report_internal_error(r
);
460 if (util_normalize_ip(ip_raw
, &ip
) < 0) {
461 LOG("Couldn't normalize ip (500)");
462 report_internal_error(r
);
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
);
473 if (strcmp(p
, "POST")) {
474 LOG("request method was not POST (405)");
475 report_bad_method(r
);
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
);
486 if (!(content_len_str
= FCGX_GetParam("CONTENT_LENGTH", r
->envp
))) {
487 LOG("Can't get CONTENT_LENGTH (500)");
488 report_internal_error(r
);
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)",
497 report_too_large(r
, "Total POST");
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");
509 if (!(buf_main
= malloc(buf_main_len
+ 1))) {
510 PERROR_MESSAGE("malloc");
514 size_t offset
= sprintf(buf_main
, "%s%s\r\n\r\n", content_type_prefix
,
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");
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");
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");
551 * XXX: the idea is to only accept REBUILD commmands from
552 * the local machine. Is this necessary and sufficient in
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");
568 /* And we can find where they wanted to do it */
571 if (!post_cmd
.raw
.board
) {
572 LOG("No board specified (400)");
573 report_bad_request(r
, "You have to give board=something");
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");
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
;
593 LOG("Invalid board \"%s\" (400)", post_cmd
.raw
.board
);
594 report_bad_request(r
, "That's not a valid board");
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
);
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
);
615 if (post_cmd
.action_id
== REPLY
||
616 post_cmd
.action_id
== NEWTHREAD
) {
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
);
628 /* This should give HTTP 429 */
629 LOG("Cooldown triggered (length=\"%s\") (429)",
631 report_cooldown(r
, cooldown_length
);
635 int correct_challenge
= 0;
637 if (!post_cmd
.raw
.challenge_id
) {
638 LOG("No challenge id given (403)");
639 report_bad_challenge(r
);
644 size_t challenge_idx
= (size_t) strtoll(
645 post_cmd
.raw
.challenge_id
, &e
, 0);
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
);
659 if (!post_cmd
.raw
.challenge_response
) {
660 LOG("No challenge response given (403)");
661 report_bad_challenge(r
);
665 for (size_t j
= 0; j
< NUM_CHALLENGE_ANSWERS
; ++j
) {
666 if (!conf
->challenges
[challenge_idx
].answers
[j
]) {
670 if (!strcasecmp(post_cmd
.raw
.challenge_response
,
671 conf
->challenges
[challenge_idx
].answers
[
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
);
688 /* Now we split into specific actions */
689 switch (post_cmd
.action_id
) {
691 LOG("reply to /%s/%ju", UBSAFES(post_cmd
.raw
.board
),
693 handle_op_or_reply(conf
, r
, &post_cmd
, ip
, post_cmd
.thread_id
);
696 LOG("newthread on /%s/", UBSAFES(post_cmd
.raw
.board
));
697 handle_op_or_reply(conf
, r
, &post_cmd
, ip
, 0);
701 handle_rebuild(conf
, r
);
704 ERROR_MESSAGE("Impossible");
705 report_internal_error(r
);
710 clean_post_cmd(&post_cmd
);
714 free(cooldown_length
);
721 FCGX_Request r
= { 0 };
722 struct configuration conf
= { 0 };
724 setlocale(LC_ALL
, "");
726 /* tedu@ is probably laughing at me right now. Hi! */
728 conf
= (struct configuration
) {
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) {
754 if (board_pages_init(&conf
) < 0) {
759 FCGX_InitRequest(&r
, 0, 0);
761 while (FCGX_Accept_r(&r
) == 0) {
771 clean_sanitize_comment();
772 clean_sanitize_file();
774 clean_write_thread();