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