Add default no-auth, RDP encryption.
[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/channels/channels.h>
48 #include <freerdp/input.h>
49
50 #include <guacamole/socket.h>
51 #include <guacamole/protocol.h>
52 #include <guacamole/client.h>
53
54 #include "client.h"
55 #include "guac_handlers.h"
56 #include "rdp_keymap.h"
57 #include "rdp_bitmap.h"
58
59 /* Client plugin arguments */
60 const char* GUAC_CLIENT_ARGS[] = {
61     "hostname",
62     "port",
63     NULL
64 };
65
66 boolean rdp_freerdp_pre_connect(freerdp* instance) {
67
68     rdpContext* context = instance->context;
69     guac_client* client = ((rdp_freerdp_context*) context)->client;
70     rdpChannels* channels = context->channels;
71     rdpBitmap* bitmap;
72
73     /* Set up bitmap handling */
74     bitmap = xnew(rdpBitmap);
75     bitmap->size = sizeof(guac_rdp_bitmap);
76     bitmap->New = guac_rdp_bitmap_new;
77     /* bitmap->Free = guac_rdp_bitmap_free; */
78     /* bitmap->Paint = guac_rdp_bitmap_paint; */
79     /* bitmap->Decompress = guac_rdp_bitmap_decompress; */
80     /* bitmap->SetSurface = guac_rdp_bitmap_setsurface; */
81     graphics_register_bitmap(context->graphics, bitmap);
82
83     /* Init channels (pre-connect) */
84     if (freerdp_channels_pre_connect(channels, instance)) {
85         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
86         guac_socket_flush(client->socket);
87         return false;
88     }
89
90     return true;
91
92 }
93
94 boolean rdp_freerdp_post_connect(freerdp* instance) {
95
96     rdpContext* context = instance->context;
97     guac_client* client = ((rdp_freerdp_context*) context)->client;
98     rdpChannels* channels = instance->context->channels;
99
100     /* Init channels (post-connect) */
101     if (freerdp_channels_post_connect(channels, instance)) {
102         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
103         guac_socket_flush(client->socket);
104         return false;
105     }
106
107     /* Client handlers */
108     client->free_handler = rdp_guac_client_free_handler;
109     client->handle_messages = rdp_guac_client_handle_messages;
110     client->mouse_handler = rdp_guac_client_mouse_handler;
111     client->key_handler = rdp_guac_client_key_handler;
112
113     return true;
114
115 }
116
117 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
118     context->channels = freerdp_channels_new();
119 }
120
121 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
122     /* EMPTY */
123 }
124
125 int guac_client_init(guac_client* client, int argc, char** argv) {
126
127     rdp_guac_client_data* guac_client_data;
128
129     freerdp* rdp_inst;
130         rdpSettings* settings;
131
132     char* hostname;
133     int port = RDP_DEFAULT_PORT;
134
135     if (argc < 2) {
136         guac_protocol_send_error(client->socket, "Wrong argument count received.");
137         guac_socket_flush(client->socket);
138         return 1;
139     }
140
141     /* If port specified, use it */
142     if (argv[1][0] != '\0')
143         port = atoi(argv[1]);
144
145     hostname = argv[0];
146
147     /* Allocate client data */
148     guac_client_data = malloc(sizeof(rdp_guac_client_data));
149
150     /* Init client */
151     freerdp_channels_global_init();
152     rdp_inst = freerdp_new();
153     rdp_inst->PreConnect = rdp_freerdp_pre_connect;
154     rdp_inst->PostConnect = rdp_freerdp_post_connect;
155
156     /* Allocate FreeRDP context */
157     rdp_inst->context_size = sizeof(rdp_freerdp_context);
158     rdp_inst->ContextNew  = (pContextNew) rdp_freerdp_context_new;
159     rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
160     freerdp_context_new(rdp_inst);
161
162     /* Set settings */
163     settings = rdp_inst->settings;
164
165     /* --no-auth */
166     settings->authentication = false;
167
168     /* --sec rdp */
169     settings->rdp_security = true;
170     settings->tls_security = false;
171     settings->nla_security = false;
172     settings->encryption = true;
173     settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
174     settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
175
176     /* Default size */
177         settings->width = 1024;
178         settings->height = 768;
179
180     /* Set hostname */
181     settings->hostname = strdup(hostname);
182         settings->window_title = strdup(hostname);
183         settings->username = "guest";
184
185     /* Store client data */
186     guac_client_data->rdp_inst = rdp_inst;
187     guac_client_data->mouse_button_mask = 0;
188     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
189
190     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
191     client->data = guac_client_data;
192
193     /* Connect to RDP server */
194     if (!freerdp_connect(rdp_inst)) {
195         guac_protocol_send_error(client->socket, "Error connecting to RDP server");
196         guac_socket_flush(client->socket);
197         return 1;
198     }
199
200     /* Success */
201     return 0;
202
203 }
204