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