Configurable TCP_NODELAY option on network connections
[u/mdw/putty] / x11fwd.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "putty.h"
6 #include "ssh.h"
7
8 #ifndef FALSE
9 #define FALSE 0
10 #endif
11 #ifndef TRUE
12 #define TRUE 1
13 #endif
14
15 #define GET_32BIT_LSB_FIRST(cp) \
16 (((unsigned long)(unsigned char)(cp)[0]) | \
17 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
18 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
19 ((unsigned long)(unsigned char)(cp)[3] << 24))
20
21 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
22 (cp)[0] = (value), \
23 (cp)[1] = (value) >> 8, \
24 (cp)[2] = (value) >> 16, \
25 (cp)[3] = (value) >> 24 )
26
27 #define GET_16BIT_LSB_FIRST(cp) \
28 (((unsigned long)(unsigned char)(cp)[0]) | \
29 ((unsigned long)(unsigned char)(cp)[1] << 8))
30
31 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
32 (cp)[0] = (value), \
33 (cp)[1] = (value) >> 8 )
34
35 #define GET_32BIT_MSB_FIRST(cp) \
36 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
37 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
38 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
39 ((unsigned long)(unsigned char)(cp)[3]))
40
41 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
42 (cp)[0] = (value) >> 24, \
43 (cp)[1] = (value) >> 16, \
44 (cp)[2] = (value) >> 8, \
45 (cp)[3] = (value) )
46
47 #define GET_16BIT_MSB_FIRST(cp) \
48 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
49 ((unsigned long)(unsigned char)(cp)[1]))
50
51 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
52 (cp)[0] = (value) >> 8, \
53 (cp)[1] = (value) )
54
55 #define GET_16BIT(endian, cp) \
56 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
57
58 #define PUT_16BIT(endian, cp, val) \
59 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
60
61 struct X11Private {
62 struct plug_function_table *fn;
63 /* the above variable absolutely *must* be the first in this structure */
64 unsigned char firstpkt[12]; /* first X data packet */
65 char *auth_protocol;
66 unsigned char *auth_data;
67 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
68 int verified;
69 int throttled, throttle_override;
70 void *c; /* data used by ssh.c */
71 Socket s;
72 };
73
74 void x11_close(Socket s);
75
76 static unsigned char x11_authdata[64];
77 static int x11_authdatalen;
78
79 void x11_invent_auth(char *proto, int protomaxlen,
80 char *data, int datamaxlen)
81 {
82 char ourdata[64];
83 int i;
84
85 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
86 x11_authdatalen = 16;
87 for (i = 0; i < 16; i++)
88 x11_authdata[i] = random_byte();
89
90 /* Now format for the recipient. */
91 strncpy(proto, "MIT-MAGIC-COOKIE-1", protomaxlen);
92 ourdata[0] = '\0';
93 for (i = 0; i < x11_authdatalen; i++)
94 sprintf(ourdata + strlen(ourdata), "%02x", x11_authdata[i]);
95 strncpy(data, ourdata, datamaxlen);
96 }
97
98 static int x11_verify(char *proto, unsigned char *data, int dlen)
99 {
100 if (strcmp(proto, "MIT-MAGIC-COOKIE-1") != 0)
101 return 0; /* wrong protocol attempted */
102 if (dlen != x11_authdatalen)
103 return 0; /* cookie was wrong length */
104 if (memcmp(x11_authdata, data, dlen) != 0)
105 return 0; /* cookie was wrong cookie! */
106 return 1;
107 }
108
109 static int x11_closing(Plug plug, char *error_msg, int error_code,
110 int calling_back)
111 {
112 struct X11Private *pr = (struct X11Private *) plug;
113
114 /*
115 * We have no way to communicate down the forwarded connection,
116 * so if an error occurred on the socket, we just ignore it
117 * and treat it like a proper close.
118 */
119 sshfwd_close(pr->c);
120 x11_close(pr->s);
121 return 1;
122 }
123
124 static int x11_receive(Plug plug, int urgent, char *data, int len)
125 {
126 struct X11Private *pr = (struct X11Private *) plug;
127
128 if (sshfwd_write(pr->c, data, len) > 0) {
129 pr->throttled = 1;
130 sk_set_frozen(pr->s, 1);
131 }
132
133 return 1;
134 }
135
136 static void x11_sent(Plug plug, int bufsize)
137 {
138 struct X11Private *pr = (struct X11Private *) plug;
139
140 sshfwd_unthrottle(pr->c, bufsize);
141 }
142
143 /*
144 * Called to set up the raw connection.
145 *
146 * Returns an error message, or NULL on success.
147 * also, fills the SocketsStructure
148 */
149 char *x11_init(Socket * s, char *display, void *c)
150 {
151 static struct plug_function_table fn_table = {
152 x11_closing,
153 x11_receive,
154 x11_sent,
155 NULL
156 };
157
158 SockAddr addr;
159 int port;
160 char *err, *dummy_realhost;
161 char host[128];
162 int n, displaynum;
163 struct X11Private *pr;
164
165 /*
166 * Split up display name into host and display-number parts.
167 */
168 n = strcspn(display, ":");
169 if (display[n])
170 displaynum = atoi(display + n + 1);
171 else
172 displaynum = 0; /* sensible default */
173 if (n > sizeof(host) - 1)
174 n = sizeof(host) - 1;
175 strncpy(host, display, n);
176 host[n] = '\0';
177
178 /*
179 * Try to find host.
180 */
181 addr = sk_namelookup(host, &dummy_realhost);
182 if ((err = sk_addr_error(addr)))
183 return err;
184
185 port = 6000 + displaynum;
186
187 /*
188 * Open socket.
189 */
190 pr = (struct X11Private *) smalloc(sizeof(struct X11Private));
191 pr->fn = &fn_table;
192 pr->auth_protocol = NULL;
193 pr->verified = 0;
194 pr->data_read = 0;
195 pr->throttled = pr->throttle_override = 0;
196 pr->c = c;
197
198 pr->s = *s = sk_new(addr, port, 0, 1, 0, (Plug) pr);
199 if ((err = sk_socket_error(*s))) {
200 sfree(pr);
201 return err;
202 }
203
204 sk_set_private_ptr(*s, pr);
205 sk_addr_free(addr);
206 return NULL;
207 }
208
209 void x11_close(Socket s)
210 {
211 struct X11Private *pr;
212 if (!s)
213 return;
214 pr = (struct X11Private *) sk_get_private_ptr(s);
215 if (pr->auth_protocol) {
216 sfree(pr->auth_protocol);
217 sfree(pr->auth_data);
218 }
219
220 sfree(pr);
221
222 sk_close(s);
223 }
224
225 void x11_unthrottle(Socket s)
226 {
227 struct X11Private *pr;
228 if (!s)
229 return;
230 pr = (struct X11Private *) sk_get_private_ptr(s);
231
232 pr->throttled = 0;
233 sk_set_frozen(s, pr->throttled || pr->throttle_override);
234 }
235
236 void x11_override_throttle(Socket s, int enable)
237 {
238 struct X11Private *pr;
239 if (!s)
240 return;
241 pr = (struct X11Private *) sk_get_private_ptr(s);
242
243 pr->throttle_override = enable;
244 sk_set_frozen(s, pr->throttled || pr->throttle_override);
245 }
246
247 /*
248 * Called to send data down the raw connection.
249 */
250 int x11_send(Socket s, char *data, int len)
251 {
252 struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s);
253
254 if (s == NULL)
255 return 0;
256
257 /*
258 * Read the first packet.
259 */
260 while (len > 0 && pr->data_read < 12)
261 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
262 if (pr->data_read < 12)
263 return 0;
264
265 /*
266 * If we have not allocated the auth_protocol and auth_data
267 * strings, do so now.
268 */
269 if (!pr->auth_protocol) {
270 pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);
271 pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);
272 pr->auth_psize = (pr->auth_plen + 3) & ~3;
273 pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
274 /* Leave room for a terminating zero, to make our lives easier. */
275 pr->auth_protocol = (char *) smalloc(pr->auth_psize + 1);
276 pr->auth_data = (char *) smalloc(pr->auth_dsize);
277 }
278
279 /*
280 * Read the auth_protocol and auth_data strings.
281 */
282 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
283 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
284 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
285 pr->auth_data[pr->data_read++ - 12 -
286 pr->auth_psize] = (unsigned char) (len--, *data++);
287 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
288 return 0;
289
290 /*
291 * If we haven't verified the authentication, do so now.
292 */
293 if (!pr->verified) {
294 int ret;
295
296 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
297 ret = x11_verify(pr->auth_protocol, pr->auth_data, pr->auth_dlen);
298
299 /*
300 * If authentication failed, construct and send an error
301 * packet, then terminate the connection.
302 */
303 if (!ret) {
304 char message[] = "Authentication failed at PuTTY X11 proxy";
305 unsigned char reply[8 + sizeof(message) + 4];
306 int msglen = sizeof(message) - 1; /* skip zero byte */
307 int msgsize = (msglen + 3) & ~3;
308 reply[0] = 0; /* failure */
309 reply[1] = msglen; /* length of reason string */
310 memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */
311 PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
312 memset(reply + 8, 0, msgsize);
313 memcpy(reply + 8, message, msglen);
314 sshfwd_write(pr->c, reply, 8 + msgsize);
315 sshfwd_close(pr->c);
316 x11_close(s);
317 return 0;
318 }
319
320 /*
321 * Now we know we're going to accept the connection. Strip
322 * the auth data. (TODO: if we ever work out how, we should
323 * replace some real auth data in here.)
324 */
325 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, 0); /* auth proto */
326 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, 0); /* auth data */
327 sk_write(s, pr->firstpkt, 12);
328 pr->verified = 1;
329 }
330
331 /*
332 * After initialisation, just copy data simply.
333 */
334
335 return sk_write(s, data, len);
336 }