remmina_protocol_widget.c 29.5 KB
Newer Older
1 2
/*
 * Remmina - The GTK+ Remote Desktop Client
llyzs's avatar
llyzs committed
3
 * Copyright (C) 2009-2011 Vic Lee
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA.
 */

#include <gtk/gtk.h>
#include <glib/gi18n.h>
23
#include <stdlib.h>
24
#include "config.h"
25 26 27 28 29 30 31
#include "remmina_public.h"
#include "remmina_pref.h"
#include "remmina_ssh.h"
#include "remmina_chat_window.h"
#include "remmina_plugin_manager.h"
#include "remmina_connection_window.h"
#include "remmina_protocol_widget.h"
32 33 34 35 36 37 38

struct _RemminaProtocolWidgetPriv
{
    GtkWidget *init_dialog;

    RemminaFile *remmina_file;
    RemminaProtocolPlugin *plugin;
39
    RemminaProtocolFeature *features;
40 41 42 43

    gint width;
    gint height;
    gboolean scale;
44
    gboolean expand;
45 46 47 48

    gboolean has_error;
    gchar *error_message;
    RemminaSSHTunnel *ssh_tunnel;
49
    RemminaTunnelInitFunc init_func;
50 51 52 53 54

    GtkWidget *chat_window;

    gboolean closed;

55 56
    RemminaHostkeyFunc hostkey_func;
    gpointer hostkey_func_data;
57 58 59 60 61 62 63 64
};

G_DEFINE_TYPE (RemminaProtocolWidget, remmina_protocol_widget, GTK_TYPE_EVENT_BOX)

enum {
    CONNECT_SIGNAL,
    DISCONNECT_SIGNAL,
    DESKTOP_RESIZE_SIGNAL,
65
    UPDATE_ALIGN_SIGNAL,
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 96 97 98 99 100
    LAST_SIGNAL
};

typedef struct _RemminaProtocolWidgetSignalData
{
    RemminaProtocolWidget *gp;
    const gchar *signal_name;
} RemminaProtocolWidgetSignalData;

static guint remmina_protocol_widget_signals[LAST_SIGNAL] = { 0 };

