/[LeafOK_CVS]/lbbs/src/test_ssh_server.c
ViewVC logotype

Diff of /lbbs/src/test_ssh_server.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1 by sysadm, Tue Jun 3 13:58:21 2025 UTC Revision 1.17 by sysadm, Mon Dec 1 14:01:36 2025 UTC
# Line 1  Line 1 
1    /* SPDX-License-Identifier: GPL-3.0-or-later */
2    /*
3     * test_ssh_server
4     *   - tester for network server with SSH support
5     *
6     * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8    
9    // This test was written based on libssh example/proxy.c
10    
11    #ifdef HAVE_CONFIG_H
12    #include "config.h"
13    #endif
14    
15  #include "log.h"  #include "log.h"
16  #include <stdio.h>  #include <stdio.h>
17    #include <libssh/callbacks.h>
18  #include <libssh/libssh.h>  #include <libssh/libssh.h>
19  #include <libssh/server.h>  #include <libssh/server.h>
 #include <libssh/callbacks.h>  
20    
21  static int ssh_auth_password_cb(ssh_session session, const char *user, const char *password, void *userdata)  enum test_ssh_server_constant_t
22    {
23            BUF_SIZE = 2048,
24    };
25    
26    static const char SSH_HOST_RSA_KEY_FILE[] = "../conf/ssh_host_rsa_key";
27    static const char SSH_HOST_ED25519_KEY_FILE[] = "../conf/ssh_host_ed25519_key";
28    static const char SSH_HOST_ECDSA_KEY_FILE[] = "../conf/ssh_host_ecdsa_key";
29    
30    static const char USER[] = "test";
31    static const char PASSWORD[] = "123456";
32    
33    static ssh_channel SSH_channel;
34    static int authenticated = 0;
35    static int tries = 0;
36    static int error = 0;
37    
38    static int auth_password(ssh_session session, const char *user,
39                                                     const char *password, void *userdata)
40  {  {
41          if (strcmp(user, "test") == 0 && strcmp(password, "123456") == 0)          (void)userdata;
42    
43            log_common("Authenticating user %s pwd %s\n", user, password);
44            if (strcmp(user, USER) == 0 && strcmp(password, PASSWORD) == 0)
45          {          {
46                    authenticated = 1;
47                    log_common("Authenticated\n");
48                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
49          }          }
50            if (tries >= 3)
51          log_std("Debug: SSH Auth OK\n");          {
52                    log_error("Too many authentication tries\n");
53                    ssh_disconnect(session);
54                    error = 1;
55                    return SSH_AUTH_DENIED;
56            }
57            tries++;
58          return SSH_AUTH_DENIED;          return SSH_AUTH_DENIED;
59  }  }
60    
61    static int pty_request(ssh_session session, ssh_channel channel, const char *term,
62                                               int x, int y, int px, int py, void *userdata)
63    {
64            (void)session;
65            (void)channel;
66            (void)term;
67            (void)x;
68            (void)y;
69            (void)px;
70            (void)py;
71            (void)userdata;
72            log_common("Allocated terminal\n");
73            return 0;
74    }
75    
76    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
77    {
78            (void)session;
79            (void)channel;
80            (void)userdata;
81            log_common("Allocated shell\n");
82            return 0;
83    }
84    
85    struct ssh_channel_callbacks_struct channel_cb = {
86            .channel_pty_request_function = pty_request,
87            .channel_shell_request_function = shell_request};
88    
89    static ssh_channel channel_open(ssh_session session, void *userdata)
90    {
91            (void)session;
92            (void)userdata;
93    
94            if (SSH_channel != NULL)
95                    return NULL;
96    
97            log_common("Allocated session channel\n");
98            SSH_channel = ssh_channel_new(session);
99            ssh_callbacks_init(&channel_cb);
100            ssh_set_channel_callbacks(SSH_channel, &channel_cb);
101    
102            return SSH_channel;
103    }
104    
105  int ssh_server(const char *hostaddr, unsigned int port)  int ssh_server(const char *hostaddr, unsigned int port)
106  {  {
107          ssh_bind sshbind;          ssh_bind sshbind;
108          ssh_session session;          ssh_session session;
109          ssh_channel channel;          ssh_event event;
110          int ssh_log_level = SSH_LOG_PROTOCOL;  
111          struct ssh_server_callbacks_struct cb = {          struct ssh_server_callbacks_struct cb = {
112                  .userdata = NULL,                  .userdata = NULL,
113                  .auth_password_function = ssh_auth_password_cb};                  .auth_password_function = auth_password,
114                    .channel_open_request_session_function = channel_open};
115    
116            long int ssh_timeout = 0;
117    
118            char buf[BUF_SIZE];
119            char host[128] = "";
120            int i, r;
121    
122            int ssh_key_valid = 0;
123            int ssh_log_level = SSH_LOG_PROTOCOL;
124    
125          ssh_init();          ssh_init();
126    
127          sshbind = ssh_bind_new();          sshbind = ssh_bind_new();
128    
129            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0)
130            {
131                    log_error("Error loading SSH RSA key: %s\n", SSH_HOST_RSA_KEY_FILE);
132            }
133            else
134            {
135                    ssh_key_valid = 1;
136            }
137            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0)
138            {
139                    log_error("Error loading SSH ED25519 key: %s\n", SSH_HOST_ED25519_KEY_FILE);
140            }
141            else
142            {
143                    ssh_key_valid = 1;
144            }
145            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ECDSA_KEY_FILE) < 0)
146            {
147                    log_error("Error loading SSH ECDSA key: %s\n", SSH_HOST_ECDSA_KEY_FILE);
148            }
149            else
150            {
151                    ssh_key_valid = 1;
152            }
153    
154            if (!ssh_key_valid)
155            {
156                    log_error("Error: no valid SSH host key\n");
157                    ssh_bind_free(sshbind);
158                    return -1;
159            }
160    
161          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
162                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
163                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, "ssh_host_rsa_key") < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "+ssh-rsa") < 0 ||
164                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
165          {          {
166                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
167                    ssh_bind_free(sshbind);
168                  return -1;                  return -1;
169          }          }
170    
171          if (ssh_bind_listen(sshbind) < 0)          if (ssh_bind_listen(sshbind) < 0)
172          {          {
173                  log_error("Error listening at SSH server port: %s\n", ssh_get_error(sshbind));                  log_error("Error listening at SSH server port: %s\n", ssh_get_error(sshbind));
174                    ssh_bind_free(sshbind);
175                  return -1;                  return -1;
176          }          }
177    
# Line 48  int ssh_server(const char *hostaddr, uns Line 179  int ssh_server(const char *hostaddr, uns
179          {          {
180                  session = ssh_new();                  session = ssh_new();
181    
182                  if (ssh_bind_accept(sshbind, session) != SSH_OK)                  if (ssh_bind_accept(sshbind, session) == SSH_OK)
                 {  
                         log_error("Error accept SSH connection: %s\n", ssh_get_error(sshbind));  
                 }  
   
                 ssh_callbacks_init(&cb);  
                 if (ssh_set_server_callbacks(session, &cb) != SSH_OK)  
183                  {                  {
184                          log_error("Error set SSH callback: %s\n", ssh_get_error(sshbind));                          pid_t pid = fork();
185                            switch (pid)
186                            {
187                            case 0:
188                                    ssh_bind_free(sshbind);
189    
190                                    ssh_callbacks_init(&cb);
191                                    ssh_set_server_callbacks(session, &cb);
192    
193                                    ssh_timeout = 60; // second
194                                    if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
195                                    {
196                                            log_error("Error setting SSH options: %s\n", ssh_get_error(session));
197                                            ssh_disconnect(session);
198                                            _exit(1);
199                                    }
200    
201                                    if (ssh_handle_key_exchange(session))
202                                    {
203                                            log_error("ssh_handle_key_exchange: %s\n", ssh_get_error(session));
204                                            ssh_disconnect(session);
205                                            _exit(1);
206                                    }
207                                    ssh_set_auth_methods(session, SSH_AUTH_METHOD_PASSWORD | SSH_AUTH_METHOD_GSSAPI_MIC);
208    
209                                    event = ssh_event_new();
210                                    ssh_event_add_session(event, session);
211    
212                                    while (!(authenticated && SSH_channel != NULL))
213                                    {
214                                            if (error)
215                                                    break;
216                                            r = ssh_event_dopoll(event, -1);
217                                            if (r == SSH_ERROR)
218                                            {
219                                                    log_error("Error : %s\n", ssh_get_error(session));
220                                                    ssh_disconnect(session);
221                                                    _exit(1);
222                                            }
223                                    }
224    
225                                    if (error)
226                                    {
227                                            log_error("Error, exiting loop\n");
228                                            _exit(1);
229                                    }
230                                    else
231                                    {
232                                            log_common("Authenticated and got a channel\n");
233                                    }
234    
235                                    ssh_timeout = 0;
236                                    if (ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
237                                    {
238                                            log_error("Error setting SSH options: %s\n", ssh_get_error(session));
239                                            ssh_disconnect(session);
240                                            _exit(1);
241                                    }
242    
243                                    snprintf(buf, sizeof(buf), "Hello, welcome to the Sample SSH proxy.\r\nPlease select your destination: ");
244                                    ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf));
245                                    do
246                                    {
247                                            i = ssh_channel_read(SSH_channel, buf, sizeof(buf), 0);
248                                            if (i > 0)
249                                            {
250                                                    ssh_channel_write(SSH_channel, buf, (uint32_t)i);
251                                                    if (strlen(host) + (size_t)i < sizeof(host))
252                                                    {
253                                                            strncat(host, buf, (size_t)i);
254                                                    }
255                                                    if (strchr(host, '\x0d'))
256                                                    {
257                                                            *strchr(host, '\x0d') = '\0';
258                                                            ssh_channel_write(SSH_channel, "\n", 1);
259                                                            break;
260                                                    }
261                                            }
262                                            else
263                                            {
264                                                    log_error("Error: %s\n", ssh_get_error(session));
265                                                    _exit(1);
266                                            }
267                                    } while (i > 0);
268                                    snprintf(buf, sizeof(buf), "Trying to connect to \"%s\"\r\n", host);
269                                    ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf));
270                                    log_common("%s", buf);
271    
272                                    ssh_disconnect(session);
273                                    ssh_free(session);
274    
275                                    _exit(0);
276                            case -1:
277                                    log_error("Failed to fork\n");
278                                    break;
279                            }
280                  }                  }
281                    else
                 ssh_set_auth_methods(session, SSH_AUTH_METHOD_PASSWORD);  
   
                 if (ssh_handle_key_exchange(session) != SSH_OK)  
282                  {                  {
283                          log_error("Error exchanging SSH keys: %s\n", ssh_get_error(sshbind));                          log_error("%s\n", ssh_get_error(sshbind));
                         ssh_disconnect(session);  
                         continue;  
284                  }                  }
285    
286                  channel = ssh_channel_new(session);                  /* Since the session has been passed to a child fork, do some cleaning
287                  if (channel == NULL || ssh_channel_open_session(channel) != SSH_OK)                   * up at the parent process. */
                 {  
                         log_error("Error opening SSH channel: %s\n", ssh_get_error(sshbind));  
                         ssh_channel_free(channel);  
                         continue;  
                 }  
   
                 log_std("Debug: #\n");  
   
                 ssh_channel_close(channel);  
288                  ssh_disconnect(session);                  ssh_disconnect(session);
289                  ssh_free(session);                  ssh_free(session);
290          }          }
# Line 97  int main(int argc, char *argv[]) Line 303  int main(int argc, char *argv[])
303                  return -1;                  return -1;
304          }          }
305    
306          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
307          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
308    
309          ssh_server("0.0.0.0", 2322);          ssh_server("0.0.0.0", 2322);
310    


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1