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