Bah, I've bumped into this often enough. Change one unsatisfactory cast for
[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
9c964e85 29struct X11Private {
302121de 30 const struct plug_function_table *fn;
7e78000d 31 /* the above variable absolutely *must* be the first in this structure */
32874aea 32 unsigned char firstpkt[12]; /* first X data packet */
8def70c3 33 struct X11Display *disp;
9c964e85 34 char *auth_protocol;
35 unsigned char *auth_data;
36 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
37 int verified;
5471d09a 38 int throttled, throttle_override;
b3ebaa28 39 unsigned long peer_ip;
40 int peer_port;
32874aea 41 void *c; /* data used by ssh.c */
7e78000d 42 Socket s;
9c964e85 43};
44
5f8ff48c 45static int xdmseen_cmp(void *a, void *b)
46{
47 struct XDMSeen *sa = a, *sb = b;
48 return sa->time > sb->time ? 1 :
49 sa->time < sb->time ? -1 :
50 memcmp(sa->clientid, sb->clientid, sizeof(sa->clientid));
51}
52
8def70c3 53struct X11Display *x11_setup_display(char *display, int authtype,
54 const Config *cfg)
32874aea 55{
8def70c3 56 struct X11Display *disp = snew(struct X11Display);
57 char *localcopy;
9c964e85 58 int i;
59
8def70c3 60 if (!display || !*display) {
61 localcopy = platform_get_x_display();
62 if (!localcopy || !*localcopy) {
63 sfree(localcopy);
64 localcopy = dupstr(":0"); /* plausible default for any platform */
65 }
66 } else
67 localcopy = dupstr(display);
68
69 /*
70 * Parse the display name.
71 *
72 * We expect this to have one of the following forms:
73 *
74 * - the standard X format which looks like
75 * [ [ protocol '/' ] host ] ':' displaynumber [ '.' screennumber ]
76 * (X11 also permits a double colon to indicate DECnet, but
77 * that's not our problem, thankfully!)
78 *
79 * - only seen in the wild on MacOS (so far): a pathname to a
80 * Unix-domain socket, which will typically and confusingly
81 * end in ":0", and which I'm currently distinguishing from
82 * the standard scheme by noting that it starts with '/'.
83 */
84 if (localcopy[0] == '/') {
85 disp->unixsocketpath = localcopy;
86 disp->unixdomain = TRUE;
87 disp->hostname = NULL;
88 disp->displaynum = -1;
89 disp->screennum = 0;
90 } else {
91 char *colon, *dot, *slash;
92 char *protocol, *hostname;
93
94 colon = strrchr(localcopy, ':');
95 if (!colon) {
96 sfree(disp);
97 sfree(localcopy);
98 return NULL; /* FIXME: report a specific error? */
99 }
100
101 *colon++ = '\0';
102 dot = strchr(colon, '.');
103 if (dot)
104 *dot++ = '\0';
105
106 disp->displaynum = atoi(colon);
107 if (dot)
108 disp->screennum = atoi(dot);
109 else
110 disp->screennum = 0;
111
112 protocol = NULL;
113 hostname = localcopy;
114 if (colon > localcopy) {
115 slash = strchr(localcopy, '/');
116 if (slash) {
117 *slash++ = '\0';
118 protocol = localcopy;
119 hostname = slash;
120 }
121 }
122
123 disp->hostname = *hostname ? dupstr(hostname) : NULL;
124
125 if (protocol)
126 disp->unixdomain = (!strcmp(protocol, "local") ||
127 !strcmp(protocol, "unix"));
128 else
129 disp->unixdomain = platform_uses_x11_unix_by_default;
130
131 if (!disp->hostname && !disp->unixdomain)
132 disp->hostname = dupstr("localhost");
133
134 disp->unixsocketpath = NULL;
135
136 sfree(localcopy);
137 }
138
139 /*
140 * Look up the display hostname, if we need to.
141 */
142 if (disp->unixdomain) {
143 disp->addr = platform_get_x11_unix_address(disp->unixsocketpath,
144 disp->displaynum);
145 if (disp->unixsocketpath)
146 disp->realhost = dupstr(disp->unixsocketpath);
147 else
148 disp->realhost = dupprintf("unix:%d", disp->displaynum);
149 disp->port = 0;
150 } else {
151 const char *err;
152
153 disp->port = 6000 + disp->displaynum;
154 disp->addr = name_lookup(disp->hostname, disp->port,
155 &disp->realhost, cfg, ADDRTYPE_UNSPEC);
156
157 if ((err = sk_addr_error(disp->addr)) != NULL) {
158 sk_addr_free(disp->addr);
159 sfree(disp->hostname);
160 sfree(disp->unixsocketpath);
161 return NULL; /* FIXME: report an error */
162 }
163 }
164
165 /*
166 * Invent the remote authorisation details.
167 */
168 if (authtype == X11_MIT) {
169 disp->remoteauthproto = X11_MIT;
e0e7dff8 170
b3ebaa28 171 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
8def70c3 172 disp->remoteauthdata = snewn(16, unsigned char);
b3ebaa28 173 for (i = 0; i < 16; i++)
8def70c3 174 disp->remoteauthdata[i] = random_byte();
175 disp->remoteauthdatalen = 16;
176
177 disp->xdmseen = NULL;
b3ebaa28 178 } else {
8def70c3 179 assert(authtype == X11_XDM);
180 disp->remoteauthproto = X11_XDM;
b3ebaa28 181
182 /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */
8def70c3 183 disp->remoteauthdata = snewn(16, unsigned char);
b3ebaa28 184 for (i = 0; i < 16; i++)
8def70c3 185 disp->remoteauthdata[i] = (i == 8 ? 0 : random_byte());
186 disp->remoteauthdatalen = 16;
187
188 disp->xdmseen = newtree234(xdmseen_cmp);
b3ebaa28 189 }
8def70c3 190 disp->remoteauthprotoname = dupstr(x11_authnames[disp->remoteauthproto]);
191 disp->remoteauthdatastring = snewn(disp->remoteauthdatalen * 2 + 1, char);
192 for (i = 0; i < disp->remoteauthdatalen; i++)
193 sprintf(disp->remoteauthdatastring + i*2, "%02x",
194 disp->remoteauthdata[i]);
9c964e85 195
8def70c3 196 /*
197 * Fetch the local authorisation details.
198 */
199 disp->localauthproto = X11_NO_AUTH;
200 disp->localauthdata = NULL;
201 disp->localauthdatalen = 0;
202 platform_get_x11_auth(disp, cfg);
302121de 203
8def70c3 204 return disp;
9c964e85 205}
206
8def70c3 207void x11_free_display(struct X11Display *disp)
fabd1805 208{
8def70c3 209 if (disp->xdmseen != NULL) {
210 struct XDMSeen *seen;
211 while ((seen = delpos234(disp->xdmseen, 0)) != NULL)
5f8ff48c 212 sfree(seen);
8def70c3 213 freetree234(disp->xdmseen);
5f8ff48c 214 }
8def70c3 215 sfree(disp->hostname);
216 sfree(disp->unixsocketpath);
217 if (disp->localauthdata)
218 memset(disp->localauthdata, 0, disp->localauthdatalen);
219 sfree(disp->localauthdata);
220 if (disp->remoteauthdata)
221 memset(disp->remoteauthdata, 0, disp->remoteauthdatalen);
222 sfree(disp->remoteauthdata);
223 sfree(disp->remoteauthprotoname);
224 sfree(disp->remoteauthdatastring);
225 sk_addr_free(disp->addr);
226 sfree(disp);
e0e7dff8 227}
228
5f8ff48c 229#define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */
230
b3ebaa28 231static char *x11_verify(unsigned long peer_ip, int peer_port,
8def70c3 232 struct X11Display *disp, char *proto,
b3ebaa28 233 unsigned char *data, int dlen)
32874aea 234{
8def70c3 235 if (strcmp(proto, x11_authnames[disp->remoteauthproto]) != 0)
236 return "wrong authorisation protocol attempted";
237 if (disp->remoteauthproto == X11_MIT) {
238 if (dlen != disp->remoteauthdatalen)
b3ebaa28 239 return "MIT-MAGIC-COOKIE-1 data was wrong length";
8def70c3 240 if (memcmp(disp->remoteauthdata, data, dlen) != 0)
b3ebaa28 241 return "MIT-MAGIC-COOKIE-1 data did not match";
242 }
8def70c3 243 if (disp->remoteauthproto == X11_XDM) {
b3ebaa28 244 unsigned long t;
245 time_t tim;
246 int i;
5f8ff48c 247 struct XDMSeen *seen, *ret;
b3ebaa28 248
249 if (dlen != 24)
250 return "XDM-AUTHORIZATION-1 data was wrong length";
251 if (peer_port == -1)
252 return "cannot do XDM-AUTHORIZATION-1 without remote address data";
8def70c3 253 des_decrypt_xdmauth(disp->remoteauthdata+9, data, 24);
254 if (memcmp(disp->remoteauthdata, data, 8) != 0)
b3ebaa28 255 return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */
256 if (GET_32BIT_MSB_FIRST(data+8) != peer_ip)
257 return "XDM-AUTHORIZATION-1 data failed check"; /* IP wrong */
258 if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port)
259 return "XDM-AUTHORIZATION-1 data failed check"; /* port wrong */
260 t = GET_32BIT_MSB_FIRST(data+14);
261 for (i = 18; i < 24; i++)
262 if (data[i] != 0) /* zero padding wrong */
263 return "XDM-AUTHORIZATION-1 data failed check";
264 tim = time(NULL);
5f8ff48c 265 if (abs(t - tim) > XDM_MAXSKEW)
b3ebaa28 266 return "XDM-AUTHORIZATION-1 time stamp was too far out";
5f8ff48c 267 seen = snew(struct XDMSeen);
268 seen->time = t;
269 memcpy(seen->clientid, data+8, 6);
8def70c3 270 assert(disp->xdmseen != NULL);
271 ret = add234(disp->xdmseen, seen);
5f8ff48c 272 if (ret != seen) {
273 sfree(seen);
274 return "XDM-AUTHORIZATION-1 data replayed";
275 }
276 /* While we're here, purge entries too old to be replayed. */
277 for (;;) {
8def70c3 278 seen = index234(disp->xdmseen, 0);
5f8ff48c 279 assert(seen != NULL);
280 if (t - seen->time <= XDM_MAXSKEW)
281 break;
8def70c3 282 sfree(delpos234(disp->xdmseen, 0));
5f8ff48c 283 }
e0e7dff8 284 }
285 /* implement other protocols here if ever required */
b3ebaa28 286 return NULL;
9c964e85 287}
288
8def70c3 289void x11_get_auth_from_authfile(struct X11Display *disp,
290 const char *authfilename)
291{
292 FILE *authfp;
293 char *buf, *ptr, *str[4];
294 int len[4];
295 int family, protocol;
296
297 authfp = fopen(authfilename, "rb");
298 if (!authfp)
299 return;
300
301 /* Records in .Xauthority contain four strings of up to 64K each */
302 buf = snewn(65537 * 4, char);
303
304 while (1) {
305 int c, i, j;
306
307#define GET do { c = fgetc(authfp); if (c == EOF) goto done; c = (unsigned char)c; } while (0)
308 /* Expect a big-endian 2-byte number giving address family */
309 GET; family = c;
310 GET; family = (family << 8) | c;
311 /* Then expect four strings, each composed of a big-endian 2-byte
312 * length field followed by that many bytes of data */
313 ptr = buf;
314 for (i = 0; i < 4; i++) {
315 GET; len[i] = c;
316 GET; len[i] = (len[i] << 8) | c;
317 str[i] = ptr;
318 for (j = 0; j < len[i]; j++) {
319 GET; *ptr++ = c;
320 }
321 *ptr++ = '\0';
322 }
323#undef GET
324
325 /*
326 * Now we have a full X authority record in memory. See
327 * whether it matches the display we're trying to
328 * authenticate to.
329 *
330 * The details we've just read should be interpreted as
331 * follows:
332 *
333 * - 'family' is the network address family used to
334 * connect to the display. 0 means IPv4; 6 means IPv6;
335 * 256 means Unix-domain sockets.
336 *
337 * - str[0] is the network address itself. For IPv4 and
338 * IPv6, this is a string of binary data of the
339 * appropriate length (respectively 4 and 16 bytes)
340 * representing the address in big-endian format, e.g.
341 * 7F 00 00 01 means IPv4 localhost. For Unix-domain
342 * sockets, this is the host name of the machine on
343 * which the Unix-domain display resides (so that an
344 * .Xauthority file on a shared file system can contain
345 * authority entries for Unix-domain displays on
346 * several machines without them clashing).
347 *
348 * - str[1] is the display number. I've no idea why
349 * .Xauthority stores this as a string when it has a
350 * perfectly good integer format, but there we go.
351 *
352 * - str[2] is the authorisation method, encoded as its
353 * canonical string name (i.e. "MIT-MAGIC-COOKIE-1",
354 * "XDM-AUTHORIZATION-1" or something we don't
355 * recognise).
356 *
357 * - str[3] is the actual authorisation data, stored in
358 * binary form.
359 */
360
361 if (disp->displaynum < 0 || disp->displaynum != atoi(str[1]))
362 continue; /* not the one */
363
364 for (protocol = 1; protocol < lenof(x11_authnames); protocol++)
365 if (!strcmp(str[2], x11_authnames[protocol]))
366 break;
367 if (protocol == lenof(x11_authnames))
368 continue; /* don't recognise this protocol, look for another */
369
370 switch (family) {
371 case 0:
372 if (!disp->unixdomain &&
373 sk_addrtype(disp->addr) == ADDRTYPE_IPV4) {
374 char buf[4];
375 sk_addrcopy(disp->addr, buf);
376 if (len[0] == 4 && !memcmp(str[0], buf, 4))
377 goto found;
378 }
379 break;
380 case 6:
381 if (!disp->unixdomain &&
382 sk_addrtype(disp->addr) == ADDRTYPE_IPV6) {
383 char buf[16];
384 sk_addrcopy(disp->addr, buf);
385 if (len[0] == 16 && !memcmp(str[0], buf, 16))
386 goto found;
387 }
388 break;
389 case 256:
390 if (disp->unixdomain && !strcmp(disp->hostname, str[0]))
391 goto found;
392 break;
393 }
394 }
395
396 found:
397 disp->localauthproto = protocol;
398 disp->localauthdata = snewn(len[3], unsigned char);
399 memcpy(disp->localauthdata, str[3], len[3]);
400 disp->localauthdatalen = len[3];
401
402 done:
403 fclose(authfp);
404 memset(buf, 0, 65537 * 4);
405 sfree(buf);
406}
407
7555d6a5 408static void x11_log(Plug p, int type, SockAddr addr, int port,
409 const char *error_msg, int error_code)
410{
411 /* We have no interface to the logging module here, so we drop these. */
412}
413
cbe2d68f 414static int x11_closing(Plug plug, const char *error_msg, int error_code,
32874aea 415 int calling_back)
416{
7e78000d 417 struct X11Private *pr = (struct X11Private *) plug;
418
419 /*
420 * We have no way to communicate down the forwarded connection,
421 * so if an error occurred on the socket, we just ignore it
422 * and treat it like a proper close.
423 */
424 sshfwd_close(pr->c);
425 x11_close(pr->s);
426 return 1;
427}
428
32874aea 429static int x11_receive(Plug plug, int urgent, char *data, int len)
430{
7e78000d 431 struct X11Private *pr = (struct X11Private *) plug;
9c964e85 432
5471d09a 433 if (sshfwd_write(pr->c, data, len) > 0) {
434 pr->throttled = 1;
435 sk_set_frozen(pr->s, 1);
436 }
437
9c964e85 438 return 1;
439}
440
5471d09a 441static void x11_sent(Plug plug, int bufsize)
442{
443 struct X11Private *pr = (struct X11Private *) plug;
444
445 sshfwd_unthrottle(pr->c, bufsize);
446}
447
9c964e85 448/*
421d6835 449 * When setting up X forwarding, we should send the screen number
450 * from the specified local display. This function extracts it from
451 * the display string.
452 */
453int x11_get_screen_number(char *display)
454{
455 int n;
456
457 n = strcspn(display, ":");
458 if (!display[n])
459 return 0;
460 n = strcspn(display, ".");
461 if (!display[n])
462 return 0;
463 return atoi(display + n + 1);
464}
465
466/*
9c964e85 467 * Called to set up the raw connection.
468 *
469 * Returns an error message, or NULL on success.
470 * also, fills the SocketsStructure
9c964e85 471 */
8def70c3 472extern const char *x11_init(Socket *s, struct X11Display *disp, void *c,
473 const char *peeraddr, int peerport,
474 const Config *cfg)
32874aea 475{
302121de 476 static const struct plug_function_table fn_table = {
7555d6a5 477 x11_log,
7e78000d 478 x11_closing,
5471d09a 479 x11_receive,
480 x11_sent,
481 NULL
7e78000d 482 };
483
cbe2d68f 484 const char *err;
9c964e85 485 struct X11Private *pr;
486
9c964e85 487 /*
488 * Open socket.
489 */
3d88e64d 490 pr = snew(struct X11Private);
7e78000d 491 pr->fn = &fn_table;
9c964e85 492 pr->auth_protocol = NULL;
8def70c3 493 pr->disp = disp;
9c964e85 494 pr->verified = 0;
495 pr->data_read = 0;
5471d09a 496 pr->throttled = pr->throttle_override = 0;
9c964e85 497 pr->c = c;
498
8def70c3 499 pr->s = *s = new_connection(sk_addr_dup(disp->addr),
500 disp->realhost, disp->port,
79bf227b 501 0, 1, 0, 0, (Plug) pr, cfg);
6518ea7b 502 if ((err = sk_socket_error(*s)) != NULL) {
32874aea 503 sfree(pr);
7e78000d 504 return err;
505 }
506
b3ebaa28 507 /*
508 * See if we can make sense of the peer address we were given.
509 */
510 {
511 int i[4];
512 if (peeraddr &&
513 4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {
514 pr->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];
515 pr->peer_port = peerport;
516 } else {
517 pr->peer_ip = 0;
518 pr->peer_port = -1;
519 }
520 }
521
9c964e85 522 sk_set_private_ptr(*s, pr);
9c964e85 523 return NULL;
524}
525
32874aea 526void x11_close(Socket s)
527{
528 struct X11Private *pr;
d74d141c 529 if (!s)
530 return;
531 pr = (struct X11Private *) sk_get_private_ptr(s);
9c964e85 532 if (pr->auth_protocol) {
32874aea 533 sfree(pr->auth_protocol);
534 sfree(pr->auth_data);
9c964e85 535 }
536
537 sfree(pr);
538
539 sk_close(s);
540}
541
5471d09a 542void x11_unthrottle(Socket s)
543{
544 struct X11Private *pr;
545 if (!s)
546 return;
547 pr = (struct X11Private *) sk_get_private_ptr(s);
548
549 pr->throttled = 0;
550 sk_set_frozen(s, pr->throttled || pr->throttle_override);
551}
552
553void x11_override_throttle(Socket s, int enable)
554{
555 struct X11Private *pr;
556 if (!s)
557 return;
558 pr = (struct X11Private *) sk_get_private_ptr(s);
559
560 pr->throttle_override = enable;
561 sk_set_frozen(s, pr->throttled || pr->throttle_override);
562}
563
9c964e85 564/*
565 * Called to send data down the raw connection.
566 */
5471d09a 567int x11_send(Socket s, char *data, int len)
32874aea 568{
970f2b02 569 struct X11Private *pr;
570 if (!s)
5471d09a 571 return 0;
970f2b02 572 pr = (struct X11Private *) sk_get_private_ptr(s);
9c964e85 573
574 /*
575 * Read the first packet.
576 */
577 while (len > 0 && pr->data_read < 12)
32874aea 578 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
9c964e85 579 if (pr->data_read < 12)
5471d09a 580 return 0;
9c964e85 581
582 /*
583 * If we have not allocated the auth_protocol and auth_data
584 * strings, do so now.
585 */
586 if (!pr->auth_protocol) {
32874aea 587 pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);
588 pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);
589 pr->auth_psize = (pr->auth_plen + 3) & ~3;
590 pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
591 /* Leave room for a terminating zero, to make our lives easier. */
3d88e64d 592 pr->auth_protocol = snewn(pr->auth_psize + 1, char);
92d60585 593 pr->auth_data = snewn(pr->auth_dsize, unsigned char);
9c964e85 594 }
595
596 /*
597 * Read the auth_protocol and auth_data strings.
598 */
599 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
32874aea 600 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
9c964e85 601 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
32874aea 602 pr->auth_data[pr->data_read++ - 12 -
603 pr->auth_psize] = (unsigned char) (len--, *data++);
9c964e85 604 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
5471d09a 605 return 0;
9c964e85 606
607 /*
8def70c3 608 * If we haven't verified the authorisation, do so now.
9c964e85 609 */
610 if (!pr->verified) {
b3ebaa28 611 char *err;
32874aea 612
613 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
b3ebaa28 614 err = x11_verify(pr->peer_ip, pr->peer_port,
8def70c3 615 pr->disp, pr->auth_protocol,
302121de 616 pr->auth_data, pr->auth_dlen);
32874aea 617
618 /*
8def70c3 619 * If authorisation failed, construct and send an error
32874aea 620 * packet, then terminate the connection.
621 */
b3ebaa28 622 if (err) {
623 char *message;
624 int msglen, msgsize;
625 unsigned char *reply;
626
627 message = dupprintf("PuTTY X11 proxy: %s", err);
628 msglen = strlen(message);
3d88e64d 629 reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
b3ebaa28 630 msgsize = (msglen + 3) & ~3;
32874aea 631 reply[0] = 0; /* failure */
632 reply[1] = msglen; /* length of reason string */
633 memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */
75105663 634 PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
32874aea 635 memset(reply + 8, 0, msgsize);
636 memcpy(reply + 8, message, msglen);
f28e6b7c 637 sshfwd_write(pr->c, (char *)reply, 8 + msgsize);
32874aea 638 sshfwd_close(pr->c);
639 x11_close(s);
b3ebaa28 640 sfree(reply);
641 sfree(message);
5471d09a 642 return 0;
32874aea 643 }
644
645 /*
646 * Now we know we're going to accept the connection. Strip
e0e7dff8 647 * the fake auth data, and optionally put real auth data in
648 * instead.
32874aea 649 */
e0e7dff8 650 {
651 char realauthdata[64];
652 int realauthlen = 0;
8def70c3 653 int authstrlen = strlen(x11_authnames[pr->disp->localauthproto]);
62ddb51e 654 int buflen = 0; /* initialise to placate optimiser */
e0e7dff8 655 static const char zeroes[4] = { 0,0,0,0 };
0a4022a1 656 void *buf;
e0e7dff8 657
8def70c3 658 if (pr->disp->localauthproto == X11_MIT) {
659 assert(pr->disp->localauthdatalen <= lenof(realauthdata));
660 realauthlen = pr->disp->localauthdatalen;
661 memcpy(realauthdata, pr->disp->localauthdata, realauthlen);
662 } else if (pr->disp->localauthproto == X11_XDM &&
663 pr->disp->localauthdatalen == 16 &&
0c5ca1f3 664 ((buf = sk_getxdmdata(s, &buflen))!=0)) {
2f92b717 665 time_t t;
0a4022a1 666 realauthlen = (buflen+12+7) & ~7;
667 assert(realauthlen <= lenof(realauthdata));
668 memset(realauthdata, 0, realauthlen);
8def70c3 669 memcpy(realauthdata, pr->disp->localauthdata, 8);
0a4022a1 670 memcpy(realauthdata+8, buf, buflen);
2f92b717 671 t = time(NULL);
0a4022a1 672 PUT_32BIT_MSB_FIRST(realauthdata+8+buflen, t);
8def70c3 673 des_encrypt_xdmauth(pr->disp->localauthdata+9,
0a4022a1 674 (unsigned char *)realauthdata,
675 realauthlen);
676 sfree(buf);
2f92b717 677 }
e0e7dff8 678 /* implement other auth methods here if required */
679
680 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
681 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
682
683 sk_write(s, (char *)pr->firstpkt, 12);
684
685 if (authstrlen) {
8def70c3 686 sk_write(s, x11_authnames[pr->disp->localauthproto],
687 authstrlen);
e0e7dff8 688 sk_write(s, zeroes, 3 & (-authstrlen));
689 }
690 if (realauthlen) {
691 sk_write(s, realauthdata, realauthlen);
692 sk_write(s, zeroes, 3 & (-realauthlen));
693 }
694 }
32874aea 695 pr->verified = 1;
9c964e85 696 }
697
698 /*
699 * After initialisation, just copy data simply.
700 */
701
5471d09a 702 return sk_write(s, data, len);
9c964e85 703}