diff --git a/common/xrdp_client_info.h b/common/xrdp_client_info.h index 2dff7358..effac271 100644 --- a/common/xrdp_client_info.h +++ b/common/xrdp_client_info.h @@ -38,6 +38,7 @@ struct xrdp_client_info int bitmap_cache_version; /* ored 1 = original version, 2 = v2, 4 = v3 */ /* pointer info */ int pointer_cache_entries; + int pointer_flags; /* 0 color, 1 new, 2 no new */ /* other */ int use_bitmap_comp; int use_bitmap_cache; @@ -69,6 +70,9 @@ struct xrdp_client_info int offscreen_cache_size; int offscreen_cache_entries; int rfx; + int nego_sec_layer; /* 0, 1, 2 = RDP security layer, TLS , Negotiate */ + int multimon; /* 0 = deny , 1 = allow */ + /* CAPSETTYPE_RAIL */ int rail_support_level; /* CAPSETTYPE_WINDOW */ @@ -90,7 +94,6 @@ struct xrdp_client_info char orders[32]; int order_flags_ex; int use_bulk_comp; - int pointer_flags; /* 0 color, 1 new, 2 no new */ int use_fast_path; int require_credentials; /* when true, credentials *must* be passed on cmd line */ char client_addr[256]; diff --git a/common/xrdp_constants.h b/common/xrdp_constants.h index 6b1685f5..f24da001 100644 --- a/common/xrdp_constants.h +++ b/common/xrdp_constants.h @@ -25,12 +25,35 @@ /* TCP port for Remote Desktop Protocol */ #define TCP_PORT_RDP 3389 -#define ISO_PDU_CR 0xE0 /* Connection Request */ -#define ISO_PDU_CC 0xD0 /* Connection Confirm */ +#define ISO_PDU_CR 0xE0 /* X.224 Connection Request */ +#define ISO_PDU_CC 0xD0 /* X.224 Connection Confirm */ #define ISO_PDU_DR 0x80 /* Disconnect Request */ #define ISO_PDU_DT 0xF0 /* Data */ #define ISO_PDU_ER 0x70 /* Error */ + +/* RDP Security Negotiation codes */ +#define RDP_NEG_REQ 0x01 +#define RDP_NEG_RSP 0x02 +#define RDP_NEG_FAILURE 0x03 +/* Protocol types codes */ +#define PROTOCOL_RDP 0x0 +#define PROTOCOL_SSL 0x1 +#define PROTOCOL_HYBRID 0x2 +#define PROTOCOL_HYBRID_EX 0x8 +/* Negotiation packet flags */ +#define EXTENDED_CLIENT_DATA_SUPPORTED 0x1 +#define DYNVC_GFX_PROTOCOL_SUPPORTED 0x2 +#define RDP_NEGRSP_RESERVED 0x4 +/* Failure Codes */ +#define SSL_REQUIRED_BY_SERVER 0x1 +#define SSL_NOT_ALLOWED_BY_SERVER 0x2 +#define SSL_CERT_NOT_ON_SERVER 0x3 +#define INCONSISTENT_FLAGS 0x4 +#define HYBRID_REQUIRED_BY_SERVER 0x5 +#define SSL_WITH_USER_AUTH_REQUIRED_BY_SERVER 0x6 + + /* MCS PDU codes */ #define MCS_EDRQ 1 /* Erect Domain Request */ #define MCS_DPUM 8 /* Disconnect Provider Ultimatum */ @@ -72,6 +95,7 @@ #define SEC_TAG_CLI_CRYPT 0xc002 #define SEC_TAG_CLI_CHANNELS 0xc003 #define SEC_TAG_CLI_4 0xc004 +#define SEC_TAG_CLI_MONITOR 0xc005 #define SEC_TAG_PUBKEY 0x0006 #define SEC_TAG_KEYSIG 0x0008 diff --git a/libxrdp/libxrdp.h b/libxrdp/libxrdp.h index 1adeb21c..f72b8b11 100644 --- a/libxrdp/libxrdp.h +++ b/libxrdp/libxrdp.h @@ -49,6 +49,8 @@ struct xrdp_iso { struct xrdp_mcs* mcs_layer; /* owner */ struct xrdp_tcp* tcp_layer; + int requestedProtocol; + int selectedProtocol; }; /* used in mcs */ @@ -59,6 +61,16 @@ struct mcs_channel_item int chanid; }; +/* used in mcs - client monitor data */ +struct mcs_monitor_item +{ + int left; + int top; + int right; + int bottom; + int is_primary; +}; + /* mcs */ struct xrdp_mcs { @@ -69,6 +81,7 @@ struct xrdp_mcs struct stream* client_mcs_data; struct stream* server_mcs_data; struct list* channel_list; + struct list* monitor_list; }; /* sec */ @@ -99,6 +112,7 @@ struct xrdp_sec char pub_sig[64]; char pri_exp[64]; int channel_code; + int multimon; }; /* channel */ @@ -309,7 +323,7 @@ xrdp_mcs_disconnect(struct xrdp_mcs* self); /* xrdp_sec.c */ struct xrdp_sec* APP_CC xrdp_sec_create(struct xrdp_rdp* owner, struct trans* trans, int crypt_level, - int channel_code); + int channel_code, int multimon); void APP_CC xrdp_sec_delete(struct xrdp_sec* self); int APP_CC diff --git a/libxrdp/xrdp_iso.c b/libxrdp/xrdp_iso.c index feba6814..dac31a9c 100644 --- a/libxrdp/xrdp_iso.c +++ b/libxrdp/xrdp_iso.c @@ -2,6 +2,7 @@ * xrdp: A Remote Desktop Protocol server. * * Copyright (C) Jay Sorg 2004-2013 + * Copyright (C) Idan Freiberg 2013 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,12 +51,59 @@ xrdp_iso_delete(struct xrdp_iso *self) /*****************************************************************************/ /* returns error */ static int APP_CC -xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code) +xrdp_iso_recv_rdpnegreq(struct xrdp_iso *self, struct stream *s) { - int ver; + int type; + int flags; int len; - *code = 0; + self->requestedProtocol = 0; + + DEBUG((" in xrdp_iso_recv_rdpnegreq")); + + in_uint8(s, type); + if (type != RDP_NEG_REQ) + { + DEBUG((" xrdp_iso_recv_rdpnegreq: type: %x",type)); + return 1; + } + + in_uint8(s, flags); + if (flags != 0x0) + { + DEBUG((" xrdp_iso_recv_rdpnegreq: flags: %x",flags)); + return 1; + } + + in_uint16_le(s, len); + if (len != 8) // fixed length + { + DEBUG((" xrdp_iso_recv_rdpnegreq: length: %x",len)); + return 1; + } + + in_uint32_le(s, self->requestedProtocol); + + //TODO: think of protocol verification logic +// if (requestedProtocol != PROTOCOL_RDP || PROTOCOL_SSL || PROTOCOL_HYBRID || PROTOCOL_HYBRID_EX) +// { +// DEBUG((" xrdp_iso_recv_rdpnegreq: wrong requestedProtocol: %x",requestedProtocol)); +// return 1; +// } + + DEBUG((" out xrdp_iso_recv_rdpnegreq")); + return 0; +} +/*****************************************************************************/ +/* returns error */ +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 + + *code = 0; // X.224 Packet Type + *len = 0; // X.224 Length Indicator if (xrdp_tcp_recv(self->tcp_layer, s, 4) != 0) { @@ -70,14 +118,14 @@ xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code) } in_uint8s(s, 1); - in_uint16_be(s, len); + in_uint16_be(s, plen); if (len < 4) { return 1; } - if (xrdp_tcp_recv(self->tcp_layer, s, len - 4) != 0) + if (xrdp_tcp_recv(self->tcp_layer, s, plen - 4) != 0) { return 1; } @@ -87,7 +135,7 @@ xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code) return 1; } - in_uint8s(s, 1); + in_uint8(s, *len); in_uint8(s, *code); if (*code == ISO_PDU_DT) @@ -109,25 +157,25 @@ xrdp_iso_recv_msg(struct xrdp_iso *self, struct stream *s, int *code) 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) != 0) + 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 (code != ISO_PDU_DT) + if (code != ISO_PDU_DT || len != 2) { - DEBUG((" out xrdp_iso_recv code != ISO_PDU_DT")); + DEBUG((" out xrdp_iso_recv code != ISO_PDU_DT or length != 2")); return 1; } @@ -137,22 +185,46 @@ xrdp_iso_recv(struct xrdp_iso *self, struct stream *s) /*****************************************************************************/ static int APP_CC -xrdp_iso_send_msg(struct xrdp_iso *self, struct stream *s, int code) +xrdp_iso_send_rdpnegrsp(struct xrdp_iso *self, struct stream *s, int code) { - if (xrdp_tcp_init(self->tcp_layer, s) != 0) + int send_rdpnegdata; + + if (xrdp_tcp_init(self->tcp_layer, s) != 0) { return 1; } - out_uint8(s, 3); + /* TPKT HEADER - 4 bytes */ + out_uint8(s, 3); /* version */ + out_uint8(s, 0); /* RESERVED */ + if (self->selectedProtocol != -1) { + out_uint16_be(s, 19); /* length */ //rdp negotiation happens. + } + else + { + out_uint16_be(s, 11); /* length */ //rdp negotiation doesn't happen. + } + /* ISO LAYER - X.224 - 7 bytes*/ + if (self->selectedProtocol != -1) { + out_uint8(s, 14); /* length */ + } + else + { + out_uint8(s, 6); /* length */ + } + out_uint8(s, code); /* SHOULD BE 0xD for CC */ + out_uint16_be(s, 0); + out_uint16_be(s, 0x1234); out_uint8(s, 0); - out_uint16_be(s, 11); /* length */ - out_uint8(s, 6); - out_uint8(s, code); - out_uint16_le(s, 0); - out_uint16_le(s, 0); - out_uint8(s, 0); - s_mark_end(s); + if (self->selectedProtocol != -1) { + /* RDP_NEG_RSP - 8 bytes*/ + out_uint8(s, RDP_NEG_RSP); + out_uint8(s, EXTENDED_CLIENT_DATA_SUPPORTED); /* flags */ + out_uint16_le(s, 8); /* fixed length */ + out_uint32_le(s, self->selectedProtocol); /* selected protocol */ + } + + s_mark_end(s); if (xrdp_tcp_send(self->tcp_layer, s) != 0) { @@ -161,21 +233,80 @@ xrdp_iso_send_msg(struct xrdp_iso *self, struct stream *s, int code) return 0; } +/*****************************************************************************/ +static int APP_CC +xrdp_iso_send_rdpnegfailure(struct xrdp_iso *self, struct stream *s, int code, 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); + /* RDP_NEG_FAILURE - 8 bytes*/ + out_uint8(s, RDP_NEG_FAILURE); + out_uint8(s, 0); /* no flags available */ + out_uint16_le(s, 8); /* fixed length */ + out_uint32_le(s, failureCode); /* failure code */ + s_mark_end(s); + + if (xrdp_tcp_send(self->tcp_layer, s) != 0) + { + return 1; + } + + return 0; +} +/*****************************************************************************/ +static int APP_CC +xrdp_iso_proccess_nego(struct xrdp_iso *self, struct stream *s) +{ + //TODO: negotiation logic here. + 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) + { + free_stream(s); + return 1; + } + } else { + self->selectedProtocol = PROTOCOL_RDP; + // Send RDP_NEG_RSP back to client + if (xrdp_iso_send_rdpnegrsp(self, s, ISO_PDU_CC) != 0) + { + free_stream(s); + return 1; + } + } + + return 0; +} /*****************************************************************************/ /* returns error */ int APP_CC xrdp_iso_incoming(struct xrdp_iso *self) { int code; + int len; + int requestedProtocol; + int selectedProtocol; struct stream *s; - make_stream(s); init_stream(s, 8192); DEBUG((" in xrdp_iso_incoming")); - if (xrdp_iso_recv_msg(self, s, &code) != 0) + if (xrdp_iso_recv_msg(self, s, &code, &len) != 0) { + DEBUG((" in xrdp_iso_recv_msg error!!")); free_stream(s); return 1; } @@ -186,11 +317,28 @@ xrdp_iso_incoming(struct xrdp_iso *self) return 1; } - if (xrdp_iso_send_msg(self, s, ISO_PDU_CC) != 0) - { - free_stream(s); - return 1; + if (len > 6) { + // Receive RDP_NEG_REQ data + if (xrdp_iso_recv_rdpnegreq(self, s) != 0) + { + free_stream(s); + return 1; + } + // Process negotiation request, should return protocol type. + if (xrdp_iso_proccess_nego(self, s) != 0) + { + free_stream(s); + return 1; + } } + else if (len == 6) { + self->selectedProtocol = -1; //we are not doing negotiation + xrdp_iso_send_rdpnegrsp(self, s, ISO_PDU_CC); + } + else { + DEBUG((" error in xrdp_iso_incoming: unknown length detected")); + } + DEBUG((" out xrdp_iso_incoming")); free_stream(s); diff --git a/libxrdp/xrdp_mcs.c b/libxrdp/xrdp_mcs.c index d110c987..86212bb1 100644 --- a/libxrdp/xrdp_mcs.c +++ b/libxrdp/xrdp_mcs.c @@ -38,6 +38,7 @@ xrdp_mcs_create(struct xrdp_sec *owner, struct trans *trans, self->server_mcs_data = server_mcs_data; self->iso_layer = xrdp_iso_create(self, trans); self->channel_list = list_create(); + self->monitor_list = list_create(); DEBUG((" out xrdp_mcs_create")); return self; } @@ -47,6 +48,7 @@ void APP_CC xrdp_mcs_delete(struct xrdp_mcs *self) { struct mcs_channel_item *channel_item; + struct mcs_monitor_item *monitor_item; int index; int count; @@ -66,6 +68,19 @@ xrdp_mcs_delete(struct xrdp_mcs *self) } list_delete(self->channel_list); + + /* here we have to free the monitor items and anything in them */ + count = self->monitor_list->count; + + for (index = count - 1; index >= 0; index--) + { + monitor_item = (struct mcs_monitor_item *) + list_get_item(self->monitor_list, index); + g_free(monitor_item); + } + + list_delete(self->monitor_list); + xrdp_iso_delete(self->iso_layer); /* make sure we get null pointer exception if struct is used again. */ DEBUG(("xrdp_mcs_delete processed")) @@ -436,6 +451,7 @@ xrdp_mcs_recv_edrq(struct xrdp_mcs *self) int opcode; struct stream *s; + DEBUG((" in xrdp_mcs_recv_edrq")); make_stream(s); init_stream(s, 8192); @@ -485,6 +501,7 @@ xrdp_mcs_recv_edrq(struct xrdp_mcs *self) } free_stream(s); + DEBUG((" out xrdp_mcs_recv_edrq")); return 0; } @@ -496,6 +513,7 @@ xrdp_mcs_recv_aurq(struct xrdp_mcs *self) int opcode; struct stream *s; + DEBUG((" in xrdp_mcs_recv_aurq")); make_stream(s); init_stream(s, 8192); @@ -536,6 +554,7 @@ xrdp_mcs_recv_aurq(struct xrdp_mcs *self) } free_stream(s); + DEBUG((" out xrdp_mcs_recv_aurq")); return 0; } diff --git a/libxrdp/xrdp_rdp.c b/libxrdp/xrdp_rdp.c index da43ed23..3fcdd734 100644 --- a/libxrdp/xrdp_rdp.c +++ b/libxrdp/xrdp_rdp.c @@ -127,6 +127,14 @@ xrdp_rdp_read_config(struct xrdp_client_info *client_info) log_message(LOG_LEVEL_DEBUG,"Info - All channels are disabled"); } } + else if (g_strcasecmp(item, "allow_multimon") == 0) + { + client_info->multimon = text2bool(value); + if (client_info->multimon == 0) + { + log_message(LOG_LEVEL_DEBUG,"Info - Multi monitor server support disabled"); + } + } else if (g_strcasecmp(item, "max_bpp") == 0) { client_info->max_bpp = g_atoi(value); @@ -219,7 +227,7 @@ xrdp_rdp_create(struct xrdp_session *session, struct trans *trans) xrdp_rdp_read_config(&self->client_info); /* create sec layer */ self->sec_layer = xrdp_sec_create(self, trans, self->client_info.crypt_level, - self->client_info.channel_code); + self->client_info.channel_code, self->client_info.multimon); /* default 8 bit v1 color bitmap cache entries and size */ self->client_info.cache1_entries = 600; self->client_info.cache1_size = 256; diff --git a/libxrdp/xrdp_sec.c b/libxrdp/xrdp_sec.c index fbdc99a4..8c4e6a2e 100644 --- a/libxrdp/xrdp_sec.c +++ b/libxrdp/xrdp_sec.c @@ -138,7 +138,7 @@ hex_str_to_bin(char *in, char *out, int out_len) /*****************************************************************************/ struct xrdp_sec *APP_CC xrdp_sec_create(struct xrdp_rdp *owner, struct trans *trans, int crypt_level, - int channel_code) + int channel_code, int multimon) { struct xrdp_sec *self; @@ -168,6 +168,7 @@ xrdp_sec_create(struct xrdp_rdp *owner, struct trans *trans, int crypt_level, } self->channel_code = channel_code; + self->multimon = multimon; if (self->decrypt_rc4_info != NULL) { @@ -950,7 +951,51 @@ xrdp_sec_process_mcs_data_channels(struct xrdp_sec *self, struct stream *s) return 0; } +/*****************************************************************************/ +/* reads the client monitors data */ +static int APP_CC +xrdp_sec_process_mcs_data_monitors(struct xrdp_sec *self, struct stream *s) +{ + int index; + int monitorCount; + int flags; + struct mcs_monitor_item *monitor_item; + DEBUG(("processing monitors data, allow_multimon is %d", self->multimon)); + /* this is an option set in xrdp.ini */ + if (self->multimon != 1) /* are multi-monitors allowed ? */ + { + DEBUG(("[INFO] xrdp_sec_process_mcs_data_monitors: multimon is not allowed, skipping")); + return 0; + } + in_uint32_le(s, flags); /* flags */ + //verify flags - must be 0x0 + if (flags != 0){ + DEBUG(("[ERROR] xrdp_sec_process_mcs_data_monitors: flags MUST be zero, detected: %d", flags)); + return 0; + } + in_uint32_le(s, monitorCount); + //verify monitorCount - max 16 + if (monitorCount > 16){ + DEBUG(("[ERROR] xrdp_sec_process_mcs_data_monitors: max allowed monitors is 16, detected: %d", monitorCount)); + return 0; + } + for (index = 0; index < monitorCount; index++) + { + monitor_item = (struct mcs_monitor_item *) + g_malloc(sizeof(struct mcs_monitor_item), 1); + in_uint32_le(s, monitor_item->left); + in_uint32_le(s, monitor_item->top); + in_uint32_le(s, monitor_item->right); + in_uint32_le(s, monitor_item->bottom); + in_uint32_le(s, monitor_item->is_primary); + list_add_item(self->mcs_layer->monitor_list, (long)monitor_item); + DEBUG(("got monitor: left: %d, top: %d, right: %d, bottom: %d, is primary: %d", + monitor_item->left, monitor_item->top, monitor_item->right, monitor_item->bottom, monitor_item->is_primary)); + } + + return 0; +} /*****************************************************************************/ /* process client mcs data, we need some things in here to create the server mcs data */ @@ -999,6 +1044,9 @@ xrdp_sec_process_mcs_data(struct xrdp_sec *self) break; case SEC_TAG_CLI_4: break; + case SEC_TAG_CLI_MONITOR: + xrdp_sec_process_mcs_data_monitors(self, s); + break; default: g_writeln("error unknown xrdp_sec_process_mcs_data tag %d size %d", tag, size); @@ -1045,13 +1093,28 @@ xrdp_sec_out_mcs_data(struct xrdp_sec *self) out_uint8(s, 0x63); /* c */ out_uint8(s, 0x44); /* D */ out_uint8(s, 0x6e); /* n */ - out_uint16_be(s, 0x80fc + (num_channels_even * 2)); + if (self->mcs_layer->iso_layer->selectedProtocol != -1) { // Check for RDPNEGDATA availability + out_uint16_be(s, 0x80fc + (num_channels_even * 2) + 4); + } + else + { + out_uint16_be(s, 0x80fc + (num_channels_even * 2)); + } out_uint16_le(s, SEC_TAG_SRV_INFO); - out_uint16_le(s, 8); /* len */ + if (self->mcs_layer->iso_layer->selectedProtocol != -1) { + out_uint16_le(s, 12); /* len */ + } + else + { + out_uint16_le(s, 8); /* len */ + } out_uint8(s, 4); /* 4 = rdp5 1 = rdp4 */ out_uint8(s, 0); out_uint8(s, 8); out_uint8(s, 0); + if (self->mcs_layer->iso_layer->selectedProtocol != -1) { + out_uint32_le(s, self->mcs_layer->iso_layer->selectedProtocol); /* clientReqeustedProtocol */ + } out_uint16_le(s, SEC_TAG_SRV_CHANNELS); out_uint16_le(s, 8 + (num_channels_even * 2)); /* len */ out_uint16_le(s, MCS_GLOBAL_CHANNEL); /* 1003, 0x03eb main channel */ diff --git a/xrdp/xrdp.ini b/xrdp/xrdp.ini index 604ac515..e8f4f59d 100644 --- a/xrdp/xrdp.ini +++ b/xrdp/xrdp.ini @@ -34,6 +34,8 @@ tcp_keepalive=yes # You can set the PAM error text in a gateway setup (MAX 256 chars) #pamerrortxt=change your password according to policy at http://url #new_cursors=no +#nego_sec_layer=0 +allow_multimon=true [Logging] LogFile=xrdp.log