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