1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is libguac-client-rdp.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2011
19 * the Initial Developer. All Rights Reserved.
23 * David PHAM-VAN <d.pham-van@ulteo.com> Ulteo SAS - http://www.ulteo.com
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 #include <sys/select.h>
45 #include <freerdp/freerdp.h>
46 #include <freerdp/utils/memory.h>
47 #include <freerdp/cache/bitmap.h>
48 #include <freerdp/cache/brush.h>
49 #include <freerdp/cache/glyph.h>
50 #include <freerdp/cache/palette.h>
51 #include <freerdp/cache/pointer.h>
52 #include <freerdp/cache/offscreen.h>
53 #include <freerdp/channels/channels.h>
54 #include <freerdp/input.h>
55 #include <freerdp/constants.h>
57 #include <guacamole/socket.h>
58 #include <guacamole/protocol.h>
59 #include <guacamole/client.h>
60 #include <guacamole/error.h>
63 #include "guac_handlers.h"
64 #include "rdp_keymap.h"
65 #include "rdp_bitmap.h"
66 #include "rdp_glyph.h"
67 #include "rdp_pointer.h"
69 #include "default_pointer.h"
71 /* Client plugin arguments */
72 const char* GUAC_CLIENT_ARGS[] = {
85 #ifdef HAVE_RDPSETTINGS_SECURITY_LAYER_NEGOTIATION
86 "security-layer-negotiation",
88 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_ID
91 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_BLOB
107 IDX_IGNORE_CERTIFICATE,
110 #ifdef HAVE_RDPSETTINGS_SECURITY_LAYER_NEGOTIATION
111 IDX_SECURITY_LAYER_NEGOTIATION,
113 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_ID
114 IDX_PRECONNECTION_ID,
116 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_BLOB
117 IDX_PRECONNECTION_BLOB,
119 IDX_END_OF_LIST_DUMMY
122 int __guac_receive_channel_data(freerdp* rdp_inst, int channelId, uint8* data, int size, int flags, int total_size) {
123 return freerdp_channels_data(rdp_inst, channelId, data, size, flags, total_size);
126 boolean rdp_freerdp_pre_connect(freerdp* instance) {
128 rdpContext* context = instance->context;
129 guac_client* client = ((rdp_freerdp_context*) context)->client;
130 rdpChannels* channels = context->channels;
134 rdpPrimaryUpdate* primary;
137 /* Load clipboard plugin */
138 freerdp_channels_load_plugin(channels, instance->settings, "cliprdr", NULL);
140 /* Init color conversion structure */
141 clrconv = xnew(CLRCONV);
145 clrconv->palette = xnew(rdpPalette);
146 ((rdp_freerdp_context*) context)->clrconv = clrconv;
148 /* Init FreeRDP cache */
149 instance->context->cache = cache_new(instance->settings);
151 /* Set up bitmap handling */
152 bitmap = xnew(rdpBitmap);
153 bitmap->size = sizeof(guac_rdp_bitmap);
154 bitmap->New = guac_rdp_bitmap_new;
155 bitmap->Free = guac_rdp_bitmap_free;
156 bitmap->Paint = guac_rdp_bitmap_paint;
157 bitmap->Decompress = guac_rdp_bitmap_decompress;
158 bitmap->SetSurface = guac_rdp_bitmap_setsurface;
159 graphics_register_bitmap(context->graphics, bitmap);
162 /* Set up glyph handling */
163 glyph = xnew(rdpGlyph);
164 glyph->size = sizeof(guac_rdp_glyph);
165 glyph->New = guac_rdp_glyph_new;
166 glyph->Free = guac_rdp_glyph_free;
167 glyph->Draw = guac_rdp_glyph_draw;
168 glyph->BeginDraw = guac_rdp_glyph_begindraw;
169 glyph->EndDraw = guac_rdp_glyph_enddraw;
170 graphics_register_glyph(context->graphics, glyph);
173 /* Set up pointer handling */
174 pointer = xnew(rdpPointer);
175 pointer->size = sizeof(guac_rdp_pointer);
176 pointer->New = guac_rdp_pointer_new;
177 pointer->Free = guac_rdp_pointer_free;
178 pointer->Set = guac_rdp_pointer_set;
179 #ifdef HAVE_RDPPOINTER_SETNULL
180 pointer->SetNull = guac_rdp_pointer_set_null;
182 #ifdef HAVE_RDPPOINTER_SETDEFAULT
183 pointer->SetDefault = guac_rdp_pointer_set_default;
185 graphics_register_pointer(context->graphics, pointer);
189 instance->update->EndPaint = guac_rdp_gdi_end_paint;
190 instance->update->Palette = guac_rdp_gdi_palette_update;
191 instance->update->SetBounds = guac_rdp_gdi_set_bounds;
193 primary = instance->update->primary;
194 primary->DstBlt = guac_rdp_gdi_dstblt;
195 primary->PatBlt = guac_rdp_gdi_patblt;
196 primary->ScrBlt = guac_rdp_gdi_scrblt;
197 primary->MemBlt = guac_rdp_gdi_memblt;
198 primary->OpaqueRect = guac_rdp_gdi_opaquerect;
200 pointer_cache_register_callbacks(instance->update);
201 glyph_cache_register_callbacks(instance->update);
202 brush_cache_register_callbacks(instance->update);
203 bitmap_cache_register_callbacks(instance->update);
204 offscreen_cache_register_callbacks(instance->update);
205 palette_cache_register_callbacks(instance->update);
207 /* Init channels (pre-connect) */
208 if (freerdp_channels_pre_connect(channels, instance)) {
209 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
210 guac_socket_flush(client->socket);
218 boolean rdp_freerdp_post_connect(freerdp* instance) {
220 rdpContext* context = instance->context;
221 guac_client* client = ((rdp_freerdp_context*) context)->client;
222 rdpChannels* channels = instance->context->channels;
224 /* Init channels (post-connect) */
225 if (freerdp_channels_post_connect(channels, instance)) {
226 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
227 guac_socket_flush(client->socket);
231 /* Client handlers */
232 client->free_handler = rdp_guac_client_free_handler;
233 client->handle_messages = rdp_guac_client_handle_messages;
234 client->mouse_handler = rdp_guac_client_mouse_handler;
235 client->key_handler = rdp_guac_client_key_handler;
236 client->clipboard_handler = rdp_guac_client_clipboard_handler;
242 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
243 context->channels = freerdp_channels_new();
246 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
250 void __guac_rdp_client_load_keymap(guac_client* client,
251 const guac_rdp_keymap* keymap) {
253 rdp_guac_client_data* guac_client_data =
254 (rdp_guac_client_data*) client->data;
257 const guac_rdp_keysym_desc* mapping = keymap->mapping;
259 /* If parent exists, load parent first */
260 if (keymap->parent != NULL)
261 __guac_rdp_client_load_keymap(client, keymap->parent);
264 guac_client_log_info(client, "Loading keymap \"%s\"", keymap->name);
266 /* Load mapping into keymap */
267 while (mapping->keysym != 0) {
270 GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, mapping->keysym) =
280 int guac_client_init(guac_client* client, int argc, char** argv) {
282 rdp_guac_client_data* guac_client_data;
285 rdpSettings* settings;
288 int port = RDP_DEFAULT_PORT;
289 boolean bitmap_cache;
292 * Selected server-side keymap. Client will be assumed to also use this
293 * keymap. Keys will be sent to server based on client input on a
296 * Currently hard-coded to en-us-qwerty.
298 const guac_rdp_keymap* chosen_keymap = &guac_rdp_keymap_en_us;
302 guac_protocol_send_error(client->socket,
303 "Wrong argument count received.");
304 guac_socket_flush(client->socket);
306 guac_error = GUAC_STATUS_BAD_ARGUMENT;
307 guac_error_message = "Wrong argument count received";
312 /* If port specified, use it */
313 if (argv[IDX_PORT][0] != '\0')
314 port = atoi(argv[IDX_PORT]);
316 hostname = argv[IDX_HOSTNAME];
318 /* Allocate client data */
319 guac_client_data = malloc(sizeof(rdp_guac_client_data));
322 freerdp_channels_global_init();
323 rdp_inst = freerdp_new();
324 rdp_inst->PreConnect = rdp_freerdp_pre_connect;
325 rdp_inst->PostConnect = rdp_freerdp_post_connect;
326 rdp_inst->ReceiveChannelData = __guac_receive_channel_data;
328 /* Allocate FreeRDP context */
329 rdp_inst->context_size = sizeof(rdp_freerdp_context);
330 rdp_inst->ContextNew = (pContextNew) rdp_freerdp_context_new;
331 rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
332 freerdp_context_new(rdp_inst);
335 settings = rdp_inst->settings;
338 settings->authentication = false;
339 if (argv[IDX_AUTHENTICATION][0] != '\0')
340 settings->authentication = (strcmp(argv[IDX_AUTHENTICATION], "true") == 0);
342 /* --sec rdp - This is a historical default, and differs from xfreerdp*/
343 settings->rdp_security = true;
344 settings->tls_security = false;
345 settings->nla_security = false;
347 if (argv[IDX_SECURITY][0] != '\0') {
348 char * p = argv[IDX_SECURITY];
349 settings->rdp_security = false;
350 settings->tls_security = false;
351 settings->nla_security = false;
353 /* skip blanks, and commas */
354 while (*p && (*p==' ' || *p==','))
358 if (!strncmp(p, "rdp", 3))
359 settings->rdp_security = true;
360 else if (!strncmp(p, "tls", 3))
361 settings->tls_security = true;
362 else if (!strncmp(p, "nla", 3))
363 settings->nla_security = true;
364 else if (!strncmp(p, "all", 3)) {
365 settings->rdp_security = true;
366 settings->tls_security = true;
367 settings->nla_security = true;
369 while (*p && *p!=' ' && *p!=',')
374 if (settings->rdp_security) {
375 settings->encryption = true;
376 settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
377 settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
381 settings->width = 1024;
382 if (argv[IDX_WIDTH][0] != '\0')
383 settings->width = atoi(argv[IDX_WIDTH]);
384 if (settings->width == 0)
385 settings->width = 1024;
388 settings->height = 768;
389 if (argv[IDX_HEIGHT][0] != '\0')
390 settings->height = atoi(argv[IDX_HEIGHT]);
391 if (settings->height == 0)
392 settings->height = 768;
395 settings->hostname = strdup(hostname);
396 settings->port = port;
397 settings->window_title = strdup(hostname);
400 if (argv[IDX_DOMAIN][0] != '\0')
401 settings->domain = strdup(argv[IDX_DOMAIN]);
404 if (argv[IDX_USERNAME][0] != '\0')
405 settings->username = strdup(argv[IDX_USERNAME]);
408 if (argv[IDX_PASSWORD][0] != '\0') {
409 settings->password = strdup(argv[IDX_PASSWORD]);
410 settings->autologon = 1;
413 /* Initial program */
414 if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
415 settings->shell = strdup(argv[IDX_INITIAL_PROGRAM]);
417 /* Ignore certificate */
418 if (argv[IDX_IGNORE_CERTIFICATE][0] != '\0')
419 settings->ignore_certificate = (strcmp(argv[IDX_IGNORE_CERTIFICATE], "true") == 0);
421 #ifdef HAVE_RDPSETTINGS_SECURITY_LAYER_NEGOTIATION
422 settings->security_layer_negotiation = true;
423 if (argv[IDX_SECURITY_LAYER_NEGOTIATION][0] != '\0')
424 settings->security_layer_negotiation = (strcmp(argv[IDX_SECURITY_LAYER_NEGOTIATION], "true") == 0);
426 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_ID
427 if (argv[IDX_PRECONNECTION_ID][0] != '\0') {
428 settings->preconnection_id = atoi(argv[IDX_PRECONNECTION_ID]);
431 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_BLOB
432 if (argv[IDX_PRECONNECTION_BLOB][0] != '\0') {
433 settings->send_preconnection_pdu = true;
434 settings->preconnection_blob = strdup(argv[IDX_PRECONNECTION_BLOB]);
439 bitmap_cache = settings->bitmap_cache;
440 settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
441 settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
442 settings->order_support[NEG_DSTBLT_INDEX] = true;
443 settings->order_support[NEG_PATBLT_INDEX] = false; /* PATBLT not yet supported */
444 settings->order_support[NEG_SCRBLT_INDEX] = true;
445 settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
446 settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
447 settings->order_support[NEG_MULTIDSTBLT_INDEX] = false;
448 settings->order_support[NEG_MULTIPATBLT_INDEX] = false;
449 settings->order_support[NEG_MULTISCRBLT_INDEX] = false;
450 settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = false;
451 settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = false;
452 settings->order_support[NEG_LINETO_INDEX] = false;
453 settings->order_support[NEG_POLYLINE_INDEX] = false;
454 settings->order_support[NEG_MEMBLT_INDEX] = bitmap_cache;
455 settings->order_support[NEG_MEM3BLT_INDEX] = false;
456 settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
457 settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
458 settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
459 settings->order_support[NEG_GLYPH_INDEX_INDEX] = true;
460 settings->order_support[NEG_FAST_INDEX_INDEX] = true;
461 settings->order_support[NEG_FAST_GLYPH_INDEX] = true;
462 settings->order_support[NEG_POLYGON_SC_INDEX] = false;
463 settings->order_support[NEG_POLYGON_CB_INDEX] = false;
464 settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
465 settings->order_support[NEG_ELLIPSE_CB_INDEX] = false;
467 /* Store client data */
468 guac_client_data->rdp_inst = rdp_inst;
469 guac_client_data->mouse_button_mask = 0;
470 guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
471 guac_client_data->clipboard = NULL;
473 /* Clear keysym state mapping and keymap */
474 memset(guac_client_data->keysym_state, 0,
475 sizeof(guac_rdp_keysym_state_map));
477 memset(guac_client_data->keymap, 0,
478 sizeof(guac_rdp_static_keymap));
480 client->data = guac_client_data;
481 ((rdp_freerdp_context*) rdp_inst->context)->client = client;
483 /* Load keymap into client */
484 __guac_rdp_client_load_keymap(client, chosen_keymap);
486 /* Set server-side keymap */
487 settings->kbd_layout = chosen_keymap->freerdp_keyboard_layout;
489 /* Connect to RDP server */
490 if (!freerdp_connect(rdp_inst)) {
492 guac_protocol_send_error(client->socket,
493 "Error connecting to RDP server");
494 guac_socket_flush(client->socket);
496 guac_error = GUAC_STATUS_BAD_STATE;
497 guac_error_message = "Error connecting to RDP server";
502 /* Send connection name */
503 guac_protocol_send_name(client->socket, settings->window_title);
506 guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
507 settings->width, settings->height);
509 /* Create glyph surfaces */
510 guac_client_data->opaque_glyph_surface = cairo_image_surface_create(
511 CAIRO_FORMAT_RGB24, settings->width, settings->height);
513 guac_client_data->trans_glyph_surface = cairo_image_surface_create(
514 CAIRO_FORMAT_ARGB32, settings->width, settings->height);
516 /* Set default pointer */
517 guac_rdp_set_default_pointer(client);