8 #define GET_32BIT_LSB_FIRST(cp) \
9 (((unsigned long)(unsigned char)(cp)[0]) | \
10 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
11 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
12 ((unsigned long)(unsigned char)(cp)[3] << 24))
14 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
16 (cp)[1] = (value) >> 8, \
17 (cp)[2] = (value) >> 16, \
18 (cp)[3] = (value) >> 24 )
20 #define GET_16BIT_LSB_FIRST(cp) \
21 (((unsigned long)(unsigned char)(cp)[0]) | \
22 ((unsigned long)(unsigned char)(cp)[1] << 8))
24 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
26 (cp)[1] = (value) >> 8 )
28 #define GET_32BIT_MSB_FIRST(cp) \
29 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
30 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
31 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
32 ((unsigned long)(unsigned char)(cp)[3]))
34 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
35 (cp)[0] = (value) >> 24, \
36 (cp)[1] = (value) >> 16, \
37 (cp)[2] = (value) >> 8, \
40 #define GET_16BIT_MSB_FIRST(cp) \
41 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
42 ((unsigned long)(unsigned char)(cp)[1]))
44 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
45 (cp)[0] = (value) >> 8, \
48 #define GET_16BIT(endian, cp) \
49 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
51 #define PUT_16BIT(endian, cp, val) \
52 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
54 const char *const x11_authnames[] = {
55 "", "MIT-MAGIC-COOKIE-1"
59 unsigned char fakedata[64], realdata[64];
60 int fakeproto, realproto;
64 extern void platform_get_x11_auth(char *display, int *proto,
65 unsigned char *data, int *datalen);
68 const struct plug_function_table *fn;
69 /* the above variable absolutely *must* be the first in this structure */
70 unsigned char firstpkt[12]; /* first X data packet */
73 unsigned char *auth_data;
74 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
76 int throttled, throttle_override;
77 void *c; /* data used by ssh.c */
81 void *x11_invent_auth(char *proto, int protomaxlen,
82 char *data, int datamaxlen)
84 struct X11Auth *auth = smalloc(sizeof(struct X11Auth));
88 auth->fakeproto = X11_MIT;
90 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
92 for (i = 0; i < 16; i++)
93 auth->fakedata[i] = random_byte();
95 /* Now format for the recipient. */
96 strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);
98 for (i = 0; i < auth->fakelen; i++)
99 sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);
100 strncpy(data, ourdata, datamaxlen);
106 * Fetch the real auth data for a given display string, and store
107 * it in an X11Auth structure. Returns NULL on success, or an error
110 void x11_get_real_auth(void *authv, char *display)
112 struct X11Auth *auth = (struct X11Auth *)authv;
114 auth->realproto = X11_NO_AUTH; /* in case next call does nothing */
116 auth->reallen = sizeof(auth->realdata);
117 platform_get_x11_auth(display, &auth->realproto,
118 auth->realdata, &auth->reallen);
121 static int x11_verify(struct X11Auth *auth,
122 char *proto, unsigned char *data, int dlen)
124 if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0)
125 return 0; /* wrong protocol attempted */
126 if (auth->fakeproto == X11_MIT) {
127 if (dlen != auth->fakelen)
128 return 0; /* cookie was wrong length */
129 if (memcmp(auth->fakedata, data, dlen) != 0)
130 return 0; /* cookie was wrong cookie! */
132 /* implement other protocols here if ever required */
136 static int x11_closing(Plug plug, char *error_msg, int error_code,
139 struct X11Private *pr = (struct X11Private *) plug;
142 * We have no way to communicate down the forwarded connection,
143 * so if an error occurred on the socket, we just ignore it
144 * and treat it like a proper close.
151 static int x11_receive(Plug plug, int urgent, char *data, int len)
153 struct X11Private *pr = (struct X11Private *) plug;
155 if (sshfwd_write(pr->c, data, len) > 0) {
157 sk_set_frozen(pr->s, 1);
163 static void x11_sent(Plug plug, int bufsize)
165 struct X11Private *pr = (struct X11Private *) plug;
167 sshfwd_unthrottle(pr->c, bufsize);
171 * When setting up X forwarding, we should send the screen number
172 * from the specified local display. This function extracts it from
173 * the display string.
175 int x11_get_screen_number(char *display)
179 n = strcspn(display, ":");
182 n = strcspn(display, ".");
185 return atoi(display + n + 1);
189 * Called to set up the raw connection.
191 * Returns an error message, or NULL on success.
192 * also, fills the SocketsStructure
194 char *x11_init(Socket * s, char *display, void *c, void *auth)
196 static const struct plug_function_table fn_table = {
205 char *err, *dummy_realhost;
208 struct X11Private *pr;
211 * Split up display name into host and display-number parts.
213 n = strcspn(display, ":");
215 displaynum = atoi(display + n + 1);
217 displaynum = 0; /* sensible default */
218 if (n > sizeof(host) - 1)
219 n = sizeof(host) - 1;
221 strncpy(host, display, n);
225 * Local display numbers, particularly on Unix, often omit
226 * the display part completely.
228 strcpy(host, "localhost");
231 port = 6000 + displaynum;
236 addr = name_lookup(host, port, &dummy_realhost);
237 if ((err = sk_addr_error(addr)) != NULL)
243 pr = (struct X11Private *) smalloc(sizeof(struct X11Private));
245 pr->auth_protocol = NULL;
246 pr->auth = (struct X11Auth *)auth;
249 pr->throttled = pr->throttle_override = 0;
252 pr->s = *s = new_connection(addr, dummy_realhost, port, 0, 1, 0, (Plug) pr);
253 if ((err = sk_socket_error(*s)) != NULL) {
258 sk_set_private_ptr(*s, pr);
263 void x11_close(Socket s)
265 struct X11Private *pr;
268 pr = (struct X11Private *) sk_get_private_ptr(s);
269 if (pr->auth_protocol) {
270 sfree(pr->auth_protocol);
271 sfree(pr->auth_data);
279 void x11_unthrottle(Socket s)
281 struct X11Private *pr;
284 pr = (struct X11Private *) sk_get_private_ptr(s);
287 sk_set_frozen(s, pr->throttled || pr->throttle_override);
290 void x11_override_throttle(Socket s, int enable)
292 struct X11Private *pr;
295 pr = (struct X11Private *) sk_get_private_ptr(s);
297 pr->throttle_override = enable;
298 sk_set_frozen(s, pr->throttled || pr->throttle_override);
302 * Called to send data down the raw connection.
304 int x11_send(Socket s, char *data, int len)
306 struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s);
312 * Read the first packet.
314 while (len > 0 && pr->data_read < 12)
315 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
316 if (pr->data_read < 12)
320 * If we have not allocated the auth_protocol and auth_data
321 * strings, do so now.
323 if (!pr->auth_protocol) {
324 pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);
325 pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);
326 pr->auth_psize = (pr->auth_plen + 3) & ~3;
327 pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
328 /* Leave room for a terminating zero, to make our lives easier. */
329 pr->auth_protocol = (char *) smalloc(pr->auth_psize + 1);
330 pr->auth_data = (unsigned char *) smalloc(pr->auth_dsize);
334 * Read the auth_protocol and auth_data strings.
336 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
337 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
338 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
339 pr->auth_data[pr->data_read++ - 12 -
340 pr->auth_psize] = (unsigned char) (len--, *data++);
341 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
345 * If we haven't verified the authentication, do so now.
350 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
351 ret = x11_verify(pr->auth, pr->auth_protocol,
352 pr->auth_data, pr->auth_dlen);
355 * If authentication failed, construct and send an error
356 * packet, then terminate the connection.
359 char message[] = "Authentication failed at PuTTY X11 proxy";
360 unsigned char reply[8 + sizeof(message) + 4];
361 int msglen = sizeof(message) - 1; /* skip zero byte */
362 int msgsize = (msglen + 3) & ~3;
363 reply[0] = 0; /* failure */
364 reply[1] = msglen; /* length of reason string */
365 memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */
366 PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
367 memset(reply + 8, 0, msgsize);
368 memcpy(reply + 8, message, msglen);
369 sshfwd_write(pr->c, (char *)reply, 8 + msgsize);
376 * Now we know we're going to accept the connection. Strip
377 * the fake auth data, and optionally put real auth data in
381 char realauthdata[64];
383 int authstrlen = strlen(x11_authnames[pr->auth->realproto]);
384 static const char zeroes[4] = { 0,0,0,0 };
386 if (pr->auth->realproto == X11_MIT) {
387 assert(pr->auth->reallen <= lenof(realauthdata));
388 realauthlen = pr->auth->reallen;
389 memcpy(realauthdata, pr->auth->realdata, realauthlen);
391 /* implement other auth methods here if required */
393 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
394 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
396 sk_write(s, (char *)pr->firstpkt, 12);
399 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);
400 sk_write(s, zeroes, 3 & (-authstrlen));
403 sk_write(s, realauthdata, realauthlen);
404 sk_write(s, zeroes, 3 & (-realauthlen));
411 * After initialisation, just copy data simply.
414 return sk_write(s, data, len);