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