--- lbbs/src/test_ssh_server.c 2025/06/04 13:42:53 1.3 +++ lbbs/src/test_ssh_server.c 2025/06/18 04:04:09 1.8 @@ -1,3 +1,21 @@ +/*************************************************************************** + test_ssh_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. * + * * + ***************************************************************************/ + +// This test was written based on libssh example/proxy.c + #include "log.h" #include #include @@ -8,12 +26,12 @@ #define BUF_SIZE 2048 #endif -#define SSH_HOST_KEYFILE "../conf/ssh_host_rsa_key" +#define SSH_HOST_RSA_KEYFILE "../conf/ssh_host_rsa_key" #define USER "test" #define PASSWORD "123456" -static ssh_channel channel; +static ssh_channel SSH_channel; static int authenticated = 0; static int tries = 0; static int error = 0; @@ -64,6 +82,7 @@ static int shell_request(ssh_session ses log_common("Allocated shell\n"); return 0; } + struct ssh_channel_callbacks_struct channel_cb = { .channel_pty_request_function = pty_request, .channel_shell_request_function = shell_request}; @@ -73,21 +92,21 @@ static ssh_channel new_session_channel(s (void)session; (void)userdata; - if (channel != NULL) + if (SSH_channel != NULL) return NULL; log_common("Allocated session channel\n"); - channel = ssh_channel_new(session); + SSH_channel = ssh_channel_new(session); ssh_callbacks_init(&channel_cb); - ssh_set_channel_callbacks(channel, &channel_cb); + ssh_set_channel_callbacks(SSH_channel, &channel_cb); - return channel; + return SSH_channel; } int ssh_server(const char *hostaddr, unsigned int port) { - ssh_session session; ssh_bind sshbind; + ssh_session session; ssh_event event; struct ssh_server_callbacks_struct cb = { @@ -99,7 +118,7 @@ int ssh_server(const char *hostaddr, uns char host[128] = ""; int i, r; - int ssh_log_level = SSH_LOG_WARNING; + int ssh_log_level = SSH_LOG_PROTOCOL; ssh_init(); @@ -107,7 +126,14 @@ int ssh_server(const char *hostaddr, uns 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, SSH_HOST_RSA_KEYFILE) < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256") < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_PUBKEY_ACCEPTED_KEY_TYPES, "ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256") < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, "curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1") < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HMAC_C_S, "umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1") < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HMAC_S_C, "umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1") < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_CIPHERS_C_S, "chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com") < 0 || + ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_CIPHERS_S_C, "chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com") < 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)); @@ -147,7 +173,7 @@ int ssh_server(const char *hostaddr, uns event = ssh_event_new(); ssh_event_add_session(event, session); - while (!(authenticated && channel != NULL)) + while (!(authenticated && SSH_channel != NULL)) { if (error) break; @@ -171,13 +197,13 @@ int ssh_server(const char *hostaddr, uns } snprintf(buf, sizeof(buf), "Hello, welcome to the Sample SSH proxy.\r\nPlease select your destination: "); - ssh_channel_write(channel, buf, (uint32_t)strlen(buf)); + ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf)); do { - i = ssh_channel_read(channel, buf, sizeof(buf), 0); + i = ssh_channel_read(SSH_channel, buf, sizeof(buf), 0); if (i > 0) { - ssh_channel_write(channel, buf, (uint32_t)i); + ssh_channel_write(SSH_channel, buf, (uint32_t)i); if (strlen(host) + (size_t)i < sizeof(host)) { strncat(host, buf, (size_t)i); @@ -185,7 +211,7 @@ int ssh_server(const char *hostaddr, uns if (strchr(host, '\x0d')) { *strchr(host, '\x0d') = '\0'; - ssh_channel_write(channel, "\n", 1); + ssh_channel_write(SSH_channel, "\n", 1); break; } } @@ -196,7 +222,7 @@ int ssh_server(const char *hostaddr, uns } } while (i > 0); snprintf(buf, sizeof(buf), "Trying to connect to \"%s\"\r\n", host); - ssh_channel_write(channel, buf, (uint32_t)strlen(buf)); + ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf)); log_common("%s", buf); ssh_disconnect(session);