/[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.7 by sysadm, Sat Jun 7 07:35:06 2025 UTC
# Line 1  Line 1 
1    // This test was written base on libssh example/proxy.c
2    
3  #include "log.h"  #include "log.h"
4  #include <stdio.h>  #include <stdio.h>
5  #include <libssh/libssh.h>  #include <libssh/libssh.h>
6  #include <libssh/server.h>  #include <libssh/server.h>
7  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
8    
9  static int ssh_auth_password_cb(ssh_session session, const char *user, const char *password, void *userdata)  #ifndef BUF_SIZE
10    #define BUF_SIZE 2048
11    #endif
12    
13    #define SSH_HOST_RSA_KEYFILE "../conf/ssh_host_rsa_key"
14    
15    #define USER "test"
16    #define PASSWORD "123456"
17    
18    static ssh_channel SSH_channel;
19    static int authenticated = 0;
20    static int tries = 0;
21    static int error = 0;
22    
23    static int auth_password(ssh_session session, const char *user,
24                                                     const char *password, void *userdata)
25  {  {
26          if (strcmp(user, "test") == 0 && strcmp(password, "123456") == 0)          (void)userdata;
27    
28            log_common("Authenticating user %s pwd %s\n", user, password);
29            if (strcmp(user, USER) == 0 && strcmp(password, PASSWORD) == 0)
30          {          {
31                    authenticated = 1;
32                    log_common("Authenticated\n");
33                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
34          }          }
35            if (tries >= 3)
36          log_std("Debug: SSH Auth OK\n");          {
37                    log_error("Too many authentication tries\n");
38                    ssh_disconnect(session);
39                    error = 1;
40                    return SSH_AUTH_DENIED;
41            }
42            tries++;
43          return SSH_AUTH_DENIED;          return SSH_AUTH_DENIED;
44  }  }
45    
46    static int pty_request(ssh_session session, ssh_channel channel, const char *term,
47                                               int x, int y, int px, int py, void *userdata)
48    {
49            (void)session;
50            (void)channel;
51            (void)term;
52            (void)x;
53            (void)y;
54            (void)px;
55            (void)py;
56            (void)userdata;
57            log_common("Allocated terminal\n");
58            return 0;
59    }
60    
61    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
62    {
63            (void)session;
64            (void)channel;
65            (void)userdata;
66            log_common("Allocated shell\n");
67            return 0;
68    }
69    
70    struct ssh_channel_callbacks_struct channel_cb = {
71            .channel_pty_request_function = pty_request,
72            .channel_shell_request_function = shell_request};
73    
74    static ssh_channel new_session_channel(ssh_session session, void *userdata)
75    {
76            (void)session;
77            (void)userdata;
78    
79            if (SSH_channel != NULL)
80                    return NULL;
81    
82            log_common("Allocated session channel\n");
83            SSH_channel = ssh_channel_new(session);
84            ssh_callbacks_init(&channel_cb);
85            ssh_set_channel_callbacks(SSH_channel, &channel_cb);
86    
87            return SSH_channel;
88    }
89    
90  int ssh_server(const char *hostaddr, unsigned int port)  int ssh_server(const char *hostaddr, unsigned int port)
91  {  {
92          ssh_bind sshbind;          ssh_bind sshbind;
93          ssh_session session;          ssh_session session;
94          ssh_channel channel;          ssh_event event;
95          int ssh_log_level = SSH_LOG_PROTOCOL;  
96          struct ssh_server_callbacks_struct cb = {          struct ssh_server_callbacks_struct cb = {
97                  .userdata = NULL,                  .userdata = NULL,
98                  .auth_password_function = ssh_auth_password_cb};                  .auth_password_function = auth_password,
99                    .channel_open_request_session_function = new_session_channel};
100    
101            char buf[BUF_SIZE];
102            char host[128] = "";
103            int i, r;
104    
105            int ssh_log_level = SSH_LOG_PROTOCOL;
106    
107          ssh_init();          ssh_init();
108    
# Line 31  int ssh_server(const char *hostaddr, uns Line 110  int ssh_server(const char *hostaddr, uns
110    
111          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
112                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
113                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, "ssh_host_rsa_key") < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEYFILE) < 0 ||
114                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256") < 0 ||
115                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_PUBKEY_ACCEPTED_KEY_TYPES, "ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256") < 0 ||
116                    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 ||
117                    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 ||
118                    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 ||
119                    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 ||
120                    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 ||
121                  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)
122          {          {
123                  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));
124                    ssh_bind_free(sshbind);
125                  return -1;                  return -1;
126          }          }
127    
128          if (ssh_bind_listen(sshbind) < 0)          if (ssh_bind_listen(sshbind) < 0)
129          {          {
130                  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));
131                    ssh_bind_free(sshbind);
132                  return -1;                  return -1;
133          }          }
134    
# Line 48  int ssh_server(const char *hostaddr, uns Line 136  int ssh_server(const char *hostaddr, uns
136          {          {
137                  session = ssh_new();                  session = ssh_new();
138    
139                  if (ssh_bind_accept(sshbind, session) != SSH_OK)                  if (ssh_bind_accept(sshbind, session) == SSH_OK)
140                  {                  {
141                          log_error("Error accept SSH connection: %s\n", ssh_get_error(sshbind));                          pid_t pid = fork();
142                            switch (pid)
143                            {
144                            case 0:
145                                    ssh_bind_free(sshbind);
146    
147                                    ssh_callbacks_init(&cb);
148                                    ssh_set_server_callbacks(session, &cb);
149    
150                                    if (ssh_handle_key_exchange(session))
151                                    {
152                                            log_error("ssh_handle_key_exchange: %s\n", ssh_get_error(session));
153                                            return 1;
154                                    }
155                                    ssh_set_auth_methods(session, SSH_AUTH_METHOD_PASSWORD | SSH_AUTH_METHOD_GSSAPI_MIC);
156    
157                                    event = ssh_event_new();
158                                    ssh_event_add_session(event, session);
159    
160                                    while (!(authenticated && SSH_channel != NULL))
161                                    {
162                                            if (error)
163                                                    break;
164                                            r = ssh_event_dopoll(event, -1);
165                                            if (r == SSH_ERROR)
166                                            {
167                                                    log_error("Error : %s\n", ssh_get_error(session));
168                                                    ssh_disconnect(session);
169                                                    _exit(1);
170                                            }
171                                    }
172    
173                                    if (error)
174                                    {
175                                            log_error("Error, exiting loop\n");
176                                            _exit(1);
177                                    }
178                                    else
179                                    {
180                                            log_common("Authenticated and got a channel\n");
181                                    }
182    
183                                    snprintf(buf, sizeof(buf), "Hello, welcome to the Sample SSH proxy.\r\nPlease select your destination: ");
184                                    ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf));
185                                    do
186                                    {
187                                            i = ssh_channel_read(SSH_channel, buf, sizeof(buf), 0);
188                                            if (i > 0)
189                                            {
190                                                    ssh_channel_write(SSH_channel, buf, (uint32_t)i);
191                                                    if (strlen(host) + (size_t)i < sizeof(host))
192                                                    {
193                                                            strncat(host, buf, (size_t)i);
194                                                    }
195                                                    if (strchr(host, '\x0d'))
196                                                    {
197                                                            *strchr(host, '\x0d') = '\0';
198                                                            ssh_channel_write(SSH_channel, "\n", 1);
199                                                            break;
200                                                    }
201                                            }
202                                            else
203                                            {
204                                                    log_error("Error: %s\n", ssh_get_error(session));
205                                                    _exit(1);
206                                            }
207                                    } while (i > 0);
208                                    snprintf(buf, sizeof(buf), "Trying to connect to \"%s\"\r\n", host);
209                                    ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf));
210                                    log_common("%s", buf);
211    
212                                    ssh_disconnect(session);
213                                    ssh_free(session);
214    
215                                    _exit(0);
216                            case -1:
217                                    log_error("Failed to fork\n");
218                                    break;
219                            }
220                  }                  }
221                    else
                 ssh_callbacks_init(&cb);  
                 if (ssh_set_server_callbacks(session, &cb) != SSH_OK)  
                 {  
                         log_error("Error set SSH callback: %s\n", ssh_get_error(sshbind));  
                 }  
   
                 ssh_set_auth_methods(session, SSH_AUTH_METHOD_PASSWORD);  
   
                 if (ssh_handle_key_exchange(session) != SSH_OK)  
222                  {                  {
223                          log_error("Error exchanging SSH keys: %s\n", ssh_get_error(sshbind));                          log_error("%s\n", ssh_get_error(sshbind));
                         ssh_disconnect(session);  
                         continue;  
224                  }                  }
225    
226                  channel = ssh_channel_new(session);                  /* Since the session has been passed to a child fork, do some cleaning
227                  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);  
228                  ssh_disconnect(session);                  ssh_disconnect(session);
229                  ssh_free(session);                  ssh_free(session);
230          }          }
# Line 97  int main(int argc, char *argv[]) Line 243  int main(int argc, char *argv[])
243                  return -1;                  return -1;
244          }          }
245    
246          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
247          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
248    
249          ssh_server("0.0.0.0", 2322);          ssh_server("0.0.0.0", 2322);
250    


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

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