winproxy.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * winproxy.c: Windows implementation of platform_new_connection(),
  3. * supporting an OpenSSH-like proxy command via the winhandl.c
  4. * mechanism.
  5. */
  6. #include <stdio.h>
  7. #include <assert.h>
  8. #include "tree234.h"
  9. #include "putty.h"
  10. #include "network.h"
  11. #include "proxy.h"
  12. Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
  13. Plug *plug, int overlapped);
  14. Socket *platform_new_connection(SockAddr *addr, const char *hostname,
  15. int port, int privport,
  16. int oobinline, int nodelay, int keepalive,
  17. Plug *plug, Conf *conf)
  18. {
  19. char *cmd;
  20. HANDLE us_to_cmd, cmd_from_us;
  21. HANDLE us_from_cmd, cmd_to_us;
  22. HANDLE us_from_cmd_err, cmd_err_to_us;
  23. SECURITY_ATTRIBUTES sa;
  24. STARTUPINFO si;
  25. PROCESS_INFORMATION pi;
  26. if (conf_get_int(conf, CONF_proxy_type) != PROXY_CMD)
  27. return NULL;
  28. cmd = format_telnet_command(addr, port, conf);
  29. /* We are responsible for this and don't need it any more */
  30. sk_addr_free(addr);
  31. {
  32. char *msg = dupprintf("Starting local proxy command: %s", cmd);
  33. plug_log(plug, 2, NULL, 0, msg, 0);
  34. sfree(msg);
  35. }
  36. /*
  37. * Create the pipes to the proxy command, and spawn the proxy
  38. * command process.
  39. */
  40. sa.nLength = sizeof(sa);
  41. sa.lpSecurityDescriptor = NULL; /* default */
  42. sa.bInheritHandle = TRUE;
  43. if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
  44. Socket *ret =
  45. new_error_socket("Unable to create pipes for proxy command", plug);
  46. sfree(cmd);
  47. return ret;
  48. }
  49. if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
  50. Socket *ret =
  51. new_error_socket("Unable to create pipes for proxy command", plug);
  52. sfree(cmd);
  53. CloseHandle(us_from_cmd);
  54. CloseHandle(cmd_to_us);
  55. return ret;
  56. }
  57. if (!CreatePipe(&us_from_cmd_err, &cmd_err_to_us, &sa, 0)) {
  58. Socket *ret = new_error_socket
  59. ("Unable to create pipes for proxy command", plug);
  60. sfree(cmd);
  61. CloseHandle(us_from_cmd);
  62. CloseHandle(cmd_to_us);
  63. CloseHandle(us_to_cmd);
  64. CloseHandle(cmd_from_us);
  65. return ret;
  66. }
  67. SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);
  68. SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);
  69. if (us_from_cmd_err != NULL)
  70. SetHandleInformation(us_from_cmd_err, HANDLE_FLAG_INHERIT, 0);
  71. si.cb = sizeof(si);
  72. si.lpReserved = NULL;
  73. si.lpDesktop = NULL;
  74. si.lpTitle = NULL;
  75. si.dwFlags = STARTF_USESTDHANDLES;
  76. si.cbReserved2 = 0;
  77. si.lpReserved2 = NULL;
  78. si.hStdInput = cmd_from_us;
  79. si.hStdOutput = cmd_to_us;
  80. si.hStdError = cmd_err_to_us;
  81. CreateProcess(NULL, cmd, NULL, NULL, TRUE,
  82. CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
  83. NULL, NULL, &si, &pi);
  84. CloseHandle(pi.hProcess);
  85. CloseHandle(pi.hThread);
  86. sfree(cmd);
  87. CloseHandle(cmd_from_us);
  88. CloseHandle(cmd_to_us);
  89. if (cmd_err_to_us != NULL)
  90. CloseHandle(cmd_err_to_us);
  91. return make_handle_socket(us_to_cmd, us_from_cmd, us_from_cmd_err,
  92. plug, FALSE);
  93. }