]> git.sesse.net Git - rdpsrv/blob - mcs.c
Support RDP5 logon packets.
[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 static void
93 ber_in_integer(STREAM s, int *value)
94 {
95         int length;
96         ber_parse_header(s, BER_TAG_INTEGER, &length);
97         in_uint16_be(s, *value);
98 }
99
100 /* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
101 static void
102 mcs_out_domain_params(STREAM s, int max_channels, int max_users, int max_tokens, int max_pdusize)
103 {
104         ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32);
105         ber_out_integer(s, max_channels);
106         ber_out_integer(s, max_users);
107         ber_out_integer(s, max_tokens);
108         ber_out_integer(s, 1);  /* num_priorities */
109         ber_out_integer(s, 0);  /* min_throughput */
110         ber_out_integer(s, 1);  /* max_height */
111         ber_out_integer(s, max_pdusize);
112         ber_out_integer(s, 2);  /* ver_protocol */
113 }
114
115 /* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
116 static BOOL
117 mcs_parse_domain_params(STREAM s)
118 {
119         int length;
120         int max_channels, max_users, max_tokens, max_pdusize;
121         int num_priorities, min_throughput, max_height;
122         int ver_protocol;
123
124         ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length);
125         printf("MCS_TAG_DOMAIN_PARAMS, len %u (expected 32)\n", length);
126         if (length == 32) {
127                 ber_in_integer(s, &max_channels);
128                 ber_in_integer(s, &max_users);
129                 ber_in_integer(s, &max_tokens);
130                 ber_in_integer(s, &num_priorities);
131                 ber_in_integer(s, &min_throughput);
132                 ber_in_integer(s, &max_height);
133                 ber_in_integer(s, &max_pdusize);
134                 ber_in_integer(s, &ver_protocol);
135
136                 printf("max_channels=%u\n", max_channels);
137                 printf("max_users=%u\n", max_users);
138                 printf("max_tokens=%u\n", max_tokens);
139                 printf("num_priorities=%u\n", num_priorities);
140                 printf("min_throughput=%u\n", min_throughput);
141                 printf("max_pdusize=%u\n", max_pdusize);
142                 printf("ver_protocol=%u\n", ver_protocol);
143         } else {
144                 hexdump(s->p, length);
145                 in_uint8s(s, length);
146         }
147         
148         return s_check(s);
149 }
150
151 /* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
152 BOOL
153 mcs_recv_connect_initial()
154 {
155         uint8 result;
156         int length;
157         STREAM s;
158         char *buf;
159
160         s = iso_recv();
161         if (s == NULL)
162                 return False;
163
164         ber_parse_header(s, MCS_CONNECT_INITIAL, &length);
165         printf("parsing MCS_CONNECT_INITIAL (len=%u)\n", length);
166         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);   /* calling domain */
167         in_uint8(s, result);
168         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);   /* called domain */
169         in_uint8(s, result);
170         
171         ber_parse_header(s, BER_TAG_BOOLEAN, &length);
172         in_uint8(s, result);
173
174         mcs_parse_domain_params(s);
175         mcs_parse_domain_params(s);
176         mcs_parse_domain_params(s);
177
178         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);
179         in_uint8p(s, buf, length);
180
181         printf("Data from MCS connect: '%*s'\n", length, buf);
182         
183         return s_check_end(s);
184 }
185
186 void
187 mcs_send_connect_response()
188 {
189         STREAM s;
190         int i;
191
192         s = iso_init(92);
193
194         ber_out_header(s, MCS_CONNECT_RESPONSE, 80);
195         ber_out_header(s, BER_TAG_RESULT, 1);
196         out_uint8(s, 0);
197
198         ber_out_header(s, BER_TAG_INTEGER, 1);
199         out_uint8(s, 1);  // connect id
200         
201         mcs_out_domain_params(s, 34, 2, 0, 0xffff);  // dumdidum?
202
203         ber_out_header(s, BER_TAG_OCTET_STRING, 40);
204
205         out_uint8s(s, 21);   // ick
206         out_uint8(s, 0);
207
208         // server info -- we claim to support RDP5
209         out_uint16_le(s, SEC_TAG_SRV_INFO);
210         out_uint16_le(s, 6);  // length
211         out_uint16_le(s, 5);
212
213         // crypto info
214         out_uint16_le(s, SEC_TAG_SRV_CRYPT);
215         out_uint16_le(s, 12); // length
216         out_uint32_le(s, 1); // 40-bit
217         out_uint32_le(s, 0); // no encryption
218         
219         s_mark_end(s);
220         printf("LEN: %u\n", s->p - s->iso_hdr);
221         iso_send(s);
222
223 }
224
225 /* Send an EDrq message (ASN.1 PER) */
226 static void
227 mcs_send_edrq(void)
228 {
229         STREAM s;
230
231         s = iso_init(5);
232
233         out_uint8(s, (MCS_EDRQ << 2));
234         out_uint16_be(s, 1);    /* height */
235         out_uint16_be(s, 1);    /* interval */
236
237         s_mark_end(s);
238         iso_send(s);
239 }
240
241 /* Send an AUrq message (ASN.1 PER) */
242 static void
243 mcs_send_aurq(void)
244 {
245         STREAM s;
246
247         s = iso_init(1);
248
249         out_uint8(s, (MCS_AURQ << 2));
250
251         s_mark_end(s);
252         iso_send(s);
253 }
254
255 /* Send a AUcf message (ASN.1 PER) */
256 static void
257 mcs_send_aucf(uint16 mcs_userid)
258 {
259         STREAM s;
260
261         s = iso_init(5);
262
263         out_uint8(s, (MCS_AUCF << 2) | 2);  // | 2 = send user ID
264         out_uint8(s, 0);  // success
265         out_uint16_be(s, 0);
266         
267         s_mark_end(s);
268         iso_send(s);
269 }
270
271 /* Send a CJrq message (ASN.1 PER) */
272 static void
273 mcs_send_cjrq(uint16 chanid)
274 {
275         STREAM s;
276
277         DEBUG_RDP5(("Sending CJRQ for channel #%d\n", chanid));
278
279         s = iso_init(5);
280
281         out_uint8(s, (MCS_CJRQ << 2));
282         out_uint16_be(s, g_mcs_userid);
283         out_uint16_be(s, chanid);
284
285         s_mark_end(s);
286         iso_send(s);
287 }
288
289 /* Expect a CJcf message (ASN.1 PER) */
290 static void
291 mcs_send_cjcf(uint16 userid, uint16 chanid)
292 {
293         STREAM s;
294
295         s = iso_init(5);
296
297         out_uint8(s, (MCS_CJCF << 2));
298         out_uint8(s, 0); // success
299         out_uint16_be(s, g_mcs_userid);
300         out_uint16_be(s, chanid);
301
302         s_mark_end(s);
303         iso_send(s);
304 }
305
306 /* Initialise an MCS transport data packet */
307 STREAM
308 mcs_init(int length)
309 {
310         STREAM s;
311
312         s = iso_init(length + 8);
313         s_push_layer(s, mcs_hdr, 8);
314
315         return s;
316 }
317
318 /* Send an MCS transport data packet to a specific channel */
319 void
320 mcs_send_to_channel(STREAM s, uint16 channel)
321 {
322         uint16 length;
323
324         s_pop_layer(s, mcs_hdr);
325         length = s->end - s->p - 8;
326         length |= 0x8000;
327
328         out_uint8(s, (MCS_SDIN << 2));
329         out_uint16_be(s, g_mcs_userid);
330         out_uint16_be(s, channel);
331         out_uint8(s, 0x70);     /* flags */
332         out_uint16_be(s, length);
333
334         iso_send(s);
335 }
336
337 /* Send an MCS transport data packet to the global channel */
338 void
339 mcs_send(STREAM s)
340 {
341         mcs_send_to_channel(s, MCS_GLOBAL_CHANNEL);
342 }
343
344 /* Receive an MCS transport data packet */
345 STREAM
346 mcs_recv(uint16 * channel)
347 {
348         uint8 opcode, appid, length, userid;
349         STREAM s;
350
351         s = iso_recv();
352         if (s == NULL)
353                 return NULL;
354
355         in_uint8(s, opcode);
356         appid = opcode >> 2;
357
358         switch (appid) {
359         case MCS_SDRQ:
360                 in_uint8s(s, 2);        /* userid */
361                 in_uint16_be(s, *channel);
362                 in_uint8s(s, 1);        /* flags */
363                 in_uint8(s, length);
364                 if (length & 0x80)
365                         in_uint8s(s, 1);        /* second byte of length */
366
367                 return s;
368         case MCS_DPUM:
369                 return NULL;
370         case MCS_EDRQ:
371                 // Erect Domain (ignore)
372                 printf("Received EDrq\n");
373                 return NULL;
374         case MCS_AURQ:
375                 // Attach User Request, respond with AUcf (Attach User Confirm)
376                 printf("Received AUrq, sending AUcf\n");
377                 mcs_send_aucf(0);
378                 return NULL;
379         case MCS_CJRQ:
380                 // Channel Join Request, respond with CJcf (Channel Join Confirm);
381                 in_uint16_be(s, userid);
382                 in_uint16_be(s, *channel);
383                 printf("Received CJrq for channel %hu, sending CJcf\n", *channel);
384                 mcs_send_cjcf(userid, *channel);
385                 return NULL;
386         default:
387                 error("expected data, got %d\n", opcode);
388                 return NULL;
389         }
390
391 }
392
393 /* Disconnect from the MCS layer */
394 void
395 mcs_disconnect(void)
396 {
397         iso_disconnect();
398 }