d7f0b8d43f1bd52d61c14e638f54bdde6d910acd
[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     "username",
75     "password",
76     "width",
77     "height",
78     "initial_program",
79     "color_depth",
80     NULL
81 };
82
83 enum ARGS_IDX {
84     IDX_HOSTNAME,
85     IDX_PORT,
86     IDX_USERNAME,
87     IDX_PASSWORD,
88     IDX_WIDTH,
89     IDX_HEIGHT,
90     IDX_INITIAL_PROGRAM,
91     IDX_COLOR_DEPTH
92 };
93
94 boolean rdp_freerdp_pre_connect(freerdp* instance) {
95
96     rdpContext* context = instance->context;
97     guac_client* client = ((rdp_freerdp_context*) context)->client;
98     rdpChannels* channels = context->channels;
99     rdpBitmap* bitmap;
100     rdpGlyph* glyph;
101     rdpPointer* pointer;
102     rdpPrimaryUpdate* primary;
103     CLRCONV* clrconv;
104
105     /* Init color conversion structure */
106     clrconv = xnew(CLRCONV);
107     clrconv->alpha = 1;
108     clrconv->invert = 0;
109     clrconv->rgb555 = 0;
110     clrconv->palette = xnew(rdpPalette);
111     ((rdp_freerdp_context*) context)->clrconv = clrconv;
112
113     /* Init FreeRDP cache */
114     instance->context->cache = cache_new(instance->settings);
115
116     /* Set up bitmap handling */
117     bitmap = xnew(rdpBitmap);
118     bitmap->size = sizeof(guac_rdp_bitmap);
119     bitmap->New = guac_rdp_bitmap_new;
120     bitmap->Free = guac_rdp_bitmap_free;
121     bitmap->Paint = guac_rdp_bitmap_paint;
122     bitmap->Decompress = guac_rdp_bitmap_decompress;
123     bitmap->SetSurface = guac_rdp_bitmap_setsurface;
124     graphics_register_bitmap(context->graphics, bitmap);
125     xfree(bitmap);
126
127     /* Set up glyph handling */
128     glyph = xnew(rdpGlyph);
129     glyph->size = sizeof(guac_rdp_glyph);
130     glyph->New = guac_rdp_glyph_new;
131     glyph->Free = guac_rdp_glyph_free;
132     glyph->Draw = guac_rdp_glyph_draw;
133     glyph->BeginDraw = guac_rdp_glyph_begindraw;
134     glyph->EndDraw = guac_rdp_glyph_enddraw;
135     graphics_register_glyph(context->graphics, glyph);
136     xfree(glyph);
137
138     /* Set up pointer handling */
139     pointer = xnew(rdpPointer);
140     pointer->size = sizeof(guac_rdp_pointer);
141     pointer->New = guac_rdp_pointer_new;
142     pointer->Free = guac_rdp_pointer_free;
143     pointer->Set = guac_rdp_pointer_set;
144     graphics_register_pointer(context->graphics, pointer);
145     xfree(pointer);
146
147     /* Set up GDI */
148     instance->update->EndPaint = guac_rdp_gdi_end_paint;
149     instance->update->Palette = guac_rdp_gdi_palette_update;
150     instance->update->SetBounds = guac_rdp_gdi_set_bounds;
151
152     primary = instance->update->primary;
153     primary->DstBlt = guac_rdp_gdi_dstblt;
154     primary->PatBlt = guac_rdp_gdi_patblt;
155     primary->ScrBlt = guac_rdp_gdi_scrblt;
156     primary->MemBlt = guac_rdp_gdi_memblt;
157     primary->OpaqueRect = guac_rdp_gdi_opaquerect;
158
159     pointer_cache_register_callbacks(instance->update);
160     glyph_cache_register_callbacks(instance->update);
161     brush_cache_register_callbacks(instance->update);
162     bitmap_cache_register_callbacks(instance->update);
163     offscreen_cache_register_callbacks(instance->update);
164     palette_cache_register_callbacks(instance->update);
165
166     /* Init channels (pre-connect) */
167     if (freerdp_channels_pre_connect(channels, instance)) {
168         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
169         guac_socket_flush(client->socket);
170         return false;
171     }
172
173     return true;
174
175 }
176
177 boolean rdp_freerdp_post_connect(freerdp* instance) {
178
179     rdpContext* context = instance->context;
180     guac_client* client = ((rdp_freerdp_context*) context)->client;
181     rdpChannels* channels = instance->context->channels;
182
183     /* Init channels (post-connect) */
184     if (freerdp_channels_post_connect(channels, instance)) {
185         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
186         guac_socket_flush(client->socket);
187         return false;
188     }
189
190     /* Client handlers */
191     client->free_handler = rdp_guac_client_free_handler;
192     client->handle_messages = rdp_guac_client_handle_messages;
193     client->mouse_handler = rdp_guac_client_mouse_handler;
194     client->key_handler = rdp_guac_client_key_handler;
195
196     /* Send size */
197     guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
198             instance->settings->width, instance->settings->height);
199
200     return true;
201
202 }
203
204 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
205     context->channels = freerdp_channels_new();
206 }
207
208 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
209     /* EMPTY */
210 }
211
212 void __guac_rdp_client_load_keymap(guac_client* client,
213         const guac_rdp_keymap* keymap) {
214
215     rdp_guac_client_data* guac_client_data =
216         (rdp_guac_client_data*) client->data;
217     /* Get mapping */
218     const guac_rdp_keysym_desc* mapping = keymap->mapping;
219
220     /* If parent exists, load parent first */
221     if (keymap->parent != NULL)
222         __guac_rdp_client_load_keymap(client, keymap->parent);
223
224     /* Log load */
225     guac_client_log_info(client, "Loading keymap \"%s\"", keymap->name);
226
227     /* Load mapping into keymap */
228     while (mapping->keysym != 0) {
229
230         /* Copy mapping */
231         GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, mapping->keysym) =
232             *mapping;
233
234         /* Next keysym */
235         mapping++;
236
237     }
238
239 }
240
241 int guac_client_init(guac_client* client, int argc, char** argv) {
242
243     rdp_guac_client_data* guac_client_data;
244
245     freerdp* rdp_inst;
246     rdpSettings* settings;
247
248     char* hostname;
249     int port = RDP_DEFAULT_PORT;
250     boolean bitmap_cache;
251
252     if (argc < 8) {
253
254         guac_protocol_send_error(client->socket,
255                 "Wrong argument count received.");
256         guac_socket_flush(client->socket);
257
258         guac_error = GUAC_STATUS_BAD_ARGUMENT;
259         guac_error_message = "Wrong argument count received";
260
261         return 1;
262     }
263
264     /* If port specified, use it */
265     if (argv[IDX_PORT][0] != '\0')
266         port = atoi(argv[IDX_PORT]);
267
268     hostname = argv[IDX_HOSTNAME];
269
270     /* Allocate client data */
271     guac_client_data = malloc(sizeof(rdp_guac_client_data));
272
273     /* Init client */
274     freerdp_channels_global_init();
275     rdp_inst = freerdp_new();
276     rdp_inst->PreConnect = rdp_freerdp_pre_connect;
277     rdp_inst->PostConnect = rdp_freerdp_post_connect;
278
279     /* Allocate FreeRDP context */
280     rdp_inst->context_size = sizeof(rdp_freerdp_context);
281     rdp_inst->ContextNew  = (pContextNew) rdp_freerdp_context_new;
282     rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
283     freerdp_context_new(rdp_inst);
284
285     /* Set settings */
286     settings = rdp_inst->settings;
287
288     /* --no-auth */
289     settings->authentication = false;
290
291     /* --sec rdp */
292     settings->rdp_security = true;
293     settings->tls_security = false;
294     settings->nla_security = false;
295     settings->encryption = true;
296     settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
297     settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
298
299     /* session width */
300     settings->width = 1024;
301     if (argv[IDX_WIDTH][0] != '\0')
302         settings->width = atoi(argv[IDX_WIDTH]);
303     if (settings->width == 0)
304         settings->width = 1024;
305
306     /* session height */
307     settings->height = 768;
308     if (argv[IDX_HEIGHT][0] != '\0')
309         settings->height = atoi(argv[IDX_HEIGHT]);
310     if (settings->height == 0)
311         settings->height = 768;
312
313     /* Set hostname */
314     settings->hostname = strdup(hostname);
315     settings->port = port;
316     settings->window_title = strdup(hostname);
317
318     /* username */
319     settings->username = "guest";
320     if (argv[IDX_USERNAME][0] != '\0')
321         settings->username = strdup (argv[IDX_USERNAME]);
322
323     /* password */
324     if (argv[IDX_PASSWORD][0] != '\0') {
325         settings->password = strdup (argv[IDX_PASSWORD]);
326         settings->autologon = 1;
327     }
328
329     /* initial program */
330     if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
331         settings->shell = strdup (argv[IDX_INITIAL_PROGRAM]);
332
333     /* Order support */
334     bitmap_cache = settings->bitmap_cache;
335     settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
336     settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
337     settings->order_support[NEG_DSTBLT_INDEX] = true;
338     settings->order_support[NEG_PATBLT_INDEX] = false; /* PATBLT not yet supported */
339     settings->order_support[NEG_SCRBLT_INDEX] = true;
340     settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
341     settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
342     settings->order_support[NEG_MULTIDSTBLT_INDEX] = false;
343     settings->order_support[NEG_MULTIPATBLT_INDEX] = false;
344     settings->order_support[NEG_MULTISCRBLT_INDEX] = false;
345     settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = false;
346     settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = false;
347     settings->order_support[NEG_LINETO_INDEX] = false;
348     settings->order_support[NEG_POLYLINE_INDEX] = false;
349     settings->order_support[NEG_MEMBLT_INDEX] = bitmap_cache;
350     settings->order_support[NEG_MEM3BLT_INDEX] = false;
351     settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
352     settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
353     settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
354     settings->order_support[NEG_GLYPH_INDEX_INDEX] = true;
355     settings->order_support[NEG_FAST_INDEX_INDEX] = true;
356     settings->order_support[NEG_FAST_GLYPH_INDEX] = true;
357     settings->order_support[NEG_POLYGON_SC_INDEX] = false;
358     settings->order_support[NEG_POLYGON_CB_INDEX] = false;
359     settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
360     settings->order_support[NEG_ELLIPSE_CB_INDEX] = false;
361
362     /* Store client data */
363     guac_client_data->rdp_inst = rdp_inst;
364     guac_client_data->mouse_button_mask = 0;
365     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
366
367     /* Clear keysym state mapping and keymap */
368     memset(guac_client_data->keysym_state, 0,
369             sizeof(guac_rdp_keysym_state_map));
370
371     memset(guac_client_data->keymap, 0,
372             sizeof(guac_rdp_static_keymap));
373
374     client->data = guac_client_data;
375     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
376
377     /* Load keymap into client */
378     __guac_rdp_client_load_keymap(client, &guac_rdp_keymap_en_us);
379
380     /* Connect to RDP server */
381     if (!freerdp_connect(rdp_inst)) {
382
383         guac_protocol_send_error(client->socket,
384                 "Error connecting to RDP server");
385         guac_socket_flush(client->socket);
386
387         guac_error = GUAC_STATUS_BAD_STATE;
388         guac_error_message = "Error connecting to RDP server";
389
390         return 1;
391     }
392
393     /* Send connection name */
394     guac_protocol_send_name(client->socket, settings->window_title);
395
396     /* Success */
397     return 0;
398
399 }
400