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