46ed7b64 |
1 | /* |
2 | * Platform-independent bits of X11 forwarding. |
3 | */ |
4 | |
9c964e85 |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
e0e7dff8 |
7 | #include <assert.h> |
2f92b717 |
8 | #include <time.h> |
9c964e85 |
9 | |
10 | #include "putty.h" |
11 | #include "ssh.h" |
5f8ff48c |
12 | #include "tree234.h" |
9c964e85 |
13 | |
9c964e85 |
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 | |
e0e7dff8 |
20 | const char *const x11_authnames[] = { |
2f92b717 |
21 | "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1" |
e0e7dff8 |
22 | }; |
23 | |
5f8ff48c |
24 | struct XDMSeen { |
25 | unsigned int time; |
26 | unsigned char clientid[6]; |
27 | }; |
28 | |
9c964e85 |
29 | struct X11Private { |
302121de |
30 | const struct plug_function_table *fn; |
7e78000d |
31 | /* the above variable absolutely *must* be the first in this structure */ |
32874aea |
32 | unsigned char firstpkt[12]; /* first X data packet */ |
8def70c3 |
33 | struct X11Display *disp; |
9c964e85 |
34 | char *auth_protocol; |
35 | unsigned char *auth_data; |
36 | int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize; |
37 | int verified; |
5471d09a |
38 | int throttled, throttle_override; |
b3ebaa28 |
39 | unsigned long peer_ip; |
40 | int peer_port; |
32874aea |
41 | void *c; /* data used by ssh.c */ |
7e78000d |
42 | Socket s; |
9c964e85 |
43 | }; |
44 | |
5f8ff48c |
45 | static int xdmseen_cmp(void *a, void *b) |
46 | { |
47 | struct XDMSeen *sa = a, *sb = b; |
48 | return sa->time > sb->time ? 1 : |
49 | sa->time < sb->time ? -1 : |
50 | memcmp(sa->clientid, sb->clientid, sizeof(sa->clientid)); |
51 | } |
52 | |
8def70c3 |
53 | struct X11Display *x11_setup_display(char *display, int authtype, |
54 | const Config *cfg) |
32874aea |
55 | { |
8def70c3 |
56 | struct X11Display *disp = snew(struct X11Display); |
57 | char *localcopy; |
9c964e85 |
58 | int i; |
59 | |
8def70c3 |
60 | if (!display || !*display) { |
61 | localcopy = platform_get_x_display(); |
62 | if (!localcopy || !*localcopy) { |
63 | sfree(localcopy); |
64 | localcopy = dupstr(":0"); /* plausible default for any platform */ |
65 | } |
66 | } else |
67 | localcopy = dupstr(display); |
68 | |
69 | /* |
70 | * Parse the display name. |
71 | * |
72 | * We expect this to have one of the following forms: |
73 | * |
74 | * - the standard X format which looks like |
75 | * [ [ protocol '/' ] host ] ':' displaynumber [ '.' screennumber ] |
76 | * (X11 also permits a double colon to indicate DECnet, but |
77 | * that's not our problem, thankfully!) |
78 | * |
79 | * - only seen in the wild on MacOS (so far): a pathname to a |
80 | * Unix-domain socket, which will typically and confusingly |
81 | * end in ":0", and which I'm currently distinguishing from |
82 | * the standard scheme by noting that it starts with '/'. |
83 | */ |
84 | if (localcopy[0] == '/') { |
85 | disp->unixsocketpath = localcopy; |
86 | disp->unixdomain = TRUE; |
87 | disp->hostname = NULL; |
88 | disp->displaynum = -1; |
89 | disp->screennum = 0; |
90 | } else { |
91 | char *colon, *dot, *slash; |
92 | char *protocol, *hostname; |
93 | |
94 | colon = strrchr(localcopy, ':'); |
95 | if (!colon) { |
96 | sfree(disp); |
97 | sfree(localcopy); |
98 | return NULL; /* FIXME: report a specific error? */ |
99 | } |
100 | |
101 | *colon++ = '\0'; |
102 | dot = strchr(colon, '.'); |
103 | if (dot) |
104 | *dot++ = '\0'; |
105 | |
106 | disp->displaynum = atoi(colon); |
107 | if (dot) |
108 | disp->screennum = atoi(dot); |
109 | else |
110 | disp->screennum = 0; |
111 | |
112 | protocol = NULL; |
113 | hostname = localcopy; |
114 | if (colon > localcopy) { |
115 | slash = strchr(localcopy, '/'); |
116 | if (slash) { |
117 | *slash++ = '\0'; |
118 | protocol = localcopy; |
119 | hostname = slash; |
120 | } |
121 | } |
122 | |
123 | disp->hostname = *hostname ? dupstr(hostname) : NULL; |
124 | |
125 | if (protocol) |
126 | disp->unixdomain = (!strcmp(protocol, "local") || |
127 | !strcmp(protocol, "unix")); |
566194cc |
128 | else if (!*hostname || !strcmp(hostname, "unix")) |
8def70c3 |
129 | disp->unixdomain = platform_uses_x11_unix_by_default; |
566194cc |
130 | else |
131 | disp->unixdomain = FALSE; |
8def70c3 |
132 | |
133 | if (!disp->hostname && !disp->unixdomain) |
134 | disp->hostname = dupstr("localhost"); |
135 | |
136 | disp->unixsocketpath = NULL; |
137 | |
138 | sfree(localcopy); |
139 | } |
140 | |
141 | /* |
142 | * Look up the display hostname, if we need to. |
143 | */ |
144 | if (disp->unixdomain) { |
145 | disp->addr = platform_get_x11_unix_address(disp->unixsocketpath, |
146 | disp->displaynum); |
147 | if (disp->unixsocketpath) |
148 | disp->realhost = dupstr(disp->unixsocketpath); |
149 | else |
150 | disp->realhost = dupprintf("unix:%d", disp->displaynum); |
151 | disp->port = 0; |
152 | } else { |
153 | const char *err; |
154 | |
155 | disp->port = 6000 + disp->displaynum; |
156 | disp->addr = name_lookup(disp->hostname, disp->port, |
157 | &disp->realhost, cfg, ADDRTYPE_UNSPEC); |
158 | |
159 | if ((err = sk_addr_error(disp->addr)) != NULL) { |
160 | sk_addr_free(disp->addr); |
161 | sfree(disp->hostname); |
162 | sfree(disp->unixsocketpath); |
163 | return NULL; /* FIXME: report an error */ |
164 | } |
165 | } |
166 | |
167 | /* |
168 | * Invent the remote authorisation details. |
169 | */ |
170 | if (authtype == X11_MIT) { |
171 | disp->remoteauthproto = X11_MIT; |
e0e7dff8 |
172 | |
b3ebaa28 |
173 | /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */ |
8def70c3 |
174 | disp->remoteauthdata = snewn(16, unsigned char); |
b3ebaa28 |
175 | for (i = 0; i < 16; i++) |
8def70c3 |
176 | disp->remoteauthdata[i] = random_byte(); |
177 | disp->remoteauthdatalen = 16; |
178 | |
179 | disp->xdmseen = NULL; |
b3ebaa28 |
180 | } else { |
8def70c3 |
181 | assert(authtype == X11_XDM); |
182 | disp->remoteauthproto = X11_XDM; |
b3ebaa28 |
183 | |
184 | /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */ |
8def70c3 |
185 | disp->remoteauthdata = snewn(16, unsigned char); |
b3ebaa28 |
186 | for (i = 0; i < 16; i++) |
8def70c3 |
187 | disp->remoteauthdata[i] = (i == 8 ? 0 : random_byte()); |
188 | disp->remoteauthdatalen = 16; |
189 | |
190 | disp->xdmseen = newtree234(xdmseen_cmp); |
b3ebaa28 |
191 | } |
8def70c3 |
192 | disp->remoteauthprotoname = dupstr(x11_authnames[disp->remoteauthproto]); |
193 | disp->remoteauthdatastring = snewn(disp->remoteauthdatalen * 2 + 1, char); |
194 | for (i = 0; i < disp->remoteauthdatalen; i++) |
195 | sprintf(disp->remoteauthdatastring + i*2, "%02x", |
196 | disp->remoteauthdata[i]); |
9c964e85 |
197 | |
8def70c3 |
198 | /* |
199 | * Fetch the local authorisation details. |
200 | */ |
201 | disp->localauthproto = X11_NO_AUTH; |
202 | disp->localauthdata = NULL; |
203 | disp->localauthdatalen = 0; |
204 | platform_get_x11_auth(disp, cfg); |
302121de |
205 | |
8def70c3 |
206 | return disp; |
9c964e85 |
207 | } |
208 | |
8def70c3 |
209 | void x11_free_display(struct X11Display *disp) |
fabd1805 |
210 | { |
8def70c3 |
211 | if (disp->xdmseen != NULL) { |
212 | struct XDMSeen *seen; |
213 | while ((seen = delpos234(disp->xdmseen, 0)) != NULL) |
5f8ff48c |
214 | sfree(seen); |
8def70c3 |
215 | freetree234(disp->xdmseen); |
5f8ff48c |
216 | } |
8def70c3 |
217 | sfree(disp->hostname); |
218 | sfree(disp->unixsocketpath); |
219 | if (disp->localauthdata) |
220 | memset(disp->localauthdata, 0, disp->localauthdatalen); |
221 | sfree(disp->localauthdata); |
222 | if (disp->remoteauthdata) |
223 | memset(disp->remoteauthdata, 0, disp->remoteauthdatalen); |
224 | sfree(disp->remoteauthdata); |
225 | sfree(disp->remoteauthprotoname); |
226 | sfree(disp->remoteauthdatastring); |
227 | sk_addr_free(disp->addr); |
228 | sfree(disp); |
e0e7dff8 |
229 | } |
230 | |
5f8ff48c |
231 | #define XDM_MAXSKEW 20*60 /* 20 minute clock skew should be OK */ |
232 | |
b3ebaa28 |
233 | static char *x11_verify(unsigned long peer_ip, int peer_port, |
8def70c3 |
234 | struct X11Display *disp, char *proto, |
b3ebaa28 |
235 | unsigned char *data, int dlen) |
32874aea |
236 | { |
8def70c3 |
237 | if (strcmp(proto, x11_authnames[disp->remoteauthproto]) != 0) |
238 | return "wrong authorisation protocol attempted"; |
239 | if (disp->remoteauthproto == X11_MIT) { |
240 | if (dlen != disp->remoteauthdatalen) |
b3ebaa28 |
241 | return "MIT-MAGIC-COOKIE-1 data was wrong length"; |
8def70c3 |
242 | if (memcmp(disp->remoteauthdata, data, dlen) != 0) |
b3ebaa28 |
243 | return "MIT-MAGIC-COOKIE-1 data did not match"; |
244 | } |
8def70c3 |
245 | if (disp->remoteauthproto == X11_XDM) { |
b3ebaa28 |
246 | unsigned long t; |
247 | time_t tim; |
248 | int i; |
5f8ff48c |
249 | struct XDMSeen *seen, *ret; |
b3ebaa28 |
250 | |
251 | if (dlen != 24) |
252 | return "XDM-AUTHORIZATION-1 data was wrong length"; |
253 | if (peer_port == -1) |
254 | return "cannot do XDM-AUTHORIZATION-1 without remote address data"; |
8def70c3 |
255 | des_decrypt_xdmauth(disp->remoteauthdata+9, data, 24); |
256 | if (memcmp(disp->remoteauthdata, data, 8) != 0) |
b3ebaa28 |
257 | return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */ |
258 | if (GET_32BIT_MSB_FIRST(data+8) != peer_ip) |
259 | return "XDM-AUTHORIZATION-1 data failed check"; /* IP wrong */ |
260 | if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port) |
261 | return "XDM-AUTHORIZATION-1 data failed check"; /* port wrong */ |
262 | t = GET_32BIT_MSB_FIRST(data+14); |
263 | for (i = 18; i < 24; i++) |
264 | if (data[i] != 0) /* zero padding wrong */ |
265 | return "XDM-AUTHORIZATION-1 data failed check"; |
266 | tim = time(NULL); |
5f8ff48c |
267 | if (abs(t - tim) > XDM_MAXSKEW) |
b3ebaa28 |
268 | return "XDM-AUTHORIZATION-1 time stamp was too far out"; |
5f8ff48c |
269 | seen = snew(struct XDMSeen); |
270 | seen->time = t; |
271 | memcpy(seen->clientid, data+8, 6); |
8def70c3 |
272 | assert(disp->xdmseen != NULL); |
273 | ret = add234(disp->xdmseen, seen); |
5f8ff48c |
274 | if (ret != seen) { |
275 | sfree(seen); |
276 | return "XDM-AUTHORIZATION-1 data replayed"; |
277 | } |
278 | /* While we're here, purge entries too old to be replayed. */ |
279 | for (;;) { |
8def70c3 |
280 | seen = index234(disp->xdmseen, 0); |
5f8ff48c |
281 | assert(seen != NULL); |
282 | if (t - seen->time <= XDM_MAXSKEW) |
283 | break; |
8def70c3 |
284 | sfree(delpos234(disp->xdmseen, 0)); |
5f8ff48c |
285 | } |
e0e7dff8 |
286 | } |
287 | /* implement other protocols here if ever required */ |
b3ebaa28 |
288 | return NULL; |
9c964e85 |
289 | } |
290 | |
8def70c3 |
291 | void x11_get_auth_from_authfile(struct X11Display *disp, |
292 | const char *authfilename) |
293 | { |
294 | FILE *authfp; |
295 | char *buf, *ptr, *str[4]; |
296 | int len[4]; |
297 | int family, protocol; |
298 | |
299 | authfp = fopen(authfilename, "rb"); |
300 | if (!authfp) |
301 | return; |
302 | |
303 | /* Records in .Xauthority contain four strings of up to 64K each */ |
304 | buf = snewn(65537 * 4, char); |
305 | |
306 | while (1) { |
307 | int c, i, j; |
308 | |
309 | #define GET do { c = fgetc(authfp); if (c == EOF) goto done; c = (unsigned char)c; } while (0) |
310 | /* Expect a big-endian 2-byte number giving address family */ |
311 | GET; family = c; |
312 | GET; family = (family << 8) | c; |
313 | /* Then expect four strings, each composed of a big-endian 2-byte |
314 | * length field followed by that many bytes of data */ |
315 | ptr = buf; |
316 | for (i = 0; i < 4; i++) { |
317 | GET; len[i] = c; |
318 | GET; len[i] = (len[i] << 8) | c; |
319 | str[i] = ptr; |
320 | for (j = 0; j < len[i]; j++) { |
321 | GET; *ptr++ = c; |
322 | } |
323 | *ptr++ = '\0'; |
324 | } |
325 | #undef GET |
326 | |
327 | /* |
328 | * Now we have a full X authority record in memory. See |
329 | * whether it matches the display we're trying to |
330 | * authenticate to. |
331 | * |
332 | * The details we've just read should be interpreted as |
333 | * follows: |
334 | * |
335 | * - 'family' is the network address family used to |
336 | * connect to the display. 0 means IPv4; 6 means IPv6; |
337 | * 256 means Unix-domain sockets. |
338 | * |
339 | * - str[0] is the network address itself. For IPv4 and |
340 | * IPv6, this is a string of binary data of the |
341 | * appropriate length (respectively 4 and 16 bytes) |
342 | * representing the address in big-endian format, e.g. |
343 | * 7F 00 00 01 means IPv4 localhost. For Unix-domain |
344 | * sockets, this is the host name of the machine on |
345 | * which the Unix-domain display resides (so that an |
346 | * .Xauthority file on a shared file system can contain |
347 | * authority entries for Unix-domain displays on |
348 | * several machines without them clashing). |
349 | * |
350 | * - str[1] is the display number. I've no idea why |
351 | * .Xauthority stores this as a string when it has a |
352 | * perfectly good integer format, but there we go. |
353 | * |
354 | * - str[2] is the authorisation method, encoded as its |
355 | * canonical string name (i.e. "MIT-MAGIC-COOKIE-1", |
356 | * "XDM-AUTHORIZATION-1" or something we don't |
357 | * recognise). |
358 | * |
359 | * - str[3] is the actual authorisation data, stored in |
360 | * binary form. |
361 | */ |
362 | |
363 | if (disp->displaynum < 0 || disp->displaynum != atoi(str[1])) |
364 | continue; /* not the one */ |
365 | |
366 | for (protocol = 1; protocol < lenof(x11_authnames); protocol++) |
367 | if (!strcmp(str[2], x11_authnames[protocol])) |
368 | break; |
369 | if (protocol == lenof(x11_authnames)) |
370 | continue; /* don't recognise this protocol, look for another */ |
371 | |
372 | switch (family) { |
373 | case 0: |
374 | if (!disp->unixdomain && |
375 | sk_addrtype(disp->addr) == ADDRTYPE_IPV4) { |
376 | char buf[4]; |
377 | sk_addrcopy(disp->addr, buf); |
378 | if (len[0] == 4 && !memcmp(str[0], buf, 4)) |
379 | goto found; |
380 | } |
381 | break; |
382 | case 6: |
383 | if (!disp->unixdomain && |
384 | sk_addrtype(disp->addr) == ADDRTYPE_IPV6) { |
385 | char buf[16]; |
386 | sk_addrcopy(disp->addr, buf); |
387 | if (len[0] == 16 && !memcmp(str[0], buf, 16)) |
388 | goto found; |
389 | } |
390 | break; |
391 | case 256: |
392 | if (disp->unixdomain && !strcmp(disp->hostname, str[0])) |
393 | goto found; |
394 | break; |
395 | } |
396 | } |
397 | |
398 | found: |
399 | disp->localauthproto = protocol; |
400 | disp->localauthdata = snewn(len[3], unsigned char); |
401 | memcpy(disp->localauthdata, str[3], len[3]); |
402 | disp->localauthdatalen = len[3]; |
403 | |
404 | done: |
405 | fclose(authfp); |
406 | memset(buf, 0, 65537 * 4); |
407 | sfree(buf); |
408 | } |
409 | |
7555d6a5 |
410 | static void x11_log(Plug p, int type, SockAddr addr, int port, |
411 | const char *error_msg, int error_code) |
412 | { |
413 | /* We have no interface to the logging module here, so we drop these. */ |
414 | } |
415 | |
cbe2d68f |
416 | static int x11_closing(Plug plug, const char *error_msg, int error_code, |
32874aea |
417 | int calling_back) |
418 | { |
7e78000d |
419 | struct X11Private *pr = (struct X11Private *) plug; |
420 | |
421 | /* |
422 | * We have no way to communicate down the forwarded connection, |
423 | * so if an error occurred on the socket, we just ignore it |
424 | * and treat it like a proper close. |
425 | */ |
426 | sshfwd_close(pr->c); |
427 | x11_close(pr->s); |
428 | return 1; |
429 | } |
430 | |
32874aea |
431 | static int x11_receive(Plug plug, int urgent, char *data, int len) |
432 | { |
7e78000d |
433 | struct X11Private *pr = (struct X11Private *) plug; |
9c964e85 |
434 | |
5471d09a |
435 | if (sshfwd_write(pr->c, data, len) > 0) { |
436 | pr->throttled = 1; |
437 | sk_set_frozen(pr->s, 1); |
438 | } |
439 | |
9c964e85 |
440 | return 1; |
441 | } |
442 | |
5471d09a |
443 | static void x11_sent(Plug plug, int bufsize) |
444 | { |
445 | struct X11Private *pr = (struct X11Private *) plug; |
446 | |
447 | sshfwd_unthrottle(pr->c, bufsize); |
448 | } |
449 | |
9c964e85 |
450 | /* |
421d6835 |
451 | * When setting up X forwarding, we should send the screen number |
452 | * from the specified local display. This function extracts it from |
453 | * the display string. |
454 | */ |
455 | int x11_get_screen_number(char *display) |
456 | { |
457 | int n; |
458 | |
459 | n = strcspn(display, ":"); |
460 | if (!display[n]) |
461 | return 0; |
462 | n = strcspn(display, "."); |
463 | if (!display[n]) |
464 | return 0; |
465 | return atoi(display + n + 1); |
466 | } |
467 | |
468 | /* |
9c964e85 |
469 | * Called to set up the raw connection. |
470 | * |
471 | * Returns an error message, or NULL on success. |
472 | * also, fills the SocketsStructure |
9c964e85 |
473 | */ |
8def70c3 |
474 | extern const char *x11_init(Socket *s, struct X11Display *disp, void *c, |
475 | const char *peeraddr, int peerport, |
476 | const Config *cfg) |
32874aea |
477 | { |
302121de |
478 | static const struct plug_function_table fn_table = { |
7555d6a5 |
479 | x11_log, |
7e78000d |
480 | x11_closing, |
5471d09a |
481 | x11_receive, |
482 | x11_sent, |
483 | NULL |
7e78000d |
484 | }; |
485 | |
cbe2d68f |
486 | const char *err; |
9c964e85 |
487 | struct X11Private *pr; |
488 | |
9c964e85 |
489 | /* |
490 | * Open socket. |
491 | */ |
3d88e64d |
492 | pr = snew(struct X11Private); |
7e78000d |
493 | pr->fn = &fn_table; |
9c964e85 |
494 | pr->auth_protocol = NULL; |
8def70c3 |
495 | pr->disp = disp; |
9c964e85 |
496 | pr->verified = 0; |
497 | pr->data_read = 0; |
5471d09a |
498 | pr->throttled = pr->throttle_override = 0; |
9c964e85 |
499 | pr->c = c; |
500 | |
8def70c3 |
501 | pr->s = *s = new_connection(sk_addr_dup(disp->addr), |
502 | disp->realhost, disp->port, |
79bf227b |
503 | 0, 1, 0, 0, (Plug) pr, cfg); |
6518ea7b |
504 | if ((err = sk_socket_error(*s)) != NULL) { |
32874aea |
505 | sfree(pr); |
7e78000d |
506 | return err; |
507 | } |
508 | |
b3ebaa28 |
509 | /* |
510 | * See if we can make sense of the peer address we were given. |
511 | */ |
512 | { |
513 | int i[4]; |
514 | if (peeraddr && |
515 | 4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) { |
516 | pr->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3]; |
517 | pr->peer_port = peerport; |
518 | } else { |
519 | pr->peer_ip = 0; |
520 | pr->peer_port = -1; |
521 | } |
522 | } |
523 | |
9c964e85 |
524 | sk_set_private_ptr(*s, pr); |
9c964e85 |
525 | return NULL; |
526 | } |
527 | |
32874aea |
528 | void x11_close(Socket s) |
529 | { |
530 | struct X11Private *pr; |
d74d141c |
531 | if (!s) |
532 | return; |
533 | pr = (struct X11Private *) sk_get_private_ptr(s); |
9c964e85 |
534 | if (pr->auth_protocol) { |
32874aea |
535 | sfree(pr->auth_protocol); |
536 | sfree(pr->auth_data); |
9c964e85 |
537 | } |
538 | |
539 | sfree(pr); |
540 | |
541 | sk_close(s); |
542 | } |
543 | |
5471d09a |
544 | void x11_unthrottle(Socket s) |
545 | { |
546 | struct X11Private *pr; |
547 | if (!s) |
548 | return; |
549 | pr = (struct X11Private *) sk_get_private_ptr(s); |
550 | |
551 | pr->throttled = 0; |
552 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
553 | } |
554 | |
555 | void x11_override_throttle(Socket s, int enable) |
556 | { |
557 | struct X11Private *pr; |
558 | if (!s) |
559 | return; |
560 | pr = (struct X11Private *) sk_get_private_ptr(s); |
561 | |
562 | pr->throttle_override = enable; |
563 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
564 | } |
565 | |
9c964e85 |
566 | /* |
567 | * Called to send data down the raw connection. |
568 | */ |
5471d09a |
569 | int x11_send(Socket s, char *data, int len) |
32874aea |
570 | { |
970f2b02 |
571 | struct X11Private *pr; |
572 | if (!s) |
5471d09a |
573 | return 0; |
970f2b02 |
574 | pr = (struct X11Private *) sk_get_private_ptr(s); |
9c964e85 |
575 | |
576 | /* |
577 | * Read the first packet. |
578 | */ |
579 | while (len > 0 && pr->data_read < 12) |
32874aea |
580 | pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++); |
9c964e85 |
581 | if (pr->data_read < 12) |
5471d09a |
582 | return 0; |
9c964e85 |
583 | |
584 | /* |
585 | * If we have not allocated the auth_protocol and auth_data |
586 | * strings, do so now. |
587 | */ |
588 | if (!pr->auth_protocol) { |
32874aea |
589 | pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6); |
590 | pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8); |
591 | pr->auth_psize = (pr->auth_plen + 3) & ~3; |
592 | pr->auth_dsize = (pr->auth_dlen + 3) & ~3; |
593 | /* Leave room for a terminating zero, to make our lives easier. */ |
3d88e64d |
594 | pr->auth_protocol = snewn(pr->auth_psize + 1, char); |
92d60585 |
595 | pr->auth_data = snewn(pr->auth_dsize, unsigned char); |
9c964e85 |
596 | } |
597 | |
598 | /* |
599 | * Read the auth_protocol and auth_data strings. |
600 | */ |
601 | while (len > 0 && pr->data_read < 12 + pr->auth_psize) |
32874aea |
602 | pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++); |
9c964e85 |
603 | while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize) |
32874aea |
604 | pr->auth_data[pr->data_read++ - 12 - |
605 | pr->auth_psize] = (unsigned char) (len--, *data++); |
9c964e85 |
606 | if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize) |
5471d09a |
607 | return 0; |
9c964e85 |
608 | |
609 | /* |
8def70c3 |
610 | * If we haven't verified the authorisation, do so now. |
9c964e85 |
611 | */ |
612 | if (!pr->verified) { |
b3ebaa28 |
613 | char *err; |
32874aea |
614 | |
615 | pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */ |
b3ebaa28 |
616 | err = x11_verify(pr->peer_ip, pr->peer_port, |
8def70c3 |
617 | pr->disp, pr->auth_protocol, |
302121de |
618 | pr->auth_data, pr->auth_dlen); |
32874aea |
619 | |
620 | /* |
8def70c3 |
621 | * If authorisation failed, construct and send an error |
32874aea |
622 | * packet, then terminate the connection. |
623 | */ |
b3ebaa28 |
624 | if (err) { |
625 | char *message; |
626 | int msglen, msgsize; |
627 | unsigned char *reply; |
628 | |
629 | message = dupprintf("PuTTY X11 proxy: %s", err); |
630 | msglen = strlen(message); |
3d88e64d |
631 | reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */ |
b3ebaa28 |
632 | msgsize = (msglen + 3) & ~3; |
32874aea |
633 | reply[0] = 0; /* failure */ |
634 | reply[1] = msglen; /* length of reason string */ |
635 | memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */ |
75105663 |
636 | PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */ |
32874aea |
637 | memset(reply + 8, 0, msgsize); |
638 | memcpy(reply + 8, message, msglen); |
f28e6b7c |
639 | sshfwd_write(pr->c, (char *)reply, 8 + msgsize); |
32874aea |
640 | sshfwd_close(pr->c); |
641 | x11_close(s); |
b3ebaa28 |
642 | sfree(reply); |
643 | sfree(message); |
5471d09a |
644 | return 0; |
32874aea |
645 | } |
646 | |
647 | /* |
648 | * Now we know we're going to accept the connection. Strip |
e0e7dff8 |
649 | * the fake auth data, and optionally put real auth data in |
650 | * instead. |
32874aea |
651 | */ |
e0e7dff8 |
652 | { |
653 | char realauthdata[64]; |
654 | int realauthlen = 0; |
8def70c3 |
655 | int authstrlen = strlen(x11_authnames[pr->disp->localauthproto]); |
62ddb51e |
656 | int buflen = 0; /* initialise to placate optimiser */ |
e0e7dff8 |
657 | static const char zeroes[4] = { 0,0,0,0 }; |
0a4022a1 |
658 | void *buf; |
e0e7dff8 |
659 | |
8def70c3 |
660 | if (pr->disp->localauthproto == X11_MIT) { |
661 | assert(pr->disp->localauthdatalen <= lenof(realauthdata)); |
662 | realauthlen = pr->disp->localauthdatalen; |
663 | memcpy(realauthdata, pr->disp->localauthdata, realauthlen); |
664 | } else if (pr->disp->localauthproto == X11_XDM && |
665 | pr->disp->localauthdatalen == 16 && |
0c5ca1f3 |
666 | ((buf = sk_getxdmdata(s, &buflen))!=0)) { |
2f92b717 |
667 | time_t t; |
0a4022a1 |
668 | realauthlen = (buflen+12+7) & ~7; |
669 | assert(realauthlen <= lenof(realauthdata)); |
670 | memset(realauthdata, 0, realauthlen); |
8def70c3 |
671 | memcpy(realauthdata, pr->disp->localauthdata, 8); |
0a4022a1 |
672 | memcpy(realauthdata+8, buf, buflen); |
2f92b717 |
673 | t = time(NULL); |
0a4022a1 |
674 | PUT_32BIT_MSB_FIRST(realauthdata+8+buflen, t); |
8def70c3 |
675 | des_encrypt_xdmauth(pr->disp->localauthdata+9, |
0a4022a1 |
676 | (unsigned char *)realauthdata, |
677 | realauthlen); |
678 | sfree(buf); |
2f92b717 |
679 | } |
e0e7dff8 |
680 | /* implement other auth methods here if required */ |
681 | |
682 | PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen); |
683 | PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen); |
684 | |
685 | sk_write(s, (char *)pr->firstpkt, 12); |
686 | |
687 | if (authstrlen) { |
8def70c3 |
688 | sk_write(s, x11_authnames[pr->disp->localauthproto], |
689 | authstrlen); |
e0e7dff8 |
690 | sk_write(s, zeroes, 3 & (-authstrlen)); |
691 | } |
692 | if (realauthlen) { |
693 | sk_write(s, realauthdata, realauthlen); |
694 | sk_write(s, zeroes, 3 & (-realauthlen)); |
695 | } |
696 | } |
32874aea |
697 | pr->verified = 1; |
9c964e85 |
698 | } |
699 | |
700 | /* |
701 | * After initialisation, just copy data simply. |
702 | */ |
703 | |
5471d09a |
704 | return sk_write(s, data, len); |
9c964e85 |
705 | } |