static void
remmina_protocol_widget_class_init (RemminaProtocolWidgetClass *klass)
{
    remmina_protocol_widget_signals[CONNECT_SIGNAL] =
        g_signal_new ("connect",
            G_TYPE_FROM_CLASS (klass),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            G_STRUCT_OFFSET (RemminaProtocolWidgetClass, connect),
            NULL, NULL,
            g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
    remmina_protocol_widget_signals[DISCONNECT_SIGNAL] =
        g_signal_new ("disconnect",
            G_TYPE_FROM_CLASS (klass),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            G_STRUCT_OFFSET (RemminaProtocolWidgetClass, disconnect),
            NULL, NULL,
            g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
    remmina_protocol_widget_signals[DESKTOP_RESIZE_SIGNAL] =
        g_signal_new ("desktop-resize",
            G_TYPE_FROM_CLASS (klass),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            G_STRUCT_OFFSET (RemminaProtocolWidgetClass, desktop_resize),
            NULL, NULL,
            g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
101 102 103 104 105 106 107
    remmina_protocol_widget_signals[UPDATE_ALIGN_SIGNAL] =
        g_signal_new ("update-align",
            G_TYPE_FROM_CLASS (klass),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            G_STRUCT_OFFSET (RemminaProtocolWidgetClass, update_align),
            NULL, NULL,
            g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
}

static void
remmina_protocol_widget_init_cancel (RemminaInitDialog *dialog, gint response_id, RemminaProtocolWidget *gp)
{
    if ((response_id == GTK_RESPONSE_CANCEL || response_id == GTK_RESPONSE_DELETE_EVENT)
        && dialog->mode == REMMINA_INIT_MODE_CONNECTING)
    {
        remmina_protocol_widget_close_connection (gp);
    }
}

static void
remmina_protocol_widget_show_init_dialog (RemminaProtocolWidget *gp, const gchar *name)
{
    if (gp->priv->init_dialog)
    {
        gtk_widget_destroy (gp->priv->init_dialog);
    }
    gp->priv->init_dialog = remmina_init_dialog_new (_("Connecting to '%s'..."), (name ? name : "*"));
    g_signal_connect (G_OBJECT (gp->priv->init_dialog), "response", G_CALLBACK (remmina_protocol_widget_init_cancel), gp);
    gtk_widget_show (gp->priv->init_dialog);
}

static void
remmina_protocol_widget_hide_init_dialog (RemminaProtocolWidget *gp)
{
    if (gp->priv->init_dialog && GTK_IS_WIDGET (gp->priv->init_dialog))
    {
        gtk_widget_destroy (gp->priv->init_dialog);
    }
    gp->priv->init_dialog = NULL;
}

static void
remmina_protocol_widget_destroy (RemminaProtocolWidget *gp, gpointer data)
{
    remmina_protocol_widget_hide_init_dialog (gp);
146
    g_free (gp->priv->features);
147 148 149 150 151 152 153
    g_free (gp->priv->error_message);
    g_free (gp->priv);
}

static void
remmina_protocol_widget_connect (RemminaProtocolWidget *gp, gpointer data)
{
154 155 156 157 158 159
#ifdef HAVE_LIBSSH
    if (gp->priv->ssh_tunnel)
    {
        remmina_ssh_tunnel_cancel_accept (gp->priv->ssh_tunnel);
    }
#endif
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    remmina_protocol_widget_hide_init_dialog (gp);
}

static void
remmina_protocol_widget_disconnect (RemminaProtocolWidget *gp, gpointer data)
{
    remmina_protocol_widget_hide_init_dialog (gp);
}

void
remmina_protocol_widget_grab_focus (RemminaProtocolWidget *gp)
{
    GtkWidget *child;

    child = gtk_bin_get_child (GTK_BIN (gp));
175 176
    if (child)
    {
177
        gtk_widget_set_can_focus (child, TRUE);
178 179
        gtk_widget_grab_focus (child);
    }
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
}

static void
remmina_protocol_widget_init (RemminaProtocolWidget *gp)
{
    RemminaProtocolWidgetPriv *priv;

    priv = g_new0 (RemminaProtocolWidgetPriv, 1);
    gp->priv = priv;

    g_signal_connect (G_OBJECT (gp), "destroy", G_CALLBACK (remmina_protocol_widget_destroy), NULL);
    g_signal_connect (G_OBJECT (gp), "connect", G_CALLBACK (remmina_protocol_widget_connect), NULL);
    g_signal_connect (G_OBJECT (gp), "disconnect", G_CALLBACK (remmina_protocol_widget_disconnect), NULL);
}

void
196
remmina_protocol_widget_open_connection_real (gpointer data)
197 198 199 200
{
    RemminaProtocolWidget *gp = REMMINA_PROTOCOL_WIDGET (data);
    RemminaProtocolPlugin *plugin;
    RemminaFile *remminafile = gp->priv->remmina_file;
201
    RemminaProtocolFeature *feature;
202 203
    gint num_plugin;
    gint num_ssh;
204 205

    /* Locate the protocol plugin */
206 207
    plugin = (RemminaProtocolPlugin *) remmina_plugin_manager_get_plugin (REMMINA_PLUGIN_TYPE_PROTOCOL,
        remmina_file_get_string (remminafile, "protocol"));
208 209
    if (!plugin || !plugin->init || !plugin->open_connection)
    {
210 211
        remmina_protocol_widget_set_error (gp, _("Protocol plugin %s is not installed."),
            remmina_file_get_string (remminafile, "protocol"));
212 213 214 215 216 217 218 219
        remmina_protocol_widget_close_connection (gp);
        return;
    }

    plugin->init (gp);

    gp->priv->plugin = plugin;

220
    for (num_plugin = 0, feature = (RemminaProtocolFeature*) plugin->features; feature && feature->type; num_plugin++, feature++)
221 222
    {
    }
223
    num_ssh = 0;
224 225 226
#ifdef HAVE_LIBSSH
    if (remmina_file_get_int (gp->priv->remmina_file, "ssh_enabled", FALSE))
    {
227
        num_ssh += 2;
228 229
    }
#endif
230
    if (num_plugin + num_ssh == 0)
231 232 233 234 235
    {
        gp->priv->features = NULL;
    }
    else
    {
236
        gp->priv->features = g_new0 (RemminaProtocolFeature, num_plugin + num_ssh + 1);
237
        feature = gp->priv->features;
238 239 240 241 242
        if (plugin->features)
        {
            memcpy (feature, plugin->features, sizeof (RemminaProtocolFeature) * num_plugin);
            feature += num_plugin;
        }
243
#ifdef HAVE_LIBSSH
244
        if (num_ssh)
245 246 247 248 249 250 251 252 253 254 255 256 257
        {
            feature->type = REMMINA_PROTOCOL_FEATURE_TYPE_TOOL;
            feature->id = REMMINA_PROTOCOL_FEATURE_TOOL_SSH;
            feature->opt1 = _("Open Secure Shell in New Terminal...");
            feature->opt2 = "utilities-terminal";
            feature++;

            feature->type = REMMINA_PROTOCOL_FEATURE_TYPE_TOOL;
            feature->id = REMMINA_PROTOCOL_FEATURE_TOOL_SFTP;
            feature->opt1 = _("Open Secure File Transfer...");
            feature->opt2 = "folder-remote";
            feature++;
        }
258
        feature->type = REMMINA_PROTOCOL_FEATURE_TYPE_END;
259 260 261
#endif
    }

262 263 264 265 266 267 268 269 270 271
    if (! plugin->open_connection (gp))
    {
        remmina_protocol_widget_close_connection (gp);
    }
}

void
remmina_protocol_widget_open_connection (RemminaProtocolWidget *gp, RemminaFile *remminafile)
{
    gp->priv->remmina_file = remminafile;
272
    gp->priv->scale =  remmina_file_get_int (remminafile, "scale", FALSE);
273

274
    remmina_protocol_widget_show_init_dialog (gp, remmina_file_get_string (remminafile, "name"));
275

276
    remmina_protocol_widget_open_connection_real (gp);
277 278 279 280 281 282 283
}

gboolean
remmina_protocol_widget_close_connection (RemminaProtocolWidget *gp)
{
    if (!GTK_IS_WIDGET (gp) || gp->priv->closed) return FALSE;
    gp->priv->closed = TRUE;
284
    gdk_keyboard_ungrab (GDK_CURRENT_TIME);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

    if (gp->priv->chat_window)
    {
        gtk_widget_destroy (gp->priv->chat_window);
        gp->priv->chat_window = NULL;
    }

#ifdef HAVE_LIBSSH
    if (gp->priv->ssh_tunnel)
    {
        remmina_ssh_tunnel_free (gp->priv->ssh_tunnel);
        gp->priv->ssh_tunnel = NULL;
    }
#endif

300
    if (!gp->priv->plugin || !gp->priv->plugin->close_connection)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    {
        remmina_protocol_widget_emit_signal (gp, "disconnect");
        return FALSE;
    }

    return gp->priv->plugin->close_connection (gp);
}

static gboolean
remmina_protocol_widget_emit_signal_timeout (gpointer user_data)
{
    RemminaProtocolWidgetSignalData *data = (RemminaProtocolWidgetSignalData*) user_data;
    g_signal_emit_by_name (G_OBJECT (data->gp), data->signal_name);
    g_free (data);
    return FALSE;
}

void
remmina_protocol_widget_emit_signal (RemminaProtocolWidget *gp, const gchar *signal_name)
{
    RemminaProtocolWidgetSignalData *data;

    data = g_new (RemminaProtocolWidgetSignalData, 1);
    data->gp = gp;
    data->signal_name = signal_name;
    TIMEOUT_ADD (0, remmina_protocol_widget_emit_signal_timeout, data);
}

329 330
const RemminaProtocolFeature*
remmina_protocol_widget_get_features (RemminaProtocolWidget *gp)
331
{
332
    return gp->priv->features;
333 334 335 336 337 338 339 340 341 342 343 344 345
}

const gchar*
remmina_protocol_widget_get_domain (RemminaProtocolWidget *gp)
{
    return gp->priv->plugin->domain;
}

gboolean
remmina_protocol_widget_query_feature_by_type (RemminaProtocolWidget *gp, RemminaProtocolFeatureType type)
{
    const RemminaProtocolFeature *feature;

346 347 348 349 350 351 352
#ifdef HAVE_LIBSSH
    if (type == REMMINA_PROTOCOL_FEATURE_TYPE_TOOL &&
        remmina_file_get_int (gp->priv->remmina_file, "ssh_enabled", FALSE))
    {
        return TRUE;
    }
#endif
353
    for (feature = gp->priv->plugin->features; feature && feature->type; feature++)
354
    {
355
        if (feature->type == type) return TRUE;
356
    }
357 358 359 360 361 362 363
    return FALSE;
}

gboolean
remmina_protocol_widget_query_feature_by_ref (RemminaProtocolWidget *gp, const RemminaProtocolFeature *feature)
{
    return gp->priv->plugin->query_feature (gp, feature);
364 365 366
}

void
367
remmina_protocol_widget_call_feature_by_type (RemminaProtocolWidget *gp, RemminaProtocolFeatureType type, gint id)
368
{
369 370 371 372 373 374
    const RemminaProtocolFeature *feature;

    for (feature = gp->priv->plugin->features; feature && feature->type; feature++)
    {
        if (feature->type == type && (id == 0 || feature->id == id))
        {
375
            remmina_protocol_widget_call_feature_by_ref (gp, feature);
376 377
            break;
        }
378
    }
379 380 381 382 383
}

void
remmina_protocol_widget_call_feature_by_ref (RemminaProtocolWidget *gp, const RemminaProtocolFeature *feature)
{
384 385 386 387
    switch (feature->id)
    {
#ifdef HAVE_LIBSSH
    case REMMINA_PROTOCOL_FEATURE_TOOL_SSH:
388 389 390 391 392 393 394
        if (gp->priv->ssh_tunnel)
        {
            remmina_connection_window_open_from_file_full (
                remmina_file_dup_temp_protocol (gp->priv->remmina_file, "SSH"), NULL, gp->priv->ssh_tunnel, NULL);
            return;
        }
        break;
395 396

    case REMMINA_PROTOCOL_FEATURE_TOOL_SFTP:
397 398 399 400 401 402 403
        if (gp->priv->ssh_tunnel)
        {
            remmina_connection_window_open_from_file_full (
                remmina_file_dup_temp_protocol (gp->priv->remmina_file, "SFTP"), NULL, gp->priv->ssh_tunnel, NULL);
            return;
        }
        break;
404 405 406 407
#endif
    default:
        break;
    }
408
    gp->priv->plugin->call_feature (gp, feature);
409 410 411 412 413
}

static gboolean
remmina_protocol_widget_on_key_press (GtkWidget *widget, GdkEventKey *event, RemminaProtocolWidget *gp)
{
414
    if (gp->priv->hostkey_func)
415
    {
416
        return gp->priv->hostkey_func (gp, event->keyval, FALSE, gp->priv->hostkey_func_data);
417 418 419 420 421 422 423
    }
    return FALSE;
}

static gboolean
remmina_protocol_widget_on_key_release (GtkWidget *widget, GdkEventKey *event, RemminaProtocolWidget *gp)
{
424
    if (gp->priv->hostkey_func)
425
    {
426
        return gp->priv->hostkey_func (gp, event->keyval, TRUE, gp->priv->hostkey_func_data);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    }
    return FALSE;
}

void
remmina_protocol_widget_register_hostkey (RemminaProtocolWidget *gp, GtkWidget *widget)
{
    g_signal_connect (G_OBJECT (widget), "key-press-event", G_CALLBACK (remmina_protocol_widget_on_key_press), gp);
    g_signal_connect (G_OBJECT (widget), "key-release-event", G_CALLBACK (remmina_protocol_widget_on_key_release), gp);
}

void
remmina_protocol_widget_set_hostkey_func (RemminaProtocolWidget *gp, RemminaHostkeyFunc func, gpointer data)
{
    gp->priv->hostkey_func = func;
    gp->priv->hostkey_func_data = data;
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
}

#ifdef HAVE_LIBSSH
static gboolean
remmina_protocol_widget_init_tunnel (RemminaProtocolWidget *gp)
{
    RemminaSSHTunnel *tunnel;
    gint ret;

    /* Reuse existing SSH connection if it's reconnecting to destination */
    if (gp->priv->ssh_tunnel == NULL)
    {
        tunnel = remmina_ssh_tunnel_new_from_file (gp->priv->remmina_file);

        THREADS_ENTER
        remmina_init_dialog_set_status (REMMINA_INIT_DIALOG (gp->priv->init_dialog),
            _("Connecting to SSH server %s..."), REMMINA_SSH (tunnel)->server);
        THREADS_LEAVE

        if (!remmina_ssh_init_session (REMMINA_SSH (tunnel)))
        {
            remmina_protocol_widget_set_error (gp, REMMINA_SSH (tunnel)->error);
            remmina_ssh_tunnel_free (tunnel);
            return FALSE;
        }

        ret = remmina_ssh_auth_gui (REMMINA_SSH (tunnel), REMMINA_INIT_DIALOG (gp->priv->init_dialog), TRUE);
        if (ret <= 0)
        {
            if (ret == 0)
            {
                remmina_protocol_widget_set_error (gp, REMMINA_SSH (tunnel)->error);
            }
            remmina_ssh_tunnel_free (tunnel);
            return FALSE;
        }

        gp->priv->ssh_tunnel = tunnel;
    }

    return TRUE;
}
#endif

gchar*
488
remmina_protocol_widget_start_direct_tunnel (RemminaProtocolWidget *gp, gint default_port, gboolean port_plus)
489
{
490
    const gchar *server;
491
    gchar *host, *dest;
492
    gint port;
493

494 495
    server = remmina_file_get_string (gp->priv->remmina_file, "server");
    if (!server)
496 497 498 499
    {
        return g_strdup("");
    }

500
    remmina_public_get_server_port (server, default_port, &host, &port);
501 502

    if (port_plus && port < 100)
503 504
    {
        /* Protocols like VNC supports using instance number :0, :1, etc as port number. */
505
        port += default_port;
506 507 508
    }

#ifdef HAVE_LIBSSH
509
    if (!remmina_file_get_int (gp->priv->remmina_file, "ssh_enabled", FALSE))
510
    {
511 512
        dest = g_strdup_printf ("[%s]:%i", host, port);
        g_free (host);
513 514 515 516 517
        return dest;
    }

    if (!remmina_protocol_widget_init_tunnel (gp))
    {
518
        g_free (host);
519 520 521
        return NULL;
    }

522 523
    THREADS_ENTER
    remmina_init_dialog_set_status (REMMINA_INIT_DIALOG (gp->priv->init_dialog),
524
        _("Connecting to %s through SSH tunnel..."), server);
525 526
    THREADS_LEAVE

527
    if (remmina_file_get_int (gp->priv->remmina_file, "ssh_loopback", FALSE))
528
    {
529 530 531
        g_free (host);
        host = g_strdup ("127.0.0.1");
    }
532

533
    if (!remmina_ssh_tunnel_open (gp->priv->ssh_tunnel, host, port, remmina_pref.sshtunnel_port))
534
    {
535
        g_free (host);
536 537 538 539
        remmina_protocol_widget_set_error (gp, REMMINA_SSH (gp->priv->ssh_tunnel)->error);
        return NULL;
    }

540
    g_free (host);
llyzs's avatar
llyzs committed
541
    return g_strdup_printf ("127.0.0.1:%i", remmina_pref.sshtunnel_port);
542 543 544

#else

545 546
    dest = g_strdup_printf ("[%s]:%i", host, port);
    g_free (host);
547 548 549 550 551
    return dest;

#endif
}

552 553 554 555
gboolean
remmina_protocol_widget_start_reverse_tunnel (RemminaProtocolWidget *gp, gint local_port)
{
#ifdef HAVE_LIBSSH
556
    if (!remmina_file_get_int (gp->priv->remmina_file, "ssh_enabled", FALSE))
557 558 559 560 561 562 563 564 565 566 567
    {
        return TRUE;
    }

    if (!remmina_protocol_widget_init_tunnel (gp))
    {
        return FALSE;
    }

    THREADS_ENTER
    remmina_init_dialog_set_status (REMMINA_INIT_DIALOG (gp->priv->init_dialog),
568
        _("Waiting for an incoming SSH tunnel at port %i..."), remmina_file_get_int (gp->priv->remmina_file, "listenport", 0));
569 570
    THREADS_LEAVE

571
    if (!remmina_ssh_tunnel_reverse (gp->priv->ssh_tunnel, remmina_file_get_int (gp->priv->remmina_file, "listenport", 0), local_port))
572 573 574 575 576 577 578 579 580
    {
        remmina_protocol_widget_set_error (gp, REMMINA_SSH (gp->priv->ssh_tunnel)->error);
        return FALSE;
    }
#endif

    return TRUE;
}

581
gboolean
582
remmina_protocol_widget_ssh_exec (RemminaProtocolWidget *gp, gboolean wait, const gchar *fmt, ...)
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
{
#ifdef HAVE_LIBSSH
    RemminaSSHTunnel *tunnel = gp->priv->ssh_tunnel;
    ssh_channel channel;
    gint status;
    gboolean ret = FALSE;
    gchar *cmd, *ptr;
    va_list args;

    if ((channel = channel_new (REMMINA_SSH (tunnel)->session)) == NULL)
    {
        return FALSE;
    }

    va_start (args, fmt);
    cmd = g_strdup_vprintf (fmt, args);
    va_end (args);

    if (channel_open_session (channel) == SSH_OK &&
        channel_request_exec (channel, cmd) == SSH_OK)
    {
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
        if (wait)
        {
            channel_send_eof (channel);
            status = channel_get_exit_status (channel);
            ptr = strchr (cmd, ' ');
            if (ptr) *ptr = '\0';
            switch (status)
            {
            case 0:
                ret = TRUE;
                break;
            case 127:
                remmina_ssh_set_application_error (REMMINA_SSH (tunnel),
                    _("Command %s not found on SSH server"), cmd);
                break;
            default:
                remmina_ssh_set_application_error (REMMINA_SSH (tunnel),
                    _("Command %s failed on SSH server (status = %i)."), cmd, status);
                break;
            }
        }
        else
626 627 628 629
        {
            ret = TRUE;
        }
    }
630 631 632 633
    else
    {
        remmina_ssh_set_error (REMMINA_SSH (tunnel), _("Failed to execute command: %s"));
    }
634
    g_free (cmd);
635
    if (wait) channel_close (channel);
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    channel_free (channel);
    return ret;

#else

    return FALSE;

#endif
}

#ifdef HAVE_LIBSSH
static gboolean
remmina_protocol_widget_tunnel_init_callback (RemminaSSHTunnel *tunnel, gpointer data)
{
    RemminaProtocolWidget *gp = REMMINA_PROTOCOL_WIDGET (data);
    gchar *server;
    gint port;
    gboolean ret;

655
    remmina_public_get_server_port (remmina_file_get_string (gp->priv->remmina_file, "server"), 177, &server, &port);
656 657
    ret = ((RemminaXPortTunnelInitFunc) gp->priv->init_func) (gp,
        tunnel->remotedisplay, (tunnel->bindlocalhost ? "localhost" : server), port);
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
    g_free (server);

    return ret;
}

static gboolean
remmina_protocol_widget_tunnel_connect_callback (RemminaSSHTunnel *tunnel, gpointer data)
{
    return TRUE;
}

static gboolean
remmina_protocol_widget_tunnel_disconnect_callback (RemminaSSHTunnel *tunnel, gpointer data)
{
    RemminaProtocolWidget *gp = REMMINA_PROTOCOL_WIDGET (data);

    if (REMMINA_SSH (tunnel)->error)
    {
        remmina_protocol_widget_set_error (gp, "%s", REMMINA_SSH (tunnel)->error);
    }
    IDLE_ADD ((GSourceFunc) remmina_protocol_widget_close_connection, gp);
    return TRUE;
}
#endif

gboolean
684
remmina_protocol_widget_start_xport_tunnel (RemminaProtocolWidget *gp, RemminaXPortTunnelInitFunc init_func)
685 686 687 688 689 690 691
{
#ifdef HAVE_LIBSSH
    gboolean bindlocalhost;
    gchar *server;

    if (!remmina_protocol_widget_init_tunnel (gp)) return FALSE;

692 693
    THREADS_ENTER
    remmina_init_dialog_set_status (REMMINA_INIT_DIALOG (gp->priv->init_dialog),
694
        _("Connecting to %s through SSH tunnel..."), remmina_file_get_string (gp->priv->remmina_file, "server"));
695 696
    THREADS_LEAVE

697 698 699 700 701 702
    gp->priv->init_func = init_func;
    gp->priv->ssh_tunnel->init_func = remmina_protocol_widget_tunnel_init_callback;
    gp->priv->ssh_tunnel->connect_func = remmina_protocol_widget_tunnel_connect_callback;
    gp->priv->ssh_tunnel->disconnect_func = remmina_protocol_widget_tunnel_disconnect_callback;
    gp->priv->ssh_tunnel->callback_data = gp;

703
    remmina_public_get_server_port (remmina_file_get_string (gp->priv->remmina_file, "server"), 0, &server, NULL);
704 705 706
    bindlocalhost = (g_strcmp0 (REMMINA_SSH (gp->priv->ssh_tunnel)->server, server) == 0);
    g_free (server);

707
    if (!remmina_ssh_tunnel_xport (gp->priv->ssh_tunnel, bindlocalhost))
708 709 710 711 712 713 714 715 716 717 718 719 720
    {
        remmina_protocol_widget_set_error (gp, "Failed to open channel : %s",
            ssh_get_error (REMMINA_SSH (gp->priv->ssh_tunnel)->session));
        return FALSE;
    }

    return TRUE;

#else
    return FALSE;
#endif
}

721 722 723
void
remmina_protocol_widget_set_display (RemminaProtocolWidget *gp, gint display)
{
724
#ifdef HAVE_LIBSSH
725 726
    if (gp->priv->ssh_tunnel->localdisplay) g_free (gp->priv->ssh_tunnel->localdisplay);
    gp->priv->ssh_tunnel->localdisplay = g_strdup_printf ("unix:%i", display);
727
#endif
728 729
}

730 731 732 733 734 735
GtkWidget*
remmina_protocol_widget_get_init_dialog (RemminaProtocolWidget *gp)
{
    return gp->priv->init_dialog;
}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
gint
remmina_protocol_widget_get_width (RemminaProtocolWidget *gp)
{
    return gp->priv->width;
}

void
remmina_protocol_widget_set_width (RemminaProtocolWidget *gp, gint width)
{
    gp->priv->width = width;
}

gint
remmina_protocol_widget_get_height (RemminaProtocolWidget *gp)
{
    return gp->priv->height;
}

void
remmina_protocol_widget_set_height (RemminaProtocolWidget *gp, gint height)
{
    gp->priv->height = height;
}

gboolean
remmina_protocol_widget_get_scale (RemminaProtocolWidget *gp)
{
    return gp->priv->scale;
}

void
remmina_protocol_widget_set_scale (RemminaProtocolWidget *gp, gboolean scale)
{
    gp->priv->scale = scale;
}

772 773 774 775 776 777 778 779 780 781 782 783
gboolean
remmina_protocol_widget_get_expand (RemminaProtocolWidget *gp)
{
    return gp->priv->expand;
}

void
remmina_protocol_widget_set_expand (RemminaProtocolWidget *gp, gboolean expand)
{
    gp->priv->expand = expand;
}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
gboolean
remmina_protocol_widget_has_error (RemminaProtocolWidget *gp)
{
    return gp->priv->has_error;
}

gchar*
remmina_protocol_widget_get_error_message (RemminaProtocolWidget *gp)
{
    return gp->priv->error_message;
}

void
remmina_protocol_widget_set_error (RemminaProtocolWidget *gp, const gchar *fmt, ...)
{
    va_list args;

    if (gp->priv->error_message) g_free (gp->priv->error_message);

    va_start (args, fmt);
    gp->priv->error_message = g_strdup_vprintf (fmt, args);
    va_end (args);

    gp->priv->has_error = TRUE;
}

gboolean
remmina_protocol_widget_is_closed (RemminaProtocolWidget *gp)
{
    return gp->priv->closed;
}

RemminaFile*
remmina_protocol_widget_get_file (RemminaProtocolWidget *gp)
{
    return gp->priv->remmina_file;
}

gint
823
remmina_protocol_widget_init_authpwd (RemminaProtocolWidget *gp, RemminaAuthpwdType authpwd_type)
824 825 826 827 828
{
    RemminaFile *remminafile = gp->priv->remmina_file;
    gchar *s;
    gint ret;

829 830 831
    switch (authpwd_type)
    {
    case REMMINA_AUTHPWD_TYPE_PROTOCOL:
llyzs's avatar
llyzs committed
832
        s = g_strdup_printf (_("%s password"), remmina_file_get_string (remminafile, "protocol"));
833 834
        break;
    case REMMINA_AUTHPWD_TYPE_SSH_PWD:
llyzs's avatar
llyzs committed
835
        s = g_strdup (_("SSH password"));
836 837
        break;
    case REMMINA_AUTHPWD_TYPE_SSH_PRIVKEY:
llyzs's avatar
llyzs committed
838
        s = g_strdup (_("SSH private key passphrase"));
839 840 841 842 843
        break;
    default:
        s = g_strdup (_("Password"));
        break;
    }
844
    ret = remmina_init_dialog_authpwd (REMMINA_INIT_DIALOG (gp->priv->init_dialog), s, 
845
        remmina_file_get_filename (remminafile) != NULL &&
846 847
        authpwd_type != REMMINA_AUTHPWD_TYPE_SSH_PWD &&
        authpwd_type != REMMINA_AUTHPWD_TYPE_SSH_PRIVKEY);
848 849 850 851 852 853
    g_free (s);

    return ret;
}

gint
llyzs's avatar
llyzs committed
854
remmina_protocol_widget_init_authuserpwd (RemminaProtocolWidget *gp, gboolean want_domain)
855 856 857
{
    RemminaFile *remminafile = gp->priv->remmina_file;

llyzs's avatar
llyzs committed
858 859 860 861
    return remmina_init_dialog_authuserpwd (REMMINA_INIT_DIALOG (gp->priv->init_dialog), want_domain,
        remmina_file_get_string (remminafile, "username"),
        want_domain ? remmina_file_get_string (remminafile, "domain") : NULL,
        (remmina_file_get_filename (remminafile) != NULL));
862 863 864 865 866 867 868 869 870 871 872 873 874 875
}

gchar*
remmina_protocol_widget_init_get_username (RemminaProtocolWidget *gp)
{
    return g_strdup (REMMINA_INIT_DIALOG (gp->priv->init_dialog)->username);
}

gchar*
remmina_protocol_widget_init_get_password (RemminaProtocolWidget *gp)
{
    return g_strdup (REMMINA_INIT_DIALOG (gp->priv->init_dialog)->password);
}

llyzs's avatar
llyzs committed
876 877 878 879 880 881
gchar*
remmina_protocol_widget_init_get_domain (RemminaProtocolWidget *gp)
{
    return g_strdup (REMMINA_INIT_DIALOG (gp->priv->init_dialog)->domain);
}

882 883 884 885 886 887
gint
remmina_protocol_widget_init_authx509 (RemminaProtocolWidget *gp)
{
    RemminaFile *remminafile = gp->priv->remmina_file;

    return remmina_init_dialog_authx509 (REMMINA_INIT_DIALOG (gp->priv->init_dialog),
888 889 890 891
        remmina_file_get_string (remminafile, "cacert"),
        remmina_file_get_string (remminafile, "cacrl"),
        remmina_file_get_string (remminafile, "clientcert"),
        remmina_file_get_string (remminafile, "clientkey"));
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
}

gchar*
remmina_protocol_widget_init_get_cacert (RemminaProtocolWidget *gp)
{
    gchar *s;

    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->cacert;
    return (s && s[0] ? g_strdup (s) : NULL);
}

gchar*
remmina_protocol_widget_init_get_cacrl (RemminaProtocolWidget *gp)
{
    gchar *s;

    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->cacrl;
    return (s && s[0] ? g_strdup (s) : NULL);
}

gchar*
remmina_protocol_widget_init_get_clientcert (RemminaProtocolWidget *gp)
{
    gchar *s;

    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->clientcert;
    return (s && s[0] ? g_strdup (s) : NULL);
}

gchar*
remmina_protocol_widget_init_get_clientkey (RemminaProtocolWidget *gp)
{
    gchar *s;

    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->clientkey;
    return (s && s[0] ? g_strdup (s) : NULL);
}

void
remmina_protocol_widget_init_save_cred (RemminaProtocolWidget *gp)
{
    RemminaFile *remminafile = gp->priv->remmina_file;
    gchar *s;
    gboolean save = FALSE;

    /* Save user name and certificates if any; save the password if it's requested to do so */
    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->username;
    if (s && s[0])
    {
941
        remmina_file_set_string (remminafile, "username", s);
942 943 944 945 946
        save = TRUE;
    }
    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->cacert;
    if (s && s[0])
    {
947
        remmina_file_set_string (remminafile, "cacert", s);
948 949 950 951 952
        save = TRUE;
    }
    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->cacrl;
    if (s && s[0])
    {
953
        remmina_file_set_string (remminafile, "cacrl", s);
954 955 956 957 958
        save = TRUE;
    }
    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->clientcert;
    if (s && s[0])
    {
959
        remmina_file_set_string (remminafile, "clientcert", s);
960 961 962 963 964
        save = TRUE;
    }
    s = REMMINA_INIT_DIALOG (gp->priv->init_dialog)->clientkey;
    if (s && s[0])
    {
965
        remmina_file_set_string (remminafile, "clientkey", s);
966 967 968 969
        save = TRUE;
    }
    if (REMMINA_INIT_DIALOG (gp->priv->init_dialog)->save_password)
    {
970 971
        remmina_file_set_string (remminafile, "password",
            REMMINA_INIT_DIALOG (gp->priv->init_dialog)->password);
972 973 974 975
        save = TRUE;
    }
    if (save)
    {
976
        remmina_file_save_group (remminafile, REMMINA_SETTING_GROUP_CREDENTIAL);
977 978 979 980 981 982 983
    }
}

void
remmina_protocol_widget_init_show_listen (RemminaProtocolWidget *gp, gint port)
{
    remmina_init_dialog_set_status (REMMINA_INIT_DIALOG (gp->priv->init_dialog),
llyzs's avatar
llyzs committed
984
        _("Listening on port %i for an incoming %s connection..."),
985
        port, remmina_file_get_string (gp->priv->remmina_file, "protocol"));
986 987 988 989 990 991 992 993 994
}

void
remmina_protocol_widget_init_show_retry (RemminaProtocolWidget *gp)
{
    remmina_init_dialog_set_status_temp (REMMINA_INIT_DIALOG (gp->priv->init_dialog),
        _("Authentication failed. Trying to reconnect..."));
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
void
remmina_protocol_widget_init_show (RemminaProtocolWidget *gp)
{
    gtk_widget_show (gp->priv->init_dialog);
}

void
remmina_protocol_widget_init_hide (RemminaProtocolWidget *gp)
{
    gtk_widget_hide (gp->priv->init_dialog);
}

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
static void
remmina_protocol_widget_chat_on_destroy (RemminaProtocolWidget *gp)
{
    gp->priv->chat_window = NULL;
}

void
remmina_protocol_widget_chat_open (RemminaProtocolWidget *gp, const gchar *name,
    void(*on_send)(RemminaProtocolWidget *gp, const gchar *text),
    void(*on_destroy)(RemminaProtocolWidget *gp))
{
    if (gp->priv->chat_window)
    {
        gtk_window_present (GTK_WINDOW (gp->priv->chat_window));
    }
    else
    {
        gp->priv->chat_window = remmina_chat_window_new (
            GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (gp))),
            name);
        g_signal_connect_swapped (G_OBJECT (gp->priv->chat_window), "send", G_CALLBACK (on_send), gp);
        g_signal_connect_swapped (G_OBJECT (gp->priv->chat_window), "destroy", G_CALLBACK (remmina_protocol_widget_chat_on_destroy), gp);
        g_signal_connect_swapped (G_OBJECT (gp->priv->chat_window), "destroy", G_CALLBACK (on_destroy), gp);
        gtk_widget_show (gp->priv->chat_window);
    }
}

void
remmina_protocol_widget_chat_close (RemminaProtocolWidget *gp)
{
    if (gp->priv->chat_window)
    {
        gtk_widget_destroy (gp->priv->chat_window);
    }
}

void
remmina_protocol_widget_chat_receive (RemminaProtocolWidget *gp, const gchar *text)
{
    if (gp->priv->chat_window)
    {
        remmina_chat_window_receive (REMMINA_CHAT_WINDOW (gp->priv->chat_window), _("Server"), text);
        gtk_window_present (GTK_WINDOW (gp->priv->chat_window));
    }
}

GtkWidget*
remmina_protocol_widget_new (void)
{
    return GTK_WIDGET (g_object_new (REMMINA_TYPE_PROTOCOL_WIDGET, NULL));
}