| 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/types.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
|
| 11 |
#include "torture.h" |
| 12 |
#include "torture_key.h" |
| 13 |
#include <libssh/libssh.h> |
| 14 |
|
| 15 |
#define TEST_SERVER_HOST "127.0.0.1" |
| 16 |
#define TEST_SERVER_PORT 2222 |
| 17 |
|
| 18 |
struct hostkey_state { |
| 19 |
const char *hostkey; |
| 20 |
char *hostkey_path; |
| 21 |
enum ssh_keytypes_e key_type; |
| 22 |
int fd; |
| 23 |
}; |
| 24 |
|
| 25 |
static int setup(void **state) { |
| 26 |
struct hostkey_state *h; |
| 27 |
mode_t mask; |
| 28 |
int rc; |
| 29 |
|
| 30 |
ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 31 |
rc = ssh_init(); |
| 32 |
if (rc != SSH_OK) { |
| 33 |
return -1; |
| 34 |
} |
| 35 |
|
| 36 |
h = malloc(sizeof(struct hostkey_state)); |
| 37 |
assert_non_null(h); |
| 38 |
|
| 39 |
h->hostkey_path = strdup("/tmp/libssh_hostkey_XXXXXX"); |
| 40 |
|
| 41 |
mask = umask(S_IRWXO | S_IRWXG); |
| 42 |
h->fd = mkstemp(h->hostkey_path); |
| 43 |
umask(mask); |
| 44 |
assert_return_code(h->fd, errno); |
| 45 |
close(h->fd); |
| 46 |
|
| 47 |
h->key_type = SSH_KEYTYPE_ECDSA_P256; |
| 48 |
h->hostkey = torture_get_testkey(h->key_type, 0); |
| 49 |
|
| 50 |
torture_write_file(h->hostkey_path, h->hostkey); |
| 51 |
|
| 52 |
*state = h; |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
static int teardown(void **state) { |
| 58 |
struct hostkey_state *h = (struct hostkey_state *)*state; |
| 59 |
|
| 60 |
unlink(h->hostkey); |
| 61 |
free(h->hostkey_path); |
| 62 |
free(h); |
| 63 |
|
| 64 |
ssh_finalize(); |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
/* For x11_screen_number, need something that is not equal to htonl |
| 70 |
itself */ |
| 71 |
static const uint32_t x11_screen_number = 1; |
| 72 |
|
| 73 |
static void *client_thread(void *arg) { |
| 74 |
unsigned int test_port = TEST_SERVER_PORT; |
| 75 |
int rc; |
| 76 |
ssh_session session; |
| 77 |
ssh_channel channel; |
| 78 |
|
| 79 |
/* unused */ |
| 80 |
(void)arg; |
| 81 |
|
| 82 |
usleep(200); |
| 83 |
session = torture_ssh_session(NULL, TEST_SERVER_HOST, |
| 84 |
&test_port, |
| 85 |
"foo", "bar"); |
| 86 |
assert_non_null(session); |
| 87 |
|
| 88 |
channel = ssh_channel_new(session); |
| 89 |
assert_non_null(channel); |
| 90 |
|
| 91 |
rc = ssh_channel_open_session(channel); |
| 92 |
assert_int_equal(rc, SSH_OK); |
| 93 |
|
| 94 |
rc = ssh_channel_request_x11(channel, 0, NULL, NULL, |
| 95 |
(uint32_t)x11_screen_number); |
| 96 |
assert_int_equal(rc, SSH_OK); |
| 97 |
|
| 98 |
ssh_free(session); |
| 99 |
return NULL; |
| 100 |
} |
| 101 |
|
| 102 |
static int auth_password_accept(ssh_session session, |
| 103 |
const char *user, |
| 104 |
const char *password, |
| 105 |
void *userdata) { |
| 106 |
/* unused */ |
| 107 |
(void)session; |
| 108 |
(void)user; |
| 109 |
(void)password; |
| 110 |
(void)userdata; |
| 111 |
|
| 112 |
return SSH_AUTH_SUCCESS; |
| 113 |
} |
| 114 |
|
| 115 |
struct channel_data { |
| 116 |
int req_seen; |
| 117 |
uint32_t screen_number; |
| 118 |
}; |
| 119 |
|
| 120 |
static void ssh_channel_x11_req(ssh_session session, |
| 121 |
ssh_channel channel, |
| 122 |
int single_connection, |
| 123 |
const char *auth_protocol, |
| 124 |
const char *auth_cookie, |
| 125 |
uint32_t screen_number, |
| 126 |
void *userdata) { |
| 127 |
struct channel_data *channel_data = userdata; |
| 128 |
|
| 129 |
/* unused */ |
| 130 |
(void)session; |
| 131 |
(void)channel; |
| 132 |
(void)single_connection; |
| 133 |
(void)auth_protocol; |
| 134 |
(void)auth_cookie; |
| 135 |
|
| 136 |
/* We've seen an x11 request. Record the screen number */ |
| 137 |
channel_data->req_seen = 1; |
| 138 |
channel_data->screen_number = screen_number; |
| 139 |
} |
| 140 |
|
| 141 |
static ssh_channel channel_open(ssh_session session, void *userdata) { |
| 142 |
ssh_channel channel = NULL; |
| 143 |
ssh_channel_callbacks channel_cb = userdata; |
| 144 |
|
| 145 |
/* unused */ |
| 146 |
(void)userdata; |
| 147 |
|
| 148 |
channel = ssh_channel_new(session); |
| 149 |
if (channel == NULL) { |
| 150 |
goto out; |
| 151 |
} |
| 152 |
ssh_set_channel_callbacks(channel, channel_cb); |
| 153 |
|
| 154 |
out: |
| 155 |
return channel; |
| 156 |
} |
| 157 |
|
| 158 |
static void test_ssh_channel_request_x11(void **state) { |
| 159 |
struct hostkey_state *h = (struct hostkey_state *)*state; |
| 160 |
int rc, event_rc; |
| 161 |
pthread_t client_pthread; |
| 162 |
ssh_bind sshbind; |
| 163 |
ssh_session server; |
| 164 |
ssh_event event; |
| 165 |
|
| 166 |
struct channel_data channel_data; |
| 167 |
struct ssh_channel_callbacks_struct channel_cb = { |
| 168 |
.userdata = &channel_data, |
| 169 |
.channel_x11_req_function = ssh_channel_x11_req |
| 170 |
}; |
| 171 |
struct ssh_server_callbacks_struct server_cb = { |
| 172 |
.userdata = &channel_cb, |
| 173 |
.auth_password_function = auth_password_accept, |
| 174 |
.channel_open_request_session_function = channel_open |
| 175 |
}; |
| 176 |
|
| 177 |
memset(&channel_data, 0, sizeof(channel_data)); |
| 178 |
ssh_callbacks_init(&channel_cb); |
| 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 |
/* Get client to connect */ |
| 189 |
rc = pthread_create(&client_pthread, NULL, client_thread, NULL); |
| 190 |
assert_return_code(rc, errno); |
| 191 |
|
| 192 |
server = ssh_new(); |
| 193 |
assert_non_null(server); |
| 194 |
|
| 195 |
rc = ssh_bind_accept(sshbind, server); |
| 196 |
assert_int_equal(rc, SSH_OK); |
| 197 |
|
| 198 |
/* Handle client connection */ |
| 199 |
ssh_set_server_callbacks(server, &server_cb); |
| 200 |
|
| 201 |
rc = ssh_handle_key_exchange(server); |
| 202 |
assert_int_equal(rc, SSH_OK); |
| 203 |
|
| 204 |
event = ssh_event_new(); |
| 205 |
assert_non_null(event); |
| 206 |
|
| 207 |
ssh_event_add_session(event, server); |
| 208 |
|
| 209 |
event_rc = SSH_OK; |
| 210 |
while (!channel_data.req_seen && event_rc == SSH_OK) { |
| 211 |
event_rc = ssh_event_dopoll(event, -1); |
| 212 |
} |
| 213 |
|
| 214 |
/* Cleanup */ |
| 215 |
ssh_event_free(event); |
| 216 |
ssh_free(server); |
| 217 |
ssh_bind_free(sshbind); |
| 218 |
|
| 219 |
rc = pthread_join(client_pthread, NULL); |
| 220 |
assert_int_equal(rc, 0); |
| 221 |
|
| 222 |
assert_true(channel_data.req_seen); |
| 223 |
assert_int_equal(channel_data.screen_number, |
| 224 |
x11_screen_number); |
| 225 |
} |
| 226 |
|
| 227 |
int torture_run_tests(void) { |
| 228 |
int rc; |
| 229 |
const struct CMUnitTest tests[] = { |
| 230 |
cmocka_unit_test_setup_teardown(test_ssh_channel_request_x11, |
| 231 |
setup, |
| 232 |
teardown) |
| 233 |
}; |
| 234 |
|
| 235 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 236 |
return rc; |
| 237 |
} |
| 238 |
|