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