--- lbbs/src/net_server.c 2025/10/25 07:58:10 1.75 +++ lbbs/src/net_server.c 2025/11/16 02:06:56 1.84 @@ -1,24 +1,22 @@ -/*************************************************************************** - net_server.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * net_server + * - network server with SSH support + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "bbs.h" #include "bbs_main.h" +#include "bwf.h" #include "common.h" #include "database.h" #include "file_loader.h" +#include "hash_dict.h" #include "io.h" #include "init.h" #include "log.h" @@ -47,21 +45,15 @@ #include #include -#define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second -#define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second - -struct process_sockaddr_t +enum _net_server_constant_t { - pid_t pid; - in_addr_t s_addr; -}; -typedef struct process_sockaddr_t PROCESS_SOCKADDR; - -static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; + WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second + WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second -#define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds + SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds +}; -#define SFTP_SERVER_PATH "/usr/lib/sftp-server" +static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; /* A userdata struct for session. */ struct session_data_struct @@ -401,7 +393,6 @@ static int fork_server(void) } // Redirect Input - close(STDIN_FILENO); if (dup2(socket_client, STDIN_FILENO) == -1) { log_error("Redirect stdin to client socket failed\n"); @@ -409,7 +400,6 @@ static int fork_server(void) } // Redirect Output - close(STDOUT_FILENO); if (dup2(socket_client, STDOUT_FILENO) == -1) { log_error("Redirect stdout to client socket failed\n"); @@ -456,6 +446,9 @@ cleanup: int net_server(const char *hostaddr, in_port_t port[]) { + HASH_DICT *hash_dict_pid_sockaddr = NULL; + HASH_DICT *hash_dict_sockaddr_count = NULL; + unsigned int addrlen; int ret; int flags[2]; @@ -551,6 +544,19 @@ int net_server(const char *hostaddr, in_ fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK); } + hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT); + if (hash_dict_pid_sockaddr == NULL) + { + log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n"); + return -1; + } + hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT); + if (hash_dict_sockaddr_count == NULL) + { + log_error("hash_dict_create(hash_dict_sockaddr_count) error\n"); + return -1; + } + // Startup complete sd_notifyf(0, "READY=1\n" "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n" @@ -580,18 +586,39 @@ int net_server(const char *hostaddr, in_ if (siginfo.si_pid != section_list_loader_pid) { - i = 0; - for (; i < BBS_max_client; i++) + j = 0; + ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j); + if (ret < 0) { - if (process_sockaddr_pool[i].pid == siginfo.si_pid) - { - process_sockaddr_pool[i].pid = 0; - break; - } + log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid); } - if (i >= BBS_max_client) + else { - log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid); + sin.sin_addr.s_addr = (uint32_t)j; + j = 0; + ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, (int64_t *)&j); + if (ret < 0) + { + log_error("hash_dict_get(hash_dict_sockaddr_count, %d) error\n", sin.sin_addr.s_addr); + } + else if (ret == 0) + { + log_error("hash_dict_get(hash_dict_sockaddr_count, %d) not found\n", sin.sin_addr.s_addr); + j = 1; + } + + j--; + ret = hash_dict_set(hash_dict_sockaddr_count, sin.sin_addr.s_addr, j); + if (ret < 0) + { + log_error("hash_dict_set(hash_dict_sockaddr_count, %d, %d) error\n", sin.sin_addr.s_addr, j); + } + + ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid); + if (ret < 0) + { + log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid); + } } } } @@ -613,7 +640,7 @@ int net_server(const char *hostaddr, in_ sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count); log_common("Notify %d child process to exit\n", SYS_child_process_count); - if (kill(0, SIGTERM) < 0) + if (kill(-getpid(), SIGTERM) < 0) { log_error("Send SIGTERM signal failed (%d)\n", errno); } @@ -625,16 +652,9 @@ int net_server(const char *hostaddr, in_ { sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count); - for (i = 0; i < BBS_max_client; i++) + if (kill(-getpid(), SIGKILL) < 0) { - if (process_sockaddr_pool[i].pid != 0) - { - 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); - } - } + log_error("Send SIGKILL signal failed (%d)\n", errno); } notify_child_exit = 2; @@ -652,12 +672,24 @@ int net_server(const char *hostaddr, in_ SYS_conf_reload = 0; sd_notify(0, "RELOADING=1"); + // Restart log + if (log_restart() < 0) + { + log_error("Restart logging failed\n"); + } + // Reload configuration if (load_conf(CONF_BBSD) < 0) { log_error("Reload conf failed\n"); } + // Reload BWF config + if (bwf_load(CONF_BWF) < 0) + { + log_error("Reload BWF conf failed\n"); + } + if (load_menu(&bbs_menu_new, CONF_MENU) < 0) { unload_menu(&bbs_menu_new); @@ -687,7 +719,7 @@ int net_server(const char *hostaddr, in_ { if (load_file(data_files_load_startup[i]) < 0) { - log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); + log_error("load_file(%s) error\n", data_files_load_startup[i]); } } log_common("Reload data files successfully\n"); @@ -702,6 +734,12 @@ int net_server(const char *hostaddr, in_ log_common("Reload section config and gen_ex successfully\n"); } + // Notify child processes to reload configuration + if (kill(-getpid(), SIGUSR1) < 0) + { + log_error("Send SIGUSR1 signal failed (%d)\n", errno); + } + sd_notify(0, "READY=1"); } @@ -760,17 +798,10 @@ int net_server(const char *hostaddr, in_ if (SYS_child_process_count - 1 < BBS_max_client) { j = 0; - 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); + if (ret < 0) { - if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr) - { - j++; - if (j >= BBS_max_client_per_ip) - { - log_common("Too many client connections (%d) from %s\n", j, hostaddr_client); - break; - } - } + log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client); } if (j < BBS_max_client_per_ip) @@ -781,26 +812,23 @@ int net_server(const char *hostaddr, in_ } else if (pid > 0) { - i = 0; - for (; i < BBS_max_client; i++) + ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr); + if (ret < 0) { - if (process_sockaddr_pool[i].pid == 0) - { - break; - } + log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client); } - if (i >= BBS_max_client) + ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, j + 1); + if (ret < 0) { - log_error("Process sockaddr pool depleted\n"); - } - else - { - process_sockaddr_pool[i].pid = pid; - process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr; + log_error("hash_dict_set(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, j + 1); } } } + else + { + log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip); + } } else { @@ -831,6 +859,9 @@ int net_server(const char *hostaddr, in_ } } + hash_dict_destroy(hash_dict_pid_sockaddr); + hash_dict_destroy(hash_dict_sockaddr_count); + ssh_bind_free(sshbind); ssh_finalize();