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