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