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

Contents of /lbbs/src/test_ssh_server.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show 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 // This test was written base on libssh example/proxy.c
2
3 #include "log.h"
4 #include <stdio.h>
5 #include <libssh/libssh.h>
6 #include <libssh/server.h>
7 #include <libssh/callbacks.h>
8
9 #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 (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;
34 }
35 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 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)
91 {
92 ssh_bind sshbind;
93 ssh_session session;
94 ssh_event event;
95
96 struct ssh_server_callbacks_struct cb = {
97 .userdata = NULL,
98 .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();
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 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)
122 {
123 log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
124 ssh_bind_free(sshbind);
125 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 ssh_bind_free(sshbind);
132 return -1;
133 }
134
135 while (1)
136 {
137 session = ssh_new();
138
139 if (ssh_bind_accept(sshbind, session) == SSH_OK)
140 {
141 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
222 {
223 log_error("%s\n", ssh_get_error(sshbind));
224 }
225
226 /* Since the session has been passed to a child fork, do some cleaning
227 * up at the parent process. */
228 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 log_common_redir(STDOUT_FILENO);
247 log_error_redir(STDERR_FILENO);
248
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