xrdp/libxrdp/xrdp_mcs.c

760 lines
17 KiB
C
Raw Normal View History

2005-06-28 07:11:35 +04:00
/*
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.
xrdp: A Remote Desktop Protocol server.
2010-05-04 13:24:24 +04:00
Copyright (C) Jay Sorg 2004-2010
2005-06-28 07:11:35 +04:00
mcs layer
*/
#include "libxrdp.h"
/*****************************************************************************/
struct xrdp_mcs* APP_CC
2010-05-04 13:24:24 +04:00
xrdp_mcs_create(struct xrdp_sec* owner, struct trans* trans,
2005-06-28 07:11:35 +04:00
struct stream* client_mcs_data,
struct stream* server_mcs_data)
{
struct xrdp_mcs* self;
2007-01-15 12:08:03 +03:00
DEBUG((" in xrdp_mcs_create"));
2005-06-28 07:11:35 +04:00
self = (struct xrdp_mcs*)g_malloc(sizeof(struct xrdp_mcs), 1);
self->sec_layer = owner;
self->userid = 1;
self->chanid = 1001;
self->client_mcs_data = client_mcs_data;
self->server_mcs_data = server_mcs_data;
2010-05-04 13:24:24 +04:00
self->iso_layer = xrdp_iso_create(self, trans);
2006-12-28 09:15:13 +03:00
self->channel_list = list_create();
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_create"));
2005-06-28 07:11:35 +04:00
return self;
}
/*****************************************************************************/
void APP_CC
xrdp_mcs_delete(struct xrdp_mcs* self)
{
2006-12-28 09:15:13 +03:00
struct mcs_channel_item* channel_item;
int index;
int count;
2005-06-28 07:11:35 +04:00
if (self == 0)
{
return;
}
2006-12-28 09:15:13 +03:00
/* here we have to free the channel items and anything in them */
count = self->channel_list->count;
for (index = count - 1; index >= 0; index--)
{
channel_item = (struct mcs_channel_item*)
list_get_item(self->channel_list, index);
2008-12-16 05:18:36 +03:00
g_free(channel_item);
2006-12-28 09:15:13 +03:00
}
list_delete(self->channel_list);
2005-06-28 07:11:35 +04:00
xrdp_iso_delete(self->iso_layer);
/* make sure we get null pointer exception if struct is used again. */
DEBUG(("xrdp_mcs_delete processed"))
g_memset(self,0,sizeof(struct xrdp_mcs)) ;
2005-06-28 07:11:35 +04:00
g_free(self);
}
/*****************************************************************************/
/* This function sends channel join confirm*/
/* returns error = 1 ok = 0*/
2005-06-28 07:11:35 +04:00
static int APP_CC
2006-12-28 09:15:13 +03:00
xrdp_mcs_send_cjcf(struct xrdp_mcs* self, int userid, int chanid)
2005-06-28 07:11:35 +04:00
{
struct stream* s;
2007-01-15 12:08:03 +03:00
DEBUG((" in xrdp_mcs_send_cjcf"));
2005-06-28 07:11:35 +04:00
make_stream(s);
init_stream(s, 8192);
if (xrdp_iso_init(self->iso_layer, s) != 0)
{
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send_cjcf error"));
2005-06-28 07:11:35 +04:00
return 1;
}
out_uint8(s, (MCS_CJCF << 2) | 2);
out_uint8(s, 0);
2006-12-28 09:15:13 +03:00
out_uint16_be(s, userid);
out_uint16_be(s, chanid); /* TODO Explain why we send this two times */
2005-06-28 07:11:35 +04:00
out_uint16_be(s, chanid);
s_mark_end(s);
if (xrdp_iso_send(self->iso_layer, s) != 0)
{
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send_cjcf error"));
2005-06-28 07:11:35 +04:00
return 1;
}
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send_cjcf"));
2005-06-28 07:11:35 +04:00
return 0;
}
/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_recv(struct xrdp_mcs* self, struct stream* s, int* chan)
{
int appid;
int opcode;
int len;
2006-12-28 09:15:13 +03:00
int userid;
int chanid;
2005-06-28 07:11:35 +04:00
2006-04-29 05:47:06 +04:00
DEBUG((" in xrdp_mcs_recv"));
2005-06-28 07:11:35 +04:00
while (1)
{
if (xrdp_iso_recv(self->iso_layer, s) != 0)
{
2006-04-29 05:47:06 +04:00
DEBUG((" out xrdp_mcs_recv xrdp_iso_recv returned non zero"));
2005-06-28 07:11:35 +04:00
return 1;
}
in_uint8(s, opcode);
appid = opcode >> 2;
if (appid == MCS_DPUM) /* Disconnect Provider Ultimatum */
2005-06-28 07:11:35 +04:00
{
g_writeln("received Disconnect Provider Ultimatum");
2006-04-29 05:47:06 +04:00
DEBUG((" out xrdp_mcs_recv appid != MCS_DPUM"));
2005-06-28 07:11:35 +04:00
return 1;
}
2006-12-28 09:15:13 +03:00
/* this is channels getting added from the client */
2005-06-28 07:11:35 +04:00
if (appid == MCS_CJRQ)
{
g_writeln("channel join request received");
2006-12-28 09:15:13 +03:00
in_uint16_be(s, userid);
in_uint16_be(s, chanid);
DEBUG(("xrdp_mcs_recv adding channel %4.4x", chanid));
if(xrdp_mcs_send_cjcf(self, userid, chanid)!=0)
{
g_writeln("Non handled error from xrdp_mcs_send_cjcf") ;
}
2005-06-28 07:11:35 +04:00
continue;
}
if(appid==MCS_SDRQ || appid==MCS_SDIN)
{
break ;
}else{
g_writeln("Recieved an unhandled appid:%d",appid);
}
2005-06-28 07:11:35 +04:00
break;
}
if (appid != MCS_SDRQ)
{
2006-04-29 05:47:06 +04:00
DEBUG((" out xrdp_mcs_recv err got 0x%x need MCS_SDRQ", appid));
2005-06-28 07:11:35 +04:00
return 1;
}
in_uint8s(s, 2);
in_uint16_be(s, *chan);
in_uint8s(s, 1);
in_uint8(s, len);
if (len & 0x80)
{
in_uint8s(s, 1);
}
2006-04-29 05:47:06 +04:00
DEBUG((" out xrdp_mcs_recv"));
2005-06-28 07:11:35 +04:00
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_parse_header(struct xrdp_mcs* self, struct stream* s,
int tag_val, int* len)
{
int tag;
int l;
int i;
if (tag_val > 0xff)
{
in_uint16_be(s, tag);
}
else
{
in_uint8(s, tag);
}
if (tag != tag_val)
{
return 1;
}
in_uint8(s, l);
if (l & 0x80)
{
l = l & ~0x80;
*len = 0;
while (l > 0)
{
in_uint8(s, i);
*len = (*len << 8) | i;
l--;
}
}
else
{
*len = l;
}
if (s_check(s))
{
return 0;
}
else
{
return 1;
}
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_parse_domain_params(struct xrdp_mcs* self, struct stream* s)
{
int len;
if (xrdp_mcs_ber_parse_header(self, s, MCS_TAG_DOMAIN_PARAMS, &len) != 0)
{
return 1;
}
in_uint8s(s, len);
if (s_check(s))
{
return 0;
}
else
{
return 1;
}
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_recv_connect_initial(struct xrdp_mcs* self)
{
int len;
struct stream* s;
make_stream(s);
init_stream(s, 8192);
if (xrdp_iso_recv(self->iso_layer, s) != 0)
{
free_stream(s);
return 1;
}
if (xrdp_mcs_ber_parse_header(self, s, MCS_CONNECT_INITIAL, &len) != 0)
{
free_stream(s);
return 1;
}
if (xrdp_mcs_ber_parse_header(self, s, BER_TAG_OCTET_STRING, &len) != 0)
{
free_stream(s);
return 1;
}
in_uint8s(s, len);
if (xrdp_mcs_ber_parse_header(self, s, BER_TAG_OCTET_STRING, &len) != 0)
{
free_stream(s);
return 1;
}
in_uint8s(s, len);
if (xrdp_mcs_ber_parse_header(self, s, BER_TAG_BOOLEAN, &len) != 0)
{
free_stream(s);
return 1;
}
in_uint8s(s, len);
if (xrdp_mcs_parse_domain_params(self, s) != 0)
{
free_stream(s);
return 1;
}
if (xrdp_mcs_parse_domain_params(self, s) != 0)
{
free_stream(s);
return 1;
}
if (xrdp_mcs_parse_domain_params(self, s) != 0)
{
free_stream(s);
return 1;
}
if (xrdp_mcs_ber_parse_header(self, s, BER_TAG_OCTET_STRING, &len) != 0)
{
free_stream(s);
return 1;
}
/* make a copy of client mcs data */
init_stream(self->client_mcs_data, len);
out_uint8a(self->client_mcs_data, s->p, len);
in_uint8s(s, len);
s_mark_end(self->client_mcs_data);
if (s_check_end(s))
{
free_stream(s);
return 0;
}
else
{
free_stream(s);
return 1;
}
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_recv_edrq(struct xrdp_mcs* self)
{
int opcode;
struct stream* s;
make_stream(s);
init_stream(s, 8192);
if (xrdp_iso_recv(self->iso_layer, s) != 0)
{
free_stream(s);
return 1;
}
in_uint8(s, opcode);
if ((opcode >> 2) != MCS_EDRQ)
{
free_stream(s);
return 1;
}
in_uint8s(s, 2);
in_uint8s(s, 2);
if (opcode & 2)
{
in_uint16_be(s, self->userid);
}
if (!(s_check_end(s)))
{
free_stream(s);
return 1;
}
free_stream(s);
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_recv_aurq(struct xrdp_mcs* self)
{
int opcode;
struct stream* s;
make_stream(s);
init_stream(s, 8192);
if (xrdp_iso_recv(self->iso_layer, s) != 0)
{
free_stream(s);
return 1;
}
in_uint8(s, opcode);
if ((opcode >> 2) != MCS_AURQ)
{
free_stream(s);
return 1;
}
if (opcode & 2)
{
in_uint16_be(s, self->userid);
}
if (!(s_check_end(s)))
{
free_stream(s);
return 1;
}
free_stream(s);
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_send_aucf(struct xrdp_mcs* self)
{
struct stream* s;
2007-01-15 12:08:03 +03:00
DEBUG((" in xrdp_mcs_send_aucf"));
2005-06-28 07:11:35 +04:00
make_stream(s);
init_stream(s, 8192);
if (xrdp_iso_init(self->iso_layer, s) != 0)
{
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send_aucf error"));
2005-06-28 07:11:35 +04:00
return 1;
}
out_uint8(s, ((MCS_AUCF << 2) | 2));
out_uint8s(s, 1);
out_uint16_be(s, self->userid);
s_mark_end(s);
if (xrdp_iso_send(self->iso_layer, s) != 0)
{
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send_aucf error"));
2005-06-28 07:11:35 +04:00
return 1;
}
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send_aucf"));
2005-06-28 07:11:35 +04:00
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_recv_cjrq(struct xrdp_mcs* self)
{
int opcode;
struct stream* s;
make_stream(s);
init_stream(s, 8192);
if (xrdp_iso_recv(self->iso_layer, s) != 0)
{
free_stream(s);
return 1;
}
in_uint8(s, opcode);
if ((opcode >> 2) != MCS_CJRQ)
{
free_stream(s);
return 1;
}
in_uint8s(s, 4);
if (opcode & 2)
{
in_uint8s(s, 2);
}
if (!(s_check_end(s)))
{
free_stream(s);
return 1;
}
free_stream(s);
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_out_header(struct xrdp_mcs* self, struct stream* s,
int tag_val, int len)
{
if (tag_val > 0xff)
{
out_uint16_be(s, tag_val);
}
else
{
out_uint8(s, tag_val);
}
if (len >= 0x80)
{
out_uint8(s, 0x82);
out_uint16_be(s, len);
}
else
{
out_uint8(s, len);
}
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_out_int8(struct xrdp_mcs* self, struct stream* s, int value)
{
xrdp_mcs_ber_out_header(self, s, BER_TAG_INTEGER, 1);
out_uint8(s, value);
return 0;
}
#if 0 /* not used */
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_out_int16(struct xrdp_mcs* self, struct stream* s, int value)
{
xrdp_mcs_ber_out_header(self, s, BER_TAG_INTEGER, 2);
out_uint8(s, (value >> 8));
out_uint8(s, value);
return 0;
}
#endif
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_ber_out_int24(struct xrdp_mcs* self, struct stream* s, int value)
{
xrdp_mcs_ber_out_header(self, s, BER_TAG_INTEGER, 3);
out_uint8(s, (value >> 16));
out_uint8(s, (value >> 8));
out_uint8(s, value);
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_out_domain_params(struct xrdp_mcs* self, struct stream* s,
int max_channels,
int max_users, int max_tokens,
int max_pdu_size)
{
xrdp_mcs_ber_out_header(self, s, MCS_TAG_DOMAIN_PARAMS, 26);
xrdp_mcs_ber_out_int8(self, s, max_channels);
xrdp_mcs_ber_out_int8(self, s, max_users);
xrdp_mcs_ber_out_int8(self, s, max_tokens);
xrdp_mcs_ber_out_int8(self, s, 1);
xrdp_mcs_ber_out_int8(self, s, 0);
xrdp_mcs_ber_out_int8(self, s, 1);
xrdp_mcs_ber_out_int24(self, s, max_pdu_size);
xrdp_mcs_ber_out_int8(self, s, 2);
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mcs_send_connect_response(struct xrdp_mcs* self)
{
int data_len;
struct stream* s;
2007-01-15 12:08:03 +03:00
DEBUG((" in xrdp_mcs_send_connect_response"));
2005-06-28 07:11:35 +04:00
make_stream(s);
init_stream(s, 8192);
data_len = self->server_mcs_data->end - self->server_mcs_data->data;
xrdp_iso_init(self->iso_layer, s);
2006-12-28 09:15:13 +03:00
xrdp_mcs_ber_out_header(self, s, MCS_CONNECT_RESPONSE, data_len + 38);
2005-06-28 07:11:35 +04:00
xrdp_mcs_ber_out_header(self, s, BER_TAG_RESULT, 1);
out_uint8(s, 0);
xrdp_mcs_ber_out_header(self, s, BER_TAG_INTEGER, 1);
out_uint8(s, 0);
2006-12-28 09:15:13 +03:00
xrdp_mcs_out_domain_params(self, s, 22, 3, 0, 0xfff8);
2005-06-28 07:11:35 +04:00
xrdp_mcs_ber_out_header(self, s, BER_TAG_OCTET_STRING, data_len);
/* mcs data */
out_uint8a(s, self->server_mcs_data->data, data_len);
s_mark_end(s);
if (xrdp_iso_send(self->iso_layer, s) != 0)
{
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send_connect_response error"));
2005-06-28 07:11:35 +04:00
return 1;
}
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send_connect_response"));
2005-06-28 07:11:35 +04:00
return 0;
}
/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_incoming(struct xrdp_mcs* self)
{
2006-04-29 05:47:06 +04:00
DEBUG((" in xrdp_mcs_incoming"));
2005-06-28 07:11:35 +04:00
if (xrdp_iso_incoming(self->iso_layer) != 0)
{
return 1;
}
if (xrdp_mcs_recv_connect_initial(self) != 0)
{
return 1;
}
2006-12-28 09:15:13 +03:00
/* in xrdp_sec.c */
if (xrdp_sec_process_mcs_data(self->sec_layer) != 0)
{
return 1;
}
/* in xrdp_sec.c */
if (xrdp_sec_out_mcs_data(self->sec_layer) != 0)
{
return 1;
}
2005-06-28 07:11:35 +04:00
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;
}
2006-12-28 09:15:13 +03:00
if (xrdp_mcs_send_cjcf(self, self->userid,
self->userid + MCS_USERCHANNEL_BASE) != 0)
2005-06-28 07:11:35 +04:00
{
return 1;
}
if (xrdp_mcs_recv_cjrq(self) != 0)
{
return 1;
}
2006-12-28 09:15:13 +03:00
if (xrdp_mcs_send_cjcf(self, self->userid, MCS_GLOBAL_CHANNEL) != 0)
2005-06-28 07:11:35 +04:00
{
return 1;
}
2006-04-29 05:47:06 +04:00
DEBUG((" out xrdp_mcs_incoming"));
2005-06-28 07:11:35 +04:00
return 0;
}
/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_init(struct xrdp_mcs* self, struct stream* s)
{
xrdp_iso_init(self->iso_layer, s);
s_push_layer(s, mcs_hdr, 8);
return 0;
}
2006-12-28 09:15:13 +03:00
/*****************************************************************************/
/* returns error */
/* Inform the callback that an mcs packet has been sent. This is needed so
the module can send any high priority mcs packets like audio. */
static int APP_CC
xrdp_mcs_call_callback(struct xrdp_mcs* self)
{
int rv;
struct xrdp_session* session;
rv = 0;
/* if there is a callback, call it here */
session = self->sec_layer->rdp_layer->session;
if (session != 0)
{
if (session->callback != 0)
{
/* in xrdp_wm.c */
rv = session->callback(session->id, 0x5556, 0, 0, 0, 0);
}
else
{
g_writeln("in xrdp_mcs_send, session->callback is nil");
}
}
else
{
g_writeln("in xrdp_mcs_send, session is nil");
}
return rv;
}
2005-06-28 07:11:35 +04:00
/*****************************************************************************/
/* returns error */
int APP_CC
2006-12-28 09:15:13 +03:00
xrdp_mcs_send(struct xrdp_mcs* self, struct stream* s, int chan)
2005-06-28 07:11:35 +04:00
{
int len;
2008-06-30 07:01:17 +04:00
char* lp;
2006-12-28 09:15:13 +03:00
//static int max_len = 0;
2005-06-28 07:11:35 +04:00
2006-04-29 05:47:06 +04:00
DEBUG((" in xrdp_mcs_send"));
2005-06-28 07:11:35 +04:00
s_pop_layer(s, mcs_hdr);
len = (s->end - s->p) - 8;
2006-12-28 09:15:13 +03:00
if (len > 8192 * 2)
{
g_writeln("error in xrdp_mcs_send, size too bog, its %d", len);
}
//if (len > max_len)
//{
// max_len = len;
// g_printf("mcs max length is %d\r\n", max_len);
//}
//g_printf("mcs length %d max length is %d\r\n", len, max_len);
//g_printf("mcs length %d\r\n", len);
2005-06-28 07:11:35 +04:00
out_uint8(s, MCS_SDIN << 2);
out_uint16_be(s, self->userid);
2006-12-28 09:15:13 +03:00
out_uint16_be(s, chan);
2005-06-28 07:11:35 +04:00
out_uint8(s, 0x70);
2008-06-30 07:01:17 +04:00
if (len >= 128)
{
len = len | 0x8000;
out_uint16_be(s, len);
}
else
{
out_uint8(s, len);
/* move everything up one byte */
lp = s->p;
while (lp < s->end)
{
lp[0] = lp[1];
lp++;
}
s->end--;
}
2005-06-28 07:11:35 +04:00
if (xrdp_iso_send(self->iso_layer, s) != 0)
{
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_send error"));
2005-06-28 07:11:35 +04:00
return 1;
}
2006-12-28 09:15:13 +03:00
/* todo, do we need to call this for every mcs packet,
maybe every 5 or so */
if (chan == MCS_GLOBAL_CHANNEL)
{
xrdp_mcs_call_callback(self);
}
2006-04-29 05:47:06 +04:00
DEBUG((" out xrdp_mcs_send"));
2005-06-28 07:11:35 +04:00
return 0;
}
/*****************************************************************************/
/* returns error */
int APP_CC
xrdp_mcs_disconnect(struct xrdp_mcs* self)
{
struct stream* s;
2007-01-15 12:08:03 +03:00
DEBUG((" in xrdp_mcs_disconnect"));
2005-06-28 07:11:35 +04:00
make_stream(s);
init_stream(s, 8192);
if (xrdp_iso_init(self->iso_layer, s) != 0)
{
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_disconnect error"));
2005-06-28 07:11:35 +04:00
return 1;
}
out_uint8(s, (MCS_DPUM << 2) | 1);
out_uint8(s, 0x80);
s_mark_end(s);
if (xrdp_iso_send(self->iso_layer, s) != 0)
{
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_disconnect error"));
2005-06-28 07:11:35 +04:00
return 1;
}
free_stream(s);
2007-01-15 12:08:03 +03:00
DEBUG((" out xrdp_mcs_disconnect"));
2005-06-28 07:11:35 +04:00
return 0;
}