/[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.2 - (hide annotations)
Wed Jun 4 10:05:07 2025 UTC (9 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.1: +171 -35 lines
Content type: text/x-csrc
Refine test_ssh_server based on libssh examples/proxy.c

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

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