9c964e85 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
e0e7dff8 |
3 | #include <assert.h> |
2f92b717 |
4 | #include <time.h> |
9c964e85 |
5 | |
6 | #include "putty.h" |
7 | #include "ssh.h" |
8 | |
9c964e85 |
9 | #define GET_32BIT_LSB_FIRST(cp) \ |
10 | (((unsigned long)(unsigned char)(cp)[0]) | \ |
11 | ((unsigned long)(unsigned char)(cp)[1] << 8) | \ |
12 | ((unsigned long)(unsigned char)(cp)[2] << 16) | \ |
13 | ((unsigned long)(unsigned char)(cp)[3] << 24)) |
14 | |
15 | #define PUT_32BIT_LSB_FIRST(cp, value) ( \ |
b3ebaa28 |
16 | (cp)[0] = (char)(value), \ |
17 | (cp)[1] = (char)((value) >> 8), \ |
18 | (cp)[2] = (char)((value) >> 16), \ |
19 | (cp)[3] = (char)((value) >> 24) ) |
9c964e85 |
20 | |
21 | #define GET_16BIT_LSB_FIRST(cp) \ |
22 | (((unsigned long)(unsigned char)(cp)[0]) | \ |
23 | ((unsigned long)(unsigned char)(cp)[1] << 8)) |
24 | |
25 | #define PUT_16BIT_LSB_FIRST(cp, value) ( \ |
b3ebaa28 |
26 | (cp)[0] = (char)(value), \ |
27 | (cp)[1] = (char)((value) >> 8) ) |
9c964e85 |
28 | |
29 | #define GET_32BIT_MSB_FIRST(cp) \ |
30 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
31 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
32 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
33 | ((unsigned long)(unsigned char)(cp)[3])) |
34 | |
35 | #define PUT_32BIT_MSB_FIRST(cp, value) ( \ |
b3ebaa28 |
36 | (cp)[0] = (char)((value) >> 24), \ |
37 | (cp)[1] = (char)((value) >> 16), \ |
38 | (cp)[2] = (char)((value) >> 8), \ |
39 | (cp)[3] = (char)(value) ) |
9c964e85 |
40 | |
41 | #define GET_16BIT_MSB_FIRST(cp) \ |
42 | (((unsigned long)(unsigned char)(cp)[0] << 8) | \ |
43 | ((unsigned long)(unsigned char)(cp)[1])) |
44 | |
45 | #define PUT_16BIT_MSB_FIRST(cp, value) ( \ |
b3ebaa28 |
46 | (cp)[0] = (char)((value) >> 8), \ |
47 | (cp)[1] = (char)(value) ) |
9c964e85 |
48 | |
49 | #define GET_16BIT(endian, cp) \ |
50 | (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp)) |
51 | |
52 | #define PUT_16BIT(endian, cp, val) \ |
53 | (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val)) |
54 | |
e0e7dff8 |
55 | const char *const x11_authnames[] = { |
2f92b717 |
56 | "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1" |
e0e7dff8 |
57 | }; |
58 | |
302121de |
59 | struct X11Auth { |
e0e7dff8 |
60 | unsigned char fakedata[64], realdata[64]; |
61 | int fakeproto, realproto; |
62 | int fakelen, reallen; |
302121de |
63 | }; |
64 | |
9c964e85 |
65 | struct X11Private { |
302121de |
66 | const struct plug_function_table *fn; |
7e78000d |
67 | /* the above variable absolutely *must* be the first in this structure */ |
32874aea |
68 | unsigned char firstpkt[12]; /* first X data packet */ |
302121de |
69 | struct X11Auth *auth; |
9c964e85 |
70 | char *auth_protocol; |
71 | unsigned char *auth_data; |
72 | int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize; |
73 | int verified; |
5471d09a |
74 | int throttled, throttle_override; |
b3ebaa28 |
75 | unsigned long peer_ip; |
76 | int peer_port; |
32874aea |
77 | void *c; /* data used by ssh.c */ |
7e78000d |
78 | Socket s; |
9c964e85 |
79 | }; |
80 | |
302121de |
81 | void *x11_invent_auth(char *proto, int protomaxlen, |
b3ebaa28 |
82 | char *data, int datamaxlen, int proto_id) |
32874aea |
83 | { |
3d88e64d |
84 | struct X11Auth *auth = snew(struct X11Auth); |
9c964e85 |
85 | char ourdata[64]; |
86 | int i; |
87 | |
b3ebaa28 |
88 | if (proto_id == X11_MIT) { |
89 | auth->fakeproto = X11_MIT; |
e0e7dff8 |
90 | |
b3ebaa28 |
91 | /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */ |
92 | auth->fakelen = 16; |
93 | for (i = 0; i < 16; i++) |
94 | auth->fakedata[i] = random_byte(); |
95 | } else { |
96 | assert(proto_id == X11_XDM); |
97 | auth->fakeproto = X11_XDM; |
98 | |
99 | /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */ |
100 | auth->fakelen = 16; |
101 | for (i = 0; i < 16; i++) |
102 | auth->fakedata[i] = (i == 8 ? 0 : random_byte()); |
103 | } |
9c964e85 |
104 | |
105 | /* Now format for the recipient. */ |
e0e7dff8 |
106 | strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen); |
9c964e85 |
107 | ourdata[0] = '\0'; |
e0e7dff8 |
108 | for (i = 0; i < auth->fakelen; i++) |
109 | sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]); |
9c964e85 |
110 | strncpy(data, ourdata, datamaxlen); |
302121de |
111 | |
112 | return auth; |
9c964e85 |
113 | } |
114 | |
fabd1805 |
115 | void x11_free_auth(void *auth) |
116 | { |
117 | |
118 | sfree(auth); |
119 | } |
120 | |
e0e7dff8 |
121 | /* |
122 | * Fetch the real auth data for a given display string, and store |
123 | * it in an X11Auth structure. Returns NULL on success, or an error |
124 | * string. |
125 | */ |
126 | void x11_get_real_auth(void *authv, char *display) |
127 | { |
128 | struct X11Auth *auth = (struct X11Auth *)authv; |
129 | |
130 | auth->realproto = X11_NO_AUTH; /* in case next call does nothing */ |
131 | |
132 | auth->reallen = sizeof(auth->realdata); |
133 | platform_get_x11_auth(display, &auth->realproto, |
134 | auth->realdata, &auth->reallen); |
135 | } |
136 | |
b3ebaa28 |
137 | static char *x11_verify(unsigned long peer_ip, int peer_port, |
138 | struct X11Auth *auth, char *proto, |
139 | unsigned char *data, int dlen) |
32874aea |
140 | { |
e0e7dff8 |
141 | if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0) |
b3ebaa28 |
142 | return "wrong authentication protocol attempted"; |
e0e7dff8 |
143 | if (auth->fakeproto == X11_MIT) { |
144 | if (dlen != auth->fakelen) |
b3ebaa28 |
145 | return "MIT-MAGIC-COOKIE-1 data was wrong length"; |
e0e7dff8 |
146 | if (memcmp(auth->fakedata, data, dlen) != 0) |
b3ebaa28 |
147 | return "MIT-MAGIC-COOKIE-1 data did not match"; |
148 | } |
149 | if (auth->fakeproto == X11_XDM) { |
150 | unsigned long t; |
151 | time_t tim; |
152 | int i; |
153 | |
154 | if (dlen != 24) |
155 | return "XDM-AUTHORIZATION-1 data was wrong length"; |
156 | if (peer_port == -1) |
157 | return "cannot do XDM-AUTHORIZATION-1 without remote address data"; |
158 | des_decrypt_xdmauth(auth->fakedata+9, data, 24); |
159 | if (memcmp(auth->fakedata, data, 8) != 0) |
160 | return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */ |
161 | if (GET_32BIT_MSB_FIRST(data+8) != peer_ip) |
162 | return "XDM-AUTHORIZATION-1 data failed check"; /* IP wrong */ |
163 | if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port) |
164 | return "XDM-AUTHORIZATION-1 data failed check"; /* port wrong */ |
165 | t = GET_32BIT_MSB_FIRST(data+14); |
166 | for (i = 18; i < 24; i++) |
167 | if (data[i] != 0) /* zero padding wrong */ |
168 | return "XDM-AUTHORIZATION-1 data failed check"; |
169 | tim = time(NULL); |
170 | if (abs(t - tim) > 20*60) /* 20 minute clock skew should be OK */ |
171 | return "XDM-AUTHORIZATION-1 time stamp was too far out"; |
e0e7dff8 |
172 | } |
173 | /* implement other protocols here if ever required */ |
b3ebaa28 |
174 | return NULL; |
9c964e85 |
175 | } |
176 | |
32874aea |
177 | static int x11_closing(Plug plug, char *error_msg, int error_code, |
178 | int calling_back) |
179 | { |
7e78000d |
180 | struct X11Private *pr = (struct X11Private *) plug; |
181 | |
182 | /* |
183 | * We have no way to communicate down the forwarded connection, |
184 | * so if an error occurred on the socket, we just ignore it |
185 | * and treat it like a proper close. |
186 | */ |
187 | sshfwd_close(pr->c); |
188 | x11_close(pr->s); |
189 | return 1; |
190 | } |
191 | |
32874aea |
192 | static int x11_receive(Plug plug, int urgent, char *data, int len) |
193 | { |
7e78000d |
194 | struct X11Private *pr = (struct X11Private *) plug; |
9c964e85 |
195 | |
5471d09a |
196 | if (sshfwd_write(pr->c, data, len) > 0) { |
197 | pr->throttled = 1; |
198 | sk_set_frozen(pr->s, 1); |
199 | } |
200 | |
9c964e85 |
201 | return 1; |
202 | } |
203 | |
5471d09a |
204 | static void x11_sent(Plug plug, int bufsize) |
205 | { |
206 | struct X11Private *pr = (struct X11Private *) plug; |
207 | |
208 | sshfwd_unthrottle(pr->c, bufsize); |
209 | } |
210 | |
9c964e85 |
211 | /* |
421d6835 |
212 | * When setting up X forwarding, we should send the screen number |
213 | * from the specified local display. This function extracts it from |
214 | * the display string. |
215 | */ |
216 | int x11_get_screen_number(char *display) |
217 | { |
218 | int n; |
219 | |
220 | n = strcspn(display, ":"); |
221 | if (!display[n]) |
222 | return 0; |
223 | n = strcspn(display, "."); |
224 | if (!display[n]) |
225 | return 0; |
226 | return atoi(display + n + 1); |
227 | } |
228 | |
229 | /* |
9c964e85 |
230 | * Called to set up the raw connection. |
231 | * |
232 | * Returns an error message, or NULL on success. |
233 | * also, fills the SocketsStructure |
9c964e85 |
234 | */ |
b3ebaa28 |
235 | char *x11_init(Socket * s, char *display, void *c, void *auth, |
e8fa8f62 |
236 | const char *peeraddr, int peerport, const Config *cfg) |
32874aea |
237 | { |
302121de |
238 | static const struct plug_function_table fn_table = { |
7e78000d |
239 | x11_closing, |
5471d09a |
240 | x11_receive, |
241 | x11_sent, |
242 | NULL |
7e78000d |
243 | }; |
244 | |
9c964e85 |
245 | SockAddr addr; |
246 | int port; |
0c6c9a70 |
247 | char *err, *dummy_realhost; |
9c964e85 |
248 | char host[128]; |
249 | int n, displaynum; |
250 | struct X11Private *pr; |
251 | |
252 | /* |
253 | * Split up display name into host and display-number parts. |
254 | */ |
255 | n = strcspn(display, ":"); |
256 | if (display[n]) |
32874aea |
257 | displaynum = atoi(display + n + 1); |
9c964e85 |
258 | else |
32874aea |
259 | displaynum = 0; /* sensible default */ |
260 | if (n > sizeof(host) - 1) |
261 | n = sizeof(host) - 1; |
ecc0ba5b |
262 | if (n > 0) { |
263 | strncpy(host, display, n); |
264 | host[n] = '\0'; |
265 | } else { |
266 | /* |
267 | * Local display numbers, particularly on Unix, often omit |
268 | * the display part completely. |
269 | */ |
270 | strcpy(host, "localhost"); |
271 | } |
9c964e85 |
272 | |
9f2f8ea0 |
273 | port = 6000 + displaynum; |
274 | |
9c964e85 |
275 | /* |
276 | * Try to find host. |
277 | */ |
e8fa8f62 |
278 | addr = name_lookup(host, port, &dummy_realhost, cfg); |
6518ea7b |
279 | if ((err = sk_addr_error(addr)) != NULL) |
9c964e85 |
280 | return err; |
281 | |
9c964e85 |
282 | /* |
283 | * Open socket. |
284 | */ |
3d88e64d |
285 | pr = snew(struct X11Private); |
7e78000d |
286 | pr->fn = &fn_table; |
9c964e85 |
287 | pr->auth_protocol = NULL; |
302121de |
288 | pr->auth = (struct X11Auth *)auth; |
9c964e85 |
289 | pr->verified = 0; |
290 | pr->data_read = 0; |
5471d09a |
291 | pr->throttled = pr->throttle_override = 0; |
9c964e85 |
292 | pr->c = c; |
293 | |
e8fa8f62 |
294 | pr->s = *s = new_connection(addr, dummy_realhost, port, |
295 | 0, 1, 0, (Plug) pr, cfg); |
6518ea7b |
296 | if ((err = sk_socket_error(*s)) != NULL) { |
32874aea |
297 | sfree(pr); |
7e78000d |
298 | return err; |
299 | } |
300 | |
b3ebaa28 |
301 | /* |
302 | * See if we can make sense of the peer address we were given. |
303 | */ |
304 | { |
305 | int i[4]; |
306 | if (peeraddr && |
307 | 4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) { |
308 | pr->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3]; |
309 | pr->peer_port = peerport; |
310 | } else { |
311 | pr->peer_ip = 0; |
312 | pr->peer_port = -1; |
313 | } |
314 | } |
315 | |
9c964e85 |
316 | sk_set_private_ptr(*s, pr); |
317 | sk_addr_free(addr); |
318 | return NULL; |
319 | } |
320 | |
32874aea |
321 | void x11_close(Socket s) |
322 | { |
323 | struct X11Private *pr; |
d74d141c |
324 | if (!s) |
325 | return; |
326 | pr = (struct X11Private *) sk_get_private_ptr(s); |
9c964e85 |
327 | if (pr->auth_protocol) { |
32874aea |
328 | sfree(pr->auth_protocol); |
329 | sfree(pr->auth_data); |
9c964e85 |
330 | } |
331 | |
332 | sfree(pr); |
333 | |
334 | sk_close(s); |
335 | } |
336 | |
5471d09a |
337 | void x11_unthrottle(Socket s) |
338 | { |
339 | struct X11Private *pr; |
340 | if (!s) |
341 | return; |
342 | pr = (struct X11Private *) sk_get_private_ptr(s); |
343 | |
344 | pr->throttled = 0; |
345 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
346 | } |
347 | |
348 | void x11_override_throttle(Socket s, int enable) |
349 | { |
350 | struct X11Private *pr; |
351 | if (!s) |
352 | return; |
353 | pr = (struct X11Private *) sk_get_private_ptr(s); |
354 | |
355 | pr->throttle_override = enable; |
356 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
357 | } |
358 | |
9c964e85 |
359 | /* |
360 | * Called to send data down the raw connection. |
361 | */ |
5471d09a |
362 | int x11_send(Socket s, char *data, int len) |
32874aea |
363 | { |
364 | struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s); |
9c964e85 |
365 | |
366 | if (s == NULL) |
5471d09a |
367 | return 0; |
9c964e85 |
368 | |
369 | /* |
370 | * Read the first packet. |
371 | */ |
372 | while (len > 0 && pr->data_read < 12) |
32874aea |
373 | pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++); |
9c964e85 |
374 | if (pr->data_read < 12) |
5471d09a |
375 | return 0; |
9c964e85 |
376 | |
377 | /* |
378 | * If we have not allocated the auth_protocol and auth_data |
379 | * strings, do so now. |
380 | */ |
381 | if (!pr->auth_protocol) { |
32874aea |
382 | pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6); |
383 | pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8); |
384 | pr->auth_psize = (pr->auth_plen + 3) & ~3; |
385 | pr->auth_dsize = (pr->auth_dlen + 3) & ~3; |
386 | /* Leave room for a terminating zero, to make our lives easier. */ |
3d88e64d |
387 | pr->auth_protocol = snewn(pr->auth_psize + 1, char); |
92d60585 |
388 | pr->auth_data = snewn(pr->auth_dsize, unsigned char); |
9c964e85 |
389 | } |
390 | |
391 | /* |
392 | * Read the auth_protocol and auth_data strings. |
393 | */ |
394 | while (len > 0 && pr->data_read < 12 + pr->auth_psize) |
32874aea |
395 | pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++); |
9c964e85 |
396 | while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize) |
32874aea |
397 | pr->auth_data[pr->data_read++ - 12 - |
398 | pr->auth_psize] = (unsigned char) (len--, *data++); |
9c964e85 |
399 | if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize) |
5471d09a |
400 | return 0; |
9c964e85 |
401 | |
402 | /* |
403 | * If we haven't verified the authentication, do so now. |
404 | */ |
405 | if (!pr->verified) { |
b3ebaa28 |
406 | char *err; |
32874aea |
407 | |
408 | pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */ |
b3ebaa28 |
409 | err = x11_verify(pr->peer_ip, pr->peer_port, |
410 | pr->auth, pr->auth_protocol, |
302121de |
411 | pr->auth_data, pr->auth_dlen); |
32874aea |
412 | |
413 | /* |
414 | * If authentication failed, construct and send an error |
415 | * packet, then terminate the connection. |
416 | */ |
b3ebaa28 |
417 | if (err) { |
418 | char *message; |
419 | int msglen, msgsize; |
420 | unsigned char *reply; |
421 | |
422 | message = dupprintf("PuTTY X11 proxy: %s", err); |
423 | msglen = strlen(message); |
3d88e64d |
424 | reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */ |
b3ebaa28 |
425 | msgsize = (msglen + 3) & ~3; |
32874aea |
426 | reply[0] = 0; /* failure */ |
427 | reply[1] = msglen; /* length of reason string */ |
428 | memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */ |
75105663 |
429 | PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */ |
32874aea |
430 | memset(reply + 8, 0, msgsize); |
431 | memcpy(reply + 8, message, msglen); |
f28e6b7c |
432 | sshfwd_write(pr->c, (char *)reply, 8 + msgsize); |
32874aea |
433 | sshfwd_close(pr->c); |
434 | x11_close(s); |
b3ebaa28 |
435 | sfree(reply); |
436 | sfree(message); |
5471d09a |
437 | return 0; |
32874aea |
438 | } |
439 | |
440 | /* |
441 | * Now we know we're going to accept the connection. Strip |
e0e7dff8 |
442 | * the fake auth data, and optionally put real auth data in |
443 | * instead. |
32874aea |
444 | */ |
e0e7dff8 |
445 | { |
446 | char realauthdata[64]; |
447 | int realauthlen = 0; |
448 | int authstrlen = strlen(x11_authnames[pr->auth->realproto]); |
2f92b717 |
449 | unsigned long ip; |
450 | int port; |
e0e7dff8 |
451 | static const char zeroes[4] = { 0,0,0,0 }; |
452 | |
453 | if (pr->auth->realproto == X11_MIT) { |
454 | assert(pr->auth->reallen <= lenof(realauthdata)); |
455 | realauthlen = pr->auth->reallen; |
456 | memcpy(realauthdata, pr->auth->realdata, realauthlen); |
2f92b717 |
457 | } else if (pr->auth->realproto == X11_XDM && |
458 | pr->auth->reallen == 16 && |
459 | sk_getxdmdata(s, &ip, &port)) { |
460 | time_t t; |
461 | realauthlen = 24; |
462 | memset(realauthdata, 0, 24); |
463 | memcpy(realauthdata, pr->auth->realdata, 8); |
464 | PUT_32BIT_MSB_FIRST(realauthdata+8, ip); |
465 | PUT_16BIT_MSB_FIRST(realauthdata+12, port); |
466 | t = time(NULL); |
467 | PUT_32BIT_MSB_FIRST(realauthdata+14, t); |
6e7f5aee |
468 | des_encrypt_xdmauth(pr->auth->realdata+9, |
469 | (unsigned char *)realauthdata, 24); |
2f92b717 |
470 | } |
e0e7dff8 |
471 | /* implement other auth methods here if required */ |
472 | |
473 | PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen); |
474 | PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen); |
475 | |
476 | sk_write(s, (char *)pr->firstpkt, 12); |
477 | |
478 | if (authstrlen) { |
479 | sk_write(s, x11_authnames[pr->auth->realproto], authstrlen); |
480 | sk_write(s, zeroes, 3 & (-authstrlen)); |
481 | } |
482 | if (realauthlen) { |
483 | sk_write(s, realauthdata, realauthlen); |
484 | sk_write(s, zeroes, 3 & (-realauthlen)); |
485 | } |
486 | } |
32874aea |
487 | pr->verified = 1; |
9c964e85 |
488 | } |
489 | |
490 | /* |
491 | * After initialisation, just copy data simply. |
492 | */ |
493 | |
5471d09a |
494 | return sk_write(s, data, len); |
9c964e85 |
495 | } |