summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp_mcs.c
blob: 366f4b46b4ad6dd01b33198f36ff3993930cb436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   Copyright (C) Jay Sorg 2004

   mcs layer

*/

#include "xrdp.h"

/*****************************************************************************/
struct xrdp_mcs* xrdp_mcs_create(struct xrdp_sec* owner)
{
  struct xrdp_mcs* self;

  self = (struct xrdp_mcs*)g_malloc(sizeof(struct xrdp_mcs), 1);
  self->owner = owner;
  self->in_s = owner->in_s;
  self->out_s = owner->out_s;
  self->userid = 1;
  self->chanid = 1001;
  self->client_mcs_data = &owner->client_mcs_data;
  self->server_mcs_data = &owner->server_mcs_data;
  self->iso_layer = xrdp_iso_create(self);
  return self;
}

/*****************************************************************************/
void xrdp_mcs_delete(struct xrdp_mcs* self)
{
  xrdp_iso_delete(self->iso_layer);
  g_free(self);
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_recv(struct xrdp_mcs* self)
{
  int appid;
  int opcode;
  int len;

  if (xrdp_iso_recv(self->iso_layer) != 0)
    return 1;
  in_uint8(self->in_s, opcode);
  appid = opcode >> 2;
  if (appid != MCS_SDRQ)
    return 1;
  in_uint8s(self->in_s, 5);
  in_uint8(self->in_s, len);
  if (len & 0x80)
    in_uint8s(self->in_s, 1);
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_ber_parse_header(struct xrdp_mcs* self, int tag_val, int* len)
{
  int tag;
  int l;

  if (tag_val > 0xff)
  {
    in_uint16_be(self->in_s, tag);
  }
  else
  {
    in_uint8(self->in_s, tag);
  }
  if (tag != tag_val)
    return 1;
  in_uint8(self->in_s, l);
  if (l & 0x80)
  {
    l = l & ~0x80;
    *len = 0;
    while (l > 0)
    {
      next_be(self->in_s, *len);
      l--;
    }
  }
  else
    *len = l;
  if (s_check(self->in_s))
    return 0;
  else
    return 1;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_parse_domain_params(struct xrdp_mcs* self)
{
  int len;

  if (xrdp_mcs_ber_parse_header(self, MCS_TAG_DOMAIN_PARAMS, &len) != 0)
    return 1;
  in_uint8s(self->in_s, len);
  if (s_check(self->in_s))
    return 0;
  else
    return 1;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_recv_connect_initial(struct xrdp_mcs* self)
{
  int len;

  if (xrdp_iso_recv(self->iso_layer) != 0)
    return 1;
  if (xrdp_mcs_ber_parse_header(self, MCS_CONNECT_INITIAL, &len) != 0)
    return 1;
  if (xrdp_mcs_ber_parse_header(self, BER_TAG_OCTET_STRING, &len) != 0)
    return 1;
  in_uint8s(self->in_s, len);
  if (xrdp_mcs_ber_parse_header(self, BER_TAG_OCTET_STRING, &len) != 0)
    return 1;
  in_uint8s(self->in_s, len);
  if (xrdp_mcs_ber_parse_header(self, BER_TAG_BOOLEAN, &len) != 0)
    return 1;
  in_uint8s(self->in_s, len);
  if (xrdp_mcs_parse_domain_params(self) != 0)
    return 1;
  if (xrdp_mcs_parse_domain_params(self) != 0)
    return 1;
  if (xrdp_mcs_parse_domain_params(self) != 0)
    return 1;
  if (xrdp_mcs_ber_parse_header(self, BER_TAG_OCTET_STRING, &len) != 0)
    return 1;
  /* make a copy of client mcs data */
  init_stream(self->client_mcs_data, len);
  out_uint8a(self->client_mcs_data, self->in_s->p, len);
  in_uint8s(self->in_s, len);
  s_mark_end(self->client_mcs_data);
  if (s_check_end(self->in_s))
    return 0;
  else
    return 1;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_recv_edrq(struct xrdp_mcs* self)
{
  int opcode;

  if (xrdp_iso_recv(self->iso_layer) != 0)
    return 1;
  in_uint8(self->in_s, opcode);
  if ((opcode >> 2) != MCS_EDRQ)
    return 1;
  in_uint8s(self->in_s, 2);
  in_uint8s(self->in_s, 2);
  if (opcode & 2)
  {
    in_uint16_be(self->in_s, self->userid);
  }
  if (!(s_check_end(self->in_s)))
    return 1;
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_recv_aurq(struct xrdp_mcs* self)
{
  int opcode;

  if (xrdp_iso_recv(self->iso_layer) != 0)
    return 1;
  in_uint8(self->in_s, opcode);
  if ((opcode >> 2) != MCS_AURQ)
    return 1;
  if (opcode & 2)
  {
    in_uint16_be(self->in_s, self->userid);
  }
  if (!(s_check_end(self->in_s)))
    return 1;
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_send_aucf(struct xrdp_mcs* self)
{
  if (xrdp_iso_init(self->iso_layer, 4) != 0)
    return 1;
  out_uint8(self->out_s, (MCS_AUCF << 2) | 2);
  out_uint8s(self->out_s, 1);
  out_uint16_be(self->out_s, self->userid);
  s_mark_end(self->out_s);
  if (xrdp_iso_send(self->iso_layer) != 0)
    return 1;
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_recv_cjrq(struct xrdp_mcs* self)
{
  int opcode;

  if (xrdp_iso_recv(self->iso_layer) != 0)
    return 1;
  in_uint8(self->in_s, opcode);
  if ((opcode >> 2) != MCS_CJRQ)
    return 1;
  in_uint8s(self->in_s, 4);
  if (opcode & 2)
  {
    in_uint8s(self->in_s, 2);
  }
  if (!(s_check_end(self->in_s)))
    return 1;
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_send_cjcf(struct xrdp_mcs* self, int chanid)
{
  if (xrdp_iso_init(self->iso_layer, 8) != 0)
    return 1;
  out_uint8(self->out_s, (MCS_CJCF << 2) | 2);
  out_uint8(self->out_s, 0);
  out_uint16_be(self->out_s, self->userid);
  out_uint16_be(self->out_s, chanid);
  out_uint16_be(self->out_s, chanid);
  s_mark_end(self->out_s);
  if (xrdp_iso_send(self->iso_layer) != 0)
    return 1;
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_ber_out_header(struct xrdp_mcs* self, int tag_val, int len)
{
  if (tag_val > 0xff)
  {
    out_uint16_be(self->out_s, tag_val);
  }
  else
  {
    out_uint8(self->out_s, tag_val);
  }
  if (len >= 0x80)
  {
    out_uint8(self->out_s, 0x82);
    out_uint16_be(self->out_s, len);
  }
  else
  {
    out_uint8(self->out_s, len);
  }
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_ber_out_int8(struct xrdp_mcs* self, int value)
{
  xrdp_mcs_ber_out_header(self, BER_TAG_INTEGER, 1);
  out_uint8(self->out_s, value);
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_ber_out_int16(struct xrdp_mcs* self, int value)
{
  xrdp_mcs_ber_out_header(self, BER_TAG_INTEGER, 2);
  out_uint8(self->out_s, value >> 8);
  out_uint8(self->out_s, value);
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_ber_out_int24(struct xrdp_mcs* self, int value)
{
  xrdp_mcs_ber_out_header(self, BER_TAG_INTEGER, 3);
  out_uint8(self->out_s, value >> 16);
  out_uint8(self->out_s, value >> 8);
  out_uint8(self->out_s, value);
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_out_domain_params(struct xrdp_mcs* self, int max_channels,
                               int max_users, int max_tokens,
                               int max_pdu_size)
{
  xrdp_mcs_ber_out_header(self, MCS_TAG_DOMAIN_PARAMS, 26);
  xrdp_mcs_ber_out_int8(self, max_channels);
  xrdp_mcs_ber_out_int8(self, max_users);
  xrdp_mcs_ber_out_int8(self, max_tokens);
  xrdp_mcs_ber_out_int8(self, 1);
  xrdp_mcs_ber_out_int8(self, 0);
  xrdp_mcs_ber_out_int8(self, 1);
  xrdp_mcs_ber_out_int24(self, max_pdu_size);
  xrdp_mcs_ber_out_int8(self, 2);
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_send_connect_response(struct xrdp_mcs* self)
{
  int data_len;

  data_len = self->server_mcs_data->end - self->server_mcs_data->data;
  xrdp_iso_init(self->iso_layer, 512);
  xrdp_mcs_ber_out_header(self, MCS_CONNECT_RESPONSE, 313);
  xrdp_mcs_ber_out_header(self, BER_TAG_RESULT, 1);
  out_uint8(self->out_s, 0);
  xrdp_mcs_ber_out_header(self, BER_TAG_INTEGER, 1);
  out_uint8(self->out_s, 0);
  xrdp_mcs_out_domain_params(self, 2, 2, 0, 0xffff);
  xrdp_mcs_ber_out_header(self, BER_TAG_OCTET_STRING, data_len);
  /* mcs data */
  out_uint8a(self->out_s, self->server_mcs_data->data, data_len);
  s_mark_end(self->out_s);
  if (xrdp_iso_send(self->iso_layer) != 0)
    return 1;
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_incoming(struct xrdp_mcs* self)
{
  if (xrdp_iso_incoming(self->iso_layer) != 0)
    return 1;
  if (xrdp_mcs_recv_connect_initial(self) != 0)
    return 1;
  if (xrdp_mcs_send_connect_response(self) != 0)
    return 1;
  if (xrdp_mcs_recv_edrq(self) != 0)
    return 1;
  if (xrdp_mcs_recv_aurq(self) != 0)
    return 1;
  if (xrdp_mcs_send_aucf(self) != 0)
    return 1;
  if (xrdp_mcs_recv_cjrq(self) != 0)
    return 1;
  if (xrdp_mcs_send_cjcf(self, self->userid + MCS_USERCHANNEL_BASE) != 0)
    return 1;
  if (xrdp_mcs_recv_cjrq(self) != 0)
    return 1;
  if (xrdp_mcs_send_cjcf(self, MCS_GLOBAL_CHANNEL) != 0)
    return 1;
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_init(struct xrdp_mcs* self, int len)
{
  xrdp_iso_init(self->iso_layer, len + 8);
  s_push_layer(self->out_s, mcs_hdr, 8);
  return 0;
}

/*****************************************************************************/
/* returns error */
int xrdp_mcs_send(struct xrdp_mcs* self)
{
  int len;

  s_pop_layer(self->out_s, mcs_hdr);
  len = (self->out_s->end - self->out_s->p) - 8;
  len = len | 0x8000;
  out_uint8(self->out_s, MCS_SDIN << 2);
  out_uint16_be(self->out_s, self->userid);
  out_uint16_be(self->out_s, MCS_GLOBAL_CHANNEL);
  out_uint8(self->out_s, 0x70);
  out_uint16_be(self->out_s, len);
  if (xrdp_iso_send(self->iso_layer) != 0)
    return 1;
  return 0;
}