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

Annotation of /lbbs/src/test_ssh_server.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Sat Jun 7 07:35:06 2025 UTC (9 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.6: +2 -0 lines
Content type: text/x-csrc
Update

1 sysadm 1.7 // This test was written base on libssh example/proxy.c
2    
3 sysadm 1.1 #include "log.h"
4     #include <stdio.h>
5     #include <libssh/libssh.h>
6     #include <libssh/server.h>
7     #include <libssh/callbacks.h>
8    
9 sysadm 1.2 #ifndef BUF_SIZE
10     #define BUF_SIZE 2048
11     #endif
12    
13 sysadm 1.6 #define SSH_HOST_RSA_KEYFILE "../conf/ssh_host_rsa_key"
14 sysadm 1.2
15     #define USER "test"
16     #define PASSWORD "123456"
17    
18 sysadm 1.4 static ssh_channel SSH_channel;
19 sysadm 1.2 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 sysadm 1.1 {
26 sysadm 1.2 (void)userdata;
27    
28 sysadm 1.3 log_common("Authenticating user %s pwd %s\n", user, password);
29 sysadm 1.2 if (strcmp(user, USER) == 0 && strcmp(password, PASSWORD) == 0)
30 sysadm 1.1 {
31 sysadm 1.2 authenticated = 1;
32 sysadm 1.3 log_common("Authenticated\n");
33 sysadm 1.1 return SSH_AUTH_SUCCESS;
34     }
35 sysadm 1.2 if (tries >= 3)
36     {
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;
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 sysadm 1.3 log_common("Allocated terminal\n");
58 sysadm 1.2 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 sysadm 1.3 log_common("Allocated shell\n");
67 sysadm 1.2 return 0;
68     }
69 sysadm 1.4
70 sysadm 1.2 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 sysadm 1.1
79 sysadm 1.4 if (SSH_channel != NULL)
80 sysadm 1.2 return NULL;
81    
82 sysadm 1.3 log_common("Allocated session channel\n");
83 sysadm 1.4 SSH_channel = ssh_channel_new(session);
84 sysadm 1.2 ssh_callbacks_init(&channel_cb);
85 sysadm 1.4 ssh_set_channel_callbacks(SSH_channel, &channel_cb);
86 sysadm 1.2
87 sysadm 1.4 return SSH_channel;
88 sysadm 1.1 }
89    
90     int ssh_server(const char *hostaddr, unsigned int port)
91     {
92 sysadm 1.4 ssh_bind sshbind;
93 sysadm 1.2 ssh_session session;
94     ssh_event event;
95    
96 sysadm 1.1 struct ssh_server_callbacks_struct cb = {
97     .userdata = NULL,
98 sysadm 1.2 .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 sysadm 1.6 int ssh_log_level = SSH_LOG_PROTOCOL;
106 sysadm 1.1
107     ssh_init();
108    
109     sshbind = ssh_bind_new();
110    
111     if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
112     ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
113 sysadm 1.6 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEYFILE) < 0 ||
114 sysadm 1.5 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256") < 0 ||
115 sysadm 1.6 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 sysadm 1.5 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 sysadm 1.1 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));
124 sysadm 1.2 ssh_bind_free(sshbind);
125 sysadm 1.1 return -1;
126     }
127    
128     if (ssh_bind_listen(sshbind) < 0)
129     {
130     log_error("Error listening at SSH server port: %s\n", ssh_get_error(sshbind));
131 sysadm 1.2 ssh_bind_free(sshbind);
132 sysadm 1.1 return -1;
133     }
134    
135     while (1)
136     {
137     session = ssh_new();
138    
139 sysadm 1.2 if (ssh_bind_accept(sshbind, session) == SSH_OK)
140 sysadm 1.1 {
141 sysadm 1.2 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 sysadm 1.4 while (!(authenticated && SSH_channel != NULL))
161 sysadm 1.2 {
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 sysadm 1.3 log_common("Authenticated and got a channel\n");
181 sysadm 1.2 }
182    
183     snprintf(buf, sizeof(buf), "Hello, welcome to the Sample SSH proxy.\r\nPlease select your destination: ");
184 sysadm 1.4 ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf));
185 sysadm 1.2 do
186     {
187 sysadm 1.4 i = ssh_channel_read(SSH_channel, buf, sizeof(buf), 0);
188 sysadm 1.2 if (i > 0)
189     {
190 sysadm 1.4 ssh_channel_write(SSH_channel, buf, (uint32_t)i);
191 sysadm 1.2 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 sysadm 1.4 ssh_channel_write(SSH_channel, "\n", 1);
199 sysadm 1.2 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 sysadm 1.4 ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf));
210 sysadm 1.3 log_common("%s", buf);
211 sysadm 1.2
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 sysadm 1.1 }
221 sysadm 1.2 else
222 sysadm 1.1 {
223 sysadm 1.2 log_error("%s\n", ssh_get_error(sshbind));
224 sysadm 1.1 }
225    
226 sysadm 1.2 /* Since the session has been passed to a child fork, do some cleaning
227     * up at the parent process. */
228 sysadm 1.1 ssh_disconnect(session);
229     ssh_free(session);
230     }
231    
232     ssh_bind_free(sshbind);
233     ssh_finalize();
234    
235     return 0;
236     }
237    
238     int main(int argc, char *argv[])
239     {
240     if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
241     {
242     printf("Open log error\n");
243     return -1;
244     }
245    
246 sysadm 1.3 log_common_redir(STDOUT_FILENO);
247     log_error_redir(STDERR_FILENO);
248 sysadm 1.1
249     ssh_server("0.0.0.0", 2322);
250    
251     log_end();
252    
253     return 0;
254     }

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