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