--- lbbs/src/net_server.c 2025/09/22 05:07:22 1.71 +++ lbbs/src/net_server.c 2025/11/05 04:19:21 1.79 @@ -1,18 +1,10 @@ -/*************************************************************************** - 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" @@ -29,10 +21,12 @@ #include "section_list_loader.h" #include #include +#include #include #include #include #include +#include #include #include #include @@ -45,8 +39,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 { @@ -57,18 +56,38 @@ typedef struct process_sockaddr_t PROCES static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT]; -#define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds +static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server"; -struct ssl_server_cb_data_t +/* A userdata struct for session. */ +struct session_data_struct { int tries; int error; }; +/* A userdata struct for channel. */ +struct channel_data_struct +{ + /* pid of the child process the channel will spawn. */ + pid_t pid; + /* For PTY allocation */ + socket_t pty_master; + socket_t pty_slave; + /* For communication with the child process. */ + socket_t child_stdin; + socket_t child_stdout; + /* Only used for subsystem and exec requests. */ + socket_t child_stderr; + /* Event which is used to poll the above descriptors. */ + ssh_event event; + /* Terminal size struct. */ + struct winsize *winsize; +}; + static int auth_password(ssh_session session, const char *user, const char *password, void *userdata) { - struct ssl_server_cb_data_t *p_data = userdata; + struct session_data_struct *sdata = (struct session_data_struct *)userdata; int ret; if (strcmp(user, "guest") == 0) @@ -85,39 +104,148 @@ static int auth_password(ssh_session ses return SSH_AUTH_SUCCESS; } - if ((++(p_data->tries)) >= BBS_login_retry_times) + if ((++(sdata->tries)) >= BBS_login_retry_times) { - p_data->error = 1; + sdata->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) + int cols, int rows, int px, int py, void *userdata) { - return 0; + struct channel_data_struct *cdata = (struct channel_data_struct *)userdata; + int rc; + + (void)session; + (void)channel; + (void)term; + + cdata->winsize->ws_row = (unsigned short int)rows; + cdata->winsize->ws_col = (unsigned short int)cols; + cdata->winsize->ws_xpixel = (unsigned short int)px; + cdata->winsize->ws_ypixel = (unsigned short int)py; + + rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize); + if (rc != 0) + { + log_error("Failed to open pty\n"); + return SSH_ERROR; + } + + return SSH_OK; +} + +static int pty_resize(ssh_session session, ssh_channel channel, int cols, int rows, + int py, int px, void *userdata) +{ + struct channel_data_struct *cdata = (struct channel_data_struct *)userdata; + + (void)session; + (void)channel; + + cdata->winsize->ws_row = (unsigned short int)rows; + cdata->winsize->ws_col = (unsigned short int)cols; + cdata->winsize->ws_xpixel = (unsigned short int)px; + cdata->winsize->ws_ypixel = (unsigned short int)py; + + if (cdata->pty_master != -1) + { + return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize); + } + + return SSH_ERROR; +} + +static int exec_pty(const char *mode, const char *command, struct channel_data_struct *cdata) +{ + (void)cdata; + + if (command != NULL) + { + log_error("Forbid exec /bin/sh %s %s)\n", mode, command); + } + + return SSH_OK; +} + +static int exec_nopty(const char *command, struct channel_data_struct *cdata) +{ + (void)cdata; + + if (command != NULL) + { + log_error("Forbid exec /bin/sh -c %s)\n", command); + } + + return SSH_OK; +} + +static int exec_request(ssh_session session, ssh_channel channel, const char *command, void *userdata) +{ + struct channel_data_struct *cdata = (struct channel_data_struct *)userdata; + + (void)session; + (void)channel; + + if (cdata->pid > 0) + { + return SSH_ERROR; + } + + if (cdata->pty_master != -1 && cdata->pty_slave != -1) + { + return exec_pty("-c", command, cdata); + } + return exec_nopty(command, cdata); } static int shell_request(ssh_session session, ssh_channel channel, void *userdata) { - return 0; + struct channel_data_struct *cdata = (struct channel_data_struct *)userdata; + + (void)session; + (void)channel; + + if (cdata->pid > 0) + { + return SSH_ERROR; + } + + if (cdata->pty_master != -1 && cdata->pty_slave != -1) + { + return exec_pty("-l", NULL, cdata); + } + /* Client requested a shell without a pty, let's pretend we allow that */ + return SSH_OK; } -static struct ssh_channel_callbacks_struct channel_cb = { - .channel_pty_request_function = pty_request, - .channel_shell_request_function = shell_request}; +static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata) +{ + (void)session; + (void)channel; + + log_error("subsystem_request(subsystem=%s)\n", subsystem); + + /* subsystem requests behave similarly to exec requests. */ + if (strcmp(subsystem, "sftp") == 0) + { + return exec_request(session, channel, SFTP_SERVER_PATH, userdata); + } + return SSH_ERROR; +} -static ssh_channel new_session_channel(ssh_session session, void *userdata) +static ssh_channel channel_open(ssh_session session, void *userdata) { + (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; } @@ -130,15 +258,41 @@ static int fork_server(void) int i; int ret; - struct ssl_server_cb_data_t cb_data = { + /* Structure for storing the pty size. */ + struct winsize wsize = { + .ws_row = 0, + .ws_col = 0, + .ws_xpixel = 0, + .ws_ypixel = 0}; + + /* Our struct holding information about the channel. */ + struct channel_data_struct cdata = { + .pid = 0, + .pty_master = -1, + .pty_slave = -1, + .child_stdin = -1, + .child_stdout = -1, + .child_stderr = -1, + .event = NULL, + .winsize = &wsize}; + + struct session_data_struct cb_data = { .tries = 0, .error = 0, }; - struct ssh_server_callbacks_struct cb = { + struct ssh_channel_callbacks_struct channel_cb = { + .userdata = &cdata, + .channel_pty_request_function = pty_request, + .channel_pty_window_change_function = pty_resize, + .channel_shell_request_function = shell_request, + .channel_exec_request_function = exec_request, + .channel_subsystem_request_function = subsystem_request}; + + struct ssh_server_callbacks_struct server_cb = { .userdata = &cb_data, .auth_password_function = auth_password, - .channel_open_request_session_function = new_session_channel, + .channel_open_request_session_function = channel_open, }; pid = fork(); @@ -174,9 +328,6 @@ static int fork_server(void) ssh_bind_free(sshbind); - ssh_callbacks_init(&cb); - ssh_set_server_callbacks(SSH_session, &cb); - ssh_timeout = 60; // second if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0) { @@ -184,12 +335,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); @@ -199,7 +356,9 @@ static int fork_server(void) ret = ssh_event_dopoll(event, 100); // 0.1 second if (ret == SSH_ERROR) { +#ifdef _DEBUG log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session)); +#endif goto cleanup; } } @@ -210,6 +369,24 @@ static int fork_server(void) goto cleanup; } + 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) { @@ -244,6 +421,11 @@ cleanup: if (SSH_v2) { + close(cdata.pty_master); + close(cdata.child_stdin); + close(cdata.child_stdout); + close(cdata.child_stderr); + ssh_channel_free(SSH_channel); ssh_disconnect(SSH_session); } @@ -280,6 +462,7 @@ int net_server(const char *hostaddr, in_ time_t tm_notify_child_exit = time(NULL); int sd_notify_stopping = 0; MENU_SET bbs_menu_new; + MENU_SET top10_menu_new; int i, j; pid_t pid; int ssh_log_level = SSH_LOG_NOLOG; @@ -473,20 +656,33 @@ int net_server(const char *hostaddr, in_ if (load_menu(&bbs_menu_new, CONF_MENU) < 0) { unload_menu(&bbs_menu_new); - log_error("Reload menu failed\n"); + log_error("Reload bbs 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 bbs menu successfully\n"); + } + + if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0) + { + unload_menu(&top10_menu_new); + log_error("Reload top10 menu failed\n"); + } + else + { + unload_menu(&top10_menu); + top10_menu_new.allow_exit = 1; + memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new)); + log_common("Reload top10 menu successfully\n"); } for (int i = 0; i < data_files_load_startup_count; i++) { 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");