A zero-length return from platform_get_x_display() (for instance, a
[u/mdw/putty] / x11fwd.c
1 /*
2 * Platform-independent bits of X11 forwarding.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include <time.h>
9
10 #include "putty.h"
11 #include "ssh.h"
12 #include "tree234.h"
13
14 #define GET_16BIT(endian, cp) \
15 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
16
17 #define PUT_16BIT(endian, cp, val) \
18 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
19
20 const char *const x11_authnames[] = {
21 "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"
22 };
23
24 struct XDMSeen {
25 unsigned int time;
26 unsigned char clientid[6];
27 };
28
29 struct X11Auth {
30 unsigned char fakedata[64], realdata[64];
31 int fakeproto, realproto;
32 int fakelen, reallen;
33 tree234 *xdmseen;
34 };
35
36 struct X11Private {
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 */
40 struct X11Auth *auth;
41 char *auth_protocol;
42 unsigned char *auth_data;
43 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
44 int verified;
45 int throttled, throttle_override;
46 unsigned long peer_ip;
47 int peer_port;
48 void *c; /* data used by ssh.c */
49 Socket s;
50 };
51
52 static int xdmseen_cmp(void *a, void *b)
53 {
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));
58 }
59
60 void *x11_invent_auth(char *proto, int protomaxlen,
61 char *data, int datamaxlen, int proto_id)
62 {
63 struct X11Auth *auth = snew(struct X11Auth);
64 char ourdata[64];
65 int i;
66
67 if (proto_id == X11_MIT) {
68 auth->fakeproto = X11_MIT;
69
70 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
71 auth->fakelen = 16;
72 for (i = 0; i < 16; i++)
73 auth->fakedata[i] = random_byte();
74 auth->xdmseen = NULL;
75 } else {
76 assert(proto_id == X11_XDM);
77 auth->fakeproto = X11_XDM;
78
79 /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */
80 auth->fakelen = 16;
81 for (i = 0; i < 16; i++)
82 auth->fakedata[i] = (i == 8 ? 0 : random_byte());
83 auth->xdmseen = newtree234(xdmseen_cmp);
84 }
85
86 /* Now format for the recipient. */
87 strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);
88 ourdata[0] = '\0';
89 for (i = 0; i < auth->fakelen; i++)
90 sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);
91 strncpy(data, ourdata, datamaxlen);
92
93 return auth;
94 }
95
96 void x11_free_auth(void *authv)
97 {
98 struct X11Auth *auth = (struct X11Auth *)authv;
99 struct XDMSeen *seen;
100
101 if (auth->xdmseen != NULL) {
102 while ((seen = delpos234(auth->xdmseen, 0)) != NULL)
103 sfree(seen);
104 freetree234(auth->xdmseen);
105 }
106 sfree(auth);
107 }
108
109 /*
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
112 * string.
113 */
114 void x11_get_real_auth(void *authv, char *display)
115 {
116 struct X11Auth *auth = (struct X11Auth *)authv;
117
118 auth->realproto = X11_NO_AUTH; /* in case next call does nothing */
119
120 auth->reallen = sizeof(auth->realdata);
121 platform_get_x11_auth(display, &auth->realproto,
122 auth->realdata, &auth->reallen);
123 }
124
125 #define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */
126
127 static char *x11_verify(unsigned long peer_ip, int peer_port,
128 struct X11Auth *auth, char *proto,
129 unsigned char *data, int dlen)
130 {
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";
138 }
139 if (auth->fakeproto == X11_XDM) {
140 unsigned long t;
141 time_t tim;
142 int i;
143 struct XDMSeen *seen, *ret;
144
145 if (dlen != 24)
146 return "XDM-AUTHORIZATION-1 data was wrong length";
147 if (peer_port == -1)
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";
160 tim = time(NULL);
161 if (abs(t - tim) > XDM_MAXSKEW)
162 return "XDM-AUTHORIZATION-1 time stamp was too far out";
163 seen = snew(struct XDMSeen);
164 seen->time = t;
165 memcpy(seen->clientid, data+8, 6);
166 assert(auth->xdmseen != NULL);
167 ret = add234(auth->xdmseen, seen);
168 if (ret != seen) {
169 sfree(seen);
170 return "XDM-AUTHORIZATION-1 data replayed";
171 }
172 /* While we're here, purge entries too old to be replayed. */
173 for (;;) {
174 seen = index234(auth->xdmseen, 0);
175 assert(seen != NULL);
176 if (t - seen->time <= XDM_MAXSKEW)
177 break;
178 sfree(delpos234(auth->xdmseen, 0));
179 }
180 }
181 /* implement other protocols here if ever required */
182 return NULL;
183 }
184
185 static void x11_log(Plug p, int type, SockAddr addr, int port,
186 const char *error_msg, int error_code)
187 {
188 /* We have no interface to the logging module here, so we drop these. */
189 }
190
191 static int x11_closing(Plug plug, const char *error_msg, int error_code,
192 int calling_back)
193 {
194 struct X11Private *pr = (struct X11Private *) plug;
195
196 /*
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.
200 */
201 sshfwd_close(pr->c);
202 x11_close(pr->s);
203 return 1;
204 }
205
206 static int x11_receive(Plug plug, int urgent, char *data, int len)
207 {
208 struct X11Private *pr = (struct X11Private *) plug;
209
210 if (sshfwd_write(pr->c, data, len) > 0) {
211 pr->throttled = 1;
212 sk_set_frozen(pr->s, 1);
213 }
214
215 return 1;
216 }
217
218 static void x11_sent(Plug plug, int bufsize)
219 {
220 struct X11Private *pr = (struct X11Private *) plug;
221
222 sshfwd_unthrottle(pr->c, bufsize);
223 }
224
225 /*
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.
229 */
230 int x11_get_screen_number(char *display)
231 {
232 int n;
233
234 n = strcspn(display, ":");
235 if (!display[n])
236 return 0;
237 n = strcspn(display, ".");
238 if (!display[n])
239 return 0;
240 return atoi(display + n + 1);
241 }
242
243 /* Find the right display, returns an allocated string */
244 char *x11_display(const char *display) {
245 char *ret;
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 */
250 ret = dupstr(":0");
251 } else
252 ret = dupstr(display);
253 if(ret[0] == ':') {
254 /* no transport specified, use whatever we think is best */
255 char *s = dupcat(platform_x11_best_transport, ret, (char *)0);
256 sfree(ret);
257 return s;
258 } else
259 return ret;
260 }
261
262 /*
263 * Called to set up the raw connection.
264 *
265 * Returns an error message, or NULL on success.
266 * also, fills the SocketsStructure
267 */
268 const char *x11_init(Socket * s, char *display, void *c, void *auth,
269 const char *peeraddr, int peerport, const Config *cfg)
270 {
271 static const struct plug_function_table fn_table = {
272 x11_log,
273 x11_closing,
274 x11_receive,
275 x11_sent,
276 NULL
277 };
278
279 SockAddr addr;
280 int port;
281 const char *err;
282 char *dummy_realhost;
283 char host[128];
284 int n, displaynum;
285 struct X11Private *pr;
286
287 /* default display */
288 display = x11_display(display);
289 /*
290 * Split up display name into host and display-number parts.
291 */
292 n = strcspn(display, ":");
293 assert(n != 0); /* x11_display() promises this */
294 if (display[n])
295 displaynum = atoi(display + n + 1);
296 else
297 displaynum = 0; /* sensible default */
298 if (n > sizeof(host) - 1)
299 n = sizeof(host) - 1;
300 strncpy(host, display, n);
301 host[n] = '\0';
302 sfree(display);
303
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,
307 &dummy_realhost);
308 port = 0; /* to show we are not confused */
309 } else {
310 port = 6000 + displaynum;
311
312 /*
313 * Try to find host.
314 */
315 addr = name_lookup(host, port, &dummy_realhost, cfg, ADDRTYPE_UNSPEC);
316 if ((err = sk_addr_error(addr)) != NULL) {
317 sk_addr_free(addr);
318 return err;
319 }
320 }
321
322 /*
323 * Open socket.
324 */
325 pr = snew(struct X11Private);
326 pr->fn = &fn_table;
327 pr->auth_protocol = NULL;
328 pr->auth = (struct X11Auth *)auth;
329 pr->verified = 0;
330 pr->data_read = 0;
331 pr->throttled = pr->throttle_override = 0;
332 pr->c = c;
333
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) {
337 sfree(pr);
338 return err;
339 }
340
341 /*
342 * See if we can make sense of the peer address we were given.
343 */
344 {
345 int i[4];
346 if (peeraddr &&
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;
350 } else {
351 pr->peer_ip = 0;
352 pr->peer_port = -1;
353 }
354 }
355
356 sk_set_private_ptr(*s, pr);
357 return NULL;
358 }
359
360 void x11_close(Socket s)
361 {
362 struct X11Private *pr;
363 if (!s)
364 return;
365 pr = (struct X11Private *) sk_get_private_ptr(s);
366 if (pr->auth_protocol) {
367 sfree(pr->auth_protocol);
368 sfree(pr->auth_data);
369 }
370
371 sfree(pr);
372
373 sk_close(s);
374 }
375
376 void x11_unthrottle(Socket s)
377 {
378 struct X11Private *pr;
379 if (!s)
380 return;
381 pr = (struct X11Private *) sk_get_private_ptr(s);
382
383 pr->throttled = 0;
384 sk_set_frozen(s, pr->throttled || pr->throttle_override);
385 }
386
387 void x11_override_throttle(Socket s, int enable)
388 {
389 struct X11Private *pr;
390 if (!s)
391 return;
392 pr = (struct X11Private *) sk_get_private_ptr(s);
393
394 pr->throttle_override = enable;
395 sk_set_frozen(s, pr->throttled || pr->throttle_override);
396 }
397
398 /*
399 * Called to send data down the raw connection.
400 */
401 int x11_send(Socket s, char *data, int len)
402 {
403 struct X11Private *pr;
404 if (!s)
405 return 0;
406 pr = (struct X11Private *) sk_get_private_ptr(s);
407
408 /*
409 * Read the first packet.
410 */
411 while (len > 0 && pr->data_read < 12)
412 pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
413 if (pr->data_read < 12)
414 return 0;
415
416 /*
417 * If we have not allocated the auth_protocol and auth_data
418 * strings, do so now.
419 */
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);
428 }
429
430 /*
431 * Read the auth_protocol and auth_data strings.
432 */
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)
439 return 0;
440
441 /*
442 * If we haven't verified the authentication, do so now.
443 */
444 if (!pr->verified) {
445 char *err;
446
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);
451
452 /*
453 * If authentication failed, construct and send an error
454 * packet, then terminate the connection.
455 */
456 if (err) {
457 char *message;
458 int msglen, msgsize;
459 unsigned char *reply;
460
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);
472 sshfwd_close(pr->c);
473 x11_close(s);
474 sfree(reply);
475 sfree(message);
476 return 0;
477 }
478
479 /*
480 * Now we know we're going to accept the connection. Strip
481 * the fake auth data, and optionally put real auth data in
482 * instead.
483 */
484 {
485 char realauthdata[64];
486 int realauthlen = 0;
487 int authstrlen = strlen(x11_authnames[pr->auth->realproto]);
488 int buflen;
489 static const char zeroes[4] = { 0,0,0,0 };
490 void *buf;
491
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)) {
499 time_t t;
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);
505 t = time(NULL);
506 PUT_32BIT_MSB_FIRST(realauthdata+8+buflen, t);
507 des_encrypt_xdmauth(pr->auth->realdata+9,
508 (unsigned char *)realauthdata,
509 realauthlen);
510 sfree(buf);
511 }
512 /* implement other auth methods here if required */
513
514 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
515 PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
516
517 sk_write(s, (char *)pr->firstpkt, 12);
518
519 if (authstrlen) {
520 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);
521 sk_write(s, zeroes, 3 & (-authstrlen));
522 }
523 if (realauthlen) {
524 sk_write(s, realauthdata, realauthlen);
525 sk_write(s, zeroes, 3 & (-realauthlen));
526 }
527 }
528 pr->verified = 1;
529 }
530
531 /*
532 * After initialisation, just copy data simply.
533 */
534
535 return sk_write(s, data, len);
536 }