--- lbbs/src/net_server.c 2025/10/25 03:11:11 1.74 +++ lbbs/src/net_server.c 2025/11/07 04:58:09 1.80 @@ -1,21 +1,14 @@ -/*************************************************************************** - 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 + */ #include "bbs.h" #include "bbs_main.h" +#include "bwf.h" #include "common.h" #include "database.h" #include "file_loader.h" @@ -47,8 +40,13 @@ #include #include -#define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second -#define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second +enum _net_server_constant_t +{ + WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second + WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second + + SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds +}; struct process_sockaddr_t { @@ -59,9 +57,7 @@ typedef struct process_sockaddr_t PROCES static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; -#define 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 @@ -333,11 +329,6 @@ static int fork_server(void) ssh_bind_free(sshbind); - ssh_callbacks_init(&channel_cb); - ssh_callbacks_init(&server_cb); - - ssh_set_server_callbacks(SSH_session, &server_cb); - ssh_timeout = 60; // second if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) { @@ -345,12 +336,18 @@ static int fork_server(void) goto cleanup; } + ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD); + + ssh_callbacks_init(&server_cb); + ssh_callbacks_init(&channel_cb); + + ssh_set_server_callbacks(SSH_session, &server_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); @@ -375,6 +372,22 @@ static int fork_server(void) ssh_set_channel_callbacks(SSH_channel, &channel_cb); + do + { + ret = ssh_event_dopoll(event, 100); // 0.1 second + if (ret == SSH_ERROR) + { + ssh_channel_close(SSH_channel); + } + + if (ret == SSH_AGAIN) // loop until SSH connection is fully established + { + /* Executed only once, once the child process starts. */ + cdata.event = event; + break; + } + } while (ssh_channel_is_open(SSH_channel)); + ssh_timeout = 0; if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) { @@ -641,6 +654,12 @@ int net_server(const char *hostaddr, in_ 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); @@ -670,7 +689,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");