Debian Codegraph @

tests/unittests/torture_server_direct_tcpip.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 static int setup(void **state)
31 {
32 struct hostkey_state *h = NULL;
33 mode_t mask;
34 int rc;
35
36 ssh_threads_set_callbacks(ssh_threads_get_pthread());
37 rc = ssh_init();
38 if (rc != SSH_OK) {
39 return -1;
40 }
41
42 h = malloc(sizeof(struct hostkey_state));
43 assert_non_null(h);
44
45 h->hostkey_path = strdup("/tmp/libssh_hostkey_XXXXXX");
46 assert_non_null(h->hostkey_path);
47
48 mask = umask(S_IRWXO | S_IRWXG);
49 h->fd = mkstemp(h->hostkey_path);
50 umask(mask);
51 assert_return_code(h->fd, errno);
52 close(h->fd);
53
54 h->key_type = SSH_KEYTYPE_ECDSA_P256;
55 h->hostkey = torture_get_testkey(h->key_type, 0);
56
57 torture_write_file(h->hostkey_path, h->hostkey);
58
59 *state = h;
60
61 return 0;
62 }
63
64 static int teardown(void **state)
65 {
66 struct hostkey_state *h = (struct hostkey_state *)*state;
67
68 unlink(h->hostkey_path);
69 free(h->hostkey_path);
70 free(h);
71
72 ssh_finalize();
73
74 return 0;
75 }
76
77 static void *client_thread(void *arg)
78 {
79 unsigned int test_port = TEST_SERVER_PORT;
80 int rc;
81 ssh_session session = NULL;
82 ssh_channel channel = NULL;
83 bool should_accept = *(bool *)arg;
84
85 session =
86 torture_ssh_session(NULL, TEST_SERVER_HOST, &test_port, "foo", "bar");
87 assert_non_null(session);
88
89 channel = ssh_channel_new(session);
90 assert_non_null(channel);
91
92 /* Open a direct-tcpip channel instead of a session channel */
93 rc = ssh_channel_open_forward(channel,
94 TEST_DEST_HOST,
95 TEST_DEST_PORT,
96 TEST_ORIG_HOST,
97 TEST_ORIG_PORT);
98 if (should_accept) {
99 assert_int_equal(rc, SSH_OK);
100 } else {
101 assert_int_equal(rc, SSH_ERROR);
102 }
103
104 /* Close the channel and session */
105 ssh_channel_close(channel);
106 ssh_channel_free(channel);
107 ssh_free(session);
108
109 return NULL;
110 }
111
112 static int auth_password_accept(ssh_session session,
113 const char *user,
114 const char *password,
115 void *userdata)
116 {
117 /* unused */
118 (void)session;
119 (void)user;
120 (void)password;
121 (void)userdata;
122
123 return SSH_AUTH_SUCCESS;
124 }
125
126 struct channel_data {
127 /* Whether the callback should accept the channel open request */
128 bool should_accept;
129
130 int req_seen;
131 char *dest_host;
132 uint32_t dest_port;
133 char *orig_host;
134 uint32_t orig_port;
135 };
136
137 static ssh_channel channel_direct_tcpip_callback(ssh_session session,
138 const char *dest_host,
139 int dest_port,
140 const char *orig_host,
141 int orig_port,
142 void *userdata)
143 {
144 struct channel_data *channel_data = userdata;
145 ssh_channel channel = NULL;
146
147 /* Record that we've seen a direct-tcpip request and store the parameters */
148 channel_data->req_seen = 1;
149 channel_data->dest_host = strdup(dest_host);
150 channel_data->dest_port = dest_port;
151 channel_data->orig_host = strdup(orig_host);
152 channel_data->orig_port = orig_port;
153
154 /* Create and return a new channel for this request */
155 if (channel_data->should_accept) {
156 channel = ssh_channel_new(session);
157 }
158 return channel;
159 }
160
161 static void torture_ssh_channel_direct_tcpip(void **state, int should_accept)
162 {
163 struct hostkey_state *h = (struct hostkey_state *)*state;
164 int rc, event_rc;
165 pthread_t client_pthread;
166 ssh_bind sshbind = NULL;
167 ssh_session server = NULL;
168 ssh_event event = NULL;
169
170 struct channel_data channel_data;
171 struct ssh_server_callbacks_struct server_cb = {
172 .userdata = &channel_data,
173 .auth_password_function = auth_password_accept,
174 .channel_open_request_direct_tcpip_function =
175 channel_direct_tcpip_callback,
176 };
177
178 memset(&channel_data, 0, sizeof(channel_data));
179 ssh_callbacks_init(&server_cb);
180
181 /* Create server */
182 sshbind = torture_ssh_bind(TEST_SERVER_HOST,
183 TEST_SERVER_PORT,
184 h->key_type,
185 h->hostkey_path);
186 assert_non_null(sshbind);
187
188 channel_data.should_accept = should_accept;
189
190 /* Get client to connect */
191 rc = pthread_create(&client_pthread,
192 NULL,
193 client_thread,
194 &channel_data.should_accept);
195 assert_return_code(rc, errno);
196
197 server = ssh_new();
198 assert_non_null(server);
199
200 rc = ssh_bind_accept(sshbind, server);
201 assert_int_equal(rc, SSH_OK);
202
203 /* Handle client connection */
204 ssh_set_server_callbacks(server, &server_cb);
205
206 rc = ssh_handle_key_exchange(server);
207 assert_int_equal(rc, SSH_OK);
208
209 event = ssh_event_new();
210 assert_non_null(event);
211
212 ssh_event_add_session(event, server);
213
214 event_rc = SSH_OK;
215 while (!channel_data.req_seen && event_rc == SSH_OK) {
216 event_rc = ssh_event_dopoll(event, -1);
217 }
218
219 /* Cleanup */
220 ssh_event_free(event);
221 ssh_free(server);
222 ssh_bind_free(sshbind);
223
224 rc = pthread_join(client_pthread, NULL);
225 assert_int_equal(rc, 0);
226
227 /* Verify direct-tcpip request parameters */
228 assert_true(channel_data.req_seen);
229 assert_string_equal(channel_data.dest_host, TEST_DEST_HOST);
230 assert_int_equal(channel_data.dest_port, TEST_DEST_PORT);
231 assert_string_equal(channel_data.orig_host, TEST_ORIG_HOST);
232 assert_int_equal(channel_data.orig_port, TEST_ORIG_PORT);
233
234 /* Free allocated memory */
235 free(channel_data.dest_host);
236 free(channel_data.orig_host);
237 }
238
239 static void torture_ssh_channel_direct_tcpip_success(void **state)
240 {
241 torture_ssh_channel_direct_tcpip(state, true);
242 }
243
244 static void torture_ssh_channel_direct_tcpip_failure(void **state)
245 {
246 torture_ssh_channel_direct_tcpip(state, false);
247 }
248
249 int torture_run_tests(void)
250 {
251 int rc;
252 const struct CMUnitTest tests[] = {
253 cmocka_unit_test_setup_teardown(
254 torture_ssh_channel_direct_tcpip_success,
255 setup,
256 teardown),
257 cmocka_unit_test_setup_teardown(
258 torture_ssh_channel_direct_tcpip_failure,
259 setup,
260 teardown),
261 };
262
263 rc = cmocka_run_group_tests(tests, NULL, NULL);
264 return rc;
265 }