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