9c964e85 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | |
4 | #include "putty.h" |
5 | #include "ssh.h" |
6 | |
9c964e85 |
7 | #define GET_32BIT_LSB_FIRST(cp) \ |
8 | (((unsigned long)(unsigned char)(cp)[0]) | \ |
9 | ((unsigned long)(unsigned char)(cp)[1] << 8) | \ |
10 | ((unsigned long)(unsigned char)(cp)[2] << 16) | \ |
11 | ((unsigned long)(unsigned char)(cp)[3] << 24)) |
12 | |
13 | #define PUT_32BIT_LSB_FIRST(cp, value) ( \ |
14 | (cp)[0] = (value), \ |
15 | (cp)[1] = (value) >> 8, \ |
16 | (cp)[2] = (value) >> 16, \ |
17 | (cp)[3] = (value) >> 24 ) |
18 | |
19 | #define GET_16BIT_LSB_FIRST(cp) \ |
20 | (((unsigned long)(unsigned char)(cp)[0]) | \ |
21 | ((unsigned long)(unsigned char)(cp)[1] << 8)) |
22 | |
23 | #define PUT_16BIT_LSB_FIRST(cp, value) ( \ |
24 | (cp)[0] = (value), \ |
25 | (cp)[1] = (value) >> 8 ) |
26 | |
27 | #define GET_32BIT_MSB_FIRST(cp) \ |
28 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
29 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
30 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
31 | ((unsigned long)(unsigned char)(cp)[3])) |
32 | |
33 | #define PUT_32BIT_MSB_FIRST(cp, value) ( \ |
34 | (cp)[0] = (value) >> 24, \ |
35 | (cp)[1] = (value) >> 16, \ |
36 | (cp)[2] = (value) >> 8, \ |
37 | (cp)[3] = (value) ) |
38 | |
39 | #define GET_16BIT_MSB_FIRST(cp) \ |
40 | (((unsigned long)(unsigned char)(cp)[0] << 8) | \ |
41 | ((unsigned long)(unsigned char)(cp)[1])) |
42 | |
43 | #define PUT_16BIT_MSB_FIRST(cp, value) ( \ |
44 | (cp)[0] = (value) >> 8, \ |
45 | (cp)[1] = (value) ) |
46 | |
47 | #define GET_16BIT(endian, cp) \ |
48 | (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp)) |
49 | |
50 | #define PUT_16BIT(endian, cp, val) \ |
51 | (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val)) |
52 | |
302121de |
53 | struct X11Auth { |
54 | unsigned char data[64]; |
55 | int len; |
56 | }; |
57 | |
9c964e85 |
58 | struct X11Private { |
302121de |
59 | const struct plug_function_table *fn; |
7e78000d |
60 | /* the above variable absolutely *must* be the first in this structure */ |
32874aea |
61 | unsigned char firstpkt[12]; /* first X data packet */ |
302121de |
62 | struct X11Auth *auth; |
9c964e85 |
63 | char *auth_protocol; |
64 | unsigned char *auth_data; |
65 | int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize; |
66 | int verified; |
5471d09a |
67 | int throttled, throttle_override; |
32874aea |
68 | void *c; /* data used by ssh.c */ |
7e78000d |
69 | Socket s; |
9c964e85 |
70 | }; |
71 | |
302121de |
72 | void *x11_invent_auth(char *proto, int protomaxlen, |
32874aea |
73 | char *data, int datamaxlen) |
74 | { |
302121de |
75 | struct X11Auth *auth = smalloc(sizeof(struct X11Auth)); |
9c964e85 |
76 | char ourdata[64]; |
77 | int i; |
78 | |
79 | /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */ |
302121de |
80 | auth->len = 16; |
9c964e85 |
81 | for (i = 0; i < 16; i++) |
302121de |
82 | auth->data[i] = random_byte(); |
9c964e85 |
83 | |
84 | /* Now format for the recipient. */ |
85 | strncpy(proto, "MIT-MAGIC-COOKIE-1", protomaxlen); |
86 | ourdata[0] = '\0'; |
302121de |
87 | for (i = 0; i < auth->len; i++) |
88 | sprintf(ourdata + strlen(ourdata), "%02x", auth->data[i]); |
9c964e85 |
89 | strncpy(data, ourdata, datamaxlen); |
302121de |
90 | |
91 | return auth; |
9c964e85 |
92 | } |
93 | |
302121de |
94 | static int x11_verify(struct X11Auth *auth, |
95 | char *proto, unsigned char *data, int dlen) |
32874aea |
96 | { |
9c964e85 |
97 | if (strcmp(proto, "MIT-MAGIC-COOKIE-1") != 0) |
32874aea |
98 | return 0; /* wrong protocol attempted */ |
302121de |
99 | if (dlen != auth->len) |
32874aea |
100 | return 0; /* cookie was wrong length */ |
302121de |
101 | if (memcmp(auth->data, data, dlen) != 0) |
32874aea |
102 | return 0; /* cookie was wrong cookie! */ |
9c964e85 |
103 | return 1; |
104 | } |
105 | |
32874aea |
106 | static int x11_closing(Plug plug, char *error_msg, int error_code, |
107 | int calling_back) |
108 | { |
7e78000d |
109 | struct X11Private *pr = (struct X11Private *) plug; |
110 | |
111 | /* |
112 | * We have no way to communicate down the forwarded connection, |
113 | * so if an error occurred on the socket, we just ignore it |
114 | * and treat it like a proper close. |
115 | */ |
116 | sshfwd_close(pr->c); |
117 | x11_close(pr->s); |
118 | return 1; |
119 | } |
120 | |
32874aea |
121 | static int x11_receive(Plug plug, int urgent, char *data, int len) |
122 | { |
7e78000d |
123 | struct X11Private *pr = (struct X11Private *) plug; |
9c964e85 |
124 | |
5471d09a |
125 | if (sshfwd_write(pr->c, data, len) > 0) { |
126 | pr->throttled = 1; |
127 | sk_set_frozen(pr->s, 1); |
128 | } |
129 | |
9c964e85 |
130 | return 1; |
131 | } |
132 | |
5471d09a |
133 | static void x11_sent(Plug plug, int bufsize) |
134 | { |
135 | struct X11Private *pr = (struct X11Private *) plug; |
136 | |
137 | sshfwd_unthrottle(pr->c, bufsize); |
138 | } |
139 | |
9c964e85 |
140 | /* |
421d6835 |
141 | * When setting up X forwarding, we should send the screen number |
142 | * from the specified local display. This function extracts it from |
143 | * the display string. |
144 | */ |
145 | int x11_get_screen_number(char *display) |
146 | { |
147 | int n; |
148 | |
149 | n = strcspn(display, ":"); |
150 | if (!display[n]) |
151 | return 0; |
152 | n = strcspn(display, "."); |
153 | if (!display[n]) |
154 | return 0; |
155 | return atoi(display + n + 1); |
156 | } |
157 | |
158 | /* |
9c964e85 |
159 | * Called to set up the raw connection. |
160 | * |
161 | * Returns an error message, or NULL on success. |
162 | * also, fills the SocketsStructure |
9c964e85 |
163 | */ |
302121de |
164 | char *x11_init(Socket * s, char *display, void *c, void *auth) |
32874aea |
165 | { |
302121de |
166 | static const struct plug_function_table fn_table = { |
7e78000d |
167 | x11_closing, |
5471d09a |
168 | x11_receive, |
169 | x11_sent, |
170 | NULL |
7e78000d |
171 | }; |
172 | |
9c964e85 |
173 | SockAddr addr; |
174 | int port; |
0c6c9a70 |
175 | char *err, *dummy_realhost; |
9c964e85 |
176 | char host[128]; |
177 | int n, displaynum; |
178 | struct X11Private *pr; |
179 | |
180 | /* |
181 | * Split up display name into host and display-number parts. |
182 | */ |
183 | n = strcspn(display, ":"); |
184 | if (display[n]) |
32874aea |
185 | displaynum = atoi(display + n + 1); |
9c964e85 |
186 | else |
32874aea |
187 | displaynum = 0; /* sensible default */ |
188 | if (n > sizeof(host) - 1) |
189 | n = sizeof(host) - 1; |
ecc0ba5b |
190 | if (n > 0) { |
191 | strncpy(host, display, n); |
192 | host[n] = '\0'; |
193 | } else { |
194 | /* |
195 | * Local display numbers, particularly on Unix, often omit |
196 | * the display part completely. |
197 | */ |
198 | strcpy(host, "localhost"); |
199 | } |
9c964e85 |
200 | |
9f2f8ea0 |
201 | port = 6000 + displaynum; |
202 | |
9c964e85 |
203 | /* |
204 | * Try to find host. |
205 | */ |
b7a189f3 |
206 | addr = name_lookup(host, port, &dummy_realhost); |
6518ea7b |
207 | if ((err = sk_addr_error(addr)) != NULL) |
9c964e85 |
208 | return err; |
209 | |
9c964e85 |
210 | /* |
211 | * Open socket. |
212 | */ |
32874aea |
213 | pr = (struct X11Private *) smalloc(sizeof(struct X11Private)); |
7e78000d |
214 | pr->fn = &fn_table; |
9c964e85 |
215 | pr->auth_protocol = NULL; |
302121de |
216 | pr->auth = (struct X11Auth *)auth; |
9c964e85 |
217 | pr->verified = 0; |
218 | pr->data_read = 0; |
5471d09a |
219 | pr->throttled = pr->throttle_override = 0; |
9c964e85 |
220 | pr->c = c; |
221 | |
8eebd221 |
222 | pr->s = *s = new_connection(addr, dummy_realhost, port, 0, 1, 0, (Plug) pr); |
6518ea7b |
223 | if ((err = sk_socket_error(*s)) != NULL) { |
32874aea |
224 | sfree(pr); |
7e78000d |
225 | return err; |
226 | } |
227 | |
9c964e85 |
228 | sk_set_private_ptr(*s, pr); |
229 | sk_addr_free(addr); |
230 | return NULL; |
231 | } |
232 | |
32874aea |
233 | void x11_close(Socket s) |
234 | { |
235 | struct X11Private *pr; |
d74d141c |
236 | if (!s) |
237 | return; |
238 | pr = (struct X11Private *) sk_get_private_ptr(s); |
9c964e85 |
239 | if (pr->auth_protocol) { |
32874aea |
240 | sfree(pr->auth_protocol); |
241 | sfree(pr->auth_data); |
9c964e85 |
242 | } |
243 | |
244 | sfree(pr); |
245 | |
246 | sk_close(s); |
247 | } |
248 | |
5471d09a |
249 | void x11_unthrottle(Socket s) |
250 | { |
251 | struct X11Private *pr; |
252 | if (!s) |
253 | return; |
254 | pr = (struct X11Private *) sk_get_private_ptr(s); |
255 | |
256 | pr->throttled = 0; |
257 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
258 | } |
259 | |
260 | void x11_override_throttle(Socket s, int enable) |
261 | { |
262 | struct X11Private *pr; |
263 | if (!s) |
264 | return; |
265 | pr = (struct X11Private *) sk_get_private_ptr(s); |
266 | |
267 | pr->throttle_override = enable; |
268 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
269 | } |
270 | |
9c964e85 |
271 | /* |
272 | * Called to send data down the raw connection. |
273 | */ |
5471d09a |
274 | int x11_send(Socket s, char *data, int len) |
32874aea |
275 | { |
276 | struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s); |
9c964e85 |
277 | |
278 | if (s == NULL) |
5471d09a |
279 | return 0; |
9c964e85 |
280 | |
281 | /* |
282 | * Read the first packet. |
283 | */ |
284 | while (len > 0 && pr->data_read < 12) |
32874aea |
285 | pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++); |
9c964e85 |
286 | if (pr->data_read < 12) |
5471d09a |
287 | return 0; |
9c964e85 |
288 | |
289 | /* |
290 | * If we have not allocated the auth_protocol and auth_data |
291 | * strings, do so now. |
292 | */ |
293 | if (!pr->auth_protocol) { |
32874aea |
294 | pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6); |
295 | pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8); |
296 | pr->auth_psize = (pr->auth_plen + 3) & ~3; |
297 | pr->auth_dsize = (pr->auth_dlen + 3) & ~3; |
298 | /* Leave room for a terminating zero, to make our lives easier. */ |
299 | pr->auth_protocol = (char *) smalloc(pr->auth_psize + 1); |
f28e6b7c |
300 | pr->auth_data = (unsigned char *) smalloc(pr->auth_dsize); |
9c964e85 |
301 | } |
302 | |
303 | /* |
304 | * Read the auth_protocol and auth_data strings. |
305 | */ |
306 | while (len > 0 && pr->data_read < 12 + pr->auth_psize) |
32874aea |
307 | pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++); |
9c964e85 |
308 | while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize) |
32874aea |
309 | pr->auth_data[pr->data_read++ - 12 - |
310 | pr->auth_psize] = (unsigned char) (len--, *data++); |
9c964e85 |
311 | if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize) |
5471d09a |
312 | return 0; |
9c964e85 |
313 | |
314 | /* |
315 | * If we haven't verified the authentication, do so now. |
316 | */ |
317 | if (!pr->verified) { |
32874aea |
318 | int ret; |
319 | |
320 | pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */ |
302121de |
321 | ret = x11_verify(pr->auth, pr->auth_protocol, |
322 | pr->auth_data, pr->auth_dlen); |
32874aea |
323 | |
324 | /* |
325 | * If authentication failed, construct and send an error |
326 | * packet, then terminate the connection. |
327 | */ |
328 | if (!ret) { |
329 | char message[] = "Authentication failed at PuTTY X11 proxy"; |
330 | unsigned char reply[8 + sizeof(message) + 4]; |
331 | int msglen = sizeof(message) - 1; /* skip zero byte */ |
332 | int msgsize = (msglen + 3) & ~3; |
333 | reply[0] = 0; /* failure */ |
334 | reply[1] = msglen; /* length of reason string */ |
335 | memcpy(reply + 2, pr->firstpkt + 2, 4); /* major/minor proto vsn */ |
75105663 |
336 | PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */ |
32874aea |
337 | memset(reply + 8, 0, msgsize); |
338 | memcpy(reply + 8, message, msglen); |
f28e6b7c |
339 | sshfwd_write(pr->c, (char *)reply, 8 + msgsize); |
32874aea |
340 | sshfwd_close(pr->c); |
341 | x11_close(s); |
5471d09a |
342 | return 0; |
32874aea |
343 | } |
344 | |
345 | /* |
346 | * Now we know we're going to accept the connection. Strip |
347 | * the auth data. (TODO: if we ever work out how, we should |
348 | * replace some real auth data in here.) |
349 | */ |
350 | PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, 0); /* auth proto */ |
351 | PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, 0); /* auth data */ |
f28e6b7c |
352 | sk_write(s, (char *)pr->firstpkt, 12); |
32874aea |
353 | pr->verified = 1; |
9c964e85 |
354 | } |
355 | |
356 | /* |
357 | * After initialisation, just copy data simply. |
358 | */ |
359 | |
5471d09a |
360 | return sk_write(s, data, len); |
9c964e85 |
361 | } |