690427c7f3cdec29f6fdd06b62a1c439375ebdef
[libguac-client-rdp.git] / src / client.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is libguac-client-rdp.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2011
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Matt Hortman
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
70 /* Client plugin arguments */
71 const char* GUAC_CLIENT_ARGS[] = {
72     "hostname",
73     "port",
74     "domain",
75     "username",
76     "password",
77     "width",
78     "height",
79     "initial-program",
80     "color-depth",
81     NULL
82 };
83
84 enum ARGS_IDX {
85     IDX_HOSTNAME,
86     IDX_PORT,
87     IDX_DOMAIN,
88     IDX_USERNAME,
89     IDX_PASSWORD,
90     IDX_WIDTH,
91     IDX_HEIGHT,
92     IDX_INITIAL_PROGRAM,
93     IDX_COLOR_DEPTH
94 };
95
96 int __guac_receive_channel_data(freerdp* rdp_inst, int channelId, uint8* data, int size, int flags, int total_size) {
97     return freerdp_channels_data(rdp_inst, channelId, data, size, flags, total_size);
98 }
99
100 boolean rdp_freerdp_pre_connect(freerdp* instance) {
101
102     rdpContext* context = instance->context;
103     guac_client* client = ((rdp_freerdp_context*) context)->client;
104     rdpChannels* channels = context->channels;
105     rdpBitmap* bitmap;
106     rdpGlyph* glyph;
107     rdpPointer* pointer;
108     rdpPrimaryUpdate* primary;
109     CLRCONV* clrconv;
110
111     /* Load clipboard plugin */
112     freerdp_channels_load_plugin(channels, instance->settings, "cliprdr", NULL);
113
114     /* Init color conversion structure */
115     clrconv = xnew(CLRCONV);
116     clrconv->alpha = 1;
117     clrconv->invert = 0;
118     clrconv->rgb555 = 0;
119     clrconv->palette = xnew(rdpPalette);
120     ((rdp_freerdp_context*) context)->clrconv = clrconv;
121
122     /* Init FreeRDP cache */
123     instance->context->cache = cache_new(instance->settings);
124
125     /* Set up bitmap handling */
126     bitmap = xnew(rdpBitmap);
127     bitmap->size = sizeof(guac_rdp_bitmap);
128     bitmap->New = guac_rdp_bitmap_new;
129     bitmap->Free = guac_rdp_bitmap_free;
130     bitmap->Paint = guac_rdp_bitmap_paint;
131     bitmap->Decompress = guac_rdp_bitmap_decompress;
132     bitmap->SetSurface = guac_rdp_bitmap_setsurface;
133     graphics_register_bitmap(context->graphics, bitmap);
134     xfree(bitmap);
135
136     /* Set up glyph handling */
137     glyph = xnew(rdpGlyph);
138     glyph->size = sizeof(guac_rdp_glyph);
139     glyph->New = guac_rdp_glyph_new;
140     glyph->Free = guac_rdp_glyph_free;
141     glyph->Draw = guac_rdp_glyph_draw;
142     glyph->BeginDraw = guac_rdp_glyph_begindraw;
143     glyph->EndDraw = guac_rdp_glyph_enddraw;
144     graphics_register_glyph(context->graphics, glyph);
145     xfree(glyph);
146
147     /* Set up pointer handling */
148     pointer = xnew(rdpPointer);
149     pointer->size = sizeof(guac_rdp_pointer);
150     pointer->New = guac_rdp_pointer_new;
151     pointer->Free = guac_rdp_pointer_free;
152     pointer->Set = guac_rdp_pointer_set;
153     graphics_register_pointer(context->graphics, pointer);
154     xfree(pointer);
155
156     /* Set up GDI */
157     instance->update->EndPaint = guac_rdp_gdi_end_paint;
158     instance->update->Palette = guac_rdp_gdi_palette_update;
159     instance->update->SetBounds = guac_rdp_gdi_set_bounds;
160
161     primary = instance->update->primary;
162     primary->DstBlt = guac_rdp_gdi_dstblt;
163     primary->PatBlt = guac_rdp_gdi_patblt;
164     primary->ScrBlt = guac_rdp_gdi_scrblt;
165     primary->MemBlt = guac_rdp_gdi_memblt;
166     primary->OpaqueRect = guac_rdp_gdi_opaquerect;
167
168     pointer_cache_register_callbacks(instance->update);
169     glyph_cache_register_callbacks(instance->update);
170     brush_cache_register_callbacks(instance->update);
171     bitmap_cache_register_callbacks(instance->update);
172     offscreen_cache_register_callbacks(instance->update);
173     palette_cache_register_callbacks(instance->update);
174
175     /* Init channels (pre-connect) */
176     if (freerdp_channels_pre_connect(channels, instance)) {
177         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
178         guac_socket_flush(client->socket);
179         return false;
180     }
181
182     return true;
183
184 }
185
186 boolean rdp_freerdp_post_connect(freerdp* instance) {
187
188     rdpContext* context = instance->context;
189     guac_client* client = ((rdp_freerdp_context*) context)->client;
190     rdpChannels* channels = instance->context->channels;
191
192     /* Init channels (post-connect) */
193     if (freerdp_channels_post_connect(channels, instance)) {
194         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
195         guac_socket_flush(client->socket);
196         return false;
197     }
198
199     /* Client handlers */
200     client->free_handler = rdp_guac_client_free_handler;
201     client->handle_messages = rdp_guac_client_handle_messages;
202     client->mouse_handler = rdp_guac_client_mouse_handler;
203     client->key_handler = rdp_guac_client_key_handler;
204     client->clipboard_handler = rdp_guac_client_clipboard_handler;
205
206     return true;
207
208 }
209
210 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
211     context->channels = freerdp_channels_new();
212 }
213
214 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
215     /* EMPTY */
216 }
217
218 void __guac_rdp_client_load_keymap(guac_client* client,
219         const guac_rdp_keymap* keymap) {
220
221     rdp_guac_client_data* guac_client_data =
222         (rdp_guac_client_data*) client->data;
223
224     /* Get mapping */
225     const guac_rdp_keysym_desc* mapping = keymap->mapping;
226
227     /* If parent exists, load parent first */
228     if (keymap->parent != NULL)
229         __guac_rdp_client_load_keymap(client, keymap->parent);
230
231     /* Log load */
232     guac_client_log_info(client, "Loading keymap \"%s\"", keymap->name);
233
234     /* Load mapping into keymap */
235     while (mapping->keysym != 0) {
236
237         /* Copy mapping */
238         GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, mapping->keysym) =
239             *mapping;
240
241         /* Next keysym */
242         mapping++;
243
244     }
245
246 }
247
248 int guac_client_init(guac_client* client, int argc, char** argv) {
249
250     rdp_guac_client_data* guac_client_data;
251
252     freerdp* rdp_inst;
253     rdpSettings* settings;
254
255     char* hostname;
256     int port = RDP_DEFAULT_PORT;
257     boolean bitmap_cache;
258
259     /**
260      * Selected server-side keymap. Client will be assumed to also use this
261      * keymap. Keys will be sent to server based on client input on a
262      * best-effort basis.
263      *
264      * Currently hard-coded to en-us-qwerty.
265      */
266     const guac_rdp_keymap* chosen_keymap = &guac_rdp_keymap_en_us;
267
268     if (argc < 9) {
269
270         guac_protocol_send_error(client->socket,
271                 "Wrong argument count received.");
272         guac_socket_flush(client->socket);
273
274         guac_error = GUAC_STATUS_BAD_ARGUMENT;
275         guac_error_message = "Wrong argument count received";
276
277         return 1;
278     }
279
280     /* If port specified, use it */
281     if (argv[IDX_PORT][0] != '\0')
282         port = atoi(argv[IDX_PORT]);
283
284     hostname = argv[IDX_HOSTNAME];
285
286     /* Allocate client data */
287     guac_client_data = malloc(sizeof(rdp_guac_client_data));
288
289     /* Init client */
290     freerdp_channels_global_init();
291     rdp_inst = freerdp_new();
292     rdp_inst->PreConnect = rdp_freerdp_pre_connect;
293     rdp_inst->PostConnect = rdp_freerdp_post_connect;
294     rdp_inst->ReceiveChannelData = __guac_receive_channel_data;
295
296     /* Allocate FreeRDP context */
297     rdp_inst->context_size = sizeof(rdp_freerdp_context);
298     rdp_inst->ContextNew  = (pContextNew) rdp_freerdp_context_new;
299     rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
300     freerdp_context_new(rdp_inst);
301
302     /* Set settings */
303     settings = rdp_inst->settings;
304
305     /* --no-auth */
306     settings->authentication = false;
307
308     /* --sec rdp */
309     settings->rdp_security = true;
310     settings->tls_security = false;
311     settings->nla_security = false;
312     settings->encryption = true;
313     settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
314     settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
315
316     /* session width */
317     settings->width = 1024;
318     if (argv[IDX_WIDTH][0] != '\0')
319         settings->width = atoi(argv[IDX_WIDTH]);
320     if (settings->width == 0)
321         settings->width = 1024;
322
323     /* session height */
324     settings->height = 768;
325     if (argv[IDX_HEIGHT][0] != '\0')
326         settings->height = atoi(argv[IDX_HEIGHT]);
327     if (settings->height == 0)
328         settings->height = 768;
329
330     /* Set hostname */
331     settings->hostname = strdup(hostname);
332     settings->port = port;
333     settings->window_title = strdup(hostname);
334
335     /* Domain */
336     if (argv[IDX_DOMAIN][0] != '\0')
337         settings->domain = strdup(argv[IDX_DOMAIN]);
338
339     /* Username */
340     settings->username = "guest";
341     if (argv[IDX_USERNAME][0] != '\0')
342         settings->username = strdup(argv[IDX_USERNAME]);
343
344     /* Password */
345     if (argv[IDX_PASSWORD][0] != '\0') {
346         settings->password = strdup(argv[IDX_PASSWORD]);
347         settings->autologon = 1;
348     }
349
350     /* Initial program */
351     if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
352         settings->shell = strdup(argv[IDX_INITIAL_PROGRAM]);
353
354     /* Order support */
355     bitmap_cache = settings->bitmap_cache;
356     settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
357     settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
358     settings->order_support[NEG_DSTBLT_INDEX] = true;
359     settings->order_support[NEG_PATBLT_INDEX] = false; /* PATBLT not yet supported */
360     settings->order_support[NEG_SCRBLT_INDEX] = true;
361     settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
362     settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
363     settings->order_support[NEG_MULTIDSTBLT_INDEX] = false;
364     settings->order_support[NEG_MULTIPATBLT_INDEX] = false;
365     settings->order_support[NEG_MULTISCRBLT_INDEX] = false;
366     settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = false;
367     settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = false;
368     settings->order_support[NEG_LINETO_INDEX] = false;
369     settings->order_support[NEG_POLYLINE_INDEX] = false;
370     settings->order_support[NEG_MEMBLT_INDEX] = bitmap_cache;
371     settings->order_support[NEG_MEM3BLT_INDEX] = false;
372     settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
373     settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
374     settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
375     settings->order_support[NEG_GLYPH_INDEX_INDEX] = true;
376     settings->order_support[NEG_FAST_INDEX_INDEX] = true;
377     settings->order_support[NEG_FAST_GLYPH_INDEX] = true;
378     settings->order_support[NEG_POLYGON_SC_INDEX] = false;
379     settings->order_support[NEG_POLYGON_CB_INDEX] = false;
380     settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
381     settings->order_support[NEG_ELLIPSE_CB_INDEX] = false;
382
383     /* Store client data */
384     guac_client_data->rdp_inst = rdp_inst;
385     guac_client_data->mouse_button_mask = 0;
386     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
387     guac_client_data->clipboard = NULL;
388
389     /* Clear keysym state mapping and keymap */
390     memset(guac_client_data->keysym_state, 0,
391             sizeof(guac_rdp_keysym_state_map));
392
393     memset(guac_client_data->keymap, 0,
394             sizeof(guac_rdp_static_keymap));
395
396     client->data = guac_client_data;
397     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
398
399     /* Load keymap into client */
400     __guac_rdp_client_load_keymap(client, chosen_keymap);
401
402     /* Set server-side keymap */
403     settings->kbd_layout = chosen_keymap->freerdp_keyboard_layout; 
404
405     /* Connect to RDP server */
406     if (!freerdp_connect(rdp_inst)) {
407
408         guac_protocol_send_error(client->socket,
409                 "Error connecting to RDP server");
410         guac_socket_flush(client->socket);
411
412         guac_error = GUAC_STATUS_BAD_STATE;
413         guac_error_message = "Error connecting to RDP server";
414
415         return 1;
416     }
417
418     /* Send connection name */
419     guac_protocol_send_name(client->socket, settings->window_title);
420
421     /* Send size */
422     guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
423             settings->width, settings->height);
424
425     /* Create glyph surfaces */
426     guac_client_data->opaque_glyph_surface = cairo_image_surface_create(
427             CAIRO_FORMAT_RGB24, settings->width, settings->height);
428
429     guac_client_data->trans_glyph_surface = cairo_image_surface_create(
430             CAIRO_FORMAT_ARGB32, settings->width, settings->height);
431
432     /* Set default pointer */
433     guac_rdp_pointer_set_default(client);
434
435     /* Success */
436     return 0;
437
438 }
439