2 * Platform-independent bits of X11 forwarding.
14 #define GET_16BIT(endian, cp) \
15 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
17 #define PUT_16BIT(endian, cp, val) \
18 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
20 const char *const x11_authnames[] = {
21 "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"
26 unsigned char clientid[6];
30 unsigned char fakedata[64], realdata[64];
31 int fakeproto, realproto;
37 const struct plug_function_table *fn;
38 /* the above variable absolutely *must* be the first in this structure */
39 unsigned char firstpkt[12]; /* first X data packet */
42 unsigned char *auth_data;
43 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
45 int throttled, throttle_override;
46 unsigned long peer_ip;
48 void *c; /* data used by ssh.c */
52 static int xdmseen_cmp(void *a, void *b)
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));
60 void *x11_invent_auth(char *proto, int protomaxlen,
61 char *data, int datamaxlen, int proto_id)
63 struct X11Auth *auth = snew(struct X11Auth);
67 if (proto_id == X11_MIT) {
68 auth->fakeproto = X11_MIT;
70 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
72 for (i = 0; i < 16; i++)
73 auth->fakedata[i] = random_byte();
76 assert(proto_id == X11_XDM);
77 auth->fakeproto = X11_XDM;
79 /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */
81 for (i = 0; i < 16; i++)
82 auth->fakedata[i] = (i == 8 ? 0 : random_byte());
83 auth->xdmseen = newtree234(xdmseen_cmp);
86 /* Now format for the recipient. */
87 strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);
89 for (i = 0; i < auth->fakelen; i++)
90 sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);
91 strncpy(data, ourdata, datamaxlen);
96 void x11_free_auth(void *authv)
98 struct X11Auth *auth = (struct X11Auth *)authv;
101 if (auth->xdmseen != NULL) {
102 while ((seen = delpos234(auth->xdmseen, 0)) != NULL)
104 freetree234(auth->xdmseen);
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
114 void x11_get_real_auth(void *authv, char *display)
116 struct X11Auth *auth = (struct X11Auth *)authv;
118 auth->realproto = X11_NO_AUTH; /* in case next call does nothing */
120 auth->reallen = sizeof(auth->realdata);
121 platform_get_x11_auth(display, &auth->realproto,
122 auth->realdata, &auth->reallen);
125 #define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */
127 static char *x11_verify(unsigned long peer_ip, int peer_port,
128 struct X11Auth *auth, char *proto,
129 unsigned char *data, int dlen)
131 if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0)
132 return "wrong authentication protocol attempted";
133 if (auth->fakeproto == X11_MIT) {
134 if (dlen != auth->fakelen)
135 return "MIT-MAGIC-COOKIE-1 data was wrong length";
136 if (memcmp(auth->fakedata, data, dlen) != 0)
137 return "MIT-MAGIC-COOKIE-1 data did not match";
139 if (auth->fakeproto == X11_XDM) {
143 struct XDMSeen *seen, *ret;
146 return "XDM-AUTHORIZATION-1 data was wrong length";
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";
161 if (abs(t - tim) > XDM_MAXSKEW)
162 return "XDM-AUTHORIZATION-1 time stamp was too far out";
163 seen = snew(struct XDMSeen);
165 memcpy(seen->clientid, data+8, 6);
166 assert(auth->xdmseen != NULL);
167 ret = add234(auth->xdmseen, seen);
170 return "XDM-AUTHORIZATION-1 data replayed";
172 /* While we're here, purge entries too old to be replayed. */
174 seen = index234(auth->xdmseen, 0);
175 assert(seen != NULL);
176 if (t - seen->time <= XDM_MAXSKEW)
178 sfree(delpos234(auth->xdmseen, 0));
181 /* implement other protocols here if ever required */
185 static void x11_log(Plug p, int type, SockAddr addr, int port,
186 const char *error_msg, int error_code)
188 /* We have no interface to the logging module here, so we drop these. */
191 static int x11_closing(Plug plug, const char *error_msg, int error_code,
194 struct X11Private *pr = (struct X11Private *) plug;
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.
206 static int x11_receive(Plug plug, int urgent, char *data, int len)
208 struct X11Private *pr = (struct X11Private *) plug;
210 if (sshfwd_write(pr->c, data, len) > 0) {
212 sk_set_frozen(pr->s, 1);
218 static void x11_sent(Plug plug, int bufsize)
220 struct X11Private *pr = (struct X11Private *) plug;
222 sshfwd_unthrottle(pr->c, bufsize);
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.
230 int x11_get_screen_number(char *display)
234 n = strcspn(display, ":");
237 n = strcspn(display, ".");
240 return atoi(display + n + 1);
243 /* Find the right display, returns an allocated string */
244 char *x11_display(const char *display) {
246 if(!display || !*display) {
247 /* try to find platform-specific local display */
248 if((ret = platform_get_x_display())==0 || !*ret)
249 /* plausible default for all platforms */
252 ret = dupstr(display);
254 /* no transport specified, use whatever we think is best */
255 char *s = dupcat(platform_x11_best_transport, ret, (char *)0);
263 * Called to set up the raw connection.
265 * Returns an error message, or NULL on success.
266 * also, fills the SocketsStructure
268 const char *x11_init(Socket * s, char *display, void *c, void *auth,
269 const char *peeraddr, int peerport, const Config *cfg)
271 static const struct plug_function_table fn_table = {
282 char *dummy_realhost;
285 struct X11Private *pr;
287 /* default display */
288 display = x11_display(display);
290 * Split up display name into host and display-number parts.
292 n = strcspn(display, ":");
293 assert(n != 0); /* x11_display() promises this */
295 displaynum = atoi(display + n + 1);
297 displaynum = 0; /* sensible default */
298 if (n > sizeof(host) - 1)
299 n = sizeof(host) - 1;
300 strncpy(host, display, n);
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,
308 port = 0; /* to show we are not confused */
310 port = 6000 + displaynum;
315 addr = name_lookup(host, port, &dummy_realhost, cfg, ADDRTYPE_UNSPEC);
316 if ((err = sk_addr_error(addr)) != NULL) {
325 pr = snew(struct X11Private);
327 pr->auth_protocol = NULL;
328 pr->auth = (struct X11Auth *)auth;
331 pr->throttled = pr->throttle_override = 0;
334 pr->s = *s = new_connection(addr, dummy_realhost, port,
335 0, 1, 0, 0, (Plug) pr, cfg);
336 if ((err = sk_socket_error(*s)) != NULL) {
342 * See if we can make sense of the peer address we were given.
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;
356 sk_set_private_ptr(*s, pr);
360 void x11_close(Socket s)
362 struct X11Private *pr;
365 pr = (struct X11Private *) sk_get_private_ptr(s);
366 if (pr->auth_protocol) {
367 sfree(pr->auth_protocol);
368 sfree(pr->auth_data);
376 void x11_unthrottle(Socket s)
378 struct X11Private *pr;
381 pr = (struct X11Private *) sk_get_private_ptr(s);
384 sk_set_frozen(s, pr->throttled || pr->throttle_override);
387 void x11_override_throttle(Socket s, int enable)
389 struct X11Private *pr;
392 pr = (struct X11Private *) sk_get_private_ptr(s);
394 pr->throttle_override = enable;
395 sk_set_frozen(s, pr->throttled || pr->throttle_override);
399 * Called to send data down the raw connection.
401 int x11_send(Socket s, char *data, int len)
403 struct X11Private *pr;
406 pr = (struct X11Private *) sk_get_private_ptr(s);
409 * Read the first packet.
411 while (len > 0 && pr->data_read < 12)
412 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
413 if (pr->data_read < 12)
417 * If we have not allocated the auth_protocol and auth_data
418 * strings, do so now.
420 if (!pr->auth_protocol) {
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. */
426 pr->auth_protocol = snewn(pr->auth_psize + 1, char);
427 pr->auth_data = snewn(pr->auth_dsize, unsigned char);
431 * Read the auth_protocol and auth_data strings.
433 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
434 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
435 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
436 pr->auth_data[pr->data_read++ - 12 -
437 pr->auth_psize] = (unsigned char) (len--, *data++);
438 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
442 * If we haven't verified the authentication, do so now.
447 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
448 err = x11_verify(pr->peer_ip, pr->peer_port,
449 pr->auth, pr->auth_protocol,
450 pr->auth_data, pr->auth_dlen);
453 * If authentication failed, construct and send an error
454 * packet, then terminate the connection.
459 unsigned char *reply;
461 message = dupprintf("PuTTY X11 proxy: %s", err);
462 msglen = strlen(message);
463 reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
464 msgsize = (msglen + 3) & ~3;
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 */
468 PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
469 memset(reply + 8, 0, msgsize);
470 memcpy(reply + 8, message, msglen);
471 sshfwd_write(pr->c, (char *)reply, 8 + msgsize);
480 * Now we know we're going to accept the connection. Strip
481 * the fake auth data, and optionally put real auth data in
485 char realauthdata[64];
487 int authstrlen = strlen(x11_authnames[pr->auth->realproto]);
489 static const char zeroes[4] = { 0,0,0,0 };
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);
496 } else if (pr->auth->realproto == X11_XDM &&
497 pr->auth->reallen == 16 &&
498 ((buf = sk_getxdmdata(s, &buflen))!=0)) {
500 realauthlen = (buflen+12+7) & ~7;
501 assert(realauthlen <= lenof(realauthdata));
502 memset(realauthdata, 0, realauthlen);
503 memcpy(realauthdata, pr->auth->realdata, 8);
504 memcpy(realauthdata+8, buf, buflen);
506 PUT_32BIT_MSB_FIRST(realauthdata+8+buflen, t);
507 des_encrypt_xdmauth(pr->auth->realdata+9,
508 (unsigned char *)realauthdata,
512 /* implement other auth methods here if required */
514 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
515 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
517 sk_write(s, (char *)pr->firstpkt, 12);
520 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);
521 sk_write(s, zeroes, 3 & (-authstrlen));
524 sk_write(s, realauthdata, realauthlen);
525 sk_write(s, zeroes, 3 & (-realauthlen));
532 * After initialisation, just copy data simply.
535 return sk_write(s, data, len);