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