]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - x11fwd.c
proxy.c now no longer refers to `cfg'. Instead, each of the three
[PuTTY.git] / x11fwd.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <time.h>
5
6 #include "putty.h"
7 #include "ssh.h"
8
9 #define GET_32BIT_LSB_FIRST(cp) \
10   (((unsigned long)(unsigned char)(cp)[0]) | \
11   ((unsigned long)(unsigned char)(cp)[1] << 8) | \
12   ((unsigned long)(unsigned char)(cp)[2] << 16) | \
13   ((unsigned long)(unsigned char)(cp)[3] << 24))
14
15 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
16   (cp)[0] = (char)(value), \
17   (cp)[1] = (char)((value) >> 8), \
18   (cp)[2] = (char)((value) >> 16), \
19   (cp)[3] = (char)((value) >> 24) )
20
21 #define GET_16BIT_LSB_FIRST(cp) \
22   (((unsigned long)(unsigned char)(cp)[0]) | \
23   ((unsigned long)(unsigned char)(cp)[1] << 8))
24
25 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
26   (cp)[0] = (char)(value), \
27   (cp)[1] = (char)((value) >> 8) )
28
29 #define GET_32BIT_MSB_FIRST(cp) \
30   (((unsigned long)(unsigned char)(cp)[0] << 24) | \
31   ((unsigned long)(unsigned char)(cp)[1] << 16) | \
32   ((unsigned long)(unsigned char)(cp)[2] << 8) | \
33   ((unsigned long)(unsigned char)(cp)[3]))
34
35 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
36   (cp)[0] = (char)((value) >> 24), \
37   (cp)[1] = (char)((value) >> 16), \
38   (cp)[2] = (char)((value) >> 8), \
39   (cp)[3] = (char)(value) )
40
41 #define GET_16BIT_MSB_FIRST(cp) \
42   (((unsigned long)(unsigned char)(cp)[0] << 8) | \
43   ((unsigned long)(unsigned char)(cp)[1]))
44
45 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
46   (cp)[0] = (char)((value) >> 8), \
47   (cp)[1] = (char)(value) )
48
49 #define GET_16BIT(endian, cp) \
50   (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
51
52 #define PUT_16BIT(endian, cp, val) \
53   (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
54
55 const char *const x11_authnames[] = {
56     "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"
57 };
58
59 struct X11Auth {
60     unsigned char fakedata[64], realdata[64];
61     int fakeproto, realproto;
62     int fakelen, reallen;
63 };
64
65 struct X11Private {
66     const struct plug_function_table *fn;
67     /* the above variable absolutely *must* be the first in this structure */
68     unsigned char firstpkt[12];        /* first X data packet */
69     struct X11Auth *auth;
70     char *auth_protocol;
71     unsigned char *auth_data;
72     int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
73     int verified;
74     int throttled, throttle_override;
75     unsigned long peer_ip;
76     int peer_port;
77     void *c;                           /* data used by ssh.c */
78     Socket s;
79 };
80
81 void *x11_invent_auth(char *proto, int protomaxlen,
82                       char *data, int datamaxlen, int proto_id)
83 {
84     struct X11Auth *auth = smalloc(sizeof(struct X11Auth));
85     char ourdata[64];
86     int i;
87
88     if (proto_id == X11_MIT) {
89         auth->fakeproto = X11_MIT;
90
91         /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
92         auth->fakelen = 16;
93         for (i = 0; i < 16; i++)
94             auth->fakedata[i] = random_byte();
95     } else {
96         assert(proto_id == X11_XDM);
97         auth->fakeproto = X11_XDM;
98
99         /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */
100         auth->fakelen = 16;
101         for (i = 0; i < 16; i++)
102             auth->fakedata[i] = (i == 8 ? 0 : random_byte());
103     }
104
105     /* Now format for the recipient. */
106     strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);
107     ourdata[0] = '\0';
108     for (i = 0; i < auth->fakelen; i++)
109         sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);
110     strncpy(data, ourdata, datamaxlen);
111
112     return auth;
113 }
114
115 /*
116  * Fetch the real auth data for a given display string, and store
117  * it in an X11Auth structure. Returns NULL on success, or an error
118  * string.
119  */
120 void x11_get_real_auth(void *authv, char *display)
121 {
122     struct X11Auth *auth = (struct X11Auth *)authv;
123
124     auth->realproto = X11_NO_AUTH;     /* in case next call does nothing */
125
126     auth->reallen = sizeof(auth->realdata);
127     platform_get_x11_auth(display, &auth->realproto,
128                           auth->realdata, &auth->reallen);
129 }
130
131 static char *x11_verify(unsigned long peer_ip, int peer_port,
132                         struct X11Auth *auth, char *proto,
133                         unsigned char *data, int dlen)
134 {
135     if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0)
136         return "wrong authentication protocol attempted";
137     if (auth->fakeproto == X11_MIT) {
138         if (dlen != auth->fakelen)
139             return "MIT-MAGIC-COOKIE-1 data was wrong length";
140         if (memcmp(auth->fakedata, data, dlen) != 0)
141             return "MIT-MAGIC-COOKIE-1 data did not match";
142     }
143     if (auth->fakeproto == X11_XDM) {
144         unsigned long t;
145         time_t tim;
146         int i;
147
148         if (dlen != 24)
149             return "XDM-AUTHORIZATION-1 data was wrong length";
150         if (peer_port == -1)
151             return "cannot do XDM-AUTHORIZATION-1 without remote address data";
152         des_decrypt_xdmauth(auth->fakedata+9, data, 24);
153         if (memcmp(auth->fakedata, data, 8) != 0)
154             return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */
155         if (GET_32BIT_MSB_FIRST(data+8) != peer_ip)
156             return "XDM-AUTHORIZATION-1 data failed check";   /* IP wrong */
157         if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port)
158             return "XDM-AUTHORIZATION-1 data failed check";   /* port wrong */
159         t = GET_32BIT_MSB_FIRST(data+14);
160         for (i = 18; i < 24; i++)
161             if (data[i] != 0)          /* zero padding wrong */
162                 return "XDM-AUTHORIZATION-1 data failed check";
163         tim = time(NULL);
164         if (abs(t - tim) > 20*60)      /* 20 minute clock skew should be OK */
165             return "XDM-AUTHORIZATION-1 time stamp was too far out";
166     }
167     /* implement other protocols here if ever required */
168     return NULL;
169 }
170
171 static int x11_closing(Plug plug, char *error_msg, int error_code,
172                        int calling_back)
173 {
174     struct X11Private *pr = (struct X11Private *) plug;
175
176     /*
177      * We have no way to communicate down the forwarded connection,
178      * so if an error occurred on the socket, we just ignore it
179      * and treat it like a proper close.
180      */
181     sshfwd_close(pr->c);
182     x11_close(pr->s);
183     return 1;
184 }
185
186 static int x11_receive(Plug plug, int urgent, char *data, int len)
187 {
188     struct X11Private *pr = (struct X11Private *) plug;
189
190     if (sshfwd_write(pr->c, data, len) > 0) {
191         pr->throttled = 1;
192         sk_set_frozen(pr->s, 1);
193     }
194
195     return 1;
196 }
197
198 static void x11_sent(Plug plug, int bufsize)
199 {
200     struct X11Private *pr = (struct X11Private *) plug;
201
202     sshfwd_unthrottle(pr->c, bufsize);
203 }
204
205 /*
206  * When setting up X forwarding, we should send the screen number
207  * from the specified local display. This function extracts it from
208  * the display string.
209  */
210 int x11_get_screen_number(char *display)
211 {
212     int n;
213
214     n = strcspn(display, ":");
215     if (!display[n])
216         return 0;
217     n = strcspn(display, ".");
218     if (!display[n])
219         return 0;
220     return atoi(display + n + 1);
221 }
222
223 /*
224  * Called to set up the raw connection.
225  * 
226  * Returns an error message, or NULL on success.
227  * also, fills the SocketsStructure
228  */
229 char *x11_init(Socket * s, char *display, void *c, void *auth,
230                const char *peeraddr, int peerport, const Config *cfg)
231 {
232     static const struct plug_function_table fn_table = {
233         x11_closing,
234         x11_receive,
235         x11_sent,
236         NULL
237     };
238
239     SockAddr addr;
240     int port;
241     char *err, *dummy_realhost;
242     char host[128];
243     int n, displaynum;
244     struct X11Private *pr;
245
246     /*
247      * Split up display name into host and display-number parts.
248      */
249     n = strcspn(display, ":");
250     if (display[n])
251         displaynum = atoi(display + n + 1);
252     else
253         displaynum = 0;                /* sensible default */
254     if (n > sizeof(host) - 1)
255         n = sizeof(host) - 1;
256     if (n > 0) {
257         strncpy(host, display, n);
258         host[n] = '\0';
259     } else {
260         /*
261          * Local display numbers, particularly on Unix, often omit
262          * the display part completely.
263          */
264         strcpy(host, "localhost");
265     }
266
267     port = 6000 + displaynum;
268
269     /*
270      * Try to find host.
271      */
272     addr = name_lookup(host, port, &dummy_realhost, cfg);
273     if ((err = sk_addr_error(addr)) != NULL)
274         return err;
275
276     /*
277      * Open socket.
278      */
279     pr = (struct X11Private *) smalloc(sizeof(struct X11Private));
280     pr->fn = &fn_table;
281     pr->auth_protocol = NULL;
282     pr->auth = (struct X11Auth *)auth;
283     pr->verified = 0;
284     pr->data_read = 0;
285     pr->throttled = pr->throttle_override = 0;
286     pr->c = c;
287
288     pr->s = *s = new_connection(addr, dummy_realhost, port,
289                                 0, 1, 0, (Plug) pr, cfg);
290     if ((err = sk_socket_error(*s)) != NULL) {
291         sfree(pr);
292         return err;
293     }
294
295     /*
296      * See if we can make sense of the peer address we were given.
297      */
298     {
299         int i[4];
300         if (peeraddr &&
301             4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {
302             pr->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];
303             pr->peer_port = peerport;
304         } else {
305             pr->peer_ip = 0;
306             pr->peer_port = -1;
307         }
308     }
309
310     sk_set_private_ptr(*s, pr);
311     sk_addr_free(addr);
312     return NULL;
313 }
314
315 void x11_close(Socket s)
316 {
317     struct X11Private *pr;
318     if (!s)
319         return;
320     pr = (struct X11Private *) sk_get_private_ptr(s);
321     if (pr->auth_protocol) {
322         sfree(pr->auth_protocol);
323         sfree(pr->auth_data);
324     }
325
326     sfree(pr);
327
328     sk_close(s);
329 }
330
331 void x11_unthrottle(Socket s)
332 {
333     struct X11Private *pr;
334     if (!s)
335         return;
336     pr = (struct X11Private *) sk_get_private_ptr(s);
337
338     pr->throttled = 0;
339     sk_set_frozen(s, pr->throttled || pr->throttle_override);
340 }
341
342 void x11_override_throttle(Socket s, int enable)
343 {
344     struct X11Private *pr;
345     if (!s)
346         return;
347     pr = (struct X11Private *) sk_get_private_ptr(s);
348
349     pr->throttle_override = enable;
350     sk_set_frozen(s, pr->throttled || pr->throttle_override);
351 }
352
353 /*
354  * Called to send data down the raw connection.
355  */
356 int x11_send(Socket s, char *data, int len)
357 {
358     struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s);
359
360     if (s == NULL)
361         return 0;
362
363     /*
364      * Read the first packet.
365      */
366     while (len > 0 && pr->data_read < 12)
367         pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);
368     if (pr->data_read < 12)
369         return 0;
370
371     /*
372      * If we have not allocated the auth_protocol and auth_data
373      * strings, do so now.
374      */
375     if (!pr->auth_protocol) {
376         pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);
377         pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);
378         pr->auth_psize = (pr->auth_plen + 3) & ~3;
379         pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
380         /* Leave room for a terminating zero, to make our lives easier. */
381         pr->auth_protocol = (char *) smalloc(pr->auth_psize + 1);
382         pr->auth_data = (unsigned char *) smalloc(pr->auth_dsize);
383     }
384
385     /*
386      * Read the auth_protocol and auth_data strings.
387      */
388     while (len > 0 && pr->data_read < 12 + pr->auth_psize)
389         pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
390     while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
391         pr->auth_data[pr->data_read++ - 12 -
392                       pr->auth_psize] = (unsigned char) (len--, *data++);
393     if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
394         return 0;
395
396     /*
397      * If we haven't verified the authentication, do so now.
398      */
399     if (!pr->verified) {
400         char *err;
401
402         pr->auth_protocol[pr->auth_plen] = '\0';        /* ASCIZ */
403         err = x11_verify(pr->peer_ip, pr->peer_port,
404                          pr->auth, pr->auth_protocol,
405                          pr->auth_data, pr->auth_dlen);
406
407         /*
408          * If authentication failed, construct and send an error
409          * packet, then terminate the connection.
410          */
411         if (err) {
412             char *message;
413             int msglen, msgsize;
414             unsigned char *reply;
415
416             message = dupprintf("PuTTY X11 proxy: %s", err);
417             msglen = strlen(message);
418             reply = smalloc(8 + msglen+1 + 4);   /* include zero byte */
419             msgsize = (msglen + 3) & ~3;
420             reply[0] = 0;              /* failure */
421             reply[1] = msglen;         /* length of reason string */
422             memcpy(reply + 2, pr->firstpkt + 2, 4);     /* major/minor proto vsn */
423             PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */
424             memset(reply + 8, 0, msgsize);
425             memcpy(reply + 8, message, msglen);
426             sshfwd_write(pr->c, (char *)reply, 8 + msgsize);
427             sshfwd_close(pr->c);
428             x11_close(s);
429             sfree(reply);
430             sfree(message);
431             return 0;
432         }
433
434         /*
435          * Now we know we're going to accept the connection. Strip
436          * the fake auth data, and optionally put real auth data in
437          * instead.
438          */
439         {
440             char realauthdata[64];
441             int realauthlen = 0;
442             int authstrlen = strlen(x11_authnames[pr->auth->realproto]);
443             unsigned long ip;
444             int port;
445             static const char zeroes[4] = { 0,0,0,0 };
446
447             if (pr->auth->realproto == X11_MIT) {
448                 assert(pr->auth->reallen <= lenof(realauthdata));
449                 realauthlen = pr->auth->reallen;
450                 memcpy(realauthdata, pr->auth->realdata, realauthlen);
451             } else if (pr->auth->realproto == X11_XDM &&
452                        pr->auth->reallen == 16 &&
453                        sk_getxdmdata(s, &ip, &port)) {
454                 time_t t;
455                 realauthlen = 24;
456                 memset(realauthdata, 0, 24);
457                 memcpy(realauthdata, pr->auth->realdata, 8);
458                 PUT_32BIT_MSB_FIRST(realauthdata+8, ip);
459                 PUT_16BIT_MSB_FIRST(realauthdata+12, port);
460                 t = time(NULL);
461                 PUT_32BIT_MSB_FIRST(realauthdata+14, t);
462                 des_encrypt_xdmauth(pr->auth->realdata+9,
463                                     (unsigned char *)realauthdata, 24);
464             }
465             /* implement other auth methods here if required */
466
467             PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);
468             PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);
469         
470             sk_write(s, (char *)pr->firstpkt, 12);
471
472             if (authstrlen) {
473                 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);
474                 sk_write(s, zeroes, 3 & (-authstrlen));
475             }
476             if (realauthlen) {
477                 sk_write(s, realauthdata, realauthlen);
478                 sk_write(s, zeroes, 3 & (-realauthlen));
479             }
480         }
481         pr->verified = 1;
482     }
483
484     /*
485      * After initialisation, just copy data simply.
486      */
487
488     return sk_write(s, data, len);
489 }