Add support for ignoring certificate, security, authentication, and pre-connection...
[libguac-client-rdp.git] / src / client.c
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
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/
8  *
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
12  * License.
13  *
14  * The Original Code is libguac-client-rdp.
15  *
16  * The Initial Developer of the Original Code is
17  * Michael Jumper.
18  * Portions created by the Initial Developer are Copyright (C) 2011
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Matt Hortman
23  * David PHAM-VAN <d.pham-van@ulteo.com> Ulteo SAS - http://www.ulteo.com
24  *
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.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <sys/select.h>
43 #include <errno.h>
44
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>
56
57 #include <guacamole/socket.h>
58 #include <guacamole/protocol.h>
59 #include <guacamole/client.h>
60 #include <guacamole/error.h>
61
62 #include "client.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"
68 #include "rdp_gdi.h"
69 #include "default_pointer.h"
70
71 /* Client plugin arguments */
72 const char* GUAC_CLIENT_ARGS[] = {
73     "hostname",
74     "port",
75     "domain",
76     "username",
77     "password",
78     "width",
79     "height",
80     "initial-program",
81     "color-depth",
82     "ignore-certificate",
83     "security",
84     "authentication",
85 #ifdef HAVE_RDPSETTINGS_SECURITY_LAYER_NEGOTIATION
86     "security-layer-negotiation",
87 #endif
88 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_ID
89     "preconnection-id",
90 #endif
91 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_BLOB
92     "preconnection-blob",
93 #endif
94     NULL
95 };
96
97 enum ARGS_IDX {
98     IDX_HOSTNAME,
99     IDX_PORT,
100     IDX_DOMAIN,
101     IDX_USERNAME,
102     IDX_PASSWORD,
103     IDX_WIDTH,
104     IDX_HEIGHT,
105     IDX_INITIAL_PROGRAM,
106     IDX_COLOR_DEPTH,
107     IDX_IGNORE_CERTIFICATE,
108     IDX_SECURITY,
109     IDX_AUTHENTICATION,
110 #ifdef HAVE_RDPSETTINGS_SECURITY_LAYER_NEGOTIATION
111     IDX_SECURITY_LAYER_NEGOTIATION,
112 #endif
113 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_ID
114     IDX_PRECONNECTION_ID,
115 #endif
116 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_BLOB
117     IDX_PRECONNECTION_BLOB,
118 #endif
119     IDX_END_OF_LIST_DUMMY
120 };
121
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);
124 }
125
126 boolean rdp_freerdp_pre_connect(freerdp* instance) {
127
128     rdpContext* context = instance->context;
129     guac_client* client = ((rdp_freerdp_context*) context)->client;
130     rdpChannels* channels = context->channels;
131     rdpBitmap* bitmap;
132     rdpGlyph* glyph;
133     rdpPointer* pointer;
134     rdpPrimaryUpdate* primary;
135     CLRCONV* clrconv;
136
137     /* Load clipboard plugin */
138     freerdp_channels_load_plugin(channels, instance->settings, "cliprdr", NULL);
139
140     /* Init color conversion structure */
141     clrconv = xnew(CLRCONV);
142     clrconv->alpha = 1;
143     clrconv->invert = 0;
144     clrconv->rgb555 = 0;
145     clrconv->palette = xnew(rdpPalette);
146     ((rdp_freerdp_context*) context)->clrconv = clrconv;
147
148     /* Init FreeRDP cache */
149     instance->context->cache = cache_new(instance->settings);
150
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);
160     xfree(bitmap);
161
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);
171     xfree(glyph);
172
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;
181 #endif
182 #ifdef HAVE_RDPPOINTER_SETDEFAULT
183     pointer->SetDefault = guac_rdp_pointer_set_default;
184 #endif
185     graphics_register_pointer(context->graphics, pointer);
186     xfree(pointer);
187
188     /* Set up GDI */
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;
192
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;
199
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);
206
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);
211         return false;
212     }
213
214     return true;
215
216 }
217
218 boolean rdp_freerdp_post_connect(freerdp* instance) {
219
220     rdpContext* context = instance->context;
221     guac_client* client = ((rdp_freerdp_context*) context)->client;
222     rdpChannels* channels = instance->context->channels;
223
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);
228         return false;
229     }
230
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;
237
238     return true;
239
240 }
241
242 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
243     context->channels = freerdp_channels_new();
244 }
245
246 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
247     /* EMPTY */
248 }
249
250 void __guac_rdp_client_load_keymap(guac_client* client,
251         const guac_rdp_keymap* keymap) {
252
253     rdp_guac_client_data* guac_client_data =
254         (rdp_guac_client_data*) client->data;
255
256     /* Get mapping */
257     const guac_rdp_keysym_desc* mapping = keymap->mapping;
258
259     /* If parent exists, load parent first */
260     if (keymap->parent != NULL)
261         __guac_rdp_client_load_keymap(client, keymap->parent);
262
263     /* Log load */
264     guac_client_log_info(client, "Loading keymap \"%s\"", keymap->name);
265
266     /* Load mapping into keymap */
267     while (mapping->keysym != 0) {
268
269         /* Copy mapping */
270         GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, mapping->keysym) =
271             *mapping;
272
273         /* Next keysym */
274         mapping++;
275
276     }
277
278 }
279
280 int guac_client_init(guac_client* client, int argc, char** argv) {
281
282     rdp_guac_client_data* guac_client_data;
283
284     freerdp* rdp_inst;
285     rdpSettings* settings;
286
287     char* hostname;
288     int port = RDP_DEFAULT_PORT;
289     boolean bitmap_cache;
290
291     /**
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
294      * best-effort basis.
295      *
296      * Currently hard-coded to en-us-qwerty.
297      */
298     const guac_rdp_keymap* chosen_keymap = &guac_rdp_keymap_en_us;
299
300     if (argc < 9) {
301
302         guac_protocol_send_error(client->socket,
303                 "Wrong argument count received.");
304         guac_socket_flush(client->socket);
305
306         guac_error = GUAC_STATUS_BAD_ARGUMENT;
307         guac_error_message = "Wrong argument count received";
308
309         return 1;
310     }
311
312     /* If port specified, use it */
313     if (argv[IDX_PORT][0] != '\0')
314         port = atoi(argv[IDX_PORT]);
315
316     hostname = argv[IDX_HOSTNAME];
317
318     /* Allocate client data */
319     guac_client_data = malloc(sizeof(rdp_guac_client_data));
320
321     /* Init client */
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;
327
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);
333
334     /* Set settings */
335     settings = rdp_inst->settings;
336
337     /* --no-auth */
338     settings->authentication = false;
339     if (argv[IDX_AUTHENTICATION][0] != '\0')
340         settings->authentication = (strcmp(argv[IDX_AUTHENTICATION], "true") == 0);
341
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;
346
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;
352         while (*p) {
353             /* skip blanks, and commas */
354             while (*p && (*p==' ' || *p==','))
355                 p++;
356             if (!*p)
357                 break;
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;
368             }
369             while (*p && *p!=' ' && *p!=',')
370                 p++;
371         }
372     }
373
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;
378     }
379
380     /* session width */
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;
386
387     /* session height */
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;
393
394     /* Set hostname */
395     settings->hostname = strdup(hostname);
396     settings->port = port;
397     settings->window_title = strdup(hostname);
398
399     /* Domain */
400     if (argv[IDX_DOMAIN][0] != '\0')
401         settings->domain = strdup(argv[IDX_DOMAIN]);
402
403     /* Username */
404     if (argv[IDX_USERNAME][0] != '\0')
405         settings->username = strdup(argv[IDX_USERNAME]);
406
407     /* Password */
408     if (argv[IDX_PASSWORD][0] != '\0') {
409         settings->password = strdup(argv[IDX_PASSWORD]);
410         settings->autologon = 1;
411     }
412
413     /* Initial program */
414     if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
415         settings->shell = strdup(argv[IDX_INITIAL_PROGRAM]);
416
417     /* Ignore certificate */
418     if (argv[IDX_IGNORE_CERTIFICATE][0] != '\0')
419         settings->ignore_certificate = (strcmp(argv[IDX_IGNORE_CERTIFICATE], "true") == 0);
420
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);
425 #endif
426 #ifdef HAVE_RDPSETTINGS_PRECONNECTION_ID
427     if (argv[IDX_PRECONNECTION_ID][0] != '\0') {
428         settings->preconnection_id = atoi(argv[IDX_PRECONNECTION_ID]);
429     }
430 #endif
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]);
435     }
436 #endif
437
438     /* Order support */
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;
466
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;
472
473     /* Clear keysym state mapping and keymap */
474     memset(guac_client_data->keysym_state, 0,
475             sizeof(guac_rdp_keysym_state_map));
476
477     memset(guac_client_data->keymap, 0,
478             sizeof(guac_rdp_static_keymap));
479
480     client->data = guac_client_data;
481     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
482
483     /* Load keymap into client */
484     __guac_rdp_client_load_keymap(client, chosen_keymap);
485
486     /* Set server-side keymap */
487     settings->kbd_layout = chosen_keymap->freerdp_keyboard_layout; 
488
489     /* Connect to RDP server */
490     if (!freerdp_connect(rdp_inst)) {
491
492         guac_protocol_send_error(client->socket,
493                 "Error connecting to RDP server");
494         guac_socket_flush(client->socket);
495
496         guac_error = GUAC_STATUS_BAD_STATE;
497         guac_error_message = "Error connecting to RDP server";
498
499         return 1;
500     }
501
502     /* Send connection name */
503     guac_protocol_send_name(client->socket, settings->window_title);
504
505     /* Send size */
506     guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
507             settings->width, settings->height);
508
509     /* Create glyph surfaces */
510     guac_client_data->opaque_glyph_surface = cairo_image_surface_create(
511             CAIRO_FORMAT_RGB24, settings->width, settings->height);
512
513     guac_client_data->trans_glyph_surface = cairo_image_surface_create(
514             CAIRO_FORMAT_ARGB32, settings->width, settings->height);
515
516     /* Set default pointer */
517     guac_rdp_set_default_pointer(client);
518
519     /* Success */
520     return 0;
521
522 }
523