rdp_file.c 11.3 KB
Newer Older
Antenore Gatta's avatar
Antenore Gatta committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Remmina - The GTK+ Remote Desktop Client
 * Copyright (C) 2010-2011 Vic Lee
 * Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU General Public License in all respects
 *  for all of the code used other than OpenSSL. *  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so. *  If you
 *  do not wish to do so, delete this exception statement from your
 *  version. *  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 *
 */
35

36
#include "common/remmina_plugin.h"
Marc-André Moreau's avatar
Marc-André Moreau committed
37 38
#include "rdp_plugin.h"
#include "rdp_file.h"
39

40
gboolean remmina_rdp_file_import_test(const gchar* from_file)
41
{
42
	TRACE_CALL("__func__");
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	gchar* ext;

	ext = strrchr(from_file, '.');

	if (!ext)
		return FALSE;

	ext++;

	if (g_strcmp0(ext, "RDP") == 0)
		return TRUE;

	if (g_strcmp0(ext, "rdp") == 0)
		return TRUE;

	return FALSE;
59 60
}

61
static void remmina_rdp_file_import_field(RemminaFile* remminafile, const gchar* key, const gchar* value)
llyzs's avatar
llyzs committed
62
{
63
	TRACE_CALL("__func__");
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	if (g_strcmp0(key, "desktopwidth") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "resolution_width", value);
	}
	else if (g_strcmp0(key, "desktopheight") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "resolution_height", value);
	}
	else if (g_strcmp0(key, "session bpp") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "colordepth", value);
	}
	else if (g_strcmp0(key, "keyboardhook") == 0)
	{
		remmina_plugin_service->file_set_int(remminafile, "keyboard_grab", (atoi (value) == 1));
	}
	else if (g_strcmp0(key, "full address") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "server", value);
	}
	else if (g_strcmp0(key, "audiomode") == 0)
	{
		switch (atoi(value))
		{
			case 0:
				remmina_plugin_service->file_set_string(remminafile, "sound", "local");
				break;
			case 1:
				remmina_plugin_service->file_set_string(remminafile, "sound", "remote");
				break;
		}
	}
96 97 98 99
	else if (g_strcmp0(key, "microphone") == 0)
	{
		remmina_plugin_service->file_set_int(remminafile, "microphone", (atoi (value) == 1));
    }
100 101 102 103
	else if (g_strcmp0(key, "redirectprinters") == 0)
	{
		remmina_plugin_service->file_set_int(remminafile, "shareprinter", (atoi (value) == 1));
	}
104 105 106 107
	else if (g_strcmp0(key, "redirectsmartcard") == 0)
	{
		remmina_plugin_service->file_set_int(remminafile, "sharesmartcard", (atoi (value) == 1));
	}
108 109 110 111 112 113 114 115 116 117 118 119
	else if (g_strcmp0(key, "redirectclipboard") == 0)
	{
		remmina_plugin_service->file_set_int(remminafile, "disableclipboard", (atoi (value) != 1));
	}
	else if (g_strcmp0(key, "alternate shell") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "exec", value);
	}
	else if (g_strcmp0(key, "shell working directory") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "execpath", value);
	}
120 121 122 123
	else if (g_strcmp0(key, "loadbalanceinfo") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "loadbalanceinfo", value);
	}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	else if (g_strcmp0(key, "gatewayhostname") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "gateway_server", value);
	}
	else if (g_strcmp0(key, "gatewayusagemethod") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "gatewayusagemethod", value);
	}
	else if (g_strcmp0(key, "gatewaycredentialssource") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "gatewaycredentialssource", value);
	}
	else if (g_strcmp0(key, "gatewayprofileusagemethod") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "gatewayprofileusagemethod", value);
	}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	/* tsclient fields, import only */
	else if (g_strcmp0(key, "client hostname") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "clientname", value);
	}
	else if (g_strcmp0(key, "domain") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "domain", value);
	}
	else if (g_strcmp0(key, "username") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "username", value);
	}
	else if (g_strcmp0(key, "password") == 0)
	{
		remmina_plugin_service->file_set_string(remminafile, "password", value);
	}
llyzs's avatar
llyzs committed
157 158
}

