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