9 #define GET_32BIT_LSB_FIRST(cp) \
10 (((unsigned long)(unsigned char)(cp)[0]) | \
11 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
12 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
13 ((unsigned long)(unsigned char)(cp)[3] << 24))
15 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
16 (cp)[0] = (char)(value), \
17 (cp)[1] = (char)((value) >> 8), \
18 (cp)[2] = (char)((value) >> 16), \
19 (cp)[3] = (char)((value) >> 24) )
21 #define GET_16BIT_LSB_FIRST(cp) \
22 (((unsigned long)(unsigned char)(cp)[0]) | \
23 ((unsigned long)(unsigned char)(cp)[1] << 8))
25 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
26 (cp)[0] = (char)(value), \
27 (cp)[1] = (char)((value) >> 8) )
29 #define GET_32BIT_MSB_FIRST(cp) \
30 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
31 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
32 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
33 ((unsigned long)(unsigned char)(cp)[3]))
35 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
36 (cp)[0] = (char)((value) >> 24), \
37 (cp)[1] = (char)((value) >> 16), \
38 (cp)[2] = (char)((value) >> 8), \
39 (cp)[3] = (char)(value) )
41 #define GET_16BIT_MSB_FIRST(cp) \
42 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
43 ((unsigned long)(unsigned char)(cp)[1]))
45 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
46 (cp)[0] = (char)((value) >> 8), \
47 (cp)[1] = (char)(value) )
49 #define GET_16BIT(endian, cp) \
50 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
52 #define PUT_16BIT(endian, cp, val) \
53 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
55 const char *const x11_authnames[] = {
56 "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"
60 unsigned char fakedata[64], realdata[64];
61 int fakeproto, realproto;
66 const struct plug_function_table *fn;
67 /* the above variable absolutely *must* be the first in this structure */
68 unsigned char firstpkt[12]; /* first X data packet */
71 unsigned char *auth_data;
72 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
74 int throttled, throttle_override;
75 unsigned long peer_ip;
77 void *c; /* data used by ssh.c */
81 void *x11_invent_auth(char *proto, int protomaxlen,
82 char *data, int datamaxlen, int proto_id)
84 struct X11Auth *auth = snew(struct X11Auth);
88 if (proto_id == X11_MIT) {
89 auth->fakeproto = X11_MIT;
91 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
93 for (i = 0; i < 16; i++)
94 auth->fakedata[i] = random_byte();
96 assert(proto_id == X11_XDM);
97 auth->fakeproto = X11_XDM;
99 /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */
101 for (i = 0; i < 16; i++)
102 auth->fakedata[i] = (i == 8 ? 0 : random_byte());
105 /* Now format for the recipient. */
106 strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);
108 for (i = 0; i < auth->fakelen; i++)
109 sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);
110 strncpy(data, ourdata, datamaxlen);
115 void x11_free_auth(void *auth)
122 * Fetch the real auth data for a given display string, and store
123 * it in an X11Auth structure. Returns NULL on success, or an error
126 void x11_get_real_auth(void *authv, char *display)
128 struct X11Auth *auth = (struct X11Auth *)authv;
130 auth->realproto = X11_NO_AUTH; /* in case next call does nothing */
132 auth->reallen = sizeof(auth->realdata);
133 platform_get_x11_auth(display, &auth->realproto,
134 auth->realdata, &auth->reallen);
137 static char *x11_verify(unsigned long peer_ip, int peer_port,
138 struct X11Auth *auth, char *proto,
139 unsigned char *data, int dlen)
141 if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0)
142 return "wrong authentication protocol attempted";
143 if (auth->fakeproto == X11_MIT) {
144 if (dlen != auth->fakelen)
145 return "MIT-MAGIC-COOKIE-1 data was wrong length";
146 if (memcmp(auth->fakedata, data, dlen) != 0)
147 return "MIT-MAGIC-COOKIE-1 data did not match";
149 if (auth->fakeproto == X11_XDM) {
155 return "XDM-AUTHORIZATION-1 data was wrong length";
157 return "cannot do XDM-AUTHORIZATION-1 without remote address data";
158 des_decrypt_xdmauth(auth->fakedata+9, data, 24);
159 if (memcmp(auth->fakedata, data, 8) != 0)
160 return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */
161 if (GET_32BIT_MSB_FIRST(data+8) != peer_ip)
162 return "XDM-AUTHORIZATION-1 data failed check"; /* IP wrong */
163 if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port)
164 return "XDM-AUTHORIZATION-1 data failed check"; /* port wrong */
165 t = GET_32BIT_MSB_FIRST(data+14);
166 for (i = 18; i < 24; i++)
167 if (data[i] != 0) /* zero padding wrong */
168 return "XDM-AUTHORIZATION-1 data failed check";
170 if (abs(t - tim) > 20*60) /* 20 minute clock skew should be OK */
171 return "XDM-AUTHORIZATION-1 time stamp was too far out";
173 /* implement other protocols here if ever required */
177 static int x11_closing(Plug plug, const char *error_msg, int error_code,
180 struct X11Private *pr = (struct X11Private *) plug;
183 * We have no way to communicate down the forwarded connection,
184 * so if an error occurred on the socket, we just ignore it
185 * and treat it like a proper close.
192 static int x11_receive(Plug plug, int urgent, char *data, int len)
194 struct X11Private *pr = (struct X11Private *) plug;
196 if (sshfwd_write(pr->c, data, len) > 0) {
198 sk_set_frozen(pr->s, 1);
204 static void x11_sent(Plug plug, int bufsize)
206 struct X11Private *pr = (struct X11Private *) plug;
208 sshfwd_unthrottle(pr->c, bufsize);
212 * When setting up X forwarding, we should send the screen number
213 * from the specified local display. This function extracts it from
214 * the display string.
216 int x11_get_screen_number(char *display)
220 n = strcspn(display, ":");
223 n = strcspn(display, ".");
226 return atoi(display + n + 1);
230 * Called to set up the raw connection.
232 * Returns an error message, or NULL on success.
233 * also, fills the SocketsStructure
235 const char *x11_init(Socket * s, char *display, void *c, void *auth,
236 const char *peeraddr, int peerport, const Config *cfg)
238 static const struct plug_function_table fn_table = {
248 char *dummy_realhost;
251 struct X11Private *pr;
254 * Split up display name into host and display-number parts.
256 n = strcspn(display, ":");
258 displaynum = atoi(display + n + 1);
260 displaynum = 0; /* sensible default */
261 if (n > sizeof(host) - 1)
262 n = sizeof(host) - 1;
264 strncpy(host, display, n);
268 * Local display numbers, particularly on Unix, often omit
269 * the display part completely.
271 strcpy(host, "localhost");
274 port = 6000 + displaynum;
279 addr = name_lookup(host, port, &dummy_realhost, cfg);
280 if ((err = sk_addr_error(addr)) != NULL) {
288 pr = snew(struct X11Private);
290 pr->auth_protocol = NULL;
291 pr->auth = (struct X11Auth *)auth;
294 pr->throttled = pr->throttle_override = 0;
297 pr->s = *s = new_connection(addr, dummy_realhost, port,
298 0, 1, 0, (Plug) pr, cfg);
299 if ((err = sk_socket_error(*s)) != NULL) {
305 * See if we can make sense of the peer address we were given.
310 4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {
311 pr->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];
312 pr->peer_port = peerport;
319 sk_set_private_ptr(*s, pr);
323 void x11_close(Socket s)
325 struct X11Private *pr;
328 pr = (struct X11Private *) sk_get_private_ptr(s);
329 if (pr->auth_protocol) {
330 sfree(pr->auth_protocol);
331 sfree(pr->auth_data);
339 void x11_unthrottle(Socket s)
341 struct X11Private *pr;
344 pr = (struct X11Private *) sk_get_private_ptr(s);
347 sk_set_frozen(s, pr->throttled || pr->throttle_override);
350 void x11_override_throttle(Socket s, int enable)
352 struct X11Private *pr;
355 pr = (struct X11Private *) sk_get_private_ptr(s);
357 pr->throttle_override = enable;
358 sk_set_frozen(s, pr->throttled || pr->throttle_override);
362 * Called to send data down the raw connection.
364 int x11_send(Socket s, char *data, int len)
366 struct X11Private *pr;
369 pr = (struct X11Private *) sk_get_private_ptr(s);
372 * Read the first packet.
374 while (len > 0 && pr->data_read < 12)
375 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
376 if (pr->data_read < 12)
380 * If we have not allocated the auth_protocol and auth_data
381 * strings, do so now.
383 if (!pr->auth_protocol) {
384 pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);
385 pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);
386 pr->auth_psize = (pr->auth_plen + 3) & ~3;
387 pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
388 /* Leave room for a terminating zero, to make our lives easier. */
389 pr->auth_protocol = snewn(pr->auth_psize + 1, char);
390 pr->auth_data = snewn(pr->auth_dsize, unsigned char);
394 * Read the auth_protocol and auth_data strings.
396 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
397 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
398 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
399 pr->auth_data[pr->data_read++ - 12 -
400 pr->auth_psize] = (unsigned char) (len--, *data++);
401 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
405 * If we haven't verified the authentication, do so now.
410 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
411 err = x11_verify(pr->peer_ip, pr->peer_port,
412 pr->auth, pr->auth_protocol,
413 pr->auth_data, pr->auth_dlen);
416 * If authentication failed, construct and send an error
417 * packet, then terminate the connection.
422 unsigned char *reply;
424 message = dupprintf("PuTTY X11 proxy: %s", err);
425 msglen = strlen(message);
426 reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
427 msgsize = (msglen + 3) & ~3;
428 reply[0] = 0; /* failure */
429 reply[1] = msglen; /* length of reason string */
430 memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */
431 PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
432 memset(reply + 8, 0, msgsize);
433 memcpy(reply + 8, message, msglen);
434 sshfwd_write(pr->c, (char *)reply, 8 + msgsize);
443 * Now we know we're going to accept the connection. Strip
444 * the fake auth data, and optionally put real auth data in
448 char realauthdata[64];
450 int authstrlen = strlen(x11_authnames[pr->auth->realproto]);
453 static const char zeroes[4] = { 0,0,0,0 };
455 if (pr->auth->realproto == X11_MIT) {
456 assert(pr->auth->reallen <= lenof(realauthdata));
457 realauthlen = pr->auth->reallen;
458 memcpy(realauthdata, pr->auth->realdata, realauthlen);
459 } else if (pr->auth->realproto == X11_XDM &&
460 pr->auth->reallen == 16 &&
461 sk_getxdmdata(s, &ip, &port)) {
464 memset(realauthdata, 0, 24);
465 memcpy(realauthdata, pr->auth->realdata, 8);
466 PUT_32BIT_MSB_FIRST(realauthdata+8, ip);
467 PUT_16BIT_MSB_FIRST(realauthdata+12, port);
469 PUT_32BIT_MSB_FIRST(realauthdata+14, t);
470 des_encrypt_xdmauth(pr->auth->realdata+9,
471 (unsigned char *)realauthdata, 24);
473 /* implement other auth methods here if required */
475 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
476 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
478 sk_write(s, (char *)pr->firstpkt, 12);
481 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);
482 sk_write(s, zeroes, 3 & (-authstrlen));
485 sk_write(s, realauthdata, realauthlen);
486 sk_write(s, zeroes, 3 & (-realauthlen));
493 * After initialisation, just copy data simply.
496 return sk_write(s, data, len);