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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* Remmina - The GTK+ Remote Desktop Client
* Copyright (C) 2017-2023 Antenore Gatta, Giovanni Panozzo
*
* Initially based on the plugin "Remmina Plugin EXEC", created and written by
* Fabio Castelli (Muflone) <muflone@vbsimple.net>.
*
* 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;
GtkTextBuffer *err;
GtkWidget *sw;
GPid pid;
} RemminaPluginExecData;
static RemminaPluginService *remmina_plugin_service = NULL;
#define REMMINA_PLUGIN_DEBUG(fmt, ...) remmina_plugin_service->_remmina_debug(__func__, fmt, ##__VA_ARGS__)
static void
cb_child_watch( GPid pid, gint status)
{
/* Close pid */
g_spawn_close_pid( pid );
}
static void cb_child_setup(gpointer data){
int pid = getpid();
setpgid(pid, 0);
}
static gboolean
cb_out_watch (GIOChannel *channel, GIOCondition cond, RemminaProtocolWidget *gp)
{
gchar *string;
gsize size;
RemminaPluginExecData *gpdata = GET_PLUGIN_DATA(gp);
if( cond == G_IO_HUP )
{
g_io_channel_unref( channel );
return FALSE;
}
g_io_channel_read_line( channel, &string, &size, NULL, NULL );
gtk_text_buffer_insert_at_cursor( gpdata->log_buffer, string, -1 );
g_free( string );
return TRUE;
}
static gboolean
cb_err_watch (GIOChannel *channel, GIOCondition cond, RemminaProtocolWidget *gp)
{
gchar *string;
gsize size;
RemminaPluginExecData *gpdata = GET_PLUGIN_DATA(gp);
if( cond == G_IO_HUP )
{
g_io_channel_unref( channel );
return FALSE;
}
g_io_channel_read_line( channel, &string, &size, NULL, NULL );
gtk_text_buffer_insert_at_cursor( gpdata->err, string, -1 );
g_free( string );
return TRUE;
}
static void remmina_plugin_exec_init(RemminaProtocolWidget *gp)
{
TRACE_CALL(__func__);
RemminaPluginExecData *gpdata;
REMMINA_PLUGIN_DEBUG("[%s] Plugin init", PLUGIN_NAME);
gpdata = g_new0(RemminaPluginExecData, 1);
g_object_set_data_full(G_OBJECT(gp), "plugin-data", gpdata, g_free);
gpdata->pid = 0;
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;
gint child_stdout, child_stderr;
GtkDialog *dialog;
GIOChannel *out_ch, *err_ch;
REMMINA_PLUGIN_DEBUG("[%s] Plugin run", 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");
if (!cmd) {
gtk_text_buffer_set_text (gpdata->log_buffer,
_("You did not set any command to be executed"), -1);
remmina_plugin_service->protocol_plugin_signal_connection_opened(gp);
return TRUE;
}
g_shell_parse_argv(cmd, NULL, &argv, &error);
if (error) {
gtk_text_buffer_set_text (gpdata->log_buffer, error->message, -1);
remmina_plugin_service->protocol_plugin_signal_connection_opened(gp);
g_error_free(error);
return TRUE;
}
if (remmina_plugin_service->file_get_int(remminafile, "runasync", FALSE)) {
REMMINA_PLUGIN_DEBUG("[%s] Run Async", PLUGIN_NAME);
g_spawn_async_with_pipes( NULL,
argv,
NULL,
G_SPAWN_DO_NOT_REAP_CHILD |
G_SPAWN_SEARCH_PATH_FROM_ENVP |
G_SPAWN_SEARCH_PATH,
cb_child_setup,
NULL,
&child_pid,
NULL,
&child_stdout,
&child_stderr,
&error);
if (error != NULL) {
gtk_text_buffer_set_text (gpdata->log_buffer, error->message, -1);
g_error_free(error);
remmina_plugin_service->protocol_plugin_signal_connection_opened(gp);
return TRUE;
}
g_child_watch_add(child_pid, (GChildWatchFunc)cb_child_watch, gp );
gpdata->pid = child_pid;
/* Create channels that will be used to read data from pipes. */
out_ch = g_io_channel_unix_new(child_stdout);
err_ch = g_io_channel_unix_new(child_stderr);
/* Add watches to channels */
g_io_add_watch(out_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_out_watch, gp );
g_io_add_watch(err_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_err_watch, gp );
}else {
dialog = GTK_DIALOG(gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
_("Warning: Running a command synchronously may cause Remmina not to respond.\nDo you really want to continue?")));
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_DEBUG("[%s] Run Sync", 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_DEBUG("[%s] Command executed", 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_signal_connection_opened(gp);
return TRUE;
}
static gboolean remmina_plugin_exec_close(RemminaProtocolWidget *gp)
{
TRACE_CALL(__func__);
REMMINA_PLUGIN_DEBUG("[%s] Plugin close", PLUGIN_NAME);
RemminaPluginExecData *gpdata = GET_PLUGIN_DATA(gp);
//if async process was started, make sure it's dead if option is selected
RemminaFile* remminafile = remmina_plugin_service->protocol_plugin_get_file(gp);
if (remmina_plugin_service->file_get_int(remminafile, "kill_proc", FALSE)) {
if (gpdata->pid !=0 ){
int pgid = getpgid(gpdata->pid);
if (pgid != 0){
kill(-gpdata->pid, SIGHUP);
}
else{
kill(gpdata->pid, SIGHUP);
}
}
}
remmina_plugin_service->protocol_plugin_signal_connection_closed(gp);
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) Setting tooltip
* g) Validation data pointer, will be passed to the validation callback method.
* h) Validation callback method (Can be NULL. Every entry will be valid then.)
* use following prototype:
* gboolean mysetting_validator_method(gpointer key, gpointer value,
* gpointer validator_data);
* gpointer key is a gchar* containing the setting's name,
* gpointer value contains the value which should be validated,
* gpointer validator_data contains your passed data.
*/
static const RemminaProtocolSetting remmina_plugin_exec_basic_settings[] =
{
{ REMMINA_PROTOCOL_SETTING_TYPE_TEXT, "execcommand", N_("Command"), FALSE, NULL, NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_CHECK, "runasync", N_("Asynchronous execution"), FALSE, NULL, NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_CHECK, "kill_proc", N_("Kill process on disconnect"), FALSE, NULL, NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_END, NULL, NULL, FALSE, NULL, NULL, 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
NULL, // No screenshot support available
NULL, // RCW map event
NULL // RCW unmap event
};
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;
}
|