Correct code to insert into a doubly-linked list.
[u/mdw/putty] / x11fwd.c
CommitLineData
9c964e85 1#include <stdio.h>
2#include <stdlib.h>
e0e7dff8 3#include <assert.h>
2f92b717 4#include <time.h>
9c964e85 5
6#include "putty.h"
7#include "ssh.h"
8
9c964e85 9#define GET_32BIT_LSB_FIRST(cp) \
10 (((unsigned long)(unsigned char)(cp)[0]) | \
11 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
12 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
13 ((unsigned long)(unsigned char)(cp)[3] << 24))
14
15#define PUT_32BIT_LSB_FIRST(cp, value) ( \
16 (cp)[0] = (value), \
17 (cp)[1] = (value) >> 8, \
18 (cp)[2] = (value) >> 16, \
19 (cp)[3] = (value) >> 24 )
20
21#define GET_16BIT_LSB_FIRST(cp) \
22 (((unsigned long)(unsigned char)(cp)[0]) | \
23 ((unsigned long)(unsigned char)(cp)[1] << 8))
24
25#define PUT_16BIT_LSB_FIRST(cp, value) ( \
26 (cp)[0] = (value), \
27 (cp)[1] = (value) >> 8 )
28
29#define GET_32BIT_MSB_FIRST(cp) \
30 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
31 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
32 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
33 ((unsigned long)(unsigned char)(cp)[3]))
34
35#define PUT_32BIT_MSB_FIRST(cp, value) ( \
36 (cp)[0] = (value) >> 24, \
37 (cp)[1] = (value) >> 16, \
38 (cp)[2] = (value) >> 8, \
39 (cp)[3] = (value) )
40
41#define GET_16BIT_MSB_FIRST(cp) \
42 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
43 ((unsigned long)(unsigned char)(cp)[1]))
44
45#define PUT_16BIT_MSB_FIRST(cp, value) ( \
46 (cp)[0] = (value) >> 8, \
47 (cp)[1] = (value) )
48
49#define GET_16BIT(endian, cp) \
50 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
51
52#define PUT_16BIT(endian, cp, val) \
53 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
54
e0e7dff8 55const char *const x11_authnames[] = {
2f92b717 56 "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"
e0e7dff8 57};
58
302121de 59struct X11Auth {
e0e7dff8 60 unsigned char fakedata[64], realdata[64];
61 int fakeproto, realproto;
62 int fakelen, reallen;
302121de 63};
64
9c964e85 65struct X11Private {
302121de 66 const struct plug_function_table *fn;
7e78000d 67 /* the above variable absolutely *must* be the first in this structure */
32874aea 68 unsigned char firstpkt[12]; /* first X data packet */
302121de 69 struct X11Auth *auth;
9c964e85 70 char *auth_protocol;
71 unsigned char *auth_data;
72 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
73 int verified;
5471d09a 74 int throttled, throttle_override;
32874aea 75 void *c; /* data used by ssh.c */
7e78000d 76 Socket s;
9c964e85 77};
78
302121de 79void *x11_invent_auth(char *proto, int protomaxlen,
32874aea 80 char *data, int datamaxlen)
81{
302121de 82 struct X11Auth *auth = smalloc(sizeof(struct X11Auth));
9c964e85 83 char ourdata[64];
84 int i;
85
e0e7dff8 86 auth->fakeproto = X11_MIT;
87
9c964e85 88 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
e0e7dff8 89 auth->fakelen = 16;
9c964e85 90 for (i = 0; i < 16; i++)
e0e7dff8 91 auth->fakedata[i] = random_byte();
9c964e85 92
93 /* Now format for the recipient. */
e0e7dff8 94 strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);
9c964e85 95 ourdata[0] = '\0';
e0e7dff8 96 for (i = 0; i < auth->fakelen; i++)
97 sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);
9c964e85 98 strncpy(data, ourdata, datamaxlen);
302121de 99
100 return auth;
9c964e85 101}
102
e0e7dff8 103/*
104 * Fetch the real auth data for a given display string, and store
105 * it in an X11Auth structure. Returns NULL on success, or an error
106 * string.
107 */
108void x11_get_real_auth(void *authv, char *display)
109{
110 struct X11Auth *auth = (struct X11Auth *)authv;
111
112 auth->realproto = X11_NO_AUTH; /* in case next call does nothing */
113
114 auth->reallen = sizeof(auth->realdata);
115 platform_get_x11_auth(display, &auth->realproto,
116 auth->realdata, &auth->reallen);
117}
118
302121de 119static int x11_verify(struct X11Auth *auth,
120 char *proto, unsigned char *data, int dlen)
32874aea 121{
e0e7dff8 122 if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0)
32874aea 123 return 0; /* wrong protocol attempted */
e0e7dff8 124 if (auth->fakeproto == X11_MIT) {
125 if (dlen != auth->fakelen)
126 return 0; /* cookie was wrong length */
127 if (memcmp(auth->fakedata, data, dlen) != 0)
128 return 0; /* cookie was wrong cookie! */
129 }
130 /* implement other protocols here if ever required */
9c964e85 131 return 1;
132}
133
32874aea 134static int x11_closing(Plug plug, char *error_msg, int error_code,
135 int calling_back)
136{
7e78000d 137 struct X11Private *pr = (struct X11Private *) plug;
138
139 /*
140 * We have no way to communicate down the forwarded connection,
141 * so if an error occurred on the socket, we just ignore it
142 * and treat it like a proper close.
143 */
144 sshfwd_close(pr->c);
145 x11_close(pr->s);
146 return 1;
147}
148
32874aea 149static int x11_receive(Plug plug, int urgent, char *data, int len)
150{
7e78000d 151 struct X11Private *pr = (struct X11Private *) plug;
9c964e85 152
5471d09a 153 if (sshfwd_write(pr->c, data, len) > 0) {
154 pr->throttled = 1;
155 sk_set_frozen(pr->s, 1);
156 }
157
9c964e85 158 return 1;
159}
160
5471d09a 161static void x11_sent(Plug plug, int bufsize)
162{
163 struct X11Private *pr = (struct X11Private *) plug;
164
165 sshfwd_unthrottle(pr->c, bufsize);
166}
167
9c964e85 168/*
421d6835 169 * When setting up X forwarding, we should send the screen number
170 * from the specified local display. This function extracts it from
171 * the display string.
172 */
173int x11_get_screen_number(char *display)
174{
175 int n;
176
177 n = strcspn(display, ":");
178 if (!display[n])
179 return 0;
180 n = strcspn(display, ".");
181 if (!display[n])
182 return 0;
183 return atoi(display + n + 1);
184}
185
186/*
9c964e85 187 * Called to set up the raw connection.
188 *
189 * Returns an error message, or NULL on success.
190 * also, fills the SocketsStructure
9c964e85 191 */
302121de 192char *x11_init(Socket * s, char *display, void *c, void *auth)
32874aea 193{
302121de 194 static const struct plug_function_table fn_table = {
7e78000d 195 x11_closing,
5471d09a 196 x11_receive,
197 x11_sent,
198 NULL
7e78000d 199 };
200
9c964e85 201 SockAddr addr;
202 int port;
0c6c9a70 203 char *err, *dummy_realhost;
9c964e85 204 char host[128];
205 int n, displaynum;
206 struct X11Private *pr;
207
208 /*
209 * Split up display name into host and display-number parts.
210 */
211 n = strcspn(display, ":");
212 if (display[n])
32874aea 213 displaynum = atoi(display + n + 1);
9c964e85 214 else
32874aea 215 displaynum = 0; /* sensible default */
216 if (n > sizeof(host) - 1)
217 n = sizeof(host) - 1;
ecc0ba5b 218 if (n > 0) {
219 strncpy(host, display, n);
220 host[n] = '\0';
221 } else {
222 /*
223 * Local display numbers, particularly on Unix, often omit
224 * the display part completely.
225 */
226 strcpy(host, "localhost");
227 }
9c964e85 228
9f2f8ea0 229 port = 6000 + displaynum;
230
9c964e85 231 /*
232 * Try to find host.
233 */
b7a189f3 234 addr = name_lookup(host, port, &dummy_realhost);
6518ea7b 235 if ((err = sk_addr_error(addr)) != NULL)
9c964e85 236 return err;
237
9c964e85 238 /*
239 * Open socket.
240 */
32874aea 241 pr = (struct X11Private *) smalloc(sizeof(struct X11Private));
7e78000d 242 pr->fn = &fn_table;
9c964e85 243 pr->auth_protocol = NULL;
302121de 244 pr->auth = (struct X11Auth *)auth;
9c964e85 245 pr->verified = 0;
246 pr->data_read = 0;
5471d09a 247 pr->throttled = pr->throttle_override = 0;
9c964e85 248 pr->c = c;
249
8eebd221 250 pr->s = *s = new_connection(addr, dummy_realhost, port, 0, 1, 0, (Plug) pr);
6518ea7b 251 if ((err = sk_socket_error(*s)) != NULL) {
32874aea 252 sfree(pr);
7e78000d 253 return err;
254 }
255
9c964e85 256 sk_set_private_ptr(*s, pr);
257 sk_addr_free(addr);
258 return NULL;
259}
260
32874aea 261void x11_close(Socket s)
262{
263 struct X11Private *pr;
d74d141c 264 if (!s)
265 return;
266 pr = (struct X11Private *) sk_get_private_ptr(s);
9c964e85 267 if (pr->auth_protocol) {
32874aea 268 sfree(pr->auth_protocol);
269 sfree(pr->auth_data);
9c964e85 270 }
271
272 sfree(pr);
273
274 sk_close(s);
275}
276
5471d09a 277void x11_unthrottle(Socket s)
278{
279 struct X11Private *pr;
280 if (!s)
281 return;
282 pr = (struct X11Private *) sk_get_private_ptr(s);
283
284 pr->throttled = 0;
285 sk_set_frozen(s, pr->throttled || pr->throttle_override);
286}
287
288void x11_override_throttle(Socket s, int enable)
289{
290 struct X11Private *pr;
291 if (!s)
292 return;
293 pr = (struct X11Private *) sk_get_private_ptr(s);
294
295 pr->throttle_override = enable;
296 sk_set_frozen(s, pr->throttled || pr->throttle_override);
297}
298
9c964e85 299/*
300 * Called to send data down the raw connection.
301 */
5471d09a 302int x11_send(Socket s, char *data, int len)
32874aea 303{
304 struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s);
9c964e85 305
306 if (s == NULL)
5471d09a 307 return 0;
9c964e85 308
309 /*
310 * Read the first packet.
311 */
312 while (len > 0 && pr->data_read < 12)
32874aea 313 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
9c964e85 314 if (pr->data_read < 12)
5471d09a 315 return 0;
9c964e85 316
317 /*
318 * If we have not allocated the auth_protocol and auth_data
319 * strings, do so now.
320 */
321 if (!pr->auth_protocol) {
32874aea 322 pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);
323 pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);
324 pr->auth_psize = (pr->auth_plen + 3) & ~3;
325 pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
326 /* Leave room for a terminating zero, to make our lives easier. */
327 pr->auth_protocol = (char *) smalloc(pr->auth_psize + 1);
f28e6b7c 328 pr->auth_data = (unsigned char *) smalloc(pr->auth_dsize);
9c964e85 329 }
330
331 /*
332 * Read the auth_protocol and auth_data strings.
333 */
334 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
32874aea 335 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
9c964e85 336 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
32874aea 337 pr->auth_data[pr->data_read++ - 12 -
338 pr->auth_psize] = (unsigned char) (len--, *data++);
9c964e85 339 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
5471d09a 340 return 0;
9c964e85 341
342 /*
343 * If we haven't verified the authentication, do so now.
344 */
345 if (!pr->verified) {
32874aea 346 int ret;
347
348 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
302121de 349 ret = x11_verify(pr->auth, pr->auth_protocol,
350 pr->auth_data, pr->auth_dlen);
32874aea 351
352 /*
353 * If authentication failed, construct and send an error
354 * packet, then terminate the connection.
355 */
356 if (!ret) {
357 char message[] = "Authentication failed at PuTTY X11 proxy";
358 unsigned char reply[8 + sizeof(message) + 4];
359 int msglen = sizeof(message) - 1; /* skip zero byte */
360 int msgsize = (msglen + 3) & ~3;
361 reply[0] = 0; /* failure */
362 reply[1] = msglen; /* length of reason string */
363 memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */
75105663 364 PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
32874aea 365 memset(reply + 8, 0, msgsize);
366 memcpy(reply + 8, message, msglen);
f28e6b7c 367 sshfwd_write(pr->c, (char *)reply, 8 + msgsize);
32874aea 368 sshfwd_close(pr->c);
369 x11_close(s);
5471d09a 370 return 0;
32874aea 371 }
372
373 /*
374 * Now we know we're going to accept the connection. Strip
e0e7dff8 375 * the fake auth data, and optionally put real auth data in
376 * instead.
32874aea 377 */
e0e7dff8 378 {
379 char realauthdata[64];
380 int realauthlen = 0;
381 int authstrlen = strlen(x11_authnames[pr->auth->realproto]);
2f92b717 382 unsigned long ip;
383 int port;
e0e7dff8 384 static const char zeroes[4] = { 0,0,0,0 };
385
386 if (pr->auth->realproto == X11_MIT) {
387 assert(pr->auth->reallen <= lenof(realauthdata));
388 realauthlen = pr->auth->reallen;
389 memcpy(realauthdata, pr->auth->realdata, realauthlen);
2f92b717 390 } else if (pr->auth->realproto == X11_XDM &&
391 pr->auth->reallen == 16 &&
392 sk_getxdmdata(s, &ip, &port)) {
393 time_t t;
394 realauthlen = 24;
395 memset(realauthdata, 0, 24);
396 memcpy(realauthdata, pr->auth->realdata, 8);
397 PUT_32BIT_MSB_FIRST(realauthdata+8, ip);
398 PUT_16BIT_MSB_FIRST(realauthdata+12, port);
399 t = time(NULL);
400 PUT_32BIT_MSB_FIRST(realauthdata+14, t);
6e7f5aee 401 des_encrypt_xdmauth(pr->auth->realdata+9,
402 (unsigned char *)realauthdata, 24);
2f92b717 403 }
e0e7dff8 404 /* implement other auth methods here if required */
405
406 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
407 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
408
409 sk_write(s, (char *)pr->firstpkt, 12);
410
411 if (authstrlen) {
412 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);
413 sk_write(s, zeroes, 3 & (-authstrlen));
414 }
415 if (realauthlen) {
416 sk_write(s, realauthdata, realauthlen);
417 sk_write(s, zeroes, 3 & (-realauthlen));
418 }
419 }
32874aea 420 pr->verified = 1;
9c964e85 421 }
422
423 /*
424 * After initialisation, just copy data simply.
425 */
426
5471d09a 427 return sk_write(s, data, len);
9c964e85 428}