/[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.4 - (hide annotations)
Thu Jun 5 05:24:56 2025 UTC (9 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.3: +13 -12 lines
Content type: text/x-csrc
Add SSH2 support

1 sysadm 1.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 sysadm 1.2 #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 sysadm 1.4 static ssh_channel SSH_channel;
17 sysadm 1.2 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 sysadm 1.1 {
24 sysadm 1.2 (void)userdata;
25    
26 sysadm 1.3 log_common("Authenticating user %s pwd %s\n", user, password);
27 sysadm 1.2 if (strcmp(user, USER) == 0 && strcmp(password, PASSWORD) == 0)
28 sysadm 1.1 {
29 sysadm 1.2 authenticated = 1;
30 sysadm 1.3 log_common("Authenticated\n");
31 sysadm 1.1 return SSH_AUTH_SUCCESS;
32     }
33 sysadm 1.2 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 sysadm 1.3 log_common("Allocated terminal\n");
56 sysadm 1.2 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 sysadm 1.3 log_common("Allocated shell\n");
65 sysadm 1.2 return 0;
66     }
67 sysadm 1.4
68 sysadm 1.2 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 sysadm 1.1
77 sysadm 1.4 if (SSH_channel != NULL)
78 sysadm 1.2 return NULL;
79    
80 sysadm 1.3 log_common("Allocated session channel\n");
81 sysadm 1.4 SSH_channel = ssh_channel_new(session);
82 sysadm 1.2 ssh_callbacks_init(&channel_cb);
83 sysadm 1.4 ssh_set_channel_callbacks(SSH_channel, &channel_cb);
84 sysadm 1.2
85 sysadm 1.4 return SSH_channel;
86 sysadm 1.1 }
87    
88     int ssh_server(const char *hostaddr, unsigned int port)
89     {
90 sysadm 1.4 ssh_bind sshbind;
91 sysadm 1.2 ssh_session session;
92     ssh_event event;
93    
94 sysadm 1.1 struct ssh_server_callbacks_struct cb = {
95     .userdata = NULL,
96 sysadm 1.2 .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_WARNING;
104 sysadm 1.1
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 sysadm 1.2 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
112 sysadm 1.1 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
113     {
114     log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
115 sysadm 1.2 ssh_bind_free(sshbind);
116 sysadm 1.1 return -1;
117     }
118    
119     if (ssh_bind_listen(sshbind) < 0)
120     {
121     log_error("Error listening at SSH server port: %s\n", ssh_get_error(sshbind));
122 sysadm 1.2 ssh_bind_free(sshbind);
123 sysadm 1.1 return -1;
124     }
125    
126     while (1)
127     {
128     session = ssh_new();
129    
130 sysadm 1.2 if (ssh_bind_accept(sshbind, session) == SSH_OK)
131 sysadm 1.1 {
132 sysadm 1.2 pid_t pid = fork();
133     switch (pid)
134     {
135     case 0:
136     ssh_bind_free(sshbind);
137    
138     ssh_callbacks_init(&cb);
139     ssh_set_server_callbacks(session, &cb);
140    
141     if (ssh_handle_key_exchange(session))
142     {
143     log_error("ssh_handle_key_exchange: %s\n", ssh_get_error(session));
144     return 1;
145     }
146     ssh_set_auth_methods(session, SSH_AUTH_METHOD_PASSWORD | SSH_AUTH_METHOD_GSSAPI_MIC);
147    
148     event = ssh_event_new();
149     ssh_event_add_session(event, session);
150    
151 sysadm 1.4 while (!(authenticated && SSH_channel != NULL))
152 sysadm 1.2 {
153     if (error)
154     break;
155     r = ssh_event_dopoll(event, -1);
156     if (r == SSH_ERROR)
157     {
158     log_error("Error : %s\n", ssh_get_error(session));
159     ssh_disconnect(session);
160     _exit(1);
161     }
162     }
163    
164     if (error)
165     {
166     log_error("Error, exiting loop\n");
167     _exit(1);
168     }
169     else
170     {
171 sysadm 1.3 log_common("Authenticated and got a channel\n");
172 sysadm 1.2 }
173    
174     snprintf(buf, sizeof(buf), "Hello, welcome to the Sample SSH proxy.\r\nPlease select your destination: ");
175 sysadm 1.4 ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf));
176 sysadm 1.2 do
177     {
178 sysadm 1.4 i = ssh_channel_read(SSH_channel, buf, sizeof(buf), 0);
179 sysadm 1.2 if (i > 0)
180     {
181 sysadm 1.4 ssh_channel_write(SSH_channel, buf, (uint32_t)i);
182 sysadm 1.2 if (strlen(host) + (size_t)i < sizeof(host))
183     {
184     strncat(host, buf, (size_t)i);
185     }
186     if (strchr(host, '\x0d'))
187     {
188     *strchr(host, '\x0d') = '\0';
189 sysadm 1.4 ssh_channel_write(SSH_channel, "\n", 1);
190 sysadm 1.2 break;
191     }
192     }
193     else
194     {
195     log_error("Error: %s\n", ssh_get_error(session));
196     _exit(1);
197     }
198     } while (i > 0);
199     snprintf(buf, sizeof(buf), "Trying to connect to \"%s\"\r\n", host);
200 sysadm 1.4 ssh_channel_write(SSH_channel, buf, (uint32_t)strlen(buf));
201 sysadm 1.3 log_common("%s", buf);
202 sysadm 1.2
203     ssh_disconnect(session);
204     ssh_free(session);
205    
206     _exit(0);
207     case -1:
208     log_error("Failed to fork\n");
209     break;
210     }
211 sysadm 1.1 }
212 sysadm 1.2 else
213 sysadm 1.1 {
214 sysadm 1.2 log_error("%s\n", ssh_get_error(sshbind));
215 sysadm 1.1 }
216    
217 sysadm 1.2 /* Since the session has been passed to a child fork, do some cleaning
218     * up at the parent process. */
219 sysadm 1.1 ssh_disconnect(session);
220     ssh_free(session);
221     }
222    
223     ssh_bind_free(sshbind);
224     ssh_finalize();
225    
226     return 0;
227     }
228    
229     int main(int argc, char *argv[])
230     {
231     if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
232     {
233     printf("Open log error\n");
234     return -1;
235     }
236    
237 sysadm 1.3 log_common_redir(STDOUT_FILENO);
238     log_error_redir(STDERR_FILENO);
239 sysadm 1.1
240     ssh_server("0.0.0.0", 2322);
241    
242     log_end();
243    
244     return 0;
245     }

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