/[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.5 - (show annotations)
Thu Jun 5 14:48:15 2025 UTC (9 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.4: +7 -1 lines
Content type: text/x-csrc
Update test

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

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