Fix `puttygen-unix-perms': f_open(), PuTTY's wrapper on fopen, now
[u/mdw/putty] / x11fwd.c
CommitLineData
46ed7b64 1/*
2 * Platform-independent bits of X11 forwarding.
3 */
4
9c964e85 5#include <stdio.h>
6#include <stdlib.h>
e0e7dff8 7#include <assert.h>
2f92b717 8#include <time.h>
9c964e85 9
10#include "putty.h"
11#include "ssh.h"
5f8ff48c 12#include "tree234.h"
9c964e85 13
9c964e85 14#define GET_16BIT(endian, cp) \
15 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
16
17#define PUT_16BIT(endian, cp, val) \
18 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
19
e0e7dff8 20const char *const x11_authnames[] = {
2f92b717 21 "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"
e0e7dff8 22};
23
5f8ff48c 24struct XDMSeen {
25 unsigned int time;
26 unsigned char clientid[6];
27};
28
302121de 29struct X11Auth {
e0e7dff8 30 unsigned char fakedata[64], realdata[64];
31 int fakeproto, realproto;
32 int fakelen, reallen;
5f8ff48c 33 tree234 *xdmseen;
302121de 34};
35
9c964e85 36struct X11Private {
302121de 37 const struct plug_function_table *fn;
7e78000d 38 /* the above variable absolutely *must* be the first in this structure */
32874aea 39 unsigned char firstpkt[12]; /* first X data packet */
302121de 40 struct X11Auth *auth;
9c964e85 41 char *auth_protocol;
42 unsigned char *auth_data;
43 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
44 int verified;
5471d09a 45 int throttled, throttle_override;
b3ebaa28 46 unsigned long peer_ip;
47 int peer_port;
32874aea 48 void *c; /* data used by ssh.c */
7e78000d 49 Socket s;
9c964e85 50};
51
5f8ff48c 52static int xdmseen_cmp(void *a, void *b)
53{
54 struct XDMSeen *sa = a, *sb = b;
55 return sa->time > sb->time ? 1 :
56 sa->time < sb->time ? -1 :
57 memcmp(sa->clientid, sb->clientid, sizeof(sa->clientid));
58}
59
302121de 60void *x11_invent_auth(char *proto, int protomaxlen,
b3ebaa28 61 char *data, int datamaxlen, int proto_id)
32874aea 62{
3d88e64d 63 struct X11Auth *auth = snew(struct X11Auth);
9c964e85 64 char ourdata[64];
65 int i;
66
b3ebaa28 67 if (proto_id == X11_MIT) {
68 auth->fakeproto = X11_MIT;
e0e7dff8 69
b3ebaa28 70 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
71 auth->fakelen = 16;
72 for (i = 0; i < 16; i++)
73 auth->fakedata[i] = random_byte();
537e9567 74 auth->xdmseen = NULL;
b3ebaa28 75 } else {
76 assert(proto_id == X11_XDM);
77 auth->fakeproto = X11_XDM;
78
79 /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */
80 auth->fakelen = 16;
81 for (i = 0; i < 16; i++)
82 auth->fakedata[i] = (i == 8 ? 0 : random_byte());
5f8ff48c 83 auth->xdmseen = newtree234(xdmseen_cmp);
b3ebaa28 84 }
9c964e85 85
86 /* Now format for the recipient. */
e0e7dff8 87 strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);
9c964e85 88 ourdata[0] = '\0';
e0e7dff8 89 for (i = 0; i < auth->fakelen; i++)
90 sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);
9c964e85 91 strncpy(data, ourdata, datamaxlen);
302121de 92
93 return auth;
9c964e85 94}
95
5f8ff48c 96void x11_free_auth(void *authv)
fabd1805 97{
5f8ff48c 98 struct X11Auth *auth = (struct X11Auth *)authv;
99 struct XDMSeen *seen;
fabd1805 100
5f8ff48c 101 if (auth->xdmseen != NULL) {
102 while ((seen = delpos234(auth->xdmseen, 0)) != NULL)
103 sfree(seen);
104 freetree234(auth->xdmseen);
105 }
fabd1805 106 sfree(auth);
107}
108
e0e7dff8 109/*
110 * Fetch the real auth data for a given display string, and store
111 * it in an X11Auth structure. Returns NULL on success, or an error
112 * string.
113 */
114void x11_get_real_auth(void *authv, char *display)
115{
116 struct X11Auth *auth = (struct X11Auth *)authv;
117
118 auth->realproto = X11_NO_AUTH; /* in case next call does nothing */
119
120 auth->reallen = sizeof(auth->realdata);
121 platform_get_x11_auth(display, &auth->realproto,
122 auth->realdata, &auth->reallen);
123}
124
5f8ff48c 125#define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */
126
b3ebaa28 127static char *x11_verify(unsigned long peer_ip, int peer_port,
128 struct X11Auth *auth, char *proto,
129 unsigned char *data, int dlen)
32874aea 130{
e0e7dff8 131 if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0)
b3ebaa28 132 return "wrong authentication protocol attempted";
e0e7dff8 133 if (auth->fakeproto == X11_MIT) {
134 if (dlen != auth->fakelen)
b3ebaa28 135 return "MIT-MAGIC-COOKIE-1 data was wrong length";
e0e7dff8 136 if (memcmp(auth->fakedata, data, dlen) != 0)
b3ebaa28 137 return "MIT-MAGIC-COOKIE-1 data did not match";
138 }
139 if (auth->fakeproto == X11_XDM) {
140 unsigned long t;
141 time_t tim;
142 int i;
5f8ff48c 143 struct XDMSeen *seen, *ret;
b3ebaa28 144
145 if (dlen != 24)
146 return "XDM-AUTHORIZATION-1 data was wrong length";
147 if (peer_port == -1)
148 return "cannot do XDM-AUTHORIZATION-1 without remote address data";
149 des_decrypt_xdmauth(auth->fakedata+9, data, 24);
150 if (memcmp(auth->fakedata, data, 8) != 0)
151 return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */
152 if (GET_32BIT_MSB_FIRST(data+8) != peer_ip)
153 return "XDM-AUTHORIZATION-1 data failed check"; /* IP wrong */
154 if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port)
155 return "XDM-AUTHORIZATION-1 data failed check"; /* port wrong */
156 t = GET_32BIT_MSB_FIRST(data+14);
157 for (i = 18; i < 24; i++)
158 if (data[i] != 0) /* zero padding wrong */
159 return "XDM-AUTHORIZATION-1 data failed check";
160 tim = time(NULL);
5f8ff48c 161 if (abs(t - tim) > XDM_MAXSKEW)
b3ebaa28 162 return "XDM-AUTHORIZATION-1 time stamp was too far out";
5f8ff48c 163 seen = snew(struct XDMSeen);
164 seen->time = t;
165 memcpy(seen->clientid, data+8, 6);
166 assert(auth->xdmseen != NULL);
167 ret = add234(auth->xdmseen, seen);
168 if (ret != seen) {
169 sfree(seen);
170 return "XDM-AUTHORIZATION-1 data replayed";
171 }
172 /* While we're here, purge entries too old to be replayed. */
173 for (;;) {
174 seen = index234(auth->xdmseen, 0);
175 assert(seen != NULL);
176 if (t - seen->time <= XDM_MAXSKEW)
177 break;
178 sfree(delpos234(auth->xdmseen, 0));
179 }
e0e7dff8 180 }
181 /* implement other protocols here if ever required */
b3ebaa28 182 return NULL;
9c964e85 183}
184
7555d6a5 185static void x11_log(Plug p, int type, SockAddr addr, int port,
186 const char *error_msg, int error_code)
187{
188 /* We have no interface to the logging module here, so we drop these. */
189}
190
cbe2d68f 191static int x11_closing(Plug plug, const char *error_msg, int error_code,
32874aea 192 int calling_back)
193{
7e78000d 194 struct X11Private *pr = (struct X11Private *) plug;
195
196 /*
197 * We have no way to communicate down the forwarded connection,
198 * so if an error occurred on the socket, we just ignore it
199 * and treat it like a proper close.
200 */
201 sshfwd_close(pr->c);
202 x11_close(pr->s);
203 return 1;
204}
205
32874aea 206static int x11_receive(Plug plug, int urgent, char *data, int len)
207{
7e78000d 208 struct X11Private *pr = (struct X11Private *) plug;
9c964e85 209
5471d09a 210 if (sshfwd_write(pr->c, data, len) > 0) {
211 pr->throttled = 1;
212 sk_set_frozen(pr->s, 1);
213 }
214
9c964e85 215 return 1;
216}
217
5471d09a 218static void x11_sent(Plug plug, int bufsize)
219{
220 struct X11Private *pr = (struct X11Private *) plug;
221
222 sshfwd_unthrottle(pr->c, bufsize);
223}
224
9c964e85 225/*
421d6835 226 * When setting up X forwarding, we should send the screen number
227 * from the specified local display. This function extracts it from
228 * the display string.
229 */
230int x11_get_screen_number(char *display)
231{
232 int n;
233
234 n = strcspn(display, ":");
235 if (!display[n])
236 return 0;
237 n = strcspn(display, ".");
238 if (!display[n])
239 return 0;
240 return atoi(display + n + 1);
241}
242
fc0f17db 243/* Find the right display, returns an allocated string */
244char *x11_display(const char *display) {
46ed7b64 245 char *ret;
246 if(!display || !*display) {
247 /* try to find platform-specific local display */
c2ec50a1 248 if((ret = platform_get_x_display())==0 || !*ret)
46ed7b64 249 /* plausible default for all platforms */
250 ret = dupstr(":0");
251 } else
252 ret = dupstr(display);
253 if(ret[0] == ':') {
fc0f17db 254 /* no transport specified, use whatever we think is best */
0ca70bfc 255 char *s = dupcat(platform_x11_best_transport, ret, (char *)0);
46ed7b64 256 sfree(ret);
257 return s;
fc0f17db 258 } else
46ed7b64 259 return ret;
fc0f17db 260}
261
421d6835 262/*
9c964e85 263 * Called to set up the raw connection.
264 *
265 * Returns an error message, or NULL on success.
266 * also, fills the SocketsStructure
9c964e85 267 */
cbe2d68f 268const char *x11_init(Socket * s, char *display, void *c, void *auth,
269 const char *peeraddr, int peerport, const Config *cfg)
32874aea 270{
302121de 271 static const struct plug_function_table fn_table = {
7555d6a5 272 x11_log,
7e78000d 273 x11_closing,
5471d09a 274 x11_receive,
275 x11_sent,
276 NULL
7e78000d 277 };
278
9c964e85 279 SockAddr addr;
280 int port;
cbe2d68f 281 const char *err;
282 char *dummy_realhost;
9c964e85 283 char host[128];
284 int n, displaynum;
285 struct X11Private *pr;
286
fc0f17db 287 /* default display */
288 display = x11_display(display);
9c964e85 289 /*
290 * Split up display name into host and display-number parts.
291 */
292 n = strcspn(display, ":");
fc0f17db 293 assert(n != 0); /* x11_display() promises this */
9c964e85 294 if (display[n])
32874aea 295 displaynum = atoi(display + n + 1);
9c964e85 296 else
32874aea 297 displaynum = 0; /* sensible default */
298 if (n > sizeof(host) - 1)
299 n = sizeof(host) - 1;
fc0f17db 300 strncpy(host, display, n);
301 host[n] = '\0';
302 sfree(display);
303
304 if(!strcmp(host, "unix")) {
305 /* use AF_UNIX sockets (doesn't make sense on all platforms) */
306 addr = platform_get_x11_unix_address(displaynum,
307 &dummy_realhost);
308 port = 0; /* to show we are not confused */
ecc0ba5b 309 } else {
fc0f17db 310 port = 6000 + displaynum;
311
ecc0ba5b 312 /*
fc0f17db 313 * Try to find host.
ecc0ba5b 314 */
05581745 315 addr = name_lookup(host, port, &dummy_realhost, cfg, ADDRTYPE_UNSPEC);
fc0f17db 316 if ((err = sk_addr_error(addr)) != NULL) {
317 sk_addr_free(addr);
318 return err;
319 }
f85e6f6e 320 }
9c964e85 321
9c964e85 322 /*
323 * Open socket.
324 */
3d88e64d 325 pr = snew(struct X11Private);
7e78000d 326 pr->fn = &fn_table;
9c964e85 327 pr->auth_protocol = NULL;
302121de 328 pr->auth = (struct X11Auth *)auth;
9c964e85 329 pr->verified = 0;
330 pr->data_read = 0;
5471d09a 331 pr->throttled = pr->throttle_override = 0;
9c964e85 332 pr->c = c;
333
e8fa8f62 334 pr->s = *s = new_connection(addr, dummy_realhost, port,
79bf227b 335 0, 1, 0, 0, (Plug) pr, cfg);
6518ea7b 336 if ((err = sk_socket_error(*s)) != NULL) {
32874aea 337 sfree(pr);
7e78000d 338 return err;
339 }
340
b3ebaa28 341 /*
342 * See if we can make sense of the peer address we were given.
343 */
344 {
345 int i[4];
346 if (peeraddr &&
347 4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {
348 pr->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];
349 pr->peer_port = peerport;
350 } else {
351 pr->peer_ip = 0;
352 pr->peer_port = -1;
353 }
354 }
355
9c964e85 356 sk_set_private_ptr(*s, pr);
9c964e85 357 return NULL;
358}
359
32874aea 360void x11_close(Socket s)
361{
362 struct X11Private *pr;
d74d141c 363 if (!s)
364 return;
365 pr = (struct X11Private *) sk_get_private_ptr(s);
9c964e85 366 if (pr->auth_protocol) {
32874aea 367 sfree(pr->auth_protocol);
368 sfree(pr->auth_data);
9c964e85 369 }
370
371 sfree(pr);
372
373 sk_close(s);
374}
375
5471d09a 376void x11_unthrottle(Socket s)
377{
378 struct X11Private *pr;
379 if (!s)
380 return;
381 pr = (struct X11Private *) sk_get_private_ptr(s);
382
383 pr->throttled = 0;
384 sk_set_frozen(s, pr->throttled || pr->throttle_override);
385}
386
387void x11_override_throttle(Socket s, int enable)
388{
389 struct X11Private *pr;
390 if (!s)
391 return;
392 pr = (struct X11Private *) sk_get_private_ptr(s);
393
394 pr->throttle_override = enable;
395 sk_set_frozen(s, pr->throttled || pr->throttle_override);
396}
397
9c964e85 398/*
399 * Called to send data down the raw connection.
400 */
5471d09a 401int x11_send(Socket s, char *data, int len)
32874aea 402{
970f2b02 403 struct X11Private *pr;
404 if (!s)
5471d09a 405 return 0;
970f2b02 406 pr = (struct X11Private *) sk_get_private_ptr(s);
9c964e85 407
408 /*
409 * Read the first packet.
410 */
411 while (len > 0 && pr->data_read < 12)
32874aea 412 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
9c964e85 413 if (pr->data_read < 12)
5471d09a 414 return 0;
9c964e85 415
416 /*
417 * If we have not allocated the auth_protocol and auth_data
418 * strings, do so now.
419 */
420 if (!pr->auth_protocol) {
32874aea 421 pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);
422 pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);
423 pr->auth_psize = (pr->auth_plen + 3) & ~3;
424 pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
425 /* Leave room for a terminating zero, to make our lives easier. */
3d88e64d 426 pr->auth_protocol = snewn(pr->auth_psize + 1, char);
92d60585 427 pr->auth_data = snewn(pr->auth_dsize, unsigned char);
9c964e85 428 }
429
430 /*
431 * Read the auth_protocol and auth_data strings.
432 */
433 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
32874aea 434 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
9c964e85 435 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
32874aea 436 pr->auth_data[pr->data_read++ - 12 -
437 pr->auth_psize] = (unsigned char) (len--, *data++);
9c964e85 438 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
5471d09a 439 return 0;
9c964e85 440
441 /*
442 * If we haven't verified the authentication, do so now.
443 */
444 if (!pr->verified) {
b3ebaa28 445 char *err;
32874aea 446
447 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
b3ebaa28 448 err = x11_verify(pr->peer_ip, pr->peer_port,
449 pr->auth, pr->auth_protocol,
302121de 450 pr->auth_data, pr->auth_dlen);
32874aea 451
452 /*
453 * If authentication failed, construct and send an error
454 * packet, then terminate the connection.
455 */
b3ebaa28 456 if (err) {
457 char *message;
458 int msglen, msgsize;
459 unsigned char *reply;
460
461 message = dupprintf("PuTTY X11 proxy: %s", err);
462 msglen = strlen(message);
3d88e64d 463 reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
b3ebaa28 464 msgsize = (msglen + 3) & ~3;
32874aea 465 reply[0] = 0; /* failure */
466 reply[1] = msglen; /* length of reason string */
467 memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */
75105663 468 PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
32874aea 469 memset(reply + 8, 0, msgsize);
470 memcpy(reply + 8, message, msglen);
f28e6b7c 471 sshfwd_write(pr->c, (char *)reply, 8 + msgsize);
32874aea 472 sshfwd_close(pr->c);
473 x11_close(s);
b3ebaa28 474 sfree(reply);
475 sfree(message);
5471d09a 476 return 0;
32874aea 477 }
478
479 /*
480 * Now we know we're going to accept the connection. Strip
e0e7dff8 481 * the fake auth data, and optionally put real auth data in
482 * instead.
32874aea 483 */
e0e7dff8 484 {
485 char realauthdata[64];
486 int realauthlen = 0;
487 int authstrlen = strlen(x11_authnames[pr->auth->realproto]);
0a4022a1 488 int buflen;
e0e7dff8 489 static const char zeroes[4] = { 0,0,0,0 };
0a4022a1 490 void *buf;
e0e7dff8 491
492 if (pr->auth->realproto == X11_MIT) {
493 assert(pr->auth->reallen <= lenof(realauthdata));
494 realauthlen = pr->auth->reallen;
495 memcpy(realauthdata, pr->auth->realdata, realauthlen);
2f92b717 496 } else if (pr->auth->realproto == X11_XDM &&
497 pr->auth->reallen == 16 &&
0c5ca1f3 498 ((buf = sk_getxdmdata(s, &buflen))!=0)) {
2f92b717 499 time_t t;
0a4022a1 500 realauthlen = (buflen+12+7) & ~7;
501 assert(realauthlen <= lenof(realauthdata));
502 memset(realauthdata, 0, realauthlen);
2f92b717 503 memcpy(realauthdata, pr->auth->realdata, 8);
0a4022a1 504 memcpy(realauthdata+8, buf, buflen);
2f92b717 505 t = time(NULL);
0a4022a1 506 PUT_32BIT_MSB_FIRST(realauthdata+8+buflen, t);
6e7f5aee 507 des_encrypt_xdmauth(pr->auth->realdata+9,
0a4022a1 508 (unsigned char *)realauthdata,
509 realauthlen);
510 sfree(buf);
2f92b717 511 }
e0e7dff8 512 /* implement other auth methods here if required */
513
514 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
515 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
516
517 sk_write(s, (char *)pr->firstpkt, 12);
518
519 if (authstrlen) {
520 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);
521 sk_write(s, zeroes, 3 & (-authstrlen));
522 }
523 if (realauthlen) {
524 sk_write(s, realauthdata, realauthlen);
525 sk_write(s, zeroes, 3 & (-realauthlen));
526 }
527 }
32874aea 528 pr->verified = 1;
9c964e85 529 }
530
531 /*
532 * After initialisation, just copy data simply.
533 */
534
5471d09a 535 return sk_write(s, data, len);
9c964e85 536}