2 * Platform-independent bits of X11 forwarding.
\r
12 #include "tree234.h"
\r
14 #define GET_16BIT(endian, cp) \
\r
15 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
\r
17 #define PUT_16BIT(endian, cp, val) \
\r
18 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
\r
20 const char *const x11_authnames[] = {
\r
21 "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"
\r
26 unsigned char clientid[6];
\r
29 struct X11Connection {
\r
30 const struct plug_function_table *fn;
\r
31 /* the above variable absolutely *must* be the first in this structure */
\r
32 unsigned char firstpkt[12]; /* first X data packet */
\r
34 struct X11Display *disp;
\r
35 char *auth_protocol;
\r
36 unsigned char *auth_data;
\r
37 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
\r
39 int throttled, throttle_override;
\r
40 int no_data_sent_to_x_client;
\r
43 struct ssh_channel *c; /* channel structure held by ssh.c */
\r
47 static int xdmseen_cmp(void *a, void *b)
\r
49 struct XDMSeen *sa = a, *sb = b;
\r
50 return sa->time > sb->time ? 1 :
\r
51 sa->time < sb->time ? -1 :
\r
52 memcmp(sa->clientid, sb->clientid, sizeof(sa->clientid));
\r
55 /* Do-nothing "plug" implementation, used by x11_setup_display() when it
\r
56 * creates a trial connection (and then immediately closes it).
\r
57 * XXX: bit out of place here, could in principle live in a platform-
\r
58 * independent network.c or something */
\r
59 static void dummy_plug_log(Plug p, int type, SockAddr addr, int port,
\r
60 const char *error_msg, int error_code) { }
\r
61 static int dummy_plug_closing
\r
62 (Plug p, const char *error_msg, int error_code, int calling_back)
\r
64 static int dummy_plug_receive(Plug p, int urgent, char *data, int len)
\r
66 static void dummy_plug_sent(Plug p, int bufsize) { }
\r
67 static int dummy_plug_accepting(Plug p, accept_fn_t constructor, accept_ctx_t ctx) { return 1; }
\r
68 static const struct plug_function_table dummy_plug = {
\r
69 dummy_plug_log, dummy_plug_closing, dummy_plug_receive,
\r
70 dummy_plug_sent, dummy_plug_accepting
\r
73 struct X11FakeAuth *x11_invent_fake_auth(tree234 *authtree, int authtype)
\r
75 struct X11FakeAuth *auth = snew(struct X11FakeAuth);
\r
79 * This function has the job of inventing a set of X11 fake auth
\r
80 * data, and adding it to 'authtree'. We must preserve the
\r
81 * property that for any given actual authorisation attempt, _at
\r
82 * most one_ thing in the tree can possibly match it.
\r
84 * For MIT-MAGIC-COOKIE-1, that's not too difficult: the match
\r
85 * criterion is simply that the entire cookie is correct, so we
\r
86 * just have to make sure we don't make up two cookies the same.
\r
87 * (Vanishingly unlikely, but we check anyway to be sure, and go
\r
88 * round again inventing a new cookie if add234 tells us the one
\r
89 * we thought of is already in use.)
\r
91 * For XDM-AUTHORIZATION-1, it's a little more fiddly. The setup
\r
92 * with XA1 is that half the cookie is used as a DES key with
\r
93 * which to CBC-encrypt an assortment of stuff. Happily, the stuff
\r
94 * encrypted _begins_ with the other half of the cookie, and the
\r
95 * IV is always zero, which means that any valid XA1 authorisation
\r
96 * attempt for a given cookie must begin with the same cipher
\r
97 * block, consisting of the DES ECB encryption of the first half
\r
98 * of the cookie using the second half as a key. So we compute
\r
99 * that cipher block here and now, and use it as the sorting key
\r
100 * for distinguishing XA1 entries in the tree.
\r
103 if (authtype == X11_MIT) {
\r
104 auth->proto = X11_MIT;
\r
106 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
\r
107 auth->datalen = 16;
\r
108 auth->data = snewn(auth->datalen, unsigned char);
\r
109 auth->xa1_firstblock = NULL;
\r
112 for (i = 0; i < auth->datalen; i++)
\r
113 auth->data[i] = random_byte();
\r
114 if (add234(authtree, auth) == auth)
\r
118 auth->xdmseen = NULL;
\r
120 assert(authtype == X11_XDM);
\r
121 auth->proto = X11_XDM;
\r
123 /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */
\r
124 auth->datalen = 16;
\r
125 auth->data = snewn(auth->datalen, unsigned char);
\r
126 auth->xa1_firstblock = snewn(8, unsigned char);
\r
127 memset(auth->xa1_firstblock, 0, 8);
\r
130 for (i = 0; i < auth->datalen; i++)
\r
131 auth->data[i] = (i == 8 ? 0 : random_byte());
\r
132 memcpy(auth->xa1_firstblock, auth->data, 8);
\r
133 des_encrypt_xdmauth(auth->data + 9, auth->xa1_firstblock, 8);
\r
134 if (add234(authtree, auth) == auth)
\r
138 auth->xdmseen = newtree234(xdmseen_cmp);
\r
140 auth->protoname = dupstr(x11_authnames[auth->proto]);
\r
141 auth->datastring = snewn(auth->datalen * 2 + 1, char);
\r
142 for (i = 0; i < auth->datalen; i++)
\r
143 sprintf(auth->datastring + i*2, "%02x",
\r
147 auth->share_cs = auth->share_chan = NULL;
\r
152 void x11_free_fake_auth(struct X11FakeAuth *auth)
\r
155 smemclr(auth->data, auth->datalen);
\r
157 sfree(auth->protoname);
\r
158 sfree(auth->datastring);
\r
159 sfree(auth->xa1_firstblock);
\r
160 if (auth->xdmseen != NULL) {
\r
161 struct XDMSeen *seen;
\r
162 while ((seen = delpos234(auth->xdmseen, 0)) != NULL)
\r
164 freetree234(auth->xdmseen);
\r
169 int x11_authcmp(void *av, void *bv)
\r
171 struct X11FakeAuth *a = (struct X11FakeAuth *)av;
\r
172 struct X11FakeAuth *b = (struct X11FakeAuth *)bv;
\r
174 if (a->proto < b->proto)
\r
176 else if (a->proto > b->proto)
\r
179 if (a->proto == X11_MIT) {
\r
180 if (a->datalen < b->datalen)
\r
182 else if (a->datalen > b->datalen)
\r
185 return memcmp(a->data, b->data, a->datalen);
\r
187 assert(a->proto == X11_XDM);
\r
189 return memcmp(a->xa1_firstblock, b->xa1_firstblock, 8);
\r
193 struct X11Display *x11_setup_display(char *display, Conf *conf)
\r
195 struct X11Display *disp = snew(struct X11Display);
\r
198 if (!display || !*display) {
\r
199 localcopy = platform_get_x_display();
\r
200 if (!localcopy || !*localcopy) {
\r
202 localcopy = dupstr(":0"); /* plausible default for any platform */
\r
205 localcopy = dupstr(display);
\r
208 * Parse the display name.
\r
210 * We expect this to have one of the following forms:
\r
212 * - the standard X format which looks like
\r
213 * [ [ protocol '/' ] host ] ':' displaynumber [ '.' screennumber ]
\r
214 * (X11 also permits a double colon to indicate DECnet, but
\r
215 * that's not our problem, thankfully!)
\r
217 * - only seen in the wild on MacOS (so far): a pathname to a
\r
218 * Unix-domain socket, which will typically and confusingly
\r
219 * end in ":0", and which I'm currently distinguishing from
\r
220 * the standard scheme by noting that it starts with '/'.
\r
222 if (localcopy[0] == '/') {
\r
223 disp->unixsocketpath = localcopy;
\r
224 disp->unixdomain = TRUE;
\r
225 disp->hostname = NULL;
\r
226 disp->displaynum = -1;
\r
227 disp->screennum = 0;
\r
230 char *colon, *dot, *slash;
\r
231 char *protocol, *hostname;
\r
233 colon = host_strrchr(localcopy, ':');
\r
237 return NULL; /* FIXME: report a specific error? */
\r
241 dot = strchr(colon, '.');
\r
245 disp->displaynum = atoi(colon);
\r
247 disp->screennum = atoi(dot);
\r
249 disp->screennum = 0;
\r
252 hostname = localcopy;
\r
253 if (colon > localcopy) {
\r
254 slash = strchr(localcopy, '/');
\r
257 protocol = localcopy;
\r
262 disp->hostname = *hostname ? dupstr(hostname) : NULL;
\r
265 disp->unixdomain = (!strcmp(protocol, "local") ||
\r
266 !strcmp(protocol, "unix"));
\r
267 else if (!*hostname || !strcmp(hostname, "unix"))
\r
268 disp->unixdomain = platform_uses_x11_unix_by_default;
\r
270 disp->unixdomain = FALSE;
\r
272 if (!disp->hostname && !disp->unixdomain)
\r
273 disp->hostname = dupstr("localhost");
\r
275 disp->unixsocketpath = NULL;
\r
282 * Look up the display hostname, if we need to.
\r
284 if (!disp->unixdomain) {
\r
287 disp->port = 6000 + disp->displaynum;
\r
288 disp->addr = name_lookup(disp->hostname, disp->port,
\r
289 &disp->realhost, conf, ADDRTYPE_UNSPEC);
\r
291 if ((err = sk_addr_error(disp->addr)) != NULL) {
\r
292 sk_addr_free(disp->addr);
\r
293 sfree(disp->hostname);
\r
294 sfree(disp->unixsocketpath);
\r
296 return NULL; /* FIXME: report an error */
\r
301 * Try upgrading an IP-style localhost display to a Unix-socket
\r
302 * display (as the standard X connection libraries do).
\r
304 if (!disp->unixdomain && sk_address_is_local(disp->addr)) {
\r
305 SockAddr ux = platform_get_x11_unix_address(NULL, disp->displaynum);
\r
306 const char *err = sk_addr_error(ux);
\r
308 /* Create trial connection to see if there is a useful Unix-domain
\r
310 const struct plug_function_table *dummy = &dummy_plug;
\r
311 Socket s = sk_new(sk_addr_dup(ux), 0, 0, 0, 0, 0, (Plug)&dummy);
\r
312 err = sk_socket_error(s);
\r
318 sk_addr_free(disp->addr);
\r
319 disp->unixdomain = TRUE;
\r
321 /* Fill in the rest in a moment */
\r
325 if (disp->unixdomain) {
\r
327 disp->addr = platform_get_x11_unix_address(disp->unixsocketpath,
\r
329 if (disp->unixsocketpath)
\r
330 disp->realhost = dupstr(disp->unixsocketpath);
\r
332 disp->realhost = dupprintf("unix:%d", disp->displaynum);
\r
337 * Fetch the local authorisation details.
\r
339 disp->localauthproto = X11_NO_AUTH;
\r
340 disp->localauthdata = NULL;
\r
341 disp->localauthdatalen = 0;
\r
342 platform_get_x11_auth(disp, conf);
\r
347 void x11_free_display(struct X11Display *disp)
\r
349 sfree(disp->hostname);
\r
350 sfree(disp->unixsocketpath);
\r
351 if (disp->localauthdata)
\r
352 smemclr(disp->localauthdata, disp->localauthdatalen);
\r
353 sfree(disp->localauthdata);
\r
354 sk_addr_free(disp->addr);
\r
358 #define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */
\r
360 static char *x11_verify(unsigned long peer_ip, int peer_port,
\r
361 tree234 *authtree, char *proto,
\r
362 unsigned char *data, int dlen,
\r
363 struct X11FakeAuth **auth_ret)
\r
365 struct X11FakeAuth match_dummy; /* for passing to find234 */
\r
366 struct X11FakeAuth *auth;
\r
369 * First, do a lookup in our tree to find the only authorisation
\r
370 * record that _might_ match.
\r
372 if (!strcmp(proto, x11_authnames[X11_MIT])) {
\r
374 * Just look up the whole cookie that was presented to us,
\r
375 * which x11_authcmp will compare against the cookies we
\r
376 * currently believe in.
\r
378 match_dummy.proto = X11_MIT;
\r
379 match_dummy.datalen = dlen;
\r
380 match_dummy.data = data;
\r
381 } else if (!strcmp(proto, x11_authnames[X11_XDM])) {
\r
383 * Look up the first cipher block, against the stored first
\r
384 * cipher blocks for the XDM-AUTHORIZATION-1 cookies we
\r
385 * currently know. (See comment in x11_invent_fake_auth.)
\r
387 match_dummy.proto = X11_XDM;
\r
388 match_dummy.xa1_firstblock = data;
\r
390 return "Unsupported authorisation protocol";
\r
393 if ((auth = find234(authtree, &match_dummy, 0)) == NULL)
\r
394 return "Authorisation not recognised";
\r
397 * If we're using MIT-MAGIC-COOKIE-1, that was all we needed. If
\r
398 * we're doing XDM-AUTHORIZATION-1, though, we have to check the
\r
399 * rest of the auth data.
\r
401 if (auth->proto == X11_XDM) {
\r
405 struct XDMSeen *seen, *ret;
\r
408 return "XDM-AUTHORIZATION-1 data was wrong length";
\r
409 if (peer_port == -1)
\r
410 return "cannot do XDM-AUTHORIZATION-1 without remote address data";
\r
411 des_decrypt_xdmauth(auth->data+9, data, 24);
\r
412 if (memcmp(auth->data, data, 8) != 0)
\r
413 return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */
\r
414 if (GET_32BIT_MSB_FIRST(data+8) != peer_ip)
\r
415 return "XDM-AUTHORIZATION-1 data failed check"; /* IP wrong */
\r
416 if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port)
\r
417 return "XDM-AUTHORIZATION-1 data failed check"; /* port wrong */
\r
418 t = GET_32BIT_MSB_FIRST(data+14);
\r
419 for (i = 18; i < 24; i++)
\r
420 if (data[i] != 0) /* zero padding wrong */
\r
421 return "XDM-AUTHORIZATION-1 data failed check";
\r
423 if (abs(t - tim) > XDM_MAXSKEW)
\r
424 return "XDM-AUTHORIZATION-1 time stamp was too far out";
\r
425 seen = snew(struct XDMSeen);
\r
427 memcpy(seen->clientid, data+8, 6);
\r
428 assert(auth->xdmseen != NULL);
\r
429 ret = add234(auth->xdmseen, seen);
\r
432 return "XDM-AUTHORIZATION-1 data replayed";
\r
434 /* While we're here, purge entries too old to be replayed. */
\r
436 seen = index234(auth->xdmseen, 0);
\r
437 assert(seen != NULL);
\r
438 if (t - seen->time <= XDM_MAXSKEW)
\r
440 sfree(delpos234(auth->xdmseen, 0));
\r
443 /* implement other protocols here if ever required */
\r
449 void x11_get_auth_from_authfile(struct X11Display *disp,
\r
450 const char *authfilename)
\r
453 char *buf, *ptr, *str[4];
\r
455 int family, protocol;
\r
456 int ideal_match = FALSE;
\r
460 * Normally we should look for precisely the details specified in
\r
461 * `disp'. However, there's an oddity when the display is local:
\r
462 * displays like "localhost:0" usually have their details stored
\r
463 * in a Unix-domain-socket record (even if there isn't actually a
\r
464 * real Unix-domain socket available, as with OpenSSH's proxy X11
\r
467 * This is apparently a fudge to get round the meaninglessness of
\r
468 * "localhost" in a shared-home-directory context -- xauth entries
\r
469 * for Unix-domain sockets already disambiguate this by storing
\r
470 * the *local* hostname in the conveniently-blank hostname field,
\r
471 * but IP "localhost" records couldn't do this. So, typically, an
\r
472 * IP "localhost" entry in the auth database isn't present and if
\r
473 * it were it would be ignored.
\r
475 * However, we don't entirely trust that (say) Windows X servers
\r
476 * won't rely on a straight "localhost" entry, bad idea though
\r
477 * that is; so if we can't find a Unix-domain-socket entry we'll
\r
478 * fall back to an IP-based entry if we can find one.
\r
480 int localhost = !disp->unixdomain && sk_address_is_local(disp->addr);
\r
482 authfp = fopen(authfilename, "rb");
\r
486 ourhostname = get_hostname();
\r
488 /* Records in .Xauthority contain four strings of up to 64K each */
\r
489 buf = snewn(65537 * 4, char);
\r
491 while (!ideal_match) {
\r
492 int c, i, j, match = FALSE;
\r
494 #define GET do { c = fgetc(authfp); if (c == EOF) goto done; c = (unsigned char)c; } while (0)
\r
495 /* Expect a big-endian 2-byte number giving address family */
\r
497 GET; family = (family << 8) | c;
\r
498 /* Then expect four strings, each composed of a big-endian 2-byte
\r
499 * length field followed by that many bytes of data */
\r
501 for (i = 0; i < 4; i++) {
\r
503 GET; len[i] = (len[i] << 8) | c;
\r
505 for (j = 0; j < len[i]; j++) {
\r
513 * Now we have a full X authority record in memory. See
\r
514 * whether it matches the display we're trying to
\r
517 * The details we've just read should be interpreted as
\r
520 * - 'family' is the network address family used to
\r
521 * connect to the display. 0 means IPv4; 6 means IPv6;
\r
522 * 256 means Unix-domain sockets.
\r
524 * - str[0] is the network address itself. For IPv4 and
\r
525 * IPv6, this is a string of binary data of the
\r
526 * appropriate length (respectively 4 and 16 bytes)
\r
527 * representing the address in big-endian format, e.g.
\r
528 * 7F 00 00 01 means IPv4 localhost. For Unix-domain
\r
529 * sockets, this is the host name of the machine on
\r
530 * which the Unix-domain display resides (so that an
\r
531 * .Xauthority file on a shared file system can contain
\r
532 * authority entries for Unix-domain displays on
\r
533 * several machines without them clashing).
\r
535 * - str[1] is the display number. I've no idea why
\r
536 * .Xauthority stores this as a string when it has a
\r
537 * perfectly good integer format, but there we go.
\r
539 * - str[2] is the authorisation method, encoded as its
\r
540 * canonical string name (i.e. "MIT-MAGIC-COOKIE-1",
\r
541 * "XDM-AUTHORIZATION-1" or something we don't
\r
544 * - str[3] is the actual authorisation data, stored in
\r
548 if (disp->displaynum < 0 || disp->displaynum != atoi(str[1]))
\r
549 continue; /* not the one */
\r
551 for (protocol = 1; protocol < lenof(x11_authnames); protocol++)
\r
552 if (!strcmp(str[2], x11_authnames[protocol]))
\r
554 if (protocol == lenof(x11_authnames))
\r
555 continue; /* don't recognise this protocol, look for another */
\r
559 if (!disp->unixdomain &&
\r
560 sk_addrtype(disp->addr) == ADDRTYPE_IPV4) {
\r
562 sk_addrcopy(disp->addr, buf);
\r
563 if (len[0] == 4 && !memcmp(str[0], buf, 4)) {
\r
565 /* If this is a "localhost" entry, note it down
\r
566 * but carry on looking for a Unix-domain entry. */
\r
567 ideal_match = !localhost;
\r
572 if (!disp->unixdomain &&
\r
573 sk_addrtype(disp->addr) == ADDRTYPE_IPV6) {
\r
575 sk_addrcopy(disp->addr, buf);
\r
576 if (len[0] == 16 && !memcmp(str[0], buf, 16)) {
\r
578 ideal_match = !localhost;
\r
582 case 256: /* Unix-domain / localhost */
\r
583 if ((disp->unixdomain || localhost)
\r
584 && ourhostname && !strcmp(ourhostname, str[0]))
\r
585 /* A matching Unix-domain socket is always the best
\r
587 match = ideal_match = TRUE;
\r
592 /* Current best guess -- may be overridden if !ideal_match */
\r
593 disp->localauthproto = protocol;
\r
594 sfree(disp->localauthdata); /* free previous guess, if any */
\r
595 disp->localauthdata = snewn(len[3], unsigned char);
\r
596 memcpy(disp->localauthdata, str[3], len[3]);
\r
597 disp->localauthdatalen = len[3];
\r
603 smemclr(buf, 65537 * 4);
\r
605 sfree(ourhostname);
\r
608 static void x11_log(Plug p, int type, SockAddr addr, int port,
\r
609 const char *error_msg, int error_code)
\r
611 /* We have no interface to the logging module here, so we drop these. */
\r
614 static void x11_send_init_error(struct X11Connection *conn,
\r
615 const char *err_message);
\r
617 static int x11_closing(Plug plug, const char *error_msg, int error_code,
\r
620 struct X11Connection *xconn = (struct X11Connection *) plug;
\r
624 * Socket error. If we're still at the connection setup stage,
\r
625 * construct an X11 error packet passing on the problem.
\r
627 if (xconn->no_data_sent_to_x_client) {
\r
628 char *err_message = dupprintf("unable to connect to forwarded "
\r
629 "X server: %s", error_msg);
\r
630 x11_send_init_error(xconn, err_message);
\r
631 sfree(err_message);
\r
635 * Whether we did that or not, now we slam the connection
\r
638 sshfwd_unclean_close(xconn->c, error_msg);
\r
641 * Ordinary EOF received on socket. Send an EOF on the SSH
\r
645 sshfwd_write_eof(xconn->c);
\r
651 static int x11_receive(Plug plug, int urgent, char *data, int len)
\r
653 struct X11Connection *xconn = (struct X11Connection *) plug;
\r
655 if (sshfwd_write(xconn->c, data, len) > 0) {
\r
656 xconn->throttled = 1;
\r
657 xconn->no_data_sent_to_x_client = FALSE;
\r
658 sk_set_frozen(xconn->s, 1);
\r
664 static void x11_sent(Plug plug, int bufsize)
\r
666 struct X11Connection *xconn = (struct X11Connection *) plug;
\r
668 sshfwd_unthrottle(xconn->c, bufsize);
\r
672 * When setting up X forwarding, we should send the screen number
\r
673 * from the specified local display. This function extracts it from
\r
674 * the display string.
\r
676 int x11_get_screen_number(char *display)
\r
680 n = host_strcspn(display, ":");
\r
683 n = strcspn(display, ".");
\r
686 return atoi(display + n + 1);
\r
690 * Called to set up the X11Connection structure, though this does not
\r
691 * yet connect to an actual server.
\r
693 struct X11Connection *x11_init(tree234 *authtree, void *c,
\r
694 const char *peeraddr, int peerport)
\r
696 static const struct plug_function_table fn_table = {
\r
704 struct X11Connection *xconn;
\r
709 xconn = snew(struct X11Connection);
\r
710 xconn->fn = &fn_table;
\r
711 xconn->auth_protocol = NULL;
\r
712 xconn->authtree = authtree;
\r
713 xconn->verified = 0;
\r
714 xconn->data_read = 0;
\r
715 xconn->throttled = xconn->throttle_override = 0;
\r
716 xconn->no_data_sent_to_x_client = TRUE;
\r
720 * We don't actually open a local socket to the X server just yet,
\r
721 * because we don't know which one it is. Instead, we'll wait
\r
722 * until we see the incoming authentication data, which may tell
\r
723 * us what display to connect to, or whether we have to divert
\r
724 * this X forwarding channel to a connection-sharing downstream
\r
725 * rather than handling it ourself.
\r
727 xconn->disp = NULL;
\r
731 * Stash the peer address we were given in its original text form.
\r
733 xconn->peer_addr = peeraddr ? dupstr(peeraddr) : NULL;
\r
734 xconn->peer_port = peerport;
\r
739 void x11_close(struct X11Connection *xconn)
\r
744 if (xconn->auth_protocol) {
\r
745 sfree(xconn->auth_protocol);
\r
746 sfree(xconn->auth_data);
\r
750 sk_close(xconn->s);
\r
752 sfree(xconn->peer_addr);
\r
756 void x11_unthrottle(struct X11Connection *xconn)
\r
761 xconn->throttled = 0;
\r
763 sk_set_frozen(xconn->s, xconn->throttled || xconn->throttle_override);
\r
766 void x11_override_throttle(struct X11Connection *xconn, int enable)
\r
771 xconn->throttle_override = enable;
\r
773 sk_set_frozen(xconn->s, xconn->throttled || xconn->throttle_override);
\r
776 static void x11_send_init_error(struct X11Connection *xconn,
\r
777 const char *err_message)
\r
779 char *full_message;
\r
780 int msglen, msgsize;
\r
781 unsigned char *reply;
\r
783 full_message = dupprintf("%s X11 proxy: %s\n", appname, err_message);
\r
785 msglen = strlen(full_message);
\r
786 reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
\r
787 msgsize = (msglen + 3) & ~3;
\r
788 reply[0] = 0; /* failure */
\r
789 reply[1] = msglen; /* length of reason string */
\r
790 memcpy(reply + 2, xconn->firstpkt + 2, 4); /* major/minor proto vsn */
\r
791 PUT_16BIT(xconn->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
\r
792 memset(reply + 8, 0, msgsize);
\r
793 memcpy(reply + 8, full_message, msglen);
\r
794 sshfwd_write(xconn->c, (char *)reply, 8 + msgsize);
\r
795 sshfwd_write_eof(xconn->c);
\r
796 xconn->no_data_sent_to_x_client = FALSE;
\r
798 sfree(full_message);
\r
801 static int x11_parse_ip(const char *addr_string, unsigned long *ip)
\r
805 * See if we can make sense of this string as an IPv4 address, for
\r
806 * XDM-AUTHORIZATION-1 purposes.
\r
810 4 == sscanf(addr_string, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {
\r
811 *ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];
\r
819 * Called to send data down the raw connection.
\r
821 int x11_send(struct X11Connection *xconn, char *data, int len)
\r
827 * Read the first packet.
\r
829 while (len > 0 && xconn->data_read < 12)
\r
830 xconn->firstpkt[xconn->data_read++] = (unsigned char) (len--, *data++);
\r
831 if (xconn->data_read < 12)
\r
835 * If we have not allocated the auth_protocol and auth_data
\r
836 * strings, do so now.
\r
838 if (!xconn->auth_protocol) {
\r
839 xconn->auth_plen = GET_16BIT(xconn->firstpkt[0], xconn->firstpkt + 6);
\r
840 xconn->auth_dlen = GET_16BIT(xconn->firstpkt[0], xconn->firstpkt + 8);
\r
841 xconn->auth_psize = (xconn->auth_plen + 3) & ~3;
\r
842 xconn->auth_dsize = (xconn->auth_dlen + 3) & ~3;
\r
843 /* Leave room for a terminating zero, to make our lives easier. */
\r
844 xconn->auth_protocol = snewn(xconn->auth_psize + 1, char);
\r
845 xconn->auth_data = snewn(xconn->auth_dsize, unsigned char);
\r
849 * Read the auth_protocol and auth_data strings.
\r
852 xconn->data_read < 12 + xconn->auth_psize)
\r
853 xconn->auth_protocol[xconn->data_read++ - 12] = (len--, *data++);
\r
855 xconn->data_read < 12 + xconn->auth_psize + xconn->auth_dsize)
\r
856 xconn->auth_data[xconn->data_read++ - 12 -
\r
857 xconn->auth_psize] = (unsigned char) (len--, *data++);
\r
858 if (xconn->data_read < 12 + xconn->auth_psize + xconn->auth_dsize)
\r
862 * If we haven't verified the authorisation, do so now.
\r
864 if (!xconn->verified) {
\r
866 struct X11FakeAuth *auth_matched = NULL;
\r
867 unsigned long peer_ip;
\r
869 int protomajor, protominor;
\r
872 unsigned char *socketdata;
\r
874 char new_peer_addr[32];
\r
877 protomajor = GET_16BIT(xconn->firstpkt[0], xconn->firstpkt + 2);
\r
878 protominor = GET_16BIT(xconn->firstpkt[0], xconn->firstpkt + 4);
\r
882 xconn->auth_protocol[xconn->auth_plen] = '\0'; /* ASCIZ */
\r
884 peer_ip = 0; /* placate optimiser */
\r
885 if (x11_parse_ip(xconn->peer_addr, &peer_ip))
\r
886 peer_port = xconn->peer_port;
\r
888 peer_port = -1; /* signal no peer address data available */
\r
890 err = x11_verify(peer_ip, peer_port,
\r
891 xconn->authtree, xconn->auth_protocol,
\r
892 xconn->auth_data, xconn->auth_dlen, &auth_matched);
\r
894 x11_send_init_error(xconn, err);
\r
897 assert(auth_matched);
\r
900 * If this auth points to a connection-sharing downstream
\r
901 * rather than an X display we know how to connect to
\r
902 * directly, pass it off to the sharing module now.
\r
904 if (auth_matched->share_cs) {
\r
905 sshfwd_x11_sharing_handover(xconn->c, auth_matched->share_cs,
\r
906 auth_matched->share_chan,
\r
907 xconn->peer_addr, xconn->peer_port,
\r
908 xconn->firstpkt[0],
\r
909 protomajor, protominor, data, len);
\r
914 * Now we know we're going to accept the connection, and what
\r
915 * X display to connect to. Actually connect to it.
\r
917 sshfwd_x11_is_local(xconn->c);
\r
918 xconn->disp = auth_matched->disp;
\r
919 xconn->s = new_connection(sk_addr_dup(xconn->disp->addr),
\r
920 xconn->disp->realhost, xconn->disp->port,
\r
921 0, 1, 0, 0, (Plug) xconn,
\r
922 sshfwd_get_conf(xconn->c));
\r
923 if ((err = sk_socket_error(xconn->s)) != NULL) {
\r
924 char *err_message = dupprintf("unable to connect to"
\r
925 " forwarded X server: %s", err);
\r
926 x11_send_init_error(xconn, err_message);
\r
927 sfree(err_message);
\r
932 * Write a new connection header containing our replacement
\r
936 socketdata = sk_getxdmdata(xconn->s, &socketdatalen);
\r
937 if (socketdata && socketdatalen==6) {
\r
938 sprintf(new_peer_addr, "%d.%d.%d.%d", socketdata[0],
\r
939 socketdata[1], socketdata[2], socketdata[3]);
\r
940 new_peer_port = GET_16BIT_MSB_FIRST(socketdata + 4);
\r
942 strcpy(new_peer_addr, "0.0.0.0");
\r
946 greeting = x11_make_greeting(xconn->firstpkt[0],
\r
947 protomajor, protominor,
\r
948 xconn->disp->localauthproto,
\r
949 xconn->disp->localauthdata,
\r
950 xconn->disp->localauthdatalen,
\r
951 new_peer_addr, new_peer_port,
\r
954 sk_write(xconn->s, greeting, greeting_len);
\r
956 smemclr(greeting, greeting_len);
\r
962 xconn->verified = 1;
\r
966 * After initialisation, just copy data simply.
\r
969 return sk_write(xconn->s, data, len);
\r
972 void x11_send_eof(struct X11Connection *xconn)
\r
975 sk_write_eof(xconn->s);
\r
978 * If EOF is received from the X client before we've got to
\r
979 * the point of actually connecting to an X server, then we
\r
980 * should send an EOF back to the client so that the
\r
981 * forwarded channel will be terminated.
\r
984 sshfwd_write_eof(xconn->c);
\r
989 * Utility functions used by connection sharing to convert textual
\r
990 * representations of an X11 auth protocol name + hex cookie into our
\r
991 * usual integer protocol id and binary auth data.
\r
993 int x11_identify_auth_proto(const char *protoname)
\r
997 for (protocol = 1; protocol < lenof(x11_authnames); protocol++)
\r
998 if (!strcmp(protoname, x11_authnames[protocol]))
\r
1003 void *x11_dehexify(const char *hex, int *outlen)
\r
1006 unsigned char *ret;
\r
1008 len = strlen(hex) / 2;
\r
1009 ret = snewn(len, unsigned char);
\r
1011 for (i = 0; i < len; i++) {
\r
1014 bytestr[0] = hex[2*i];
\r
1015 bytestr[1] = hex[2*i+1];
\r
1016 bytestr[2] = '\0';
\r
1017 sscanf(bytestr, "%x", &val);
\r
1026 * Construct an X11 greeting packet, including making up the right
\r
1027 * authorisation data.
\r
1029 void *x11_make_greeting(int endian, int protomajor, int protominor,
\r
1030 int auth_proto, const void *auth_data, int auth_len,
\r
1031 const char *peer_addr, int peer_port,
\r
1034 unsigned char *greeting;
\r
1035 unsigned char realauthdata[64];
\r
1036 const char *authname;
\r
1037 const unsigned char *authdata;
\r
1038 int authnamelen, authnamelen_pad;
\r
1039 int authdatalen, authdatalen_pad;
\r
1042 authname = x11_authnames[auth_proto];
\r
1043 authnamelen = strlen(authname);
\r
1044 authnamelen_pad = (authnamelen + 3) & ~3;
\r
1046 if (auth_proto == X11_MIT) {
\r
1047 authdata = auth_data;
\r
1048 authdatalen = auth_len;
\r
1049 } else if (auth_proto == X11_XDM && auth_len == 16) {
\r
1051 unsigned long peer_ip = 0;
\r
1053 x11_parse_ip(peer_addr, &peer_ip);
\r
1055 authdata = realauthdata;
\r
1057 memset(realauthdata, 0, authdatalen);
\r
1058 memcpy(realauthdata, auth_data, 8);
\r
1059 PUT_32BIT_MSB_FIRST(realauthdata+8, peer_ip);
\r
1060 PUT_16BIT_MSB_FIRST(realauthdata+12, peer_port);
\r
1062 PUT_32BIT_MSB_FIRST(realauthdata+14, t);
\r
1064 des_encrypt_xdmauth((const unsigned char *)auth_data + 9,
\r
1065 realauthdata, authdatalen);
\r
1067 authdata = realauthdata;
\r
1071 authdatalen_pad = (authdatalen + 3) & ~3;
\r
1072 greeting_len = 12 + authnamelen_pad + authdatalen_pad;
\r
1074 greeting = snewn(greeting_len, unsigned char);
\r
1075 memset(greeting, 0, greeting_len);
\r
1076 greeting[0] = endian;
\r
1077 PUT_16BIT(endian, greeting+2, protomajor);
\r
1078 PUT_16BIT(endian, greeting+4, protominor);
\r
1079 PUT_16BIT(endian, greeting+6, authnamelen);
\r
1080 PUT_16BIT(endian, greeting+8, authdatalen);
\r
1081 memcpy(greeting+12, authname, authnamelen);
\r
1082 memcpy(greeting+12+authnamelen_pad, authdata, authdatalen);
\r
1084 smemclr(realauthdata, sizeof(realauthdata));
\r
1086 *outlen = greeting_len;
\r