winproxy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #define DEFINE_PLUG_METHOD_MACROS
  9. #include "tree234.h"
  10. #include "putty.h"
  11. #include "network.h"
  12. #include "proxy.h"
  13. typedef struct Socket_localproxy_tag *Local_Proxy_Socket;
  14. #ifdef MPEXT
  15. extern char *do_select(Plug plug, SOCKET skt, int startup);
  16. #endif
  17. struct Socket_localproxy_tag {
  18. const struct socket_function_table *fn;
  19. /* the above variable absolutely *must* be the first in this structure */
  20. HANDLE to_cmd_H, from_cmd_H;
  21. struct handle *to_cmd_h, *from_cmd_h;
  22. char *error;
  23. Plug plug;
  24. void *privptr;
  25. };
  26. int localproxy_gotdata(struct handle *h, void *data, int len)
  27. {
  28. Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);
  29. if (len < 0) {
  30. return plug_closing(ps->plug, "Read error from local proxy command",
  31. 0, 0);
  32. } else if (len == 0) {
  33. return plug_closing(ps->plug, NULL, 0, 0);
  34. } else {
  35. return plug_receive(ps->plug, 0, data, len);
  36. }
  37. }
  38. void localproxy_sentdata(struct handle *h, int new_backlog)
  39. {
  40. Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);
  41. plug_sent(ps->plug, new_backlog);
  42. }
  43. static Plug sk_localproxy_plug (Socket s, Plug p)
  44. {
  45. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  46. Plug ret = ps->plug;
  47. if (p)
  48. ps->plug = p;
  49. return ret;
  50. }
  51. static void sk_localproxy_close (Socket s)
  52. {
  53. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  54. #ifdef MPEXT
  55. // WinSCP core uses do_select as signalization of connection up/down
  56. do_select(ps->plug, INVALID_SOCKET, 0);
  57. #endif
  58. handle_free(ps->to_cmd_h);
  59. handle_free(ps->from_cmd_h);
  60. CloseHandle(ps->to_cmd_H);
  61. CloseHandle(ps->from_cmd_H);
  62. sfree(ps);
  63. }
  64. static int sk_localproxy_write (Socket s, const char *data, int len)
  65. {
  66. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  67. return handle_write(ps->to_cmd_h, data, len);
  68. }
  69. static int sk_localproxy_write_oob(Socket s, const char *data, int len)
  70. {
  71. /*
  72. * oob data is treated as inband; nasty, but nothing really
  73. * better we can do
  74. */
  75. return sk_localproxy_write(s, data, len);
  76. }
  77. static void sk_localproxy_write_eof(Socket s)
  78. {
  79. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  80. handle_write_eof(ps->to_cmd_h);
  81. }
  82. static void sk_localproxy_flush(Socket s)
  83. {
  84. /* Local_Proxy_Socket ps = (Local_Proxy_Socket) s; */
  85. /* do nothing */
  86. }
  87. static void sk_localproxy_set_private_ptr(Socket s, void *ptr)
  88. {
  89. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  90. ps->privptr = ptr;
  91. }
  92. static void *sk_localproxy_get_private_ptr(Socket s)
  93. {
  94. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  95. return ps->privptr;
  96. }
  97. static void sk_localproxy_set_frozen(Socket s, int is_frozen)
  98. {
  99. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  100. /*
  101. * FIXME
  102. */
  103. }
  104. static const char *sk_localproxy_socket_error(Socket s)
  105. {
  106. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  107. return ps->error;
  108. }
  109. Socket platform_new_connection(SockAddr addr, char *hostname,
  110. int port, int privport,
  111. int oobinline, int nodelay, int keepalive,
  112. Plug plug, Conf *conf)
  113. {
  114. char *cmd;
  115. static const struct socket_function_table socket_fn_table = {
  116. sk_localproxy_plug,
  117. sk_localproxy_close,
  118. sk_localproxy_write,
  119. sk_localproxy_write_oob,
  120. sk_localproxy_write_eof,
  121. sk_localproxy_flush,
  122. sk_localproxy_set_private_ptr,
  123. sk_localproxy_get_private_ptr,
  124. sk_localproxy_set_frozen,
  125. sk_localproxy_socket_error
  126. };
  127. Local_Proxy_Socket ret;
  128. HANDLE us_to_cmd, us_from_cmd, cmd_to_us, cmd_from_us;
  129. SECURITY_ATTRIBUTES sa;
  130. STARTUPINFO si;
  131. PROCESS_INFORMATION pi;
  132. if (conf_get_int(conf, CONF_proxy_type) != PROXY_CMD)
  133. return NULL;
  134. cmd = format_telnet_command(addr, port, conf);
  135. {
  136. char *msg = dupprintf("Starting local proxy command: %s", cmd);
  137. /* We're allowed to pass NULL here, because we're part of the Windows
  138. * front end so we know logevent doesn't expect any data. */
  139. logevent(NULL, msg);
  140. sfree(msg);
  141. }
  142. ret = snew(struct Socket_localproxy_tag);
  143. ret->fn = &socket_fn_table;
  144. ret->plug = plug;
  145. ret->error = NULL;
  146. /*
  147. * Create the pipes to the proxy command, and spawn the proxy
  148. * command process.
  149. */
  150. sa.nLength = sizeof(sa);
  151. sa.lpSecurityDescriptor = NULL; /* default */
  152. sa.bInheritHandle = TRUE;
  153. if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
  154. ret->error = dupprintf("Unable to create pipes for proxy command");
  155. sfree(cmd);
  156. return (Socket)ret;
  157. }
  158. if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
  159. CloseHandle(us_from_cmd);
  160. CloseHandle(cmd_to_us);
  161. ret->error = dupprintf("Unable to create pipes for proxy command");
  162. sfree(cmd);
  163. return (Socket)ret;
  164. }
  165. SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);
  166. SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);
  167. si.cb = sizeof(si);
  168. si.lpReserved = NULL;
  169. si.lpDesktop = NULL;
  170. si.lpTitle = NULL;
  171. si.dwFlags = STARTF_USESTDHANDLES;
  172. si.cbReserved2 = 0;
  173. si.lpReserved2 = NULL;
  174. si.hStdInput = cmd_from_us;
  175. si.hStdOutput = cmd_to_us;
  176. si.hStdError = NULL;
  177. CreateProcess(NULL, cmd, NULL, NULL, TRUE,
  178. CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
  179. NULL, NULL, &si, &pi);
  180. CloseHandle(pi.hProcess);
  181. CloseHandle(pi.hThread);
  182. sfree(cmd);
  183. CloseHandle(cmd_from_us);
  184. CloseHandle(cmd_to_us);
  185. ret->to_cmd_H = us_to_cmd;
  186. ret->from_cmd_H = us_from_cmd;
  187. ret->from_cmd_h = handle_input_new(ret->from_cmd_H, localproxy_gotdata,
  188. ret, 0);
  189. ret->to_cmd_h = handle_output_new(ret->to_cmd_H, localproxy_sentdata,
  190. ret, 0);
  191. /* We are responsible for this and don't need it any more */
  192. sk_addr_free(addr);
  193. #ifdef MPEXT
  194. // WinSCP core uses do_select as signalization of connection up/down
  195. do_select(plug, INVALID_SOCKET, 1);
  196. #endif
  197. return (Socket) ret;
  198. }