Introduce framework for authenticating with the local X server.
[u/mdw/putty] / x11fwd.c
CommitLineData
9c964e85 1#include <stdio.h>
2#include <stdlib.h>
e0e7dff8 3#include <assert.h>
9c964e85 4
5#include "putty.h"
6#include "ssh.h"
7
9c964e85 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))
13
14#define PUT_32BIT_LSB_FIRST(cp, value) ( \
15 (cp)[0] = (value), \
16 (cp)[1] = (value) >> 8, \
17 (cp)[2] = (value) >> 16, \
18 (cp)[3] = (value) >> 24 )
19
20#define GET_16BIT_LSB_FIRST(cp) \
21 (((unsigned long)(unsigned char)(cp)[0]) | \
22 ((unsigned long)(unsigned char)(cp)[1] << 8))
23
24#define PUT_16BIT_LSB_FIRST(cp, value) ( \
25 (cp)[0] = (value), \
26 (cp)[1] = (value) >> 8 )
27
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]))
33
34#define PUT_32BIT_MSB_FIRST(cp, value) ( \
35 (cp)[0] = (value) >> 24, \
36 (cp)[1] = (value) >> 16, \
37 (cp)[2] = (value) >> 8, \
38 (cp)[3] = (value) )
39
40#define GET_16BIT_MSB_FIRST(cp) \
41 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
42 ((unsigned long)(unsigned char)(cp)[1]))
43
44#define PUT_16BIT_MSB_FIRST(cp, value) ( \
45 (cp)[0] = (value) >> 8, \
46 (cp)[1] = (value) )
47
48#define GET_16BIT(endian, cp) \
49 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
50
51#define PUT_16BIT(endian, cp, val) \
52 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
53
e0e7dff8 54const char *const x11_authnames[] = {
55 "", "MIT-MAGIC-COOKIE-1"
56};
57
302121de 58struct X11Auth {
e0e7dff8 59 unsigned char fakedata[64], realdata[64];
60 int fakeproto, realproto;
61 int fakelen, reallen;
302121de 62};
63
e0e7dff8 64extern void platform_get_x11_auth(char *display, int *proto,
65 unsigned char *data, int *datalen);
66
9c964e85 67struct X11Private {
302121de 68 const struct plug_function_table *fn;
7e78000d 69 /* the above variable absolutely *must* be the first in this structure */
32874aea 70 unsigned char firstpkt[12]; /* first X data packet */
302121de 71 struct X11Auth *auth;
9c964e85 72 char *auth_protocol;
73 unsigned char *auth_data;
74 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
75 int verified;
5471d09a 76 int throttled, throttle_override;
32874aea 77 void *c; /* data used by ssh.c */
7e78000d 78 Socket s;
9c964e85 79};
80
302121de 81void *x11_invent_auth(char *proto, int protomaxlen,
32874aea 82 char *data, int datamaxlen)
83{
302121de 84 struct X11Auth *auth = smalloc(sizeof(struct X11Auth));
9c964e85 85 char ourdata[64];
86 int i;
87
e0e7dff8 88 auth->fakeproto = X11_MIT;
89
9c964e85 90 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
e0e7dff8 91 auth->fakelen = 16;
9c964e85 92 for (i = 0; i < 16; i++)
e0e7dff8 93 auth->fakedata[i] = random_byte();
9c964e85 94
95 /* Now format for the recipient. */
e0e7dff8 96 strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);
9c964e85 97 ourdata[0] = '\0';
e0e7dff8 98 for (i = 0; i < auth->fakelen; i++)
99 sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);
9c964e85 100 strncpy(data, ourdata, datamaxlen);
302121de 101
102 return auth;
9c964e85 103}
104
e0e7dff8 105/*
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
108 * string.
109 */
110void x11_get_real_auth(void *authv, char *display)
111{
112 struct X11Auth *auth = (struct X11Auth *)authv;
113
114 auth->realproto = X11_NO_AUTH; /* in case next call does nothing */
115
116 auth->reallen = sizeof(auth->realdata);
117 platform_get_x11_auth(display, &auth->realproto,
118 auth->realdata, &auth->reallen);
119}
120
302121de 121static int x11_verify(struct X11Auth *auth,
122 char *proto, unsigned char *data, int dlen)
32874aea 123{
e0e7dff8 124 if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0)
32874aea 125 return 0; /* wrong protocol attempted */
e0e7dff8 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! */
131 }
132 /* implement other protocols here if ever required */
9c964e85 133 return 1;
134}
135
32874aea 136static int x11_closing(Plug plug, char *error_msg, int error_code,
137 int calling_back)
138{
7e78000d 139 struct X11Private *pr = (struct X11Private *) plug;
140
141 /*
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.
145 */
146 sshfwd_close(pr->c);
147 x11_close(pr->s);
148 return 1;
149}
150
32874aea 151static int x11_receive(Plug plug, int urgent, char *data, int len)
152{
7e78000d 153 struct X11Private *pr = (struct X11Private *) plug;
9c964e85 154
5471d09a 155 if (sshfwd_write(pr->c, data, len) > 0) {
156 pr->throttled = 1;
157 sk_set_frozen(pr->s, 1);
158 }
159
9c964e85 160 return 1;
161}
162
5471d09a 163static void x11_sent(Plug plug, int bufsize)
164{
165 struct X11Private *pr = (struct X11Private *) plug;
166
167 sshfwd_unthrottle(pr->c, bufsize);
168}
169
9c964e85 170/*
421d6835 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.
174 */
175int x11_get_screen_number(char *display)
176{
177 int n;
178
179 n = strcspn(display, ":");
180 if (!display[n])
181 return 0;
182 n = strcspn(display, ".");
183 if (!display[n])
184 return 0;
185 return atoi(display + n + 1);
186}
187
188/*
9c964e85 189 * Called to set up the raw connection.
190 *
191 * Returns an error message, or NULL on success.
192 * also, fills the SocketsStructure
9c964e85 193 */
302121de 194char *x11_init(Socket * s, char *display, void *c, void *auth)
32874aea 195{
302121de 196 static const struct plug_function_table fn_table = {
7e78000d 197 x11_closing,
5471d09a 198 x11_receive,
199 x11_sent,
200 NULL
7e78000d 201 };
202
9c964e85 203 SockAddr addr;
204 int port;
0c6c9a70 205 char *err, *dummy_realhost;
9c964e85 206 char host[128];
207 int n, displaynum;
208 struct X11Private *pr;
209
210 /*
211 * Split up display name into host and display-number parts.
212 */
213 n = strcspn(display, ":");
214 if (display[n])
32874aea 215 displaynum = atoi(display + n + 1);
9c964e85 216 else
32874aea 217 displaynum = 0; /* sensible default */
218 if (n > sizeof(host) - 1)
219 n = sizeof(host) - 1;
ecc0ba5b 220 if (n > 0) {
221 strncpy(host, display, n);
222 host[n] = '\0';
223 } else {
224 /*
225 * Local display numbers, particularly on Unix, often omit
226 * the display part completely.
227 */
228 strcpy(host, "localhost");
229 }
9c964e85 230
9f2f8ea0 231 port = 6000 + displaynum;
232
9c964e85 233 /*
234 * Try to find host.
235 */
b7a189f3 236 addr = name_lookup(host, port, &dummy_realhost);
6518ea7b 237 if ((err = sk_addr_error(addr)) != NULL)
9c964e85 238 return err;
239
9c964e85 240 /*
241 * Open socket.
242 */
32874aea 243 pr = (struct X11Private *) smalloc(sizeof(struct X11Private));
7e78000d 244 pr->fn = &fn_table;
9c964e85 245 pr->auth_protocol = NULL;
302121de 246 pr->auth = (struct X11Auth *)auth;
9c964e85 247 pr->verified = 0;
248 pr->data_read = 0;
5471d09a 249 pr->throttled = pr->throttle_override = 0;
9c964e85 250 pr->c = c;
251
8eebd221 252 pr->s = *s = new_connection(addr, dummy_realhost, port, 0, 1, 0, (Plug) pr);
6518ea7b 253 if ((err = sk_socket_error(*s)) != NULL) {
32874aea 254 sfree(pr);
7e78000d 255 return err;
256 }
257
9c964e85 258 sk_set_private_ptr(*s, pr);
259 sk_addr_free(addr);
260 return NULL;
261}
262
32874aea 263void x11_close(Socket s)
264{
265 struct X11Private *pr;
d74d141c 266 if (!s)
267 return;
268 pr = (struct X11Private *) sk_get_private_ptr(s);
9c964e85 269 if (pr->auth_protocol) {
32874aea 270 sfree(pr->auth_protocol);
271 sfree(pr->auth_data);
9c964e85 272 }
273
274 sfree(pr);
275
276 sk_close(s);
277}
278
5471d09a 279void x11_unthrottle(Socket s)
280{
281 struct X11Private *pr;
282 if (!s)
283 return;
284 pr = (struct X11Private *) sk_get_private_ptr(s);
285
286 pr->throttled = 0;
287 sk_set_frozen(s, pr->throttled || pr->throttle_override);
288}
289
290void x11_override_throttle(Socket s, int enable)
291{
292 struct X11Private *pr;
293 if (!s)
294 return;
295 pr = (struct X11Private *) sk_get_private_ptr(s);
296
297 pr->throttle_override = enable;
298 sk_set_frozen(s, pr->throttled || pr->throttle_override);
299}
300
9c964e85 301/*
302 * Called to send data down the raw connection.
303 */
5471d09a 304int x11_send(Socket s, char *data, int len)
32874aea 305{
306 struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s);
9c964e85 307
308 if (s == NULL)
5471d09a 309 return 0;
9c964e85 310
311 /*
312 * Read the first packet.
313 */
314 while (len > 0 && pr->data_read < 12)
32874aea 315 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
9c964e85 316 if (pr->data_read < 12)
5471d09a 317 return 0;
9c964e85 318
319 /*
320 * If we have not allocated the auth_protocol and auth_data
321 * strings, do so now.
322 */
323 if (!pr->auth_protocol) {
32874aea 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);
f28e6b7c 330 pr->auth_data = (unsigned char *) smalloc(pr->auth_dsize);
9c964e85 331 }
332
333 /*
334 * Read the auth_protocol and auth_data strings.
335 */
336 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
32874aea 337 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
9c964e85 338 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
32874aea 339 pr->auth_data[pr->data_read++ - 12 -
340 pr->auth_psize] = (unsigned char) (len--, *data++);
9c964e85 341 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
5471d09a 342 return 0;
9c964e85 343
344 /*
345 * If we haven't verified the authentication, do so now.
346 */
347 if (!pr->verified) {
32874aea 348 int ret;
349
350 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
302121de 351 ret = x11_verify(pr->auth, pr->auth_protocol,
352 pr->auth_data, pr->auth_dlen);
32874aea 353
354 /*
355 * If authentication failed, construct and send an error
356 * packet, then terminate the connection.
357 */
358 if (!ret) {
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 */
75105663 366 PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
32874aea 367 memset(reply + 8, 0, msgsize);
368 memcpy(reply + 8, message, msglen);
f28e6b7c 369 sshfwd_write(pr->c, (char *)reply, 8 + msgsize);
32874aea 370 sshfwd_close(pr->c);
371 x11_close(s);
5471d09a 372 return 0;
32874aea 373 }
374
375 /*
376 * Now we know we're going to accept the connection. Strip
e0e7dff8 377 * the fake auth data, and optionally put real auth data in
378 * instead.
32874aea 379 */
e0e7dff8 380 {
381 char realauthdata[64];
382 int realauthlen = 0;
383 int authstrlen = strlen(x11_authnames[pr->auth->realproto]);
384 static const char zeroes[4] = { 0,0,0,0 };
385
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);
390 }
391 /* implement other auth methods here if required */
392
393 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
394 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
395
396 sk_write(s, (char *)pr->firstpkt, 12);
397
398 if (authstrlen) {
399 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);
400 sk_write(s, zeroes, 3 & (-authstrlen));
401 }
402 if (realauthlen) {
403 sk_write(s, realauthdata, realauthlen);
404 sk_write(s, zeroes, 3 & (-realauthlen));
405 }
406 }
32874aea 407 pr->verified = 1;
9c964e85 408 }
409
410 /*
411 * After initialisation, just copy data simply.
412 */
413
5471d09a 414 return sk_write(s, data, len);
9c964e85 415}