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))
21 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
23 (cp)[1] = (value) >> 8, \
24 (cp)[2] = (value) >> 16, \
25 (cp)[3] = (value) >> 24 )
27 #define GET_16BIT_LSB_FIRST(cp) \
28 (((unsigned long)(unsigned char)(cp)[0]) | \
29 ((unsigned long)(unsigned char)(cp)[1] << 8))
31 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
33 (cp)[1] = (value) >> 8 )
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]))
41 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
42 (cp)[0] = (value) >> 24, \
43 (cp)[1] = (value) >> 16, \
44 (cp)[2] = (value) >> 8, \
47 #define GET_16BIT_MSB_FIRST(cp) \
48 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
49 ((unsigned long)(unsigned char)(cp)[1]))
51 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
52 (cp)[0] = (value) >> 8, \
55 #define GET_16BIT(endian, cp) \
56 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
58 #define PUT_16BIT(endian, cp, val) \
59 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
61 extern void sshfwd_close(void *);
62 extern void sshfwd_write(void *, char *, int);
65 unsigned char firstpkt[12]; /* first X data packet */
67 unsigned char *auth_data;
68 int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
70 void *c; /* data used by ssh.c */
73 void x11_close (Socket s);
75 static unsigned char x11_authdata[64];
76 static int x11_authdatalen;
78 void x11_invent_auth(char *proto, int protomaxlen,
79 char *data, int datamaxlen) {
83 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
85 for (i = 0; i < 16; i++)
86 x11_authdata[i] = random_byte();
88 /* Now format for the recipient. */
89 strncpy(proto, "MIT-MAGIC-COOKIE-1", protomaxlen);
91 for (i = 0; i < x11_authdatalen; i++)
92 sprintf(ourdata+strlen(ourdata), "%02x", x11_authdata[i]);
93 strncpy(data, ourdata, datamaxlen);
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! */
106 static int x11_receive (Socket s, int urgent, char *data, int len) {
107 struct X11Private *pr = (struct X11Private *)sk_get_private_ptr(s);
110 /* Connection has closed. */
115 sshfwd_write(pr->c, data, len);
120 * Called to set up the raw connection.
122 * Returns an error message, or NULL on success.
123 * also, fills the SocketsStructure
125 * Also places the canonical host name into `realhost'.
127 char *x11_init (Socket *s, char *display, void *c, char **realhost) {
133 struct X11Private *pr;
136 * Split up display name into host and display-number parts.
138 n = strcspn(display, ":");
140 displaynum = atoi(display+n+1);
142 displaynum = 0; /* sensible default */
143 if (n > sizeof(host)-1)
145 strncpy(host, display, n);
151 addr = sk_namelookup(host, realhost);
152 if ( (err = sk_addr_error(addr)) )
155 port = 6000 + displaynum;
160 *s = sk_new(addr, port, 0, x11_receive);
161 if ( (err = sk_socket_error(*s)) )
164 pr = (struct X11Private *)smalloc(sizeof(struct X11Private));
165 pr->auth_protocol = NULL;
170 sk_set_private_ptr(*s, pr);
175 void x11_close (Socket s) {
176 struct X11Private *pr = (struct X11Private *)sk_get_private_ptr(s);
178 if (pr->auth_protocol) {
179 sfree(pr->auth_protocol);
180 sfree(pr->auth_data);
189 * Called to send data down the raw connection.
191 void x11_send (Socket s, char *data, int len) {
192 struct X11Private *pr = (struct X11Private *)sk_get_private_ptr(s);
198 * Read the first packet.
200 while (len > 0 && pr->data_read < 12)
201 pr->firstpkt[pr->data_read++] = (unsigned char)(len--, *data++);
202 if (pr->data_read < 12)
206 * If we have not allocated the auth_protocol and auth_data
207 * strings, do so now.
209 if (!pr->auth_protocol) {
210 pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt+6);
211 pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt+8);
212 pr->auth_psize = (pr->auth_plen + 3) &~ 3;
213 pr->auth_dsize = (pr->auth_dlen + 3) &~ 3;
214 /* Leave room for a terminating zero, to make our lives easier. */
215 pr->auth_protocol = (char *)smalloc(pr->auth_psize+1);
216 pr->auth_data = (char *)smalloc(pr->auth_dsize);
220 * Read the auth_protocol and auth_data strings.
222 while (len > 0 && pr->data_read < 12 + pr->auth_psize)
223 pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);
224 while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
225 pr->auth_data[pr->data_read++ - 12 -
226 pr->auth_psize] = (unsigned char)(len--, *data++);
227 if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)
231 * If we haven't verified the authentication, do so now.
236 pr->auth_protocol[pr->auth_plen] = '\0'; /* ASCIZ */
237 ret = x11_verify(pr->auth_protocol, pr->auth_data, pr->auth_dlen);
240 * If authentication failed, construct and send an error
241 * packet, then terminate the connection.
244 char message[] = "Authentication failed at PuTTY X11 proxy";
245 unsigned char reply[8 + sizeof(message) + 4];
246 int msglen = sizeof(message)-1; /* skip zero byte */
247 int msgsize = (msglen+3) &~ 3;
248 reply[0] = 0; /* failure */
249 reply[1] = msglen; /* length of reason string */
250 memcpy(reply+2, pr->firstpkt+2, 4); /* major/minor proto vsn */
251 PUT_16BIT(pr->firstpkt[0], reply+6, msglen >> 2); /* data len */
252 memset(reply+8, 0, msgsize);
253 memcpy(reply+8, message, msglen);
254 sshfwd_write(pr->c, reply, 8+msgsize);
261 * Now we know we're going to accept the connection. Strip
262 * the auth data. (TODO: if we ever work out how, we should
263 * replace some real auth data in here.)
265 PUT_16BIT(pr->firstpkt[0], pr->firstpkt+6, 0); /* auth proto */
266 PUT_16BIT(pr->firstpkt[0], pr->firstpkt+8, 0); /* auth data */
267 sk_write(s, pr->firstpkt, 12);
272 * After initialisation, just copy data simply.
275 sk_write(s, data, len);