]> git.sesse.net Git - rdpsrv/blob - mcs.c
Various changes to try to work better with MS RDC (no big luck)
[rdpsrv] / mcs.c
1 /* -*- c-basic-offset: 8 -*-
2    rdesktop: A Remote Desktop Protocol client.
3    Protocol services - Multipoint Communications Service
4    Copyright (C) Matthew Chapman 1999-2002
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 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         if (tag != tagval)
42         {
43                 error("expected tag %d, got %d\n", tagval, tag);
44                 return False;
45         }
46
47         in_uint8(s, len);
48
49         if (len & 0x80)
50         {
51                 len &= ~0x80;
52                 *length = 0;
53                 while (len--)
54                         next_be(s, *length);
55         }
56         else
57                 *length = len;
58
59         return s_check(s);
60 }
61
62 /* Output an ASN.1 BER header */
63 static void
64 ber_out_header(STREAM s, int tagval, int length)
65 {
66         if (tagval > 0xff)
67         {
68                 out_uint16_be(s, tagval);
69         }
70         else
71         {
72                 out_uint8(s, tagval);
73         }
74
75         if (length >= 0x80)
76         {
77                 out_uint8(s, 0x82);
78                 out_uint16_be(s, length);
79         }
80         else
81                 out_uint8(s, length);
82 }
83
84 /* Output an ASN.1 BER integer */
85 static void
86 ber_out_integer(STREAM s, int value)
87 {
88         ber_out_header(s, BER_TAG_INTEGER, 2);
89         out_uint16_be(s, value);
90 }
91
92 /* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
93 static void
94 mcs_out_domain_params(STREAM s, int max_channels, int max_users, int max_tokens, int max_pdusize)
95 {
96         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         ber_out_integer(s, 1);  /* num_priorities */
101         ber_out_integer(s, 0);  /* min_throughput */
102         ber_out_integer(s, 1);  /* max_height */
103         ber_out_integer(s, max_pdusize);
104         ber_out_integer(s, 2);  /* ver_protocol */
105 }
106
107 /* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
108 static BOOL
109 mcs_parse_domain_params(STREAM s)
110 {
111         int length;
112
113         ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length);
114         in_uint8s(s, length);
115
116         return s_check(s);
117 }
118
119 /* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
120 BOOL
121 mcs_recv_connect_initial()
122 {
123         uint8 result;
124         int length;
125         STREAM s;
126         char *buf;
127
128         s = iso_recv();
129         if (s == NULL)
130                 return False;
131
132         ber_parse_header(s, MCS_CONNECT_INITIAL, &length);
133         printf("parsing MCS_CONNECT_INITIAL (len=%u)\n", length);
134         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);   /* calling domain */
135         in_uint8(s, result);
136         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);   /* called domain */
137         in_uint8(s, result);
138         
139         ber_parse_header(s, BER_TAG_BOOLEAN, &length);
140         in_uint8(s, result);
141
142         mcs_parse_domain_params(s);
143         mcs_parse_domain_params(s);
144         mcs_parse_domain_params(s);
145
146         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);
147         in_uint8p(s, buf, length);
148
149         printf("Data from MCS connect: '%*s'\n", length, buf);
150         
151         return s_check_end(s);
152 }
153
154 void
155 mcs_send_connect_response()
156 {
157         STREAM s;
158         int i;
159
160         s = iso_init(92);
161
162         ber_out_header(s, MCS_CONNECT_RESPONSE, 80);
163         ber_out_header(s, BER_TAG_RESULT, 1);
164         out_uint8(s, 0);
165
166         ber_out_header(s, BER_TAG_INTEGER, 1);
167         out_uint8(s, 1);  // connect id
168         
169         mcs_out_domain_params(s, 34, 2, 0, 0xffff);  // dumdidum?
170
171         ber_out_header(s, BER_TAG_OCTET_STRING, 40);
172
173         out_uint8s(s, 21);   // ick
174         out_uint8(s, 0);
175
176         // server info -- we claim to support RDP1
177         out_uint16_le(s, SEC_TAG_SRV_INFO);
178         out_uint16_le(s, 6);  // length
179         out_uint16_le(s, 1);
180
181         // crypto info
182         out_uint16_le(s, SEC_TAG_SRV_CRYPT);
183         out_uint16_le(s, 12); // length
184         out_uint32_le(s, 1); // 40-bit
185         out_uint32_le(s, 0); // no encryption
186         
187         s_mark_end(s);
188         printf("LEN: %u\n", s->p - s->iso_hdr);
189         iso_send(s);
190
191 }
192
193 /* Send an EDrq message (ASN.1 PER) */
194 static void
195 mcs_send_edrq(void)
196 {
197         STREAM s;
198
199         s = iso_init(5);
200
201         out_uint8(s, (MCS_EDRQ << 2));
202         out_uint16_be(s, 1);    /* height */
203         out_uint16_be(s, 1);    /* interval */
204
205         s_mark_end(s);
206         iso_send(s);
207 }
208
209 /* Send an AUrq message (ASN.1 PER) */
210 static void
211 mcs_send_aurq(void)
212 {
213         STREAM s;
214
215         s = iso_init(1);
216
217         out_uint8(s, (MCS_AURQ << 2));
218
219         s_mark_end(s);
220         iso_send(s);
221 }
222
223 /* Send a AUcf message (ASN.1 PER) */
224 static void
225 mcs_send_aucf(uint16 mcs_userid)
226 {
227         STREAM s;
228
229         s = iso_init(5);
230
231         out_uint8(s, (MCS_AUCF << 2) | 2);  // | 2 = send user ID
232         out_uint8(s, 0);  // success
233         out_uint16_be(s, 0);
234         
235         s_mark_end(s);
236         iso_send(s);
237 }
238
239 /* Send a CJrq message (ASN.1 PER) */
240 static void
241 mcs_send_cjrq(uint16 chanid)
242 {
243         STREAM s;
244
245         DEBUG_RDP5(("Sending CJRQ for channel #%d\n", chanid));
246
247         s = iso_init(5);
248
249         out_uint8(s, (MCS_CJRQ << 2));
250         out_uint16_be(s, g_mcs_userid);
251         out_uint16_be(s, chanid);
252
253         s_mark_end(s);
254         iso_send(s);
255 }
256
257 /* Expect a CJcf message (ASN.1 PER) */
258 static void
259 mcs_send_cjcf(uint16 userid, uint16 chanid)
260 {
261         STREAM s;
262
263         s = iso_init(5);
264
265         out_uint8(s, (MCS_CJCF << 2));
266         out_uint8(s, 0); // success
267         out_uint16_be(s, g_mcs_userid);
268         out_uint16_be(s, chanid);
269
270         s_mark_end(s);
271         iso_send(s);
272 }
273
274 /* Initialise an MCS transport data packet */
275 STREAM
276 mcs_init(int length)
277 {
278         STREAM s;
279
280         s = iso_init(length + 8);
281         s_push_layer(s, mcs_hdr, 8);
282
283         return s;
284 }
285
286 /* Send an MCS transport data packet to a specific channel */
287 void
288 mcs_send_to_channel(STREAM s, uint16 channel)
289 {
290         uint16 length;
291
292         s_pop_layer(s, mcs_hdr);
293         length = s->end - s->p - 8;
294         length |= 0x8000;
295
296         out_uint8(s, (MCS_SDIN << 2));
297         out_uint16_be(s, g_mcs_userid);
298         out_uint16_be(s, channel);
299         out_uint8(s, 0x70);     /* flags */
300         out_uint16_be(s, length);
301
302         iso_send(s);
303 }
304
305 /* Send an MCS transport data packet to the global channel */
306 void
307 mcs_send(STREAM s)
308 {
309         mcs_send_to_channel(s, MCS_GLOBAL_CHANNEL);
310 }
311
312 /* Receive an MCS transport data packet */
313 STREAM
314 mcs_recv(uint16 * channel)
315 {
316         uint8 opcode, appid, length, userid;
317         STREAM s;
318
319         s = iso_recv();
320         if (s == NULL)
321                 return NULL;
322
323         in_uint8(s, opcode);
324         appid = opcode >> 2;
325
326         switch (appid) {
327         case MCS_SDRQ:
328                 in_uint8s(s, 2);        /* userid */
329                 in_uint16_be(s, *channel);
330                 in_uint8s(s, 1);        /* flags */
331                 in_uint8(s, length);
332                 if (length & 0x80)
333                         in_uint8s(s, 1);        /* second byte of length */
334
335                 return s;
336         case MCS_DPUM:
337                 return NULL;
338         case MCS_EDRQ:
339                 // Erect Domain (ignore)
340                 printf("Received EDrq\n");
341                 return NULL;
342         case MCS_AURQ:
343                 // Attach User Request, respond with AUcf (Attach User Confirm)
344                 printf("Received AUrq, sending AUcf\n");
345                 mcs_send_aucf(0);
346                 return NULL;
347         case MCS_CJRQ:
348                 // Channel Join Request, respond with CJcf (Channel Join Confirm);
349                 in_uint16_be(s, userid);
350                 in_uint16_be(s, *channel);
351                 printf("Received CJrq for channel %hu, sending CJcf\n", *channel);
352                 mcs_send_cjcf(userid, *channel);
353                 return NULL;
354         default:
355                 error("expected data, got %d\n", opcode);
356                 return NULL;
357         }
358
359 }
360
361 /* Disconnect from the MCS layer */
362 void
363 mcs_disconnect(void)
364 {
365         iso_disconnect();
366 }