159
static RemminaFile* remmina_rdp_file_import_channel(GIOChannel* channel)
llyzs's avatar
llyzs committed
160
{
161
	TRACE_CALL("__func__");
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	gchar* p;
	const gchar* enc;
	gchar* line = NULL;
	GError* error = NULL;
	gsize bytes_read = 0;
	RemminaFile* remminafile;
	guchar magic[2] = { 0 };

	if (g_io_channel_set_encoding(channel, NULL, &error) != G_IO_STATUS_NORMAL)
	{
		g_print("g_io_channel_set_encoding: %s\n", error->message);
		return NULL;
	}

	/* Try to detect the UTF-16 encoding */
	if (g_io_channel_read_chars(channel, (gchar*) magic, 2, &bytes_read, &error) != G_IO_STATUS_NORMAL)
	{
		g_print("g_io_channel_read_chars: %s\n", error->message);
		return NULL;
	}

	if (magic[0] == 0xFF && magic[1] == 0xFE)
	{
		enc = "UTF-16LE";
	}
	else if (magic[0] == 0xFE && magic[1] == 0xFF)
	{
		enc = "UTF-16BE";
	}
	else
	{
		enc = "UTF-8";
194
		if (g_io_channel_seek_position(channel, 0, G_SEEK_SET, &error) != G_IO_STATUS_NORMAL)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		{
			g_print("g_io_channel_seek: failed\n");
			return NULL;
		}
	}

	if (g_io_channel_set_encoding(channel, enc, &error) != G_IO_STATUS_NORMAL)
	{
		g_print("g_io_channel_set_encoding: %s\n", error->message);
		return NULL;
	}

	remminafile = remmina_plugin_service->file_new();

	while (g_io_channel_read_line(channel, &line, NULL, &bytes_read, &error) == G_IO_STATUS_NORMAL)
	{
		if (line == NULL)
			break;

		line[bytes_read] = '\0';
		p = strchr(line, ':');

		if (p)
		{
			*p++ = '\0';
			p = strchr (p, ':');

			if (p)
			{
				p++;
				remmina_rdp_file_import_field(remminafile, line, p);
			}
		}

		g_free(line);
	}

	if (remmina_plugin_service->file_get_int(remminafile, "resolution_width", 0) > 0 &&
		remmina_plugin_service->file_get_int(remminafile, "resolution_height", 0) > 0)
	{
		p = g_strdup_printf("%ix%i",
			remmina_plugin_service->file_get_int(remminafile, "resolution_width", 0),
			remmina_plugin_service->file_get_int(remminafile, "resolution_height", 0));
		remmina_plugin_service->file_set_string(remminafile, "resolution", p);
		g_free(p);
	}

	remmina_plugin_service->file_set_string(remminafile, "name",
		remmina_plugin_service->file_get_string(remminafile, "server"));
	remmina_plugin_service->file_set_string(remminafile, "protocol", "RDP");

	return remminafile;
llyzs's avatar
llyzs committed
247 248
}

249
RemminaFile* remmina_rdp_file_import(const gchar* from_file)
250
{
251
	TRACE_CALL("__func__");
252 253 254 255 256 257 258 259 260 261 262 263 264
	GIOChannel* channel;
	GError* error = NULL;
	RemminaFile* remminafile;

	channel = g_io_channel_new_file(from_file, "r", &error);

	if (channel == NULL)
	{
		g_print("Failed to import %s: %s\n", from_file, error->message);
		return NULL;
	}

	remminafile = remmina_rdp_file_import_channel(channel);
265
	g_io_channel_shutdown(channel, TRUE, &error);
266 267

	return remminafile;
268 269
}

270
gboolean remmina_rdp_file_export_test(RemminaFile* remminafile)
271
{
272
	TRACE_CALL("__func__");
273 274 275 276
	if (g_strcmp0(remmina_plugin_service->file_get_string(remminafile, "protocol"), "RDP") == 0)
		return TRUE;

	return FALSE;
277 278
}

279
gboolean remmina_rdp_file_export_channel(RemminaFile* remminafile, FILE* fp)
280
{
281
	TRACE_CALL("__func__");
282 283 284 285 286
	gchar* s;
	gchar* p;
	const gchar* cs;

	fprintf(fp, "screen mode id:i:2\r\n");
287 288
	cs = remmina_plugin_service->file_get_string(remminafile, "resolution");
	if (cs)
289
	{
290 291 292 293 294 295 296 297 298
		s = g_strdup(cs);
		p = strchr(s, 'x');
		if (p)
		{
			*p++ = '\0';
			fprintf(fp, "desktopwidth:i:%s\r\n", s);
			fprintf(fp, "desktopheight:i:%s\r\n", p);
		}
		g_free(s);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	}

	fprintf(fp, "session bpp:i:%i\r\n", remmina_plugin_service->file_get_int(remminafile, "colordepth", 8));
	//fprintf(fp, "winposstr:s:0,1,123,34,931,661\r\n");
	fprintf(fp, "compression:i:1\r\n");
	fprintf(fp, "keyboardhook:i:2\r\n");
	fprintf(fp, "displayconnectionbar:i:1\r\n");
	fprintf(fp, "disable wallpaper:i:1\r\n");
	fprintf(fp, "disable full window drag:i:1\r\n");
	fprintf(fp, "allow desktop composition:i:0\r\n");
	fprintf(fp, "allow font smoothing:i:0\r\n");
	fprintf(fp, "disable menu anims:i:1\r\n");
	fprintf(fp, "disable themes:i:0\r\n");
	fprintf(fp, "disable cursor setting:i:0\r\n");
	fprintf(fp, "bitmapcachepersistenable:i:1\r\n");
314 315
	cs = remmina_plugin_service->file_get_string(remminafile, "server");
	fprintf(fp, "full address:s:%s\r\n", cs ? cs : "" );
316 317 318 319 320 321
	if (g_strcmp0(remmina_plugin_service->file_get_string(remminafile, "sound"), "local") == 0)
		fprintf(fp, "audiomode:i:0\r\n");
	else if (g_strcmp0(remmina_plugin_service->file_get_string(remminafile, "sound"), "remote") == 0)
		fprintf(fp, "audiomode:i:1\r\n");
	else
		fprintf(fp, "audiomode:i:2\r\n");
322
	fprintf(fp, "microphone:i:%i\r\n", remmina_plugin_service->file_get_int(remminafile, "microphone", FALSE) ? 1 : 0);
323
	fprintf(fp, "redirectprinters:i:%i\r\n", remmina_plugin_service->file_get_int(remminafile, "shareprinter", FALSE) ? 1 : 0);
324
	fprintf(fp, "redirectsmartcard:i:%i\r\n", remmina_plugin_service->file_get_int(remminafile, "sharesmartcard", FALSE) ? 1 : 0);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	fprintf(fp, "redirectcomports:i:0\r\n");
	fprintf(fp, "redirectsmartcards:i:0\r\n");
	fprintf(fp, "redirectclipboard:i:1\r\n");
	fprintf(fp, "redirectposdevices:i:0\r\n");
	fprintf(fp, "autoreconnection enabled:i:1\r\n");
	fprintf(fp, "authentication level:i:0\r\n");
	fprintf(fp, "prompt for credentials:i:1\r\n");
	fprintf(fp, "negotiate security layer:i:1\r\n");
	fprintf(fp, "remoteapplicationmode:i:0\r\n");
	cs = remmina_plugin_service->file_get_string(remminafile, "exec");
	fprintf(fp, "alternate shell:s:%s\r\n", cs ? cs : "");
	cs = remmina_plugin_service->file_get_string(remminafile, "execpath");
	fprintf(fp, "shell working directory:s:%s\r\n", cs ? cs : "");
	fprintf(fp, "gatewayhostname:s:\r\n");
	fprintf(fp, "gatewayusagemethod:i:4\r\n");
	fprintf(fp, "gatewaycredentialssource:i:4\r\n");
	fprintf(fp, "gatewayprofileusagemethod:i:0\r\n");
342
	fprintf(fp, "precommand:s:\r\n");
343 344 345 346
	fprintf(fp, "promptcredentialonce:i:1\r\n");
	fprintf(fp, "drivestoredirect:s:\r\n");

	return TRUE;
347 348
}

349
gboolean remmina_rdp_file_export(RemminaFile* remminafile, const gchar* to_file)
350
{
351
	TRACE_CALL("__func__");
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	FILE* fp;
	gchar* p;
	gboolean ret;

	p = strrchr (to_file, '.');

	if (p && (g_strcmp0(p + 1, "rdp") == 0 || g_strcmp0(p + 1, "RDP") == 0))
	{
		p = g_strdup(to_file);
	}
	else
	{
		p = g_strdup_printf("%s.rdp", to_file);
	}

	fp = g_fopen(p, "w+");

	if (fp == NULL)
	{
		g_print("Failed to export %s\n", p);
		g_free(p);
		return FALSE;
	}

	g_free(p);
	ret = remmina_rdp_file_export_channel(remminafile, fp);
	fclose(fp);

	return ret;
381 382
}