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