--- lbbs/src/net_server.c 2025/06/05 09:04:55 1.53 +++ lbbs/src/net_server.c 2025/06/25 02:49:20 1.67 @@ -14,31 +14,35 @@ * * ***************************************************************************/ -#define _XOPEN_SOURCE 500 -#define _POSIX_C_SOURCE 200809L -#define _GNU_SOURCE - -#include "net_server.h" +#include "bbs.h" +#include "bbs_main.h" #include "common.h" -#include "log.h" +#include "database.h" +#include "file_loader.h" #include "io.h" #include "init.h" -#include "fork.h" +#include "log.h" +#include "login.h" #include "menu.h" -#include "file_loader.h" +#include "net_server.h" +#include "section_list.h" #include "section_list_loader.h" #include #include -#include #include #include +#include #include -#include -#include -#include -#include #include +#include +#include +#include #include +#include +#include +#include +#include +#include #include struct process_sockaddr_t @@ -50,6 +54,201 @@ typedef struct process_sockaddr_t PROCES static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; +#define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds + +struct ssl_server_cb_data_t +{ + int tries; + int error; +}; + +static int auth_password(ssh_session session, const char *user, + const char *password, void *userdata) +{ + struct ssl_server_cb_data_t *p_data = userdata; + int ret; + + if (strcmp(user, "guest") == 0) + { + ret = load_guest_info(); + } + else + { + ret = check_user(user, password); + } + + if (ret == 0) + { + return SSH_AUTH_SUCCESS; + } + + if ((++(p_data->tries)) >= BBS_login_retry_times) + { + p_data->error = 1; + } + + return SSH_AUTH_DENIED; +} + +static int pty_request(ssh_session session, ssh_channel channel, const char *term, + int x, int y, int px, int py, void *userdata) +{ + return 0; +} + +static int shell_request(ssh_session session, ssh_channel channel, void *userdata) +{ + return 0; +} + +static struct ssh_channel_callbacks_struct channel_cb = { + .channel_pty_request_function = pty_request, + .channel_shell_request_function = shell_request}; + +static ssh_channel new_session_channel(ssh_session session, void *userdata) +{ + if (SSH_channel != NULL) + { + return NULL; + } + + SSH_channel = ssh_channel_new(session); + ssh_callbacks_init(&channel_cb); + ssh_set_channel_callbacks(SSH_channel, &channel_cb); + + return SSH_channel; +} + +static int fork_server(void) +{ + ssh_event event; + int pid; + int i; + int ret; + + struct ssl_server_cb_data_t cb_data = { + .tries = 0, + .error = 0, + }; + + struct ssh_server_callbacks_struct cb = { + .userdata = &cb_data, + .auth_password_function = auth_password, + .channel_open_request_session_function = new_session_channel, + }; + + pid = fork(); + + if (pid > 0) // Parent process + { + SYS_child_process_count++; + log_common("Child process (%d) start\n", pid); + return pid; + } + else if (pid < 0) // Error + { + log_error("fork() error (%d)\n", errno); + return -1; + } + + // Child process + + if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1) + { + log_error("Close server socket failed\n"); + } + + SSH_session = ssh_new(); + + if (SSH_v2) + { + if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK) + { + log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session)); + goto cleanup; + } + + ssh_bind_free(sshbind); + + ssh_callbacks_init(&cb); + ssh_set_server_callbacks(SSH_session, &cb); + + if (ssh_handle_key_exchange(SSH_session)) + { + log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session)); + goto cleanup; + } + ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD); + + event = ssh_event_new(); + ssh_event_add_session(event, SSH_session); + + for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100) + { + ret = ssh_event_dopoll(event, 100); // 0.1 second + if (ret == SSH_ERROR) + { + log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session)); + goto cleanup; + } + } + + if (cb_data.error) + { + log_error("SSH auth error, tried %d times\n", cb_data.tries); + goto cleanup; + } + } + + // Redirect Input + close(STDIN_FILENO); + if (dup2(socket_client, STDIN_FILENO) == -1) + { + log_error("Redirect stdin to client socket failed\n"); + goto cleanup; + } + + // Redirect Output + close(STDOUT_FILENO); + if (dup2(socket_client, STDOUT_FILENO) == -1) + { + log_error("Redirect stdout to client socket failed\n"); + goto cleanup; + } + + SYS_child_process_count = 0; + + bbs_main(); + +cleanup: + // Child process exit + SYS_server_exit = 1; + + if (SSH_v2) + { + ssh_channel_free(SSH_channel); + ssh_disconnect(SSH_session); + } + else if (close(socket_client) == -1) + { + log_error("Close client socket failed\n"); + } + + ssh_free(SSH_session); + ssh_finalize(); + + // Close Input and Output for client + close(STDIN_FILENO); + close(STDOUT_FILENO); + + log_common("Process exit normally\n"); + log_end(); + + _exit(0); + + return 0; +} + int net_server(const char *hostaddr, in_port_t port[]) { unsigned int addrlen; @@ -60,7 +259,7 @@ int net_server(const char *hostaddr, in_ int nfds, epollfd; siginfo_t siginfo; int sd_notify_stopping = 0; - MENU_SET *p_bbs_menu_new; + MENU_SET bbs_menu_new; int i, j; pid_t pid; int ssh_log_level = SSH_LOG_NOLOG; @@ -72,6 +271,7 @@ int net_server(const char *hostaddr, in_ if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 || ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 || ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 || ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0) { log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind)); @@ -220,28 +420,32 @@ int net_server(const char *hostaddr, in_ log_error("Reload conf failed\n"); } - p_bbs_menu_new = calloc(1, sizeof(MENU_SET)); - if (p_bbs_menu_new == NULL) + // acquire rw lock of all sections to avoid conflict with menu reload in data loader process + ret = section_list_rw_lock(NULL); + if (ret < 0) { - log_error("OOM: calloc(MENU_SET)\n"); - } - else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0) - { - unload_menu(p_bbs_menu_new); - free(p_bbs_menu_new); - p_bbs_menu_new = NULL; - - log_error("Reload menu failed\n"); + log_error("section_list_rw_lock(NULL) error\n"); } else { - unload_menu(p_bbs_menu); - free(p_bbs_menu); - - p_bbs_menu = p_bbs_menu_new; - p_bbs_menu_new = NULL; + if (load_menu(&bbs_menu_new, CONF_MENU) < 0) + { + unload_menu(&bbs_menu_new); + log_error("Reload menu failed\n"); + } + else + { + unload_menu(&bbs_menu); + memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new)); + log_common("Reload menu successfully\n"); + } - log_common("Reload menu successfully\n"); + // release rw lock of all sections + ret = section_list_rw_unlock(NULL); + if (ret < 0) + { + log_error("section_list_rw_unlock(NULL) error\n"); + } } sd_notify(0, "READY=1"); @@ -270,7 +474,7 @@ int net_server(const char *hostaddr, in_ if (section_list_loader_reload() < 0) { - log_error("ksection_list_loader_reload() failed\n"); + log_error("section_list_loader_reload() failed\n"); } } @@ -324,7 +528,7 @@ int net_server(const char *hostaddr, in_ port_client = ntohs(sin.sin_port); - log_common("Accept connection from %s:%d\n", hostaddr_client, port_client); + log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client); if (SYS_child_process_count - 1 < BBS_max_client) {