openssh_fixture.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Copyright (C) 2016 Alexander Lamaison
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms,
  5. * with or without modification, are permitted provided
  6. * that the following conditions are met:
  7. *
  8. * Redistributions of source code must retain the above
  9. * copyright notice, this list of conditions and the
  10. * following disclaimer.
  11. *
  12. * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. *
  17. * Neither the name of the copyright holder nor the names
  18. * of any other contributors may be used to endorse or
  19. * promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  23. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  35. * OF SUCH DAMAGE.
  36. */
  37. #include "openssh_fixture.h"
  38. #include "libssh2_config.h"
  39. #ifdef HAVE_WINSOCK2_H
  40. #include <winsock2.h>
  41. #endif
  42. #ifdef HAVE_SYS_SOCKET_H
  43. #include <sys/socket.h>
  44. #endif
  45. #ifdef HAVE_ARPA_INET_H
  46. #include <arpa/inet.h>
  47. #endif
  48. #ifdef HAVE_NETINET_IN_H
  49. #include <netinet/in.h>
  50. #endif
  51. #ifdef HAVE_UNISTD_H
  52. #include <unistd.h>
  53. #endif
  54. #include <ctype.h>
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. static int run_command(const char *command, char **output)
  59. {
  60. FILE *pipe;
  61. char command_buf[BUFSIZ];
  62. int ret;
  63. if (output) {
  64. *output = NULL;
  65. }
  66. /* Rewrite the command to redirect stderr to stdout to we can output it */
  67. ret = snprintf(command_buf, sizeof(command_buf), "%s 2>&1", command);
  68. if (ret < 0 || ret >= BUFSIZ) {
  69. fprintf(stderr, "Unable to format command (%s)\n", command);
  70. return -1;
  71. }
  72. fprintf(stdout, "Command: %s\n", command);
  73. #ifdef WIN32
  74. pipe = _popen(command_buf, "r");
  75. #else
  76. pipe = popen(command_buf, "r");
  77. #endif
  78. if (pipe) {
  79. char buf[BUFSIZ];
  80. char *p = buf;
  81. while (fgets(p, sizeof(buf) - (p - buf), pipe) != NULL)
  82. ;
  83. #ifdef WIN32
  84. ret = _pclose(pipe);
  85. #else
  86. ret = pclose(pipe);
  87. #endif
  88. if (ret == 0) {
  89. if (output) {
  90. /* command output may contain a trailing newline, so we trim
  91. * whitespace here */
  92. size_t end = strlen(buf) - 1;
  93. while (end > 0 && isspace(buf[end])) {
  94. buf[end] = '\0';
  95. }
  96. *output = strdup(buf);
  97. }
  98. }
  99. else {
  100. fprintf(stderr, "Error running command '%s' (exit %d): %s\n",
  101. command, ret, buf);
  102. }
  103. return ret;
  104. }
  105. else {
  106. fprintf(stderr, "Unable to execute command '%s'\n", command);
  107. return -1;
  108. }
  109. }
  110. static int build_openssh_server_docker_image()
  111. {
  112. return run_command("docker build -t libssh2/openssh_server openssh_server",
  113. NULL);
  114. }
  115. static int start_openssh_server(char **container_id_out)
  116. {
  117. return run_command("docker run --detach -P libssh2/openssh_server",
  118. container_id_out);
  119. }
  120. static int stop_openssh_server(char *container_id)
  121. {
  122. char command_buf[BUFSIZ];
  123. int rc = snprintf(command_buf, sizeof(command_buf), "docker stop %s",
  124. container_id);
  125. if (rc > -1 && rc < BUFSIZ) {
  126. return run_command(command_buf, NULL);
  127. }
  128. else {
  129. return rc;
  130. }
  131. }
  132. static const char *docker_machine_name()
  133. {
  134. return getenv("DOCKER_MACHINE_NAME");
  135. }
  136. static int ip_address_from_container(char *container_id, char **ip_address_out)
  137. {
  138. const char *active_docker_machine = docker_machine_name();
  139. if (active_docker_machine != NULL) {
  140. // This can be flaky when tests run in parallel (see
  141. // https://github.com/docker/machine/issues/2612), so we retry a few
  142. // times with exponential backoff if it fails
  143. int attempt_no = 0;
  144. int wait_time = 500;
  145. for (;;) {
  146. char command_buf[BUFSIZ];
  147. int rc = snprintf(command_buf, sizeof(command_buf),
  148. "docker-machine ip %s", active_docker_machine);
  149. if (rc > -1 && rc < BUFSIZ) {
  150. return run_command(command_buf, ip_address_out);
  151. }
  152. if (attempt_no > 5) {
  153. fprintf(
  154. stderr,
  155. "Unable to get IP from docker-machine after %d attempts\n",
  156. attempt_no);
  157. return -1;
  158. }
  159. else {
  160. #ifdef WIN32
  161. #pragma warning(push)
  162. #pragma warning(disable : 4996)
  163. _sleep(wait_time);
  164. #pragma warning(pop)
  165. #else
  166. sleep(wait_time);
  167. #endif
  168. ++attempt_no;
  169. wait_time *= 2;
  170. }
  171. }
  172. }
  173. else {
  174. char command_buf[BUFSIZ];
  175. int rc = snprintf(
  176. command_buf, sizeof(command_buf),
  177. "docker inspect --format \"{{ index (index (index "
  178. ".NetworkSettings.Ports \\\"22/tcp\\\") 0) \\\"HostIp\\\" }}\" %s",
  179. container_id);
  180. if (rc > -1 && rc < BUFSIZ) {
  181. return run_command(command_buf, ip_address_out);
  182. }
  183. else {
  184. return rc;
  185. }
  186. }
  187. }
  188. static int port_from_container(char *container_id, char **port_out)
  189. {
  190. char command_buf[BUFSIZ];
  191. int rc = snprintf(
  192. command_buf, sizeof(command_buf),
  193. "docker inspect --format \"{{ index (index (index "
  194. ".NetworkSettings.Ports \\\"22/tcp\\\") 0) \\\"HostPort\\\" }}\" %s",
  195. container_id);
  196. if (rc > -1 && rc < BUFSIZ) {
  197. return run_command(command_buf, port_out);
  198. }
  199. else {
  200. return rc;
  201. }
  202. }
  203. static int open_socket_to_container(char *container_id)
  204. {
  205. char *ip_address = NULL;
  206. int ret = ip_address_from_container(container_id, &ip_address);
  207. if (ret == 0) {
  208. char *port_string = NULL;
  209. ret = port_from_container(container_id, &port_string);
  210. if (ret == 0) {
  211. unsigned long hostaddr = inet_addr(ip_address);
  212. if (hostaddr != (unsigned long)(-1)) {
  213. int sock = socket(AF_INET, SOCK_STREAM, 0);
  214. if (sock > -1) {
  215. struct sockaddr_in sin;
  216. sin.sin_family = AF_INET;
  217. sin.sin_port = htons((short)strtol(port_string, NULL, 0));
  218. sin.sin_addr.s_addr = hostaddr;
  219. if (connect(sock, (struct sockaddr *)(&sin),
  220. sizeof(struct sockaddr_in)) == 0) {
  221. ret = sock;
  222. }
  223. else {
  224. fprintf(stderr, "Failed to connect to %s:%s\n",
  225. ip_address, port_string);
  226. ret = -1;
  227. }
  228. }
  229. else {
  230. fprintf(stderr, "Failed to open socket (%d)\n", sock);
  231. ret = -1;
  232. }
  233. }
  234. else {
  235. fprintf(stderr, "Failed to convert %s host address\n",
  236. ip_address);
  237. ret = -1;
  238. }
  239. free(port_string);
  240. }
  241. else {
  242. fprintf(stderr, "Failed to get port for container %s\n",
  243. container_id);
  244. ret = -1;
  245. }
  246. free(ip_address);
  247. }
  248. else {
  249. fprintf(stderr, "Failed to get IP address for container %s\n",
  250. container_id);
  251. ret = -1;
  252. }
  253. return ret;
  254. }
  255. static char *running_container_id = NULL;
  256. int start_openssh_fixture()
  257. {
  258. int ret;
  259. #ifdef HAVE_WINSOCK2_H
  260. WSADATA wsadata;
  261. ret = WSAStartup(MAKEWORD(2, 0), &wsadata);
  262. if (ret != 0) {
  263. fprintf(stderr, "WSAStartup failed with error: %d\n", ret);
  264. return 1;
  265. }
  266. #endif
  267. ret = build_openssh_server_docker_image();
  268. if (ret == 0) {
  269. return start_openssh_server(&running_container_id);
  270. }
  271. else {
  272. fprintf(stderr, "Failed to build docker image\n");
  273. return ret;
  274. }
  275. }
  276. void stop_openssh_fixture()
  277. {
  278. if (running_container_id) {
  279. stop_openssh_server(running_container_id);
  280. free(running_container_id);
  281. running_container_id = NULL;
  282. }
  283. else {
  284. fprintf(stderr, "Cannot stop container - none started");
  285. }
  286. }
  287. int open_socket_to_openssh_server()
  288. {
  289. return open_socket_to_container(running_container_id);
  290. }