diff --git a/common/parse.h b/common/parse.h index 69a57ff8..97911abb 100644 --- a/common/parse.h +++ b/common/parse.h @@ -116,7 +116,8 @@ struct stream (v) = *((unsigned char*)((s)->p)); \ (s)->p++; \ } while (0) - +/******************************************************************************/ +#define in_uint8_peek(s, v) do { v = *s->p; } while (0) /******************************************************************************/ #if defined(B_ENDIAN) || defined(NEED_ALIGN) #define in_sint16_le(s, v) do \ diff --git a/libxrdp/libxrdp.c b/libxrdp/libxrdp.c index ac5bbe98..c220f56f 100644 --- a/libxrdp/libxrdp.c +++ b/libxrdp/libxrdp.c @@ -128,7 +128,6 @@ libxrdp_process_data(struct xrdp_session *session) session->s); break; case RDP_PDU_DATA: /* 7 */ - if (xrdp_rdp_process_data((struct xrdp_rdp *)session->rdp, session->s) != 0) { @@ -139,7 +138,7 @@ libxrdp_process_data(struct xrdp_session *session) break; default: - g_writeln("unknown in libxrdp_process_data"); + g_writeln("unknown in libxrdp_process_data: code= %d", code); dead_lock_counter++; break; } diff --git a/libxrdp/libxrdp.h b/libxrdp/libxrdp.h index 70c8a124..a9150111 100644 --- a/libxrdp/libxrdp.h +++ b/libxrdp/libxrdp.h @@ -290,6 +290,8 @@ int APP_CC xrdp_iso_send(struct xrdp_iso* self, struct stream* s); int APP_CC xrdp_iso_incoming(struct xrdp_iso* self); +int APP_CC +xrdp_iso_detect_tpkt(struct xrdp_iso *self, struct stream *s); /* xrdp_mcs.c */ struct xrdp_mcs* APP_CC diff --git a/libxrdp/xrdp_fastpath.c b/libxrdp/xrdp_fastpath.c index 10f844e5..96d84972 100644 --- a/libxrdp/xrdp_fastpath.c +++ b/libxrdp/xrdp_fastpath.c @@ -2,7 +2,7 @@ * xrdp: A Remote Desktop Protocol server. * * Copyright (C) Jay Sorg 2012-2013 - * Copyright (C) Kevin Zhou 2012 + * Copyright (C) Idan Freiberg 2013-2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/libxrdp/xrdp_iso.c b/libxrdp/xrdp_iso.c index 69c242d3..09c08f94 100644 --- a/libxrdp/xrdp_iso.c +++ b/libxrdp/xrdp_iso.c @@ -89,44 +89,125 @@ xrdp_iso_recv_rdpnegreq(struct xrdp_iso *self, struct stream *s) static int APP_CC xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code, int *len) { - int ver; // TPKT Version - int plen; // TPKT PacketLength + int plen; // PacketLength *code = 0; // X.224 Packet Type *len = 0; // X.224 Length Indicator - if (xrdp_tcp_recv(self->tcp_layer, s, 4) != 0) + plen = xrdp_iso_recv_tpkt_header(self, s); + if (plen == 1) { + DEBUG((" xrdp_iso_recv_msg: error in tpkt header reading")); return 1; } - in_uint8(s, ver); - - if (ver != 3) + // receive the left bytes + if (xrdp_tcp_recv(self->tcp_layer, s, plen - 4) != 0) { return 1; } - in_uint8s(s, 1); - in_uint16_be(s, plen); + xrdp_iso_read_x224_header(s, code, len); - if (plen < 4) + return 0; +} +/*****************************************************************************/ +/* returns error */ +int APP_CC +xrdp_iso_recv(struct xrdp_iso *self, struct stream *s) +{ + int code; + int len; + + DEBUG((" in xrdp_iso_recv")); + + if (xrdp_iso_recv_msg(self, s, &code, &len) != 0) { + DEBUG((" out xrdp_iso_recv xrdp_iso_recv_msg return non zero")); return 1; } - if (xrdp_tcp_recv(self->tcp_layer, s, plen - 4) != 0) + if (code != ISO_PDU_DT || len != 2) { + DEBUG((" out xrdp_iso_recv code != ISO_PDU_DT or length != 2")); return 1; } + DEBUG((" out xrdp_iso_recv")); + return 0; +} +/*****************************************************************************/ +/* returns error */ +int APP_CC +xrdp_iso_detect_tpkt(struct xrdp_iso *self, struct stream *s) +{ + int ver; + + DEBUG((" in xrdp_iso_detect_tpkt")); + if (xrdp_tcp_recv(self->tcp_layer, s, 1) != 0) + { + return 1; + } + + in_uint8_peek(s, ver); + g_writeln("tpkt version: %x", ver); + + if (ver != 3) + { + return 1; + } + + DEBUG((" out xrdp_iso_detect_tpkt")); + return 0; +} +/*****************************************************************************/ +/* returns packet length or error (1) */ +int APP_CC +xrdp_iso_recv_tpkt_header(struct xrdp_iso *self, struct stream *s) +{ + int plen; + + DEBUG((" in xrdp_iso_recv_tpkt_header")); + + if (xrdp_tcp_recv(self->tcp_layer, s, 3) != 0) + { + return 1; + } + + in_uint8s(s, 1); + in_uint16_be(s, plen); + + if (plen < 4) + { + return 1; // tpkt must be >= 4 bytes length + } + + DEBUG((" out xrdp_iso_recv_tpkt_header")); + + return plen; +} +/*****************************************************************************/ +void APP_CC +xrdp_iso_write_tpkt_header(struct stream *s, int len) +{ + /* TPKT HEADER - 4 bytes */ + out_uint8(s, 3); /* version */ + out_uint8(s, 0); /* RESERVED */ + out_uint16_be(s, len); /* length */ +} +/*****************************************************************************/ +/* returns error */ +int APP_CC +xrdp_iso_read_x224_header(struct stream *s, int *code, int *len) +{ + DEBUG((" in xrdp_iso_read_x224_header")); if (!s_check_rem(s, 2)) { return 1; } - in_uint8(s, *len); - in_uint8(s, *code); + in_uint8(s, *len); /* length */ + in_uint8(s, *code); /* code */ if (*code == ISO_PDU_DT) { @@ -144,68 +225,59 @@ xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code, int *len) } in_uint8s(s, 5); } + DEBUG((" out xrdp_iso_read_x224_header")); return 0; } /*****************************************************************************/ -/* returns error */ -int APP_CC -xrdp_iso_recv(struct xrdp_iso *self, struct stream *s) +void APP_CC +xrdp_iso_write_x224_header(struct stream *s, int len, int code) { - int code; - int len; - - DEBUG((" in xrdp_iso_recv")); - - if (xrdp_iso_recv_msg(self, s, &code, &len) != 0) - { - DEBUG((" out xrdp_iso_recv xrdp_iso_recv_msg return non zero")); - return 1; - } + /* ISO LAYER - X.224 - 7 bytes*/ + out_uint8(s, len); /* length */ + out_uint8(s, code); /* code */ - if (code != ISO_PDU_DT || len != 2) + if (code == ISO_PDU_DT) { - DEBUG((" out xrdp_iso_recv code != ISO_PDU_DT or length != 2")); - return 1; + out_uint8(s, 0x80); + } else { + out_uint16_be(s, 0); + out_uint16_be(s, 0x1234); + out_uint8(s, 0); } - - DEBUG((" out xrdp_iso_recv")); - return 0; } - /*****************************************************************************/ static int APP_CC -xrdp_iso_send_rdpnegrsp(struct xrdp_iso *self, struct stream *s, int code) +xrdp_iso_send_rdpnegrsp(struct xrdp_iso *self, struct stream *s) { if (xrdp_tcp_init(self->tcp_layer, s) != 0) { return 1; } - /* TPKT HEADER - 4 bytes */ - out_uint8(s, 3); /* version */ - out_uint8(s, 0); /* RESERVED */ + // Write TPKT Header if (self->selectedProtocol != -1) { - out_uint16_be(s, 19); /* length */ //rdp negotiation happens. + //rdp negotiation happens. + xrdp_iso_write_tpkt_header(s, 19); } else { - out_uint16_be(s, 11); /* length */ //rdp negotiation doesn't happen. + //rdp negotiation doesn't happen. + xrdp_iso_write_tpkt_header(s, 11); } - /* ISO LAYER - X.224 - 7 bytes*/ + + // Write x224 header if (self->selectedProtocol != -1) { - out_uint8(s, 14); /* length */ + xrdp_iso_write_x224_header(s, 14, ISO_PDU_CC); } else { - out_uint8(s, 6); /* length */ + xrdp_iso_write_x224_header(s, 6, ISO_PDU_CC); } - out_uint8(s, code); /* SHOULD BE 0xD for CC */ - out_uint16_be(s, 0); - out_uint16_be(s, 0x1234); - out_uint8(s, 0); + + /* RDP_NEG */ if (self->selectedProtocol != -1) { /* RDP_NEG_RSP - 8 bytes*/ @@ -226,23 +298,17 @@ xrdp_iso_send_rdpnegrsp(struct xrdp_iso *self, struct stream *s, int code) } /*****************************************************************************/ static int APP_CC -xrdp_iso_send_rdpnegfailure(struct xrdp_iso *self, struct stream *s, int code, int failureCode) +xrdp_iso_send_rdpnegfailure(struct xrdp_iso *self, struct stream *s, int failureCode) { if (xrdp_tcp_init(self->tcp_layer, s) != 0) { return 1; } - /* TPKT HEADER - 4 bytes */ - out_uint8(s, 3); /* version */ - out_uint8(s, 0); /* RESERVED */ - out_uint16_be(s, 19); /* length */ - /* ISO LAYER - X.224 - 7 bytes*/ - out_uint8(s, 14); /* length */ - out_uint8(s, code); /* SHOULD BE 0xD for CC */ - out_uint16_be(s, 0); - out_uint16_be(s, 0x1234); - out_uint8(s, 0); + xrdp_iso_write_tpkt_header(s, 19); + + xrdp_iso_write_x224_header(s, 14, ISO_PDU_CC); + /* RDP_NEG_FAILURE - 8 bytes*/ out_uint8(s, RDP_NEG_FAILURE); out_uint8(s, 0); /* no flags available */ @@ -271,8 +337,7 @@ xrdp_iso_send_nego(struct xrdp_iso *self) if (self->requestedProtocol != PROTOCOL_RDP) { // Send RDP_NEG_FAILURE back to client - if (xrdp_iso_send_rdpnegfailure(self, s, ISO_PDU_CC, - SSL_NOT_ALLOWED_BY_SERVER) != 0) + if (xrdp_iso_send_rdpnegfailure(self, s, SSL_NOT_ALLOWED_BY_SERVER) != 0) { free_stream(s); return 1; @@ -282,7 +347,7 @@ xrdp_iso_send_nego(struct xrdp_iso *self) { self->selectedProtocol = PROTOCOL_RDP; // Send RDP_NEG_RSP back to client - if (xrdp_iso_send_rdpnegrsp(self, s, ISO_PDU_CC) != 0) + if (xrdp_iso_send_rdpnegrsp(self, s) != 0) { free_stream(s); return 1; @@ -309,6 +374,13 @@ xrdp_iso_incoming(struct xrdp_iso *self) init_stream(s, 8192); DEBUG((" in xrdp_iso_incoming")); + if (xrdp_iso_detect_tpkt(self, s) != 0) + { + g_writeln("xrdp_iso_incoming: TPKT not detected"); + free_stream(s); + return 1; + } + if (xrdp_iso_recv_msg(self, s, &code, &len) != 0) { DEBUG((" in xrdp_iso_recv_msg error!!")); @@ -318,6 +390,7 @@ xrdp_iso_incoming(struct xrdp_iso *self) if ((code != ISO_PDU_CR) || (len < 6)) { + DEBUG((" in xrdp_iso_recv_msg error: non iso_pdu_cr msg")); free_stream(s); return 1; } @@ -394,12 +467,9 @@ xrdp_iso_send(struct xrdp_iso *self, struct stream *s) DEBUG((" in xrdp_iso_send")); s_pop_layer(s, iso_hdr); len = (int)(s->end - s->p); - out_uint8(s, 3); - out_uint8(s, 0); - out_uint16_be(s, len); - out_uint8(s, 2); - out_uint8(s, ISO_PDU_DT); - out_uint8(s, 0x80); + + xrdp_iso_write_tpkt_header(s, len); + xrdp_iso_write_x224_header(s, 2, ISO_PDU_DT); if (xrdp_tcp_send(self->tcp_layer, s) != 0) { diff --git a/libxrdp/xrdp_mcs.c b/libxrdp/xrdp_mcs.c index c145158c..1828b28b 100644 --- a/libxrdp/xrdp_mcs.c +++ b/libxrdp/xrdp_mcs.c @@ -127,12 +127,14 @@ xrdp_mcs_recv(struct xrdp_mcs *self, struct stream *s, int *chan) while (1) { + if (xrdp_iso_recv(self->iso_layer, s) != 0) { - DEBUG((" out xrdp_mcs_recv xrdp_iso_recv returned non zero")); + free_stream(s); return 1; } + if (!s_check_rem(s, 1)) { return 1; @@ -320,10 +322,16 @@ xrdp_mcs_recv_connect_initial(struct xrdp_mcs *self) make_stream(s); init_stream(s, 16 * 1024); - if (xrdp_iso_recv(self->iso_layer, s) != 0) - { - free_stream(s); - return 1; + if (xrdp_iso_detect_tpkt(self->iso_layer, s) == 0) { + if (xrdp_iso_recv(self->iso_layer, s) != 0) + { + free_stream(s); + return 1; + } + } else { + g_writeln("xrdp_mcs_recv_connect_initial: TPKT not detected"); + free_stream(s); + return 1; } if (xrdp_mcs_ber_parse_header(self, s, MCS_CONNECT_INITIAL, &len) != 0) @@ -441,10 +449,16 @@ xrdp_mcs_recv_edrq(struct xrdp_mcs *self) make_stream(s); init_stream(s, 8192); - if (xrdp_iso_recv(self->iso_layer, s) != 0) - { - free_stream(s); - return 1; + if (xrdp_iso_detect_tpkt(self->iso_layer, s) == 0) { + if (xrdp_iso_recv(self->iso_layer, s) != 0) + { + free_stream(s); + return 1; + } + } else { + g_writeln("xrdp_mcs_recv_edrq: TPKT not detected"); + free_stream(s); + return 1; } if (!s_check_rem(s, 1)) @@ -503,12 +517,19 @@ xrdp_mcs_recv_aurq(struct xrdp_mcs *self) make_stream(s); init_stream(s, 8192); - if (xrdp_iso_recv(self->iso_layer, s) != 0) - { - free_stream(s); - return 1; + if (xrdp_iso_detect_tpkt(self->iso_layer, s) == 0) { + if (xrdp_iso_recv(self->iso_layer, s) != 0) + { + free_stream(s); + return 1; + } + } else { + g_writeln("xrdp_mcs_recv_aurq: TPKT not detected"); + free_stream(s); + return 1; } + if (!s_check_rem(s, 1)) { free_stream(s); @@ -590,10 +611,16 @@ xrdp_mcs_recv_cjrq(struct xrdp_mcs *self) make_stream(s); init_stream(s, 8192); - if (xrdp_iso_recv(self->iso_layer, s) != 0) - { - free_stream(s); - return 1; + if (xrdp_iso_detect_tpkt(self->iso_layer, s) == 0) { + if (xrdp_iso_recv(self->iso_layer, s) != 0) + { + free_stream(s); + return 1; + } + } else { + g_writeln("xrdp_mcs_recv_cjrq: TPKT not detected"); + free_stream(s); + return 1; } if (!s_check_rem(s, 1)) diff --git a/libxrdp/xrdp_rdp.c b/libxrdp/xrdp_rdp.c index 7e68ec1f..fa62c9c2 100644 --- a/libxrdp/xrdp_rdp.c +++ b/libxrdp/xrdp_rdp.c @@ -259,9 +259,22 @@ xrdp_rdp_init_data(struct xrdp_rdp *self, struct stream *s) } /*****************************************************************************/ -/* returns erros */ +/* returns error */ int APP_CC xrdp_rdp_recv(struct xrdp_rdp *self, struct stream *s, int *code) +{ + // Detect TPKT or FastPath + if (xrdp_iso_detect_tpkt(self->sec_layer->mcs_layer->iso_layer, s) == 0) { + return xrdp_rdp_recv_tpkt(self, s, code); + } else { + return xrdp_rdp_recv_fastpath(self, s, code); + } + +} +/*****************************************************************************/ +/* returns error */ +int APP_CC +xrdp_rdp_recv_tpkt(struct xrdp_rdp *self, struct stream *s, int *code) { int error = 0; int len = 0; @@ -338,7 +351,14 @@ xrdp_rdp_recv(struct xrdp_rdp *self, struct stream *s, int *code) return 0; } } - +/*****************************************************************************/ +/* returns error */ +int APP_CC +xrdp_rdp_recv_fastpath(struct xrdp_rdp *self, struct stream *s, int *code) +{ + g_writeln("Booyah!"); + return 0; +} /*****************************************************************************/ int APP_CC xrdp_rdp_send(struct xrdp_rdp *self, struct stream *s, int pdu_type) diff --git a/libxrdp/xrdp_sec.c b/libxrdp/xrdp_sec.c index df2cc653..38ee93f3 100644 --- a/libxrdp/xrdp_sec.c +++ b/libxrdp/xrdp_sec.c @@ -1075,10 +1075,10 @@ xrdp_sec_process_mcs_data(struct xrdp_sec *self) switch (tag) { case SEC_TAG_CLI_INFO: - if (xrdp_sec_process_mcs_cli_info(self, s) != 0) - { - return 1; - } +// if (xrdp_sec_process_mcs_cli_info(self, s) != 0) +// { +// return 1; +// } break; case SEC_TAG_CLI_CRYPT: break; diff --git a/libxrdp/xrdp_tcp.c b/libxrdp/xrdp_tcp.c index 473f3deb..384556ba 100644 --- a/libxrdp/xrdp_tcp.c +++ b/libxrdp/xrdp_tcp.c @@ -56,7 +56,7 @@ xrdp_tcp_init(struct xrdp_tcp *self, struct stream *s) int APP_CC xrdp_tcp_recv(struct xrdp_tcp *self, struct stream *s, int len) { - DEBUG((" in xrdp_tcp_recv, gota get %d bytes", len)); + DEBUG((" in xrdp_tcp_recv, gota get %d bytes", len)); init_stream(s, len); if (trans_force_read_s(self->trans, s, len) != 0) @@ -65,7 +65,7 @@ xrdp_tcp_recv(struct xrdp_tcp *self, struct stream *s, int len) return 1; } - DEBUG((" out xrdp_tcp_recv")); + DEBUG((" out xrdp_tcp_recv")); return 0; }