forked from libssh2/libssh2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenssh_fixture.c
490 lines (436 loc) · 13.1 KB
/
openssh_fixture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* Copyright (C) Alexander Lamaison
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of the copyright holder nor the names
* of any other contributors may be used to endorse or
* promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "session_fixture.h"
#include "openssh_fixture.h"
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#if defined(_WIN32) && defined(_WIN64)
#define LIBSSH2_SOCKET_MASK "%lld"
#else
#define LIBSSH2_SOCKET_MASK "%d"
#endif
#ifdef LIBSSH2_WINDOWS_UWP
#define popen(x, y) (NULL)
#define pclose(x) (-1)
#elif defined(_WIN32)
#define popen _popen
#define pclose _pclose
#endif
static int have_docker = 0;
int openssh_fixture_have_docker(void)
{
return have_docker;
}
static int run_command_varg(char **output, const char *command, va_list args)
LIBSSH2_PRINTF(2, 0);
static int run_command_varg(char **output, const char *command, va_list args)
{
static const char redirect_stderr[] = "%s 2>&1";
FILE *pipe;
char command_buf[BUFSIZ];
char buf[BUFSIZ + sizeof(redirect_stderr)];
int ret;
size_t buf_len;
if(output) {
*output = NULL;
}
/* Format the command string */
ret = vsnprintf(command_buf, sizeof(command_buf), command, args);
if(ret < 0 || ret >= BUFSIZ) {
fprintf(stderr, "Unable to format command (%s)\n", command);
return -1;
}
/* Rewrite the command to redirect stderr to stdout to we can output it */
if(strlen(command_buf) + strlen(redirect_stderr) >= sizeof(buf)) {
fprintf(stderr, "Unable to rewrite command (%s)\n", command);
return -1;
}
ret = snprintf(buf, sizeof(buf), redirect_stderr, command_buf);
if(ret < 0 || ret >= BUFSIZ) {
fprintf(stderr, "Unable to rewrite command (%s)\n", command);
return -1;
}
fprintf(stdout, "Command: %s\n", command_buf);
pipe = popen(buf, "r");
if(!pipe) {
fprintf(stderr, "Unable to execute command '%s'\n", command);
return -1;
}
buf[0] = 0;
buf_len = 0;
while(buf_len < (sizeof(buf) - 1) &&
fgets(&buf[buf_len], (int)(sizeof(buf) - buf_len), pipe)) {
buf_len = strlen(buf);
}
ret = pclose(pipe);
if(ret) {
fprintf(stderr, "Error running command '%s' (exit %d): %s\n",
command, ret, buf);
}
if(output) {
/* command output may contain a trailing newline, so we trim
* whitespace here */
size_t end = strlen(buf);
while(end > 0 && isspace((int)buf[end - 1])) {
buf[end - 1] = '\0';
}
*output = strdup(buf);
}
return ret;
}
static int run_command(char **output, const char *command, ...)
LIBSSH2_PRINTF(2, 3);
static int run_command(char **output, const char *command, ...)
{
va_list args;
int ret;
va_start(args, command);
ret = run_command_varg(output, command, args);
va_end(args);
return ret;
}
static const char *openssh_server_image(void)
{
return getenv("OPENSSH_SERVER_IMAGE");
}
static int build_openssh_server_docker_image(void)
{
if(have_docker) {
const char *container_image_name = openssh_server_image();
if(container_image_name) {
int ret = run_command(NULL, "docker pull %s",
container_image_name);
if(ret == 0) {
ret = run_command(NULL, "docker tag %s libssh2/openssh_server",
container_image_name);
if(ret == 0) {
return ret;
}
}
}
return run_command(NULL,
"docker build --quiet -t libssh2/openssh_server %s",
srcdir_path("openssh_server"));
}
else {
return 0;
}
}
static const char *openssh_server_port(void)
{
return getenv("OPENSSH_SERVER_PORT");
}
static int start_openssh_server(char **container_id_out)
{
if(have_docker) {
const char *container_host_port = openssh_server_port();
if(container_host_port) {
return run_command(container_id_out,
"docker run --rm -d -p %s:22 "
"libssh2/openssh_server",
container_host_port);
}
return run_command(container_id_out,
"docker run --rm -d -p 22 "
"libssh2/openssh_server");
}
else {
*container_id_out = strdup("");
return 0;
}
}
static int stop_openssh_server(char *container_id)
{
if(have_docker) {
return run_command(NULL, "docker stop %s", container_id);
}
else {
return 0;
}
}
static const char *docker_machine_name(void)
{
return getenv("DOCKER_MACHINE_NAME");
}
static int is_running_inside_a_container(void)
{
#ifdef _WIN32
return 0;
#else
const char *cgroup_filename = "/proc/self/cgroup";
FILE *f;
char *line = NULL;
size_t len = 0;
ssize_t read;
int found = 0;
f = fopen(cgroup_filename, "r");
if(!f) {
/* Don't go further, we are not in a container */
return 0;
}
while((read = getline(&line, &len, f)) != -1) {
if(strstr(line, "docker")) {
found = 1;
break;
}
}
fclose(f);
free(line);
return found;
#endif
}
static void portable_sleep(unsigned int seconds)
{
#ifdef _WIN32
Sleep(seconds);
#else
sleep(seconds);
#endif
}
static int ip_address_from_container(char *container_id, char **ip_address_out)
{
const char *active_docker_machine = docker_machine_name();
if(active_docker_machine) {
/* This can be flaky when tests run in parallel (see
https://github.com/docker/machine/issues/2612), so we retry a few
times with exponential backoff if it fails */
int attempt_no = 0;
unsigned int wait_time = 500;
for(;;) {
int ret = run_command(ip_address_out, "docker-machine ip %s",
active_docker_machine);
if(ret == 0) {
return 0;
}
else if(attempt_no > 5) {
fprintf(
stderr,
"Unable to get IP from docker-machine after %d attempts\n",
attempt_no);
return -1;
}
else {
portable_sleep(wait_time);
++attempt_no;
wait_time *= 2;
}
}
}
else {
if(is_running_inside_a_container()) {
return run_command(ip_address_out,
"docker inspect --format "
"\"{{ .NetworkSettings.IPAddress }}\""
" %s",
container_id);
}
else {
return run_command(ip_address_out,
"docker inspect --format "
"\"{{ index (index (index "
".NetworkSettings.Ports "
"\\\"22/tcp\\\") 0) \\\"HostIp\\\" }}\" %s",
container_id);
}
}
}
static int port_from_container(char *container_id, char **port_out)
{
if(is_running_inside_a_container()) {
*port_out = strdup("22");
return 0;
}
else {
return run_command(port_out,
"docker inspect --format "
"\"{{ index (index (index .NetworkSettings.Ports "
"\\\"22/tcp\\\") 0) \\\"HostPort\\\" }}\" %s",
container_id);
}
}
static libssh2_socket_t open_socket_to_container(char *container_id)
{
char *ip_address = NULL;
char *port_string = NULL;
uint32_t hostaddr;
libssh2_socket_t sock;
struct sockaddr_in sin;
unsigned int counter;
libssh2_socket_t ret = LIBSSH2_INVALID_SOCKET;
if(have_docker) {
int res;
res = ip_address_from_container(container_id, &ip_address);
if(res) {
fprintf(stderr, "Failed to get IP address for container %s\n",
container_id);
goto cleanup;
}
res = port_from_container(container_id, &port_string);
if(res) {
fprintf(stderr, "Failed to get port for container %s\n",
container_id);
goto cleanup;
}
}
else {
const char *env;
env = getenv("OPENSSH_SERVER_HOST");
if(!env) {
env = "127.0.0.1";
}
ip_address = strdup(env);
env = openssh_server_port();
if(!env) {
env = "4711";
}
port_string = strdup(env);
}
/* 0.0.0.0 is returned by Docker for Windows, because the container
is reachable from anywhere. But we cannot connect to 0.0.0.0,
instead we assume localhost and try to connect to 127.0.0.1. */
if(ip_address && strcmp(ip_address, "0.0.0.0") == 0) {
free(ip_address);
ip_address = strdup("127.0.0.1");
}
hostaddr = inet_addr(ip_address);
if(hostaddr == (uint32_t)(-1)) {
fprintf(stderr, "Failed to convert %s host address\n", ip_address);
goto cleanup;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == LIBSSH2_INVALID_SOCKET) {
fprintf(stderr,
"Failed to open socket (" LIBSSH2_SOCKET_MASK ")\n", sock);
goto cleanup;
}
sin.sin_family = AF_INET;
sin.sin_port = htons((unsigned short)strtol(port_string, NULL, 0));
sin.sin_addr.s_addr = hostaddr;
for(counter = 0; counter < 3; ++counter) {
if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in))) {
fprintf(stderr,
"Connection to %s:%s attempt #%d failed: retrying...\n",
ip_address, port_string, counter);
portable_sleep(1 + 2*counter);
}
else {
ret = sock;
break;
}
}
if(ret == LIBSSH2_INVALID_SOCKET) {
fprintf(stderr, "Failed to connect to %s:%s\n",
ip_address, port_string);
goto cleanup;
}
cleanup:
free(ip_address);
free(port_string);
return ret;
}
static void close_socket_to_container(libssh2_socket_t sock)
{
if(sock != LIBSSH2_INVALID_SOCKET) {
shutdown(sock, 2 /* SHUT_RDWR */);
#ifdef _WIN32
closesocket(sock);
#else
close(sock);
#endif
}
}
static char *running_container_id = NULL;
int start_openssh_fixture(void)
{
int ret;
#ifdef _WIN32
WSADATA wsadata;
ret = WSAStartup(MAKEWORD(2, 0), &wsadata);
if(ret) {
fprintf(stderr, "WSAStartup failed with error: %d\n", ret);
return 1;
}
#endif
have_docker = !getenv("OPENSSH_NO_DOCKER");
ret = build_openssh_server_docker_image();
if(!ret) {
return start_openssh_server(&running_container_id);
}
else {
fprintf(stderr, "Failed to build docker image\n");
return ret;
}
}
void stop_openssh_fixture(void)
{
if(running_container_id) {
stop_openssh_server(running_container_id);
free(running_container_id);
running_container_id = NULL;
}
else if(have_docker) {
fprintf(stderr, "Cannot stop container - none started\n");
}
#ifdef _WIN32
WSACleanup();
#endif
}
libssh2_socket_t open_socket_to_openssh_server(void)
{
return open_socket_to_container(running_container_id);
}
void close_socket_to_openssh_server(libssh2_socket_t sock)
{
close_socket_to_container(sock);
}