Welcome to mirror list, hosted at ThFree Co, Russian Federation.

exec_plugin.c « exec « remmina-plugins - gitlab.com/Remmina/Remmina.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9709eb5cf25d0f4950e84596627831cc86a26097 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
96
97
98
99
100
101
102
103
104
105
106
107
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
194
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
/*
 * Remmina - The GTK+ Remote Desktop Client
 * Copyright (C) 2017 Antenore Gatta, 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.
 *
 */

#include "exec_plugin_config.h"

#include "common/remmina_plugin.h"

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

#define GET_PLUGIN_DATA(gp) (RemminaPluginExecData*)g_object_get_data(G_OBJECT(gp), "plugin-data")

typedef struct _RemminaPluginExecData {
	GtkWidget *log_view;
	GtkTextBuffer *log_buffer;
	GtkWidget *sw;
	//pthread_t thread;
} RemminaPluginExecData;

static RemminaPluginService *remmina_plugin_service = NULL;

static void remmina_plugin_exec_init(RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	RemminaPluginExecData *gpdata;

	remmina_plugin_service->log_printf("[%s] Plugin init\n", PLUGIN_NAME);

	gpdata = g_new0(RemminaPluginExecData, 1);
	g_object_set_data_full(G_OBJECT(gp), "plugin-data", gpdata, g_free);

	gpdata->log_view = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(gpdata->log_view), GTK_WRAP_CHAR);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(gpdata->log_view), FALSE);
	gtk_text_view_set_left_margin (GTK_TEXT_VIEW (gpdata->log_view), 20);
	gtk_text_view_set_right_margin (GTK_TEXT_VIEW (gpdata->log_view), 20);
	gpdata->log_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (gpdata->log_view));
	gpdata->sw = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_set_size_request (gpdata->sw, 640, 480);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (gpdata->sw),
			GTK_POLICY_AUTOMATIC,
			GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(gp), gpdata->sw);
	gtk_container_add(GTK_CONTAINER(gpdata->sw), gpdata->log_view);
	gtk_text_buffer_set_text (gpdata->log_buffer, "Remmina Exec Plugin Logger", -1);

	gtk_widget_show_all(gpdata->sw);
}

static gboolean remmina_plugin_exec_run(RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	RemminaFile* remminafile;
	const gchar *cmd;
	gchar *stdout_buffer;
	gchar *stderr_buffer;
	char **argv;
	GError *error = NULL;
	GPid child_pid;
	GtkDialog *dialog;
	gchar *sync_warning;

	remmina_plugin_service->log_printf("[%s] Plugin run\n", PLUGIN_NAME);
	RemminaPluginExecData *gpdata = GET_PLUGIN_DATA(gp);
	remminafile = remmina_plugin_service->protocol_plugin_get_file(gp);

	cmd = remmina_plugin_service->file_get_string(remminafile, "execcommand");

	g_shell_parse_argv(cmd, NULL, &argv, &error);
	if (error) {
		g_warning("%s\n", error->message);
		g_error_free(error);
	}

	if (remmina_plugin_service->file_get_int(remminafile, "runasync", FALSE)) {
		remmina_plugin_service->log_printf("[%s] Run Async\n", PLUGIN_NAME);
		g_spawn_async(  NULL,                           // cwd
				argv,                                   // argv
				NULL,                                   // envp
				G_SPAWN_SEARCH_PATH |
				G_SPAWN_SEARCH_PATH_FROM_ENVP |
				G_SPAWN_DO_NOT_REAP_CHILD,              // flags
				NULL,                                   // child_setup
				NULL,                                   // child_setup user data
				&child_pid,                             // pid location
				&error);                                // error
		/* We should do something with child_pid */
	}else {
		sync_warning =
			_("WARNING! Executing a command synchronously, may hung Remmina.\r"
					"Do you really want to continue?");
		dialog = GTK_DIALOG(gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
					GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, sync_warning));
		gint result = gtk_dialog_run (GTK_DIALOG (dialog));

		switch (result)
		{
			case GTK_RESPONSE_YES:
				break;
			default:
				gtk_widget_destroy(GTK_WIDGET(dialog));
				return FALSE;
				break;
		}
		gtk_widget_destroy(GTK_WIDGET(dialog));
		remmina_plugin_service->log_printf("[%s] Run Sync\n", PLUGIN_NAME);
		g_spawn_sync (NULL,				    // CWD or NULL
				argv,
				NULL,				    // ENVP or NULL
				G_SPAWN_SEARCH_PATH |
				G_SPAWN_SEARCH_PATH_FROM_ENVP,
				NULL,
				NULL,
				&stdout_buffer, 		    // STDOUT
				&stderr_buffer,			    // STDERR
				NULL,				    // Exit status
				&error);
	}
	if (!error) {
		remmina_plugin_service->log_printf("[%s] Command executed\n", PLUGIN_NAME);
		gtk_text_buffer_set_text (gpdata->log_buffer, stdout_buffer, -1);
	}else  {
		g_warning("Command %s exited with error: %s\n", cmd, error->message);
		gtk_text_buffer_set_text (gpdata->log_buffer, error->message, -1);
		g_error_free(error);
	}

	g_strfreev(argv);

	remmina_plugin_service->protocol_plugin_emit_signal(gp, "connect");
	return TRUE;
}

static gboolean remmina_plugin_exec_close(RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	remmina_plugin_service->log_printf("[%s] Plugin close\n", PLUGIN_NAME);
	remmina_plugin_service->protocol_plugin_emit_signal(gp, "disconnect");
	return FALSE;
}

/* Array of RemminaProtocolSetting for basic settings.
 * Each item is composed by:
 * a) RemminaProtocolSettingType for setting type
 * b) Setting name
 * c) Setting description
 * d) Compact disposition
 * e) Values for REMMINA_PROTOCOL_SETTING_TYPE_SELECT or REMMINA_PROTOCOL_SETTING_TYPE_COMBO
 * f) Unused pointer
 */
static const RemminaProtocolSetting remmina_plugin_exec_basic_settings[] =
{
	{ REMMINA_PROTOCOL_SETTING_TYPE_TEXT,	"execcommand",  N_("Command"),  FALSE,  NULL,   NULL},
	{ REMMINA_PROTOCOL_SETTING_TYPE_CHECK,	"runasync", N_("Asynchrounous execution"),  FALSE,  NULL,   NULL},
	{ REMMINA_PROTOCOL_SETTING_TYPE_END,	NULL,	    NULL,	    FALSE,  NULL,   NULL }
};

/* Protocol plugin definition and features */
static RemminaProtocolPlugin remmina_plugin = {
	REMMINA_PLUGIN_TYPE_PROTOCOL,                   // Type
	PLUGIN_NAME,                                    // Name
	PLUGIN_DESCRIPTION,                             // Description
	GETTEXT_PACKAGE,                                // Translation domain
	PLUGIN_VERSION,                                 // Version number
	PLUGIN_APPICON,                                 // Icon for normal connection
	PLUGIN_APPICON,                                 // Icon for SSH connection
	remmina_plugin_exec_basic_settings,             // Array for basic settings
	NULL,                                           // Array for advanced settings
	REMMINA_PROTOCOL_SSH_SETTING_NONE,              // SSH settings type
	NULL,                                           // Array for available features
	remmina_plugin_exec_init,                       // Plugin initialization
	remmina_plugin_exec_run,                        // Plugin open connection
	remmina_plugin_exec_close,                      // Plugin close connection
	NULL,                                           // Query for available features
	NULL,                                           // Call a feature
	NULL,                                           // Send a keystroke    */
};

G_MODULE_EXPORT gboolean remmina_plugin_entry(RemminaPluginService *service)
{
	TRACE_CALL(__func__);
	remmina_plugin_service = service;

	bindtextdomain(GETTEXT_PACKAGE, REMMINA_RUNTIME_LOCALEDIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");

	if (!service->register_plugin((RemminaPlugin*)&remmina_plugin)) {
		return FALSE;
	}

	return TRUE;
}