2aed21418a91293350fec8c15b6f38dc07f9d24d
[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  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <sys/select.h>
42 #include <errno.h>
43
44 #include <freerdp/freerdp.h>
45 #include <freerdp/utils/memory.h>
46 #include <freerdp/cache/bitmap.h>
47 #include <freerdp/cache/brush.h>
48 #include <freerdp/cache/glyph.h>
49 #include <freerdp/cache/palette.h>
50 #include <freerdp/cache/pointer.h>
51 #include <freerdp/cache/offscreen.h>
52 #include <freerdp/channels/channels.h>
53 #include <freerdp/input.h>
54 #include <freerdp/constants.h>
55
56 #include <guacamole/socket.h>
57 #include <guacamole/protocol.h>
58 #include <guacamole/client.h>
59
60 #include "client.h"
61 #include "guac_handlers.h"
62 #include "rdp_keymap.h"
63 #include "rdp_bitmap.h"
64 #include "rdp_glyph.h"
65 #include "rdp_pointer.h"
66 #include "rdp_gdi.h"
67
68 /* Client plugin arguments */
69 const char* GUAC_CLIENT_ARGS[] = {
70     "hostname",
71     "port",
72         "username",
73         "password",
74         "width",
75         "height",
76         "initial_program",
77         "color_depth",
78     NULL
79 };
80
81 enum ARGS_IDX {
82         IDX_HOSTNAME,
83         IDX_PORT,
84         IDX_USERNAME,
85         IDX_PASSWORD,
86         IDX_WIDTH,
87         IDX_HEIGHT,
88         IDX_INITIAL_PROGRAM,
89         IDX_COLOR_DEPTH
90 };
91
92 boolean rdp_freerdp_pre_connect(freerdp* instance) {
93
94     rdpContext* context = instance->context;
95     guac_client* client = ((rdp_freerdp_context*) context)->client;
96     rdpChannels* channels = context->channels;
97     rdpBitmap* bitmap;
98     rdpGlyph* glyph;
99     rdpPointer* pointer;
100     rdpPrimaryUpdate* primary;
101     CLRCONV* clrconv;
102
103     /* Init color conversion structure */
104     clrconv = xnew(CLRCONV);
105     clrconv->alpha = 1;
106     clrconv->invert = 0;
107     clrconv->rgb555 = 0;
108     clrconv->palette = xnew(rdpPalette);
109     ((rdp_freerdp_context*) context)->clrconv = clrconv;
110
111     /* Init FreeRDP cache */
112     instance->context->cache = cache_new(instance->settings);
113
114     /* Set up bitmap handling */
115     bitmap = xnew(rdpBitmap);
116     bitmap->size = sizeof(guac_rdp_bitmap);
117     bitmap->New = guac_rdp_bitmap_new;
118     bitmap->Free = guac_rdp_bitmap_free;
119     bitmap->Paint = guac_rdp_bitmap_paint;
120     bitmap->Decompress = guac_rdp_bitmap_decompress;
121     bitmap->SetSurface = guac_rdp_bitmap_setsurface;
122     graphics_register_bitmap(context->graphics, bitmap);
123
124     /* Set up glyph handling */
125     glyph = xnew(rdpGlyph);
126     glyph->size = sizeof(guac_rdp_glyph);
127     glyph->New = guac_rdp_glyph_new;
128     glyph->Free = guac_rdp_glyph_free;
129     glyph->Draw = guac_rdp_glyph_draw;
130     glyph->BeginDraw = guac_rdp_glyph_begindraw;
131     glyph->EndDraw = guac_rdp_glyph_enddraw;
132     graphics_register_glyph(context->graphics, glyph);
133
134     /* Set up pointer handling */
135     pointer = xnew(rdpPointer);
136     pointer->size = sizeof(guac_rdp_pointer);
137     pointer->New = guac_rdp_pointer_new;
138     pointer->Free = guac_rdp_pointer_free;
139     pointer->Set = guac_rdp_pointer_set;
140     graphics_register_pointer(context->graphics, pointer);
141
142     /* Set up GDI */
143     instance->update->Palette = guac_rdp_gdi_palette_update;
144     instance->update->SetBounds = guac_rdp_gdi_set_bounds;
145
146     primary = instance->update->primary;
147     primary->DstBlt = guac_rdp_gdi_dstblt;
148     primary->PatBlt = guac_rdp_gdi_patblt;
149     primary->ScrBlt = guac_rdp_gdi_scrblt;
150     primary->MemBlt = guac_rdp_gdi_memblt;
151     primary->OpaqueRect = guac_rdp_gdi_opaquerect;
152
153     pointer_cache_register_callbacks(instance->update);
154     glyph_cache_register_callbacks(instance->update);
155     brush_cache_register_callbacks(instance->update);
156     bitmap_cache_register_callbacks(instance->update);
157     offscreen_cache_register_callbacks(instance->update);
158     palette_cache_register_callbacks(instance->update);
159
160     /* Init channels (pre-connect) */
161     if (freerdp_channels_pre_connect(channels, instance)) {
162         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
163         guac_socket_flush(client->socket);
164         return false;
165     }
166
167     return true;
168
169 }
170
171 boolean rdp_freerdp_post_connect(freerdp* instance) {
172
173     rdpContext* context = instance->context;
174     guac_client* client = ((rdp_freerdp_context*) context)->client;
175     rdpChannels* channels = instance->context->channels;
176
177     /* Init channels (post-connect) */
178     if (freerdp_channels_post_connect(channels, instance)) {
179         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
180         guac_socket_flush(client->socket);
181         return false;
182     }
183
184     /* Client handlers */
185     client->free_handler = rdp_guac_client_free_handler;
186     client->handle_messages = rdp_guac_client_handle_messages;
187     client->mouse_handler = rdp_guac_client_mouse_handler;
188     client->key_handler = rdp_guac_client_key_handler;
189
190     /* Send size */
191     guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
192             instance->settings->width, instance->settings->height);
193
194     return true;
195
196 }
197
198 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
199     context->channels = freerdp_channels_new();
200 }
201
202 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
203     /* EMPTY */
204 }
205
206 int guac_client_init(guac_client* client, int argc, char** argv) {
207
208     rdp_guac_client_data* guac_client_data;
209
210     freerdp* rdp_inst;
211         rdpSettings* settings;
212
213     char* hostname;
214     int port = RDP_DEFAULT_PORT;
215     boolean bitmap_cache;
216
217     if (argc < 8) {
218         guac_protocol_send_error(client->socket, "Wrong argument count received.");
219         guac_socket_flush(client->socket);
220         return 1;
221     }
222
223     /* If port specified, use it */
224     if (argv[IDX_PORT][0] != '\0')
225         port = atoi(argv[IDX_PORT]);
226
227     hostname = argv[IDX_HOSTNAME];
228
229     /* Allocate client data */
230     guac_client_data = malloc(sizeof(rdp_guac_client_data));
231
232     /* Init client */
233     freerdp_channels_global_init();
234     rdp_inst = freerdp_new();
235     rdp_inst->PreConnect = rdp_freerdp_pre_connect;
236     rdp_inst->PostConnect = rdp_freerdp_post_connect;
237
238     /* Allocate FreeRDP context */
239     rdp_inst->context_size = sizeof(rdp_freerdp_context);
240     rdp_inst->ContextNew  = (pContextNew) rdp_freerdp_context_new;
241     rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
242     freerdp_context_new(rdp_inst);
243
244     /* Set settings */
245     settings = rdp_inst->settings;
246
247     /* --no-auth */
248     settings->authentication = false;
249
250     /* --sec rdp */
251     settings->rdp_security = true;
252     settings->tls_security = false;
253     settings->nla_security = false;
254     settings->encryption = true;
255     settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
256     settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
257
258     /* session width */
259         settings->width = 1024;
260         if (argv[IDX_WIDTH][0] != '\0')
261                 settings->width = atoi(argv[IDX_WIDTH]);
262         if (settings->width == 0)
263                 settings->width = 1024;
264
265         /* session height */
266         settings->height = 768;
267         if (argv[IDX_HEIGHT][0] != '\0')
268                 settings->height = atoi(argv[IDX_HEIGHT]);
269         if (settings->height == 0)
270                 settings->height = 768;
271
272     /* Set hostname */
273     settings->hostname = strdup(hostname);
274         settings->window_title = strdup(hostname);
275
276         /* username */
277         settings->username = "guest";
278         if (argv[IDX_USERNAME][0] != '\0')
279                 settings->username = strdup (argv[IDX_USERNAME]);
280
281         /* password */
282         if (argv[IDX_PASSWORD][0] != '\0') {
283                 settings->password = strdup (argv[IDX_PASSWORD]);
284                 settings->autologon = 1;
285         }
286
287         /* initial program */
288         if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
289                 settings->shell = strdup (argv[IDX_INITIAL_PROGRAM]);
290
291     /* Order support */
292     bitmap_cache = settings->bitmap_cache;
293     settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
294     settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
295     settings->order_support[NEG_DSTBLT_INDEX] = true;
296     settings->order_support[NEG_PATBLT_INDEX] = true;
297     settings->order_support[NEG_SCRBLT_INDEX] = true;
298     settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
299     settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
300     settings->order_support[NEG_MULTIDSTBLT_INDEX] = false;
301     settings->order_support[NEG_MULTIPATBLT_INDEX] = false;
302     settings->order_support[NEG_MULTISCRBLT_INDEX] = false;
303     settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = false;
304     settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = false;
305     settings->order_support[NEG_LINETO_INDEX] = false;
306     settings->order_support[NEG_POLYLINE_INDEX] = false;
307     settings->order_support[NEG_MEMBLT_INDEX] = bitmap_cache;
308     settings->order_support[NEG_MEM3BLT_INDEX] = false;
309     settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
310     settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
311     settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
312     settings->order_support[NEG_GLYPH_INDEX_INDEX] = false;
313     settings->order_support[NEG_FAST_INDEX_INDEX] = false;
314     settings->order_support[NEG_FAST_GLYPH_INDEX] = false;
315     settings->order_support[NEG_POLYGON_SC_INDEX] = false;
316     settings->order_support[NEG_POLYGON_CB_INDEX] = false;
317     settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
318     settings->order_support[NEG_ELLIPSE_CB_INDEX] = false;
319
320     /* Store client data */
321     guac_client_data->rdp_inst = rdp_inst;
322     guac_client_data->mouse_button_mask = 0;
323     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
324
325     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
326     client->data = guac_client_data;
327
328     /* Connect to RDP server */
329     if (!freerdp_connect(rdp_inst)) {
330         guac_protocol_send_error(client->socket, "Error connecting to RDP server");
331         guac_socket_flush(client->socket);
332         return 1;
333     }
334
335     /* Success */
336     return 0;
337
338 }
339