| 3 |
* net_server |
* net_server |
| 4 |
* - network server with SSH support |
* - network server with SSH support |
| 5 |
* |
* |
| 6 |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 12 |
|
|
| 13 |
#include "bbs.h" |
#include "bbs.h" |
| 14 |
#include "bbs_main.h" |
#include "bbs_main.h" |
| 15 |
|
#include "bwf.h" |
| 16 |
#include "common.h" |
#include "common.h" |
| 17 |
#include "database.h" |
#include "database.h" |
| 18 |
#include "file_loader.h" |
#include "file_loader.h" |
| 19 |
|
#include "hash_dict.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "init.h" |
#include "init.h" |
| 22 |
#include "log.h" |
#include "log.h" |
| 43 |
#include <sys/syscall.h> |
#include <sys/syscall.h> |
| 44 |
#include <sys/types.h> |
#include <sys/types.h> |
| 45 |
#include <sys/wait.h> |
#include <sys/wait.h> |
|
#include <systemd/sd-daemon.h> |
|
| 46 |
|
|
| 47 |
#define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second |
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 48 |
#define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second |
#include <systemd/sd-daemon.h> |
| 49 |
|
#endif |
| 50 |
|
|
| 51 |
struct process_sockaddr_t |
enum _net_server_constant_t |
| 52 |
{ |
{ |
| 53 |
pid_t pid; |
WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second |
| 54 |
in_addr_t s_addr; |
WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second |
|
}; |
|
|
typedef struct process_sockaddr_t PROCESS_SOCKADDR; |
|
|
|
|
|
static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; |
|
| 55 |
|
|
| 56 |
#define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds |
SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds |
| 57 |
|
}; |
| 58 |
|
|
| 59 |
#define SFTP_SERVER_PATH "/usr/lib/sftp-server" |
static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; |
| 60 |
|
|
| 61 |
/* A userdata struct for session. */ |
/* A userdata struct for session. */ |
| 62 |
struct session_data_struct |
struct session_data_struct |
| 396 |
} |
} |
| 397 |
|
|
| 398 |
// Redirect Input |
// Redirect Input |
|
close(STDIN_FILENO); |
|
| 399 |
if (dup2(socket_client, STDIN_FILENO) == -1) |
if (dup2(socket_client, STDIN_FILENO) == -1) |
| 400 |
{ |
{ |
| 401 |
log_error("Redirect stdin to client socket failed\n"); |
log_error("Redirect stdin to client socket failed\n"); |
| 403 |
} |
} |
| 404 |
|
|
| 405 |
// Redirect Output |
// Redirect Output |
|
close(STDOUT_FILENO); |
|
| 406 |
if (dup2(socket_client, STDOUT_FILENO) == -1) |
if (dup2(socket_client, STDOUT_FILENO) == -1) |
| 407 |
{ |
{ |
| 408 |
log_error("Redirect stdout to client socket failed\n"); |
log_error("Redirect stdout to client socket failed\n"); |
| 449 |
|
|
| 450 |
int net_server(const char *hostaddr, in_port_t port[]) |
int net_server(const char *hostaddr, in_port_t port[]) |
| 451 |
{ |
{ |
| 452 |
|
HASH_DICT *hash_dict_pid_sockaddr = NULL; |
| 453 |
|
HASH_DICT *hash_dict_sockaddr_count = NULL; |
| 454 |
|
|
| 455 |
unsigned int addrlen; |
unsigned int addrlen; |
| 456 |
int ret; |
int ret; |
| 457 |
int flags[2]; |
int flags[2]; |
| 461 |
siginfo_t siginfo; |
siginfo_t siginfo; |
| 462 |
int notify_child_exit = 0; |
int notify_child_exit = 0; |
| 463 |
time_t tm_notify_child_exit = time(NULL); |
time_t tm_notify_child_exit = time(NULL); |
|
int sd_notify_stopping = 0; |
|
| 464 |
MENU_SET bbs_menu_new; |
MENU_SET bbs_menu_new; |
| 465 |
MENU_SET top10_menu_new; |
MENU_SET top10_menu_new; |
| 466 |
int i, j; |
int i, j; |
| 467 |
pid_t pid; |
pid_t pid; |
| 468 |
int ssh_log_level = SSH_LOG_NOLOG; |
int ssh_log_level = SSH_LOG_NOLOG; |
| 469 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 470 |
|
int sd_notify_stopping = 0; |
| 471 |
|
#endif |
| 472 |
|
|
| 473 |
ssh_init(); |
ssh_init(); |
| 474 |
|
|
| 549 |
fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK); |
fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK); |
| 550 |
} |
} |
| 551 |
|
|
| 552 |
|
hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); |
| 553 |
|
if (hash_dict_pid_sockaddr == NULL) |
| 554 |
|
{ |
| 555 |
|
log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n"); |
| 556 |
|
return -1; |
| 557 |
|
} |
| 558 |
|
hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT); |
| 559 |
|
if (hash_dict_sockaddr_count == NULL) |
| 560 |
|
{ |
| 561 |
|
log_error("hash_dict_create(hash_dict_sockaddr_count) error\n"); |
| 562 |
|
return -1; |
| 563 |
|
} |
| 564 |
|
|
| 565 |
// Startup complete |
// Startup complete |
| 566 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 567 |
sd_notifyf(0, "READY=1\n" |
sd_notifyf(0, "READY=1\n" |
| 568 |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
"STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" |
| 569 |
"MAINPID=%d", |
"MAINPID=%d", |
| 570 |
hostaddr, port[0], hostaddr, port[1], getpid()); |
hostaddr, port[0], hostaddr, port[1], getpid()); |
| 571 |
|
#endif |
| 572 |
|
|
| 573 |
while (!SYS_server_exit || SYS_child_process_count > 0) |
while (!SYS_server_exit || SYS_child_process_count > 0) |
| 574 |
{ |
{ |
| 575 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 576 |
if (SYS_server_exit && !sd_notify_stopping) |
if (SYS_server_exit && !sd_notify_stopping) |
| 577 |
{ |
{ |
| 578 |
sd_notify(0, "STOPPING=1"); |
sd_notify(0, "STOPPING=1"); |
| 579 |
sd_notify_stopping = 1; |
sd_notify_stopping = 1; |
| 580 |
} |
} |
| 581 |
|
#endif |
| 582 |
|
|
| 583 |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0) |
| 584 |
{ |
{ |
| 595 |
|
|
| 596 |
if (siginfo.si_pid != section_list_loader_pid) |
if (siginfo.si_pid != section_list_loader_pid) |
| 597 |
{ |
{ |
| 598 |
i = 0; |
j = 0; |
| 599 |
for (; i < BBS_max_client; i++) |
ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j); |
| 600 |
|
if (ret < 0) |
| 601 |
{ |
{ |
| 602 |
if (process_sockaddr_pool[i].pid == siginfo.si_pid) |
log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid); |
|
{ |
|
|
process_sockaddr_pool[i].pid = 0; |
|
|
break; |
|
|
} |
|
| 603 |
} |
} |
| 604 |
if (i >= BBS_max_client) |
else |
| 605 |
{ |
{ |
| 606 |
log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid); |
ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1); |
| 607 |
|
if (ret < 0) |
| 608 |
|
{ |
| 609 |
|
log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j); |
| 610 |
|
} |
| 611 |
|
|
| 612 |
|
ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid); |
| 613 |
|
if (ret < 0) |
| 614 |
|
{ |
| 615 |
|
log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid); |
| 616 |
|
} |
| 617 |
} |
} |
| 618 |
} |
} |
| 619 |
} |
} |
| 632 |
{ |
{ |
| 633 |
if (notify_child_exit == 0) |
if (notify_child_exit == 0) |
| 634 |
{ |
{ |
| 635 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 636 |
sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count); |
| 637 |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
log_common("Notify %d child process to exit\n", SYS_child_process_count); |
| 638 |
|
#endif |
| 639 |
|
|
| 640 |
if (kill(0, SIGTERM) < 0) |
if (kill(-getpid(), SIGTERM) < 0) |
| 641 |
{ |
{ |
| 642 |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
log_error("Send SIGTERM signal failed (%d)\n", errno); |
| 643 |
} |
} |
| 647 |
} |
} |
| 648 |
else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT) |
else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT) |
| 649 |
{ |
{ |
| 650 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 651 |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); |
| 652 |
|
#endif |
| 653 |
|
|
| 654 |
for (i = 0; i < BBS_max_client; i++) |
if (kill(-getpid(), SIGKILL) < 0) |
| 655 |
{ |
{ |
| 656 |
if (process_sockaddr_pool[i].pid != 0) |
log_error("Send SIGKILL signal failed (%d)\n", errno); |
|
{ |
|
|
log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid); |
|
|
if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0) |
|
|
{ |
|
|
log_error("Send SIGKILL signal failed (%d)\n", errno); |
|
|
} |
|
|
} |
|
| 657 |
} |
} |
| 658 |
|
|
| 659 |
notify_child_exit = 2; |
notify_child_exit = 2; |
| 669 |
if (SYS_conf_reload && !SYS_server_exit) |
if (SYS_conf_reload && !SYS_server_exit) |
| 670 |
{ |
{ |
| 671 |
SYS_conf_reload = 0; |
SYS_conf_reload = 0; |
| 672 |
|
|
| 673 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 674 |
sd_notify(0, "RELOADING=1"); |
sd_notify(0, "RELOADING=1"); |
| 675 |
|
#endif |
| 676 |
|
|
| 677 |
|
// Restart log |
| 678 |
|
if (log_restart() < 0) |
| 679 |
|
{ |
| 680 |
|
log_error("Restart logging failed\n"); |
| 681 |
|
} |
| 682 |
|
|
| 683 |
// Reload configuration |
// Reload configuration |
| 684 |
if (load_conf(CONF_BBSD) < 0) |
if (load_conf(CONF_BBSD) < 0) |
| 686 |
log_error("Reload conf failed\n"); |
log_error("Reload conf failed\n"); |
| 687 |
} |
} |
| 688 |
|
|
| 689 |
|
// Reload BWF config |
| 690 |
|
if (bwf_load(CONF_BWF) < 0) |
| 691 |
|
{ |
| 692 |
|
log_error("Reload BWF conf failed\n"); |
| 693 |
|
} |
| 694 |
|
|
| 695 |
if (load_menu(&bbs_menu_new, CONF_MENU) < 0) |
if (load_menu(&bbs_menu_new, CONF_MENU) < 0) |
| 696 |
{ |
{ |
| 697 |
unload_menu(&bbs_menu_new); |
unload_menu(&bbs_menu_new); |
| 736 |
log_common("Reload section config and gen_ex successfully\n"); |
log_common("Reload section config and gen_ex successfully\n"); |
| 737 |
} |
} |
| 738 |
|
|
| 739 |
|
// Notify child processes to reload configuration |
| 740 |
|
if (kill(-getpid(), SIGUSR1) < 0) |
| 741 |
|
{ |
| 742 |
|
log_error("Send SIGUSR1 signal failed (%d)\n", errno); |
| 743 |
|
} |
| 744 |
|
|
| 745 |
|
#ifdef HAVE_SYSTEMD_SD_DAEMON_H |
| 746 |
sd_notify(0, "READY=1"); |
sd_notify(0, "READY=1"); |
| 747 |
|
#endif |
| 748 |
} |
} |
| 749 |
|
|
| 750 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 802 |
if (SYS_child_process_count - 1 < BBS_max_client) |
if (SYS_child_process_count - 1 < BBS_max_client) |
| 803 |
{ |
{ |
| 804 |
j = 0; |
j = 0; |
| 805 |
for (i = 0; i < BBS_max_client; i++) |
ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j); |
| 806 |
|
if (ret < 0) |
| 807 |
{ |
{ |
| 808 |
if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr) |
log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client); |
|
{ |
|
|
j++; |
|
|
if (j >= BBS_max_client_per_ip) |
|
|
{ |
|
|
log_common("Too many client connections (%d) from %s\n", j, hostaddr_client); |
|
|
break; |
|
|
} |
|
|
} |
|
| 809 |
} |
} |
| 810 |
|
|
| 811 |
if (j < BBS_max_client_per_ip) |
if (j < BBS_max_client_per_ip) |
| 816 |
} |
} |
| 817 |
else if (pid > 0) |
else if (pid > 0) |
| 818 |
{ |
{ |
| 819 |
i = 0; |
ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr); |
| 820 |
for (; i < BBS_max_client; i++) |
if (ret < 0) |
| 821 |
{ |
{ |
| 822 |
if (process_sockaddr_pool[i].pid == 0) |
log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client); |
|
{ |
|
|
break; |
|
|
} |
|
| 823 |
} |
} |
| 824 |
|
|
| 825 |
if (i >= BBS_max_client) |
ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1); |
| 826 |
{ |
if (ret < 0) |
|
log_error("Process sockaddr pool depleted\n"); |
|
|
} |
|
|
else |
|
| 827 |
{ |
{ |
| 828 |
process_sockaddr_pool[i].pid = pid; |
log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1); |
|
process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr; |
|
| 829 |
} |
} |
| 830 |
} |
} |
| 831 |
} |
} |
| 832 |
|
else |
| 833 |
|
{ |
| 834 |
|
log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip); |
| 835 |
|
} |
| 836 |
} |
} |
| 837 |
else |
else |
| 838 |
{ |
{ |
| 863 |
} |
} |
| 864 |
} |
} |
| 865 |
|
|
| 866 |
|
hash_dict_destroy(hash_dict_pid_sockaddr); |
| 867 |
|
hash_dict_destroy(hash_dict_sockaddr_count); |
| 868 |
|
|
| 869 |
ssh_bind_free(sshbind); |
ssh_bind_free(sshbind); |
| 870 |
ssh_finalize(); |
ssh_finalize(); |
| 871 |
|
|