1 | /* -*- c-basic-offset: 8 -*-
|
---|
2 | rdesktop: A Remote Desktop Protocol client.
|
---|
3 | Protocol services - Multipoint Communications Service
|
---|
4 | Copyright (C) Matthew Chapman 1999-2007
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Sun GPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
24 | * the General Public License version 2 (GPLv2) at this time for any software where
|
---|
25 | * a choice of GPL license versions is made available with the language indicating
|
---|
26 | * that GPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the GPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include "rdesktop.h"
|
---|
31 |
|
---|
32 | uint16 g_mcs_userid;
|
---|
33 | extern VCHANNEL g_channels[];
|
---|
34 | extern unsigned int g_num_channels;
|
---|
35 |
|
---|
36 | /* Parse an ASN.1 BER header */
|
---|
37 | static RD_BOOL
|
---|
38 | ber_parse_header(STREAM s, int tagval, int *length)
|
---|
39 | {
|
---|
40 | int tag, len;
|
---|
41 |
|
---|
42 | if (tagval > 0xff)
|
---|
43 | {
|
---|
44 | in_uint16_be(s, tag);
|
---|
45 | }
|
---|
46 | else
|
---|
47 | {
|
---|
48 | in_uint8(s, tag);
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (tag != tagval)
|
---|
52 | {
|
---|
53 | error("expected tag %d, got %d\n", tagval, tag);
|
---|
54 | return False;
|
---|
55 | }
|
---|
56 |
|
---|
57 | in_uint8(s, len);
|
---|
58 |
|
---|
59 | if (len & 0x80)
|
---|
60 | {
|
---|
61 | len &= ~0x80;
|
---|
62 | *length = 0;
|
---|
63 | while (len--)
|
---|
64 | next_be(s, *length);
|
---|
65 | }
|
---|
66 | else
|
---|
67 | *length = len;
|
---|
68 |
|
---|
69 | return s_check(s);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* Output an ASN.1 BER header */
|
---|
73 | static void
|
---|
74 | ber_out_header(STREAM s, int tagval, int length)
|
---|
75 | {
|
---|
76 | if (tagval > 0xff)
|
---|
77 | {
|
---|
78 | out_uint16_be(s, tagval);
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | out_uint8(s, tagval);
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (length >= 0x80)
|
---|
86 | {
|
---|
87 | out_uint8(s, 0x82);
|
---|
88 | out_uint16_be(s, length);
|
---|
89 | }
|
---|
90 | else
|
---|
91 | out_uint8(s, length);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /* Output an ASN.1 BER integer */
|
---|
95 | static void
|
---|
96 | ber_out_integer(STREAM s, int value)
|
---|
97 | {
|
---|
98 | ber_out_header(s, BER_TAG_INTEGER, 2);
|
---|
99 | out_uint16_be(s, value);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
|
---|
103 | static void
|
---|
104 | mcs_out_domain_params(STREAM s, int max_channels, int max_users, int max_tokens, int max_pdusize)
|
---|
105 | {
|
---|
106 | ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32);
|
---|
107 | ber_out_integer(s, max_channels);
|
---|
108 | ber_out_integer(s, max_users);
|
---|
109 | ber_out_integer(s, max_tokens);
|
---|
110 | ber_out_integer(s, 1); /* num_priorities */
|
---|
111 | ber_out_integer(s, 0); /* min_throughput */
|
---|
112 | ber_out_integer(s, 1); /* max_height */
|
---|
113 | ber_out_integer(s, max_pdusize);
|
---|
114 | ber_out_integer(s, 2); /* ver_protocol */
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
|
---|
118 | static RD_BOOL
|
---|
119 | mcs_parse_domain_params(STREAM s)
|
---|
120 | {
|
---|
121 | int length;
|
---|
122 |
|
---|
123 | ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length);
|
---|
124 | in_uint8s(s, length);
|
---|
125 |
|
---|
126 | return s_check(s);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* Send an MCS_CONNECT_INITIAL message (ASN.1 BER) */
|
---|
130 | static void
|
---|
131 | mcs_send_connect_initial(STREAM mcs_data)
|
---|
132 | {
|
---|
133 | int datalen = mcs_data->end - mcs_data->data;
|
---|
134 | int length = 9 + 3 * 34 + 4 + datalen;
|
---|
135 | STREAM s;
|
---|
136 |
|
---|
137 | s = iso_init(length + 5);
|
---|
138 |
|
---|
139 | ber_out_header(s, MCS_CONNECT_INITIAL, length);
|
---|
140 | ber_out_header(s, BER_TAG_OCTET_STRING, 1); /* calling domain */
|
---|
141 | out_uint8(s, 1);
|
---|
142 | ber_out_header(s, BER_TAG_OCTET_STRING, 1); /* called domain */
|
---|
143 | out_uint8(s, 1);
|
---|
144 |
|
---|
145 | ber_out_header(s, BER_TAG_BOOLEAN, 1);
|
---|
146 | out_uint8(s, 0xff); /* upward flag */
|
---|
147 |
|
---|
148 | mcs_out_domain_params(s, 34, 2, 0, 0xffff); /* target params */
|
---|
149 | mcs_out_domain_params(s, 1, 1, 1, 0x420); /* min params */
|
---|
150 | mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff); /* max params */
|
---|
151 |
|
---|
152 | ber_out_header(s, BER_TAG_OCTET_STRING, datalen);
|
---|
153 | out_uint8p(s, mcs_data->data, datalen);
|
---|
154 |
|
---|
155 | s_mark_end(s);
|
---|
156 | iso_send(s);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
|
---|
160 | static RD_BOOL
|
---|
161 | mcs_recv_connect_response(STREAM mcs_data)
|
---|
162 | {
|
---|
163 | uint8 result;
|
---|
164 | int length;
|
---|
165 | STREAM s;
|
---|
166 |
|
---|
167 | s = iso_recv(NULL);
|
---|
168 | if (s == NULL)
|
---|
169 | return False;
|
---|
170 |
|
---|
171 | ber_parse_header(s, MCS_CONNECT_RESPONSE, &length);
|
---|
172 |
|
---|
173 | ber_parse_header(s, BER_TAG_RESULT, &length);
|
---|
174 | in_uint8(s, result);
|
---|
175 | if (result != 0)
|
---|
176 | {
|
---|
177 | error("MCS connect: %d\n", result);
|
---|
178 | return False;
|
---|
179 | }
|
---|
180 |
|
---|
181 | ber_parse_header(s, BER_TAG_INTEGER, &length);
|
---|
182 | in_uint8s(s, length); /* connect id */
|
---|
183 | mcs_parse_domain_params(s);
|
---|
184 |
|
---|
185 | ber_parse_header(s, BER_TAG_OCTET_STRING, &length);
|
---|
186 |
|
---|
187 | sec_process_mcs_data(s);
|
---|
188 | /*
|
---|
189 | if (length > mcs_data->size)
|
---|
190 | {
|
---|
191 | error("MCS data length %d, expected %d\n", length,
|
---|
192 | mcs_data->size);
|
---|
193 | length = mcs_data->size;
|
---|
194 | }
|
---|
195 |
|
---|
196 | in_uint8a(s, mcs_data->data, length);
|
---|
197 | mcs_data->p = mcs_data->data;
|
---|
198 | mcs_data->end = mcs_data->data + length;
|
---|
199 | */
|
---|
200 | return s_check_end(s);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /* Send an EDrq message (ASN.1 PER) */
|
---|
204 | static void
|
---|
205 | mcs_send_edrq(void)
|
---|
206 | {
|
---|
207 | STREAM s;
|
---|
208 |
|
---|
209 | s = iso_init(5);
|
---|
210 |
|
---|
211 | out_uint8(s, (MCS_EDRQ << 2));
|
---|
212 | out_uint16_be(s, 1); /* height */
|
---|
213 | out_uint16_be(s, 1); /* interval */
|
---|
214 |
|
---|
215 | s_mark_end(s);
|
---|
216 | iso_send(s);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /* Send an AUrq message (ASN.1 PER) */
|
---|
220 | static void
|
---|
221 | mcs_send_aurq(void)
|
---|
222 | {
|
---|
223 | STREAM s;
|
---|
224 |
|
---|
225 | s = iso_init(1);
|
---|
226 |
|
---|
227 | out_uint8(s, (MCS_AURQ << 2));
|
---|
228 |
|
---|
229 | s_mark_end(s);
|
---|
230 | iso_send(s);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /* Expect a AUcf message (ASN.1 PER) */
|
---|
234 | static RD_BOOL
|
---|
235 | mcs_recv_aucf(uint16 * mcs_userid)
|
---|
236 | {
|
---|
237 | uint8 opcode, result;
|
---|
238 | STREAM s;
|
---|
239 |
|
---|
240 | s = iso_recv(NULL);
|
---|
241 | if (s == NULL)
|
---|
242 | return False;
|
---|
243 |
|
---|
244 | in_uint8(s, opcode);
|
---|
245 | if ((opcode >> 2) != MCS_AUCF)
|
---|
246 | {
|
---|
247 | error("expected AUcf, got %d\n", opcode);
|
---|
248 | return False;
|
---|
249 | }
|
---|
250 |
|
---|
251 | in_uint8(s, result);
|
---|
252 | if (result != 0)
|
---|
253 | {
|
---|
254 | error("AUrq: %d\n", result);
|
---|
255 | return False;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (opcode & 2)
|
---|
259 | in_uint16_be(s, *mcs_userid);
|
---|
260 |
|
---|
261 | return s_check_end(s);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* Send a CJrq message (ASN.1 PER) */
|
---|
265 | static void
|
---|
266 | mcs_send_cjrq(uint16 chanid)
|
---|
267 | {
|
---|
268 | STREAM s;
|
---|
269 |
|
---|
270 | DEBUG_RDP5(("Sending CJRQ for channel #%d\n", chanid));
|
---|
271 |
|
---|
272 | s = iso_init(5);
|
---|
273 |
|
---|
274 | out_uint8(s, (MCS_CJRQ << 2));
|
---|
275 | out_uint16_be(s, g_mcs_userid);
|
---|
276 | out_uint16_be(s, chanid);
|
---|
277 |
|
---|
278 | s_mark_end(s);
|
---|
279 | iso_send(s);
|
---|
280 | }
|
---|
281 |
|
---|
282 | /* Expect a CJcf message (ASN.1 PER) */
|
---|
283 | static RD_BOOL
|
---|
284 | mcs_recv_cjcf(void)
|
---|
285 | {
|
---|
286 | uint8 opcode, result;
|
---|
287 | STREAM s;
|
---|
288 |
|
---|
289 | s = iso_recv(NULL);
|
---|
290 | if (s == NULL)
|
---|
291 | return False;
|
---|
292 |
|
---|
293 | in_uint8(s, opcode);
|
---|
294 | if ((opcode >> 2) != MCS_CJCF)
|
---|
295 | {
|
---|
296 | error("expected CJcf, got %d\n", opcode);
|
---|
297 | return False;
|
---|
298 | }
|
---|
299 |
|
---|
300 | in_uint8(s, result);
|
---|
301 | if (result != 0)
|
---|
302 | {
|
---|
303 | error("CJrq: %d\n", result);
|
---|
304 | return False;
|
---|
305 | }
|
---|
306 |
|
---|
307 | in_uint8s(s, 4); /* mcs_userid, req_chanid */
|
---|
308 | if (opcode & 2)
|
---|
309 | in_uint8s(s, 2); /* join_chanid */
|
---|
310 |
|
---|
311 | return s_check_end(s);
|
---|
312 | }
|
---|
313 |
|
---|
314 | /* Initialise an MCS transport data packet */
|
---|
315 | STREAM
|
---|
316 | mcs_init(int length)
|
---|
317 | {
|
---|
318 | STREAM s;
|
---|
319 |
|
---|
320 | s = iso_init(length + 8);
|
---|
321 | s_push_layer(s, mcs_hdr, 8);
|
---|
322 |
|
---|
323 | return s;
|
---|
324 | }
|
---|
325 |
|
---|
326 | /* Send an MCS transport data packet to a specific channel */
|
---|
327 | void
|
---|
328 | mcs_send_to_channel(STREAM s, uint16 channel)
|
---|
329 | {
|
---|
330 | uint16 length;
|
---|
331 |
|
---|
332 | s_pop_layer(s, mcs_hdr);
|
---|
333 | length = s->end - s->p - 8;
|
---|
334 | length |= 0x8000;
|
---|
335 |
|
---|
336 | out_uint8(s, (MCS_SDRQ << 2));
|
---|
337 | out_uint16_be(s, g_mcs_userid);
|
---|
338 | out_uint16_be(s, channel);
|
---|
339 | out_uint8(s, 0x70); /* flags */
|
---|
340 | out_uint16_be(s, length);
|
---|
341 |
|
---|
342 | iso_send(s);
|
---|
343 | }
|
---|
344 |
|
---|
345 | /* Send an MCS transport data packet to the global channel */
|
---|
346 | void
|
---|
347 | mcs_send(STREAM s)
|
---|
348 | {
|
---|
349 | mcs_send_to_channel(s, MCS_GLOBAL_CHANNEL);
|
---|
350 | }
|
---|
351 |
|
---|
352 | /* Receive an MCS transport data packet */
|
---|
353 | STREAM
|
---|
354 | mcs_recv(uint16 * channel, uint8 * rdpver)
|
---|
355 | {
|
---|
356 | uint8 opcode, appid, length;
|
---|
357 | STREAM s;
|
---|
358 |
|
---|
359 | s = iso_recv(rdpver);
|
---|
360 | if (s == NULL)
|
---|
361 | return NULL;
|
---|
362 | if (rdpver != NULL)
|
---|
363 | if (*rdpver != 3)
|
---|
364 | return s;
|
---|
365 | in_uint8(s, opcode);
|
---|
366 | appid = opcode >> 2;
|
---|
367 | if (appid != MCS_SDIN)
|
---|
368 | {
|
---|
369 | if (appid != MCS_DPUM)
|
---|
370 | {
|
---|
371 | error("expected data, got %d\n", opcode);
|
---|
372 | }
|
---|
373 | return NULL;
|
---|
374 | }
|
---|
375 | in_uint8s(s, 2); /* userid */
|
---|
376 | in_uint16_be(s, *channel);
|
---|
377 | in_uint8s(s, 1); /* flags */
|
---|
378 | in_uint8(s, length);
|
---|
379 | if (length & 0x80)
|
---|
380 | in_uint8s(s, 1); /* second byte of length */
|
---|
381 | return s;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* Establish a connection up to the MCS layer */
|
---|
385 | RD_BOOL
|
---|
386 | mcs_connect(char *server, STREAM mcs_data, char *username)
|
---|
387 | {
|
---|
388 | unsigned int i;
|
---|
389 |
|
---|
390 | if (!iso_connect(server, username))
|
---|
391 | return False;
|
---|
392 |
|
---|
393 | mcs_send_connect_initial(mcs_data);
|
---|
394 | if (!mcs_recv_connect_response(mcs_data))
|
---|
395 | goto error;
|
---|
396 |
|
---|
397 | mcs_send_edrq();
|
---|
398 |
|
---|
399 | mcs_send_aurq();
|
---|
400 | if (!mcs_recv_aucf(&g_mcs_userid))
|
---|
401 | goto error;
|
---|
402 |
|
---|
403 | mcs_send_cjrq(g_mcs_userid + MCS_USERCHANNEL_BASE);
|
---|
404 |
|
---|
405 | if (!mcs_recv_cjcf())
|
---|
406 | goto error;
|
---|
407 |
|
---|
408 | mcs_send_cjrq(MCS_GLOBAL_CHANNEL);
|
---|
409 | if (!mcs_recv_cjcf())
|
---|
410 | goto error;
|
---|
411 |
|
---|
412 | for (i = 0; i < g_num_channels; i++)
|
---|
413 | {
|
---|
414 | mcs_send_cjrq(g_channels[i].mcs_id);
|
---|
415 | if (!mcs_recv_cjcf())
|
---|
416 | goto error;
|
---|
417 | }
|
---|
418 | return True;
|
---|
419 |
|
---|
420 | error:
|
---|
421 | iso_disconnect();
|
---|
422 | return False;
|
---|
423 | }
|
---|
424 |
|
---|
425 | /* Establish a connection up to the MCS layer */
|
---|
426 | RD_BOOL
|
---|
427 | mcs_reconnect(char *server, STREAM mcs_data)
|
---|
428 | {
|
---|
429 | unsigned int i;
|
---|
430 |
|
---|
431 | if (!iso_reconnect(server))
|
---|
432 | return False;
|
---|
433 |
|
---|
434 | mcs_send_connect_initial(mcs_data);
|
---|
435 | if (!mcs_recv_connect_response(mcs_data))
|
---|
436 | goto error;
|
---|
437 |
|
---|
438 | mcs_send_edrq();
|
---|
439 |
|
---|
440 | mcs_send_aurq();
|
---|
441 | if (!mcs_recv_aucf(&g_mcs_userid))
|
---|
442 | goto error;
|
---|
443 |
|
---|
444 | mcs_send_cjrq(g_mcs_userid + MCS_USERCHANNEL_BASE);
|
---|
445 |
|
---|
446 | if (!mcs_recv_cjcf())
|
---|
447 | goto error;
|
---|
448 |
|
---|
449 | mcs_send_cjrq(MCS_GLOBAL_CHANNEL);
|
---|
450 | if (!mcs_recv_cjcf())
|
---|
451 | goto error;
|
---|
452 |
|
---|
453 | for (i = 0; i < g_num_channels; i++)
|
---|
454 | {
|
---|
455 | mcs_send_cjrq(g_channels[i].mcs_id);
|
---|
456 | if (!mcs_recv_cjcf())
|
---|
457 | goto error;
|
---|
458 | }
|
---|
459 | return True;
|
---|
460 |
|
---|
461 | error:
|
---|
462 | iso_disconnect();
|
---|
463 | return False;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /* Disconnect from the MCS layer */
|
---|
467 | void
|
---|
468 | mcs_disconnect(void)
|
---|
469 | {
|
---|
470 | iso_disconnect();
|
---|
471 | }
|
---|
472 |
|
---|
473 | /* reset the state of the mcs layer */
|
---|
474 | void
|
---|
475 | mcs_reset_state(void)
|
---|
476 | {
|
---|
477 | g_mcs_userid = 0;
|
---|
478 | iso_reset_state();
|
---|
479 | }
|
---|