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