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))
13 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
15 (cp)[1] = (value) >> 8, \
16 (cp)[2] = (value) >> 16, \
17 (cp)[3] = (value) >> 24 )
19 #define GET_16BIT_LSB_FIRST(cp) \
20 (((unsigned long)(unsigned char)(cp)[0]) | \
21 ((unsigned long)(unsigned char)(cp)[1] << 8))
23 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
25 (cp)[1] = (value) >> 8 )
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]))
33 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
34 (cp)[0] = (value) >> 24, \
35 (cp)[1] = (value) >> 16, \
36 (cp)[2] = (value) >> 8, \
39 #define GET_16BIT_MSB_FIRST(cp) \
40 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
41 ((unsigned long)(unsigned char)(cp)[1]))
43 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
44 (cp)[0] = (value) >> 8, \
47 #define GET_16BIT(endian, cp) \
48 (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))
50 #define PUT_16BIT(endian, cp, val) \
51 (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))
54 unsigned char data
[64];
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 */
64 unsigned char *auth_data
;
65 int data_read
, auth_plen
, auth_psize
, auth_dlen
, auth_dsize
;
67 int throttled
, throttle_override
;
68 void *c
; /* data used by ssh.c */
72 void x11_close(Socket s
);
74 void *x11_invent_auth(char *proto
, int protomaxlen
,
75 char *data
, int datamaxlen
)
77 struct X11Auth
*auth
= smalloc(sizeof(struct X11Auth
));
81 /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */
83 for (i
= 0; i
< 16; i
++)
84 auth
->data
[i
] = random_byte();
86 /* Now format for the recipient. */
87 strncpy(proto
, "MIT-MAGIC-COOKIE-1", protomaxlen
);
89 for (i
= 0; i
< auth
->len
; i
++)
90 sprintf(ourdata
+ strlen(ourdata
), "%02x", auth
->data
[i
]);
91 strncpy(data
, ourdata
, datamaxlen
);
96 static int x11_verify(struct X11Auth
*auth
,
97 char *proto
, unsigned char *data
, int dlen
)
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! */
108 static int x11_closing(Plug plug
, char *error_msg
, int error_code
,
111 struct X11Private
*pr
= (struct X11Private
*) plug
;
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.
123 static int x11_receive(Plug plug
, int urgent
, char *data
, int len
)
125 struct X11Private
*pr
= (struct X11Private
*) plug
;
127 if (sshfwd_write(pr
->c
, data
, len
) > 0) {
129 sk_set_frozen(pr
->s
, 1);
135 static void x11_sent(Plug plug
, int bufsize
)
137 struct X11Private
*pr
= (struct X11Private
*) plug
;
139 sshfwd_unthrottle(pr
->c
, bufsize
);
143 * Called to set up the raw connection.
145 * Returns an error message, or NULL on success.
146 * also, fills the SocketsStructure
148 char *x11_init(Socket
* s
, char *display
, void *c
, void *auth
)
150 static const struct plug_function_table fn_table
= {
159 char *err
, *dummy_realhost
;
162 struct X11Private
*pr
;
165 * Split up display name into host and display-number parts.
167 n
= strcspn(display
, ":");
169 displaynum
= atoi(display
+ n
+ 1);
171 displaynum
= 0; /* sensible default */
172 if (n
> sizeof(host
) - 1)
173 n
= sizeof(host
) - 1;
174 strncpy(host
, display
, n
);
180 addr
= name_lookup(host
, port
, &dummy_realhost
);
181 if ((err
= sk_addr_error(addr
)))
184 port
= 6000 + displaynum
;
189 pr
= (struct X11Private
*) smalloc(sizeof(struct X11Private
));
191 pr
->auth_protocol
= NULL
;
192 pr
->auth
= (struct X11Auth
*)auth
;
195 pr
->throttled
= pr
->throttle_override
= 0;
198 pr
->s
= *s
= new_connection(addr
, dummy_realhost
, port
, 0, 1, 0, (Plug
) pr
);
199 if ((err
= sk_socket_error(*s
))) {
204 sk_set_private_ptr(*s
, pr
);
209 void x11_close(Socket s
)
211 struct X11Private
*pr
;
214 pr
= (struct X11Private
*) sk_get_private_ptr(s
);
215 if (pr
->auth_protocol
) {
216 sfree(pr
->auth_protocol
);
217 sfree(pr
->auth_data
);
225 void x11_unthrottle(Socket s
)
227 struct X11Private
*pr
;
230 pr
= (struct X11Private
*) sk_get_private_ptr(s
);
233 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
236 void x11_override_throttle(Socket s
, int enable
)
238 struct X11Private
*pr
;
241 pr
= (struct X11Private
*) sk_get_private_ptr(s
);
243 pr
->throttle_override
= enable
;
244 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
248 * Called to send data down the raw connection.
250 int x11_send(Socket s
, char *data
, int len
)
252 struct X11Private
*pr
= (struct X11Private
*) sk_get_private_ptr(s
);
258 * Read the first packet.
260 while (len
> 0 && pr
->data_read
< 12)
261 pr
->firstpkt
[pr
->data_read
++] = (unsigned char) (len
--, *data
++);
262 if (pr
->data_read
< 12)
266 * If we have not allocated the auth_protocol and auth_data
267 * strings, do so now.
269 if (!pr
->auth_protocol
) {
270 pr
->auth_plen
= GET_16BIT(pr
->firstpkt
[0], pr
->firstpkt
+ 6);
271 pr
->auth_dlen
= GET_16BIT(pr
->firstpkt
[0], pr
->firstpkt
+ 8);
272 pr
->auth_psize
= (pr
->auth_plen
+ 3) & ~3;
273 pr
->auth_dsize
= (pr
->auth_dlen
+ 3) & ~3;
274 /* Leave room for a terminating zero, to make our lives easier. */
275 pr
->auth_protocol
= (char *) smalloc(pr
->auth_psize
+ 1);
276 pr
->auth_data
= (char *) smalloc(pr
->auth_dsize
);
280 * Read the auth_protocol and auth_data strings.
282 while (len
> 0 && pr
->data_read
< 12 + pr
->auth_psize
)
283 pr
->auth_protocol
[pr
->data_read
++ - 12] = (len
--, *data
++);
284 while (len
> 0 && pr
->data_read
< 12 + pr
->auth_psize
+ pr
->auth_dsize
)
285 pr
->auth_data
[pr
->data_read
++ - 12 -
286 pr
->auth_psize
] = (unsigned char) (len
--, *data
++);
287 if (pr
->data_read
< 12 + pr
->auth_psize
+ pr
->auth_dsize
)
291 * If we haven't verified the authentication, do so now.
296 pr
->auth_protocol
[pr
->auth_plen
] = '\0'; /* ASCIZ */
297 ret
= x11_verify(pr
->auth
, pr
->auth_protocol
,
298 pr
->auth_data
, pr
->auth_dlen
);
301 * If authentication failed, construct and send an error
302 * packet, then terminate the connection.
305 char message
[] = "Authentication failed at PuTTY X11 proxy";
306 unsigned char reply
[8 + sizeof(message
) + 4];
307 int msglen
= sizeof(message
) - 1; /* skip zero byte */
308 int msgsize
= (msglen
+ 3) & ~3;
309 reply
[0] = 0; /* failure */
310 reply
[1] = msglen
; /* length of reason string */
311 memcpy(reply
+ 2, pr
->firstpkt
+ 2, 4); /* major/minor proto vsn */
312 PUT_16BIT(pr
->firstpkt
[0], reply
+ 6, msgsize
>> 2);/* data len */
313 memset(reply
+ 8, 0, msgsize
);
314 memcpy(reply
+ 8, message
, msglen
);
315 sshfwd_write(pr
->c
, reply
, 8 + msgsize
);
322 * Now we know we're going to accept the connection. Strip
323 * the auth data. (TODO: if we ever work out how, we should
324 * replace some real auth data in here.)
326 PUT_16BIT(pr
->firstpkt
[0], pr
->firstpkt
+ 6, 0); /* auth proto */
327 PUT_16BIT(pr
->firstpkt
[0], pr
->firstpkt
+ 8, 0); /* auth data */
328 sk_write(s
, pr
->firstpkt
, 12);
333 * After initialisation, just copy data simply.
336 return sk_write(s
, data
, len
);