Debian Codegraph @

tests/unittests/torture_forwarded_tcpip_callback.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #include <errno.h>
6 #include <pthread.h>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10
11 #include "torture.h"
12 #include "torture_key.h"
13
14 #include <libssh/libssh.h>
15
16 #define TEST_SERVER_HOST "127.0.0.1"
17 #define TEST_SERVER_PORT 2222
18 #define TEST_DEST_HOST "127.0.0.1"
19 #define TEST_DEST_PORT 12345
20 #define TEST_ORIG_HOST "127.0.0.1"
21 #define TEST_ORIG_PORT 54321
22
23 struct hostkey_state {
24 const char *hostkey;
25 char *hostkey_path;
26 enum ssh_keytypes_e key_type;
27 int fd;
28 };
29
30 struct server_thread_args {
31 struct hostkey_state *h;
32 bool should_accept;
33 };
34
35 static bool is_server_ready = false;
36 static pthread_mutex_t server_mutex = PTHREAD_MUTEX_INITIALIZER;
37 static pthread_cond_t server_cond = PTHREAD_COND_INITIALIZER;
38
39 static bool client_callbacks_initialised = false;
40 static pthread_mutex_t client_mutex = PTHREAD_MUTEX_INITIALIZER;
41 static pthread_cond_t client_cond = PTHREAD_COND_INITIALIZER;
42
43 static int setup(void **state)
44 {
45 struct hostkey_state *h = NULL;
46 mode_t mask;
47 int rc;
48
49 ssh_threads_set_callbacks(ssh_threads_get_pthread());
50 rc = ssh_init();
51 if (rc != SSH_OK) {
52 return -1;
53 }
54
55 h = (struct hostkey_state *)malloc(sizeof(struct hostkey_state));
56 assert_non_null(h);
57
58 h->hostkey_path = strdup("/tmp/libssh_hostkey_XXXXXX");
59 assert_non_null(h->hostkey_path);
60
61 mask = umask(S_IRWXO | S_IRWXG);
62 h->fd = mkstemp(h->hostkey_path);
63 umask(mask);
64 assert_return_code(h->fd, errno);
65 close(h->fd);
66
67 h->key_type = SSH_KEYTYPE_ECDSA_P256;
68 h->hostkey = torture_get_testkey(h->key_type, 0);
69
70 torture_write_file(h->hostkey_path, h->hostkey);
71
72 *state = h;
73
74 /* Reset before every test */
75 is_server_ready = false;
76 client_callbacks_initialised = false;
77
78 return 0;
79 }
80
81 static int teardown(void **state)
82 {
83 struct hostkey_state *h = (struct hostkey_state *)*state;
84
85 unlink(h->hostkey_path);
86 free(h->hostkey_path);
87 free(h);
88
89 ssh_finalize();
90
91 return 0;
92 }
93
94 static int auth_password_accept(ssh_session session,
95 const char *user,
96 const char *password,
97 void *userdata)
98 {
99 /* unused */
100 (void)session;
101 (void)user;
102 (void)password;
103 (void)userdata;
104
105 return SSH_AUTH_SUCCESS;
106 }
107
108 static void *server_thread(void *arg)
109 {
110 struct server_thread_args *args = (struct server_thread_args *)arg;
111 struct hostkey_state *h = args->h;
112 bool should_accept = args->should_accept;
113 ssh_bind sshbind = NULL;
114 ssh_session server = NULL;
115 ssh_channel channel = NULL;
116 ssh_event event = NULL;
117 int rc;
118
119 struct ssh_server_callbacks_struct server_cb = {
120 .auth_password_function = auth_password_accept,
121 };
122 ssh_callbacks_init(&server_cb);
123
124 /* Create server */
125 sshbind = torture_ssh_bind(TEST_SERVER_HOST,
126 TEST_SERVER_PORT,
127 h->key_type,
128 h->hostkey_path);
129 assert_non_null(sshbind);
130
131 server = ssh_new();
132 assert_non_null(server);
133
134 rc = ssh_set_server_callbacks(server, &server_cb);
135 assert_int_equal(rc, SSH_OK);
136
137 /* Signal that the server is ready */
138 pthread_mutex_lock(&server_mutex);
139 is_server_ready = true;
140 pthread_cond_signal(&server_cond);
141 pthread_mutex_unlock(&server_mutex);
142
143 rc = ssh_bind_accept(sshbind, server);
144 assert_int_equal(rc, SSH_OK);
145
146 rc = ssh_handle_key_exchange(server);
147 assert_int_equal(rc, SSH_OK);
148
149 /* Handle client connection */
150 event = ssh_event_new();
151 assert_non_null(event);
152
153 rc = ssh_event_add_session(event, server);
154 assert_int_equal(rc, SSH_OK);
155
156 /* Poll until authentication is complete */
157 while (server->session_state != SSH_SESSION_STATE_AUTHENTICATED) {
158 rc = ssh_event_dopoll(event, -1);
159 if (rc == SSH_ERROR) {
160 break;
161 }
162 }
163
164 /* Cleanup the event */
165 ssh_event_free(event);
166
167 /* Wait for client callbacks to be initialized before proceeding */
168 pthread_mutex_lock(&client_mutex);
169 while (!client_callbacks_initialised) {
170 pthread_cond_wait(&client_cond, &client_mutex);
171 }
172 pthread_mutex_unlock(&client_mutex);
173
174 channel = ssh_channel_new(server);
175 assert_non_null(channel);
176
177 rc = ssh_channel_open_reverse_forward(channel,
178 TEST_DEST_HOST,
179 TEST_DEST_PORT,
180 TEST_ORIG_HOST,
181 TEST_ORIG_PORT);
182 if (should_accept) {
183 assert_int_equal(rc, SSH_OK);
184 } else {
185 assert_int_equal(rc, SSH_ERROR);
186 }
187
188 ssh_channel_close(channel);
189 ssh_channel_free(channel);
190 ssh_bind_free(sshbind);
191 ssh_free(server);
192
193 return NULL;
194 }
195
196 struct channel_data {
197 /* Whether the callback should accept the channel open request */
198 bool should_accept;
199
200 int req_seen;
201 char *dest_host;
202 uint32_t dest_port;
203 char *orig_host;
204 uint32_t orig_port;
205 };
206
207 static ssh_channel channel_forwarded_tcpip_callback(ssh_session session,
208 const char *dest_host,
209 int dest_port,
210 const char *orig_host,
211 int orig_port,
212 void *userdata)
213 {
214 struct channel_data *channel_data = (struct channel_data *)userdata;
215 ssh_channel channel = NULL;
216
217 /* Record that we've seen a forwarded-tcpip request and store the parameters
218 */
219 channel_data->req_seen = 1;
220 channel_data->dest_host = strdup(dest_host);
221 channel_data->dest_port = dest_port;
222 channel_data->orig_host = strdup(orig_host);
223 channel_data->orig_port = orig_port;
224
225 /* Create and return a new channel for this request */
226 if (channel_data->should_accept) {
227 channel = ssh_channel_new(session);
228 }
229
230 return channel;
231 }
232
233 static void torture_forwarded_tcpip_callback(void **state, bool should_accept)
234 {
235 int rc, event_rc;
236 pthread_t server_pthread;
237 ssh_session session = NULL;
238 ssh_event event = NULL;
239 struct channel_data channel_data;
240 unsigned int server_port = TEST_SERVER_PORT;
241
242 struct server_thread_args args = {
243 .h = (struct hostkey_state *)*state,
244 .should_accept = should_accept,
245 };
246
247 struct ssh_callbacks_struct client_cb = {
248 .userdata = &channel_data,
249 .channel_open_request_forwarded_tcpip_function =
250 channel_forwarded_tcpip_callback,
251 };
252 ssh_callbacks_init(&client_cb);
253
254 memset(&channel_data, 0, sizeof(channel_data));
255 channel_data.should_accept = should_accept;
256
257 rc = pthread_create(&server_pthread, NULL, server_thread, &args);
258 assert_return_code(rc, errno);
259
260 /* Wait for the server to be ready using condition variable */
261 pthread_mutex_lock(&server_mutex);
262 while (!is_server_ready) {
263 pthread_cond_wait(&server_cond, &server_mutex);
264 }
265 pthread_mutex_unlock(&server_mutex);
266
267 session =
268 torture_ssh_session(NULL, "127.0.0.1", &server_port, "foo", "bar");
269 assert_non_null(session);
270
271 rc = ssh_set_callbacks(session, &client_cb);
272 assert_int_equal(rc, SSH_OK);
273
274 event = ssh_event_new();
275 assert_non_null(event);
276
277 rc = ssh_event_add_session(event, session);
278 assert_int_equal(rc, SSH_OK);
279
280 /* Signal that client callbacks are initialized */
281 pthread_mutex_lock(&client_mutex);
282 client_callbacks_initialised = true;
283 pthread_cond_signal(&client_cond);
284 pthread_mutex_unlock(&client_mutex);
285
286 event_rc = SSH_OK;
287 while (channel_data.req_seen != 1 && event_rc == SSH_OK) {
288 event_rc = ssh_event_dopoll(event, -1);
289 }
290
291 /* Cleanup */
292 ssh_event_free(event);
293 ssh_free(session);
294
295 rc = pthread_join(server_pthread, NULL);
296 assert_int_equal(rc, 0);
297
298 /* Verify forwarded-tcpip request parameters */
299 assert_true(channel_data.req_seen);
300 assert_string_equal(channel_data.dest_host, TEST_DEST_HOST);
301 assert_int_equal(channel_data.dest_port, TEST_DEST_PORT);
302 assert_string_equal(channel_data.orig_host, TEST_ORIG_HOST);
303 assert_int_equal(channel_data.orig_port, TEST_ORIG_PORT);
304
305 /* Free allocated memory */
306 free(channel_data.dest_host);
307 free(channel_data.orig_host);
308 }
309
310 static void torture_forwarded_tcpip_callback_success(void **state)
311 {
312 torture_forwarded_tcpip_callback(state, true);
313 }
314
315 static void torture_forwarded_tcpip_callback_failure(void **state)
316 {
317 torture_forwarded_tcpip_callback(state, false);
318 }
319
320 int torture_run_tests(void)
321 {
322 int rc;
323 const struct CMUnitTest tests[] = {
324 cmocka_unit_test_setup_teardown(
325 torture_forwarded_tcpip_callback_success,
326 setup,
327 teardown),
328 cmocka_unit_test_setup_teardown(
329 torture_forwarded_tcpip_callback_failure,
330 setup,
331 teardown),
332 };
333
334 rc = cmocka_run_group_tests(tests, NULL, NULL);
335 return rc;
336 }