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
341
342
343
344
345
346
347
348
349
350
351
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/*
* Remmina - The GTK+ Remote Desktop Client
* Copyright (C) 2010 Vic Lee
* Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
* Copyright (C) 2016-2021 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 "config.h"
#include "remmina/remmina_trace_calls.h"
#ifdef HAVE_LIBSSH
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "remmina_public.h"
#include "remmina_sftp_client.h"
#include "remmina_plugin_manager.h"
#include "remmina_ssh.h"
#include "remmina_protocol_widget.h"
#include "remmina_sftp_plugin.h"
#define REMMINA_PLUGIN_SFTP_FEATURE_PREF_SHOW_HIDDEN 1
#define REMMINA_PLUGIN_SFTP_FEATURE_PREF_OVERWRITE_ALL 2
#define REMMINA_PLUGIN_SFTP_FEATURE_PREF_RESUME_ALL 3
#define REMMINA_PLUGIN_SFTP_FEATURE_PREF_OVERWRITE_ALL_KEY "overwrite_all"
#define REMMINA_PLUGIN_SFTP_FEATURE_PREF_RESUME_ALL_KEY "resume_all"
#define GET_PLUGIN_DATA(gp) (RemminaPluginSftpData *)g_object_get_data(G_OBJECT(gp), "plugin-data");
typedef struct _RemminaPluginSftpData {
RemminaSFTPClient * client;
pthread_t thread;
RemminaSFTP * sftp;
} RemminaPluginSftpData;
static RemminaPluginService *remmina_plugin_service = NULL;
gboolean remmina_plugin_sftp_start_direct_tunnel(RemminaProtocolWidget *gp, char **phost, int *pport)
{
gchar *hostport;
hostport = remmina_plugin_service->protocol_plugin_start_direct_tunnel(gp, 22, FALSE);
if (hostport == NULL) {
remmina_plugin_service->protocol_plugin_signal_connection_closed(gp);
return FALSE;
}
remmina_plugin_service->get_server_port(hostport, 22, phost, pport);
return TRUE;
}
static gpointer
remmina_plugin_sftp_main_thread(gpointer data)
{
TRACE_CALL(__func__);
RemminaProtocolWidget *gp = (RemminaProtocolWidget *)data;
RemminaPluginSftpData *gpdata;
RemminaFile *remminafile;
RemminaSSH *ssh;
RemminaSFTP *sftp = NULL;
gboolean cont = FALSE;
gint ret;
const gchar *cs;
gchar *host;
int port;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
CANCEL_ASYNC
gpdata = GET_PLUGIN_DATA(gp);
remminafile = remmina_plugin_service->protocol_plugin_get_file(gp);
/* we may need to open a new tunnel too */
host = NULL;
port = 0;
if (!remmina_plugin_sftp_start_direct_tunnel(gp, &host, &port))
return NULL;
ssh = g_object_get_data(G_OBJECT(gp), "user-data");
if (ssh) {
/* Create SFTP connection based on existing SSH session */
sftp = remmina_sftp_new_from_ssh(ssh);
ssh->tunnel_entrance_host = host;
ssh->tunnel_entrance_port = port;
if (remmina_ssh_init_session(REMMINA_SSH(sftp)) &&
remmina_ssh_auth(REMMINA_SSH(sftp), NULL, gp, remminafile) == REMMINA_SSH_AUTH_SUCCESS &&
remmina_sftp_open(sftp))
cont = TRUE;
} else {
/* New SFTP connection */
sftp = remmina_sftp_new_from_file(remminafile);
ssh = REMMINA_SSH(sftp);
ssh->tunnel_entrance_host = host;
ssh->tunnel_entrance_port = port;
while (1) {
if (!remmina_ssh_init_session(ssh)) {
remmina_plugin_service->protocol_plugin_set_error(gp, "%s", ssh->error);
break;
}
ret = remmina_ssh_auth_gui(ssh, gp, remminafile);
if (ret != REMMINA_SSH_AUTH_SUCCESS) {
if (ret == REMMINA_SSH_AUTH_RECONNECT) {
if (ssh->session) {
ssh_disconnect(ssh->session);
ssh_free(ssh->session);
ssh->session = NULL;
}
g_free(ssh->callback);
continue;
}
if (ret != REMMINA_SSH_AUTH_USERCANCEL)
remmina_plugin_service->protocol_plugin_set_error(gp, "%s", ssh->error);
break;
}
if (!remmina_sftp_open(sftp)) {
remmina_plugin_service->protocol_plugin_set_error(gp, "%s", ssh->error);
break;
}
cs = remmina_plugin_service->file_get_string(remminafile, "execpath");
if (cs && cs[0])
remmina_ftp_client_set_dir(REMMINA_FTP_CLIENT(gpdata->client), cs);
cont = TRUE;
break;
}
}
if (!cont) {
if (sftp) remmina_sftp_free(sftp);
remmina_plugin_service->protocol_plugin_signal_connection_closed(gp);
return NULL;
}
remmina_sftp_client_open(REMMINA_SFTP_CLIENT(gpdata->client), sftp);
/* RemminaSFTPClient owns the object, we just take the reference */
gpdata->sftp = sftp;
remmina_plugin_service->protocol_plugin_signal_connection_opened(gp);
gpdata->thread = 0;
return NULL;
}
static void
remmina_plugin_sftp_client_on_realize(GtkWidget *widget, RemminaProtocolWidget *gp)
{
TRACE_CALL(__func__);
RemminaFile *remminafile;
remminafile = remmina_plugin_service->protocol_plugin_get_file(gp);
remmina_ftp_client_load_state(REMMINA_FTP_CLIENT(widget), remminafile);
}
static void
remmina_plugin_sftp_init(RemminaProtocolWidget *gp)
{
TRACE_CALL(__func__);
RemminaPluginSftpData *gpdata;
RemminaFile *remminafile;
gpdata = g_new0(RemminaPluginSftpData, 1);
g_object_set_data_full(G_OBJECT(gp), "plugin-data", gpdata, g_free);
remminafile = remmina_plugin_service->protocol_plugin_get_file(gp);
gpdata->client = remmina_sftp_client_new();
gpdata->client->gp = gp;
gtk_widget_show(GTK_WIDGET(gpdata->client));
gtk_container_add(GTK_CONTAINER(gp), GTK_WIDGET(gpdata->client));
remmina_ftp_client_set_show_hidden(REMMINA_FTP_CLIENT(gpdata->client),
remmina_plugin_service->file_get_int(remminafile, "showhidden", FALSE));
remmina_ftp_client_set_overwrite_status(REMMINA_FTP_CLIENT(gpdata->client),
remmina_plugin_service->file_get_int(remminafile,
REMMINA_PLUGIN_SFTP_FEATURE_PREF_OVERWRITE_ALL_KEY, FALSE));
remmina_ftp_client_set_resume_status(REMMINA_FTP_CLIENT(gpdata->client),
remmina_plugin_service->file_get_int(remminafile,
REMMINA_PLUGIN_SFTP_FEATURE_PREF_RESUME_ALL_KEY, FALSE));
remmina_plugin_service->protocol_plugin_register_hostkey(gp, GTK_WIDGET(gpdata->client));
g_signal_connect(G_OBJECT(gpdata->client),
"realize", G_CALLBACK(remmina_plugin_sftp_client_on_realize), gp);
}
static gboolean
remmina_plugin_sftp_open_connection(RemminaProtocolWidget *gp)
{
TRACE_CALL(__func__);
RemminaPluginSftpData *gpdata = GET_PLUGIN_DATA(gp);
remmina_plugin_service->protocol_plugin_set_expand(gp, TRUE);
remmina_plugin_service->protocol_plugin_set_width(gp, 640);
remmina_plugin_service->protocol_plugin_set_height(gp, 480);
if (pthread_create(&gpdata->thread, NULL, remmina_plugin_sftp_main_thread, gp)) {
remmina_plugin_service->protocol_plugin_set_error(gp,
"Could not initialize pthread. Falling back to non-thread mode…");
gpdata->thread = 0;
return FALSE;
} else {
return TRUE;
}
return TRUE;
}
static gboolean
remmina_plugin_sftp_close_connection(RemminaProtocolWidget *gp)
{
TRACE_CALL(__func__);
RemminaPluginSftpData *gpdata = GET_PLUGIN_DATA(gp);
RemminaFile *remminafile;
remminafile = remmina_plugin_service->protocol_plugin_get_file(gp);
if (gpdata->thread) {
pthread_cancel(gpdata->thread);
if (gpdata->thread) pthread_join(gpdata->thread, NULL);
}
remmina_ftp_client_save_state(REMMINA_FTP_CLIENT(gpdata->client), remminafile);
remmina_plugin_service->protocol_plugin_signal_connection_closed(gp);
/* The session preference overwrite_all is always saved to FALSE in order
* to avoid unwanted overwriting.
* If we'd change idea just remove the next line to save the preference. */
remmina_file_set_int(remminafile,
REMMINA_PLUGIN_SFTP_FEATURE_PREF_OVERWRITE_ALL_KEY, FALSE);
return FALSE;
}
static gboolean
remmina_plugin_sftp_query_feature(RemminaProtocolWidget *gp, const RemminaProtocolFeature *feature)
{
TRACE_CALL(__func__);
return TRUE;
}
static void
remmina_plugin_sftp_call_feature(RemminaProtocolWidget *gp, const RemminaProtocolFeature *feature)
{
TRACE_CALL(__func__);
RemminaPluginSftpData *gpdata = GET_PLUGIN_DATA(gp);
RemminaFile *remminafile;
remminafile = remmina_plugin_service->protocol_plugin_get_file(gp);
switch (feature->id) {
case REMMINA_PROTOCOL_FEATURE_TOOL_SSH:
remmina_plugin_service->open_connection(
remmina_file_dup_temp_protocol(remmina_plugin_service->protocol_plugin_get_file(gp), "SSH"),
NULL, gpdata->sftp, NULL);
return;
case REMMINA_PROTOCOL_FEATURE_TOOL_SFTP:
remmina_plugin_service->open_connection(
remmina_file_dup_temp_protocol(remmina_plugin_service->protocol_plugin_get_file(gp), "SFTP"),
NULL, gpdata->sftp, NULL);
return;
case REMMINA_PLUGIN_SFTP_FEATURE_PREF_SHOW_HIDDEN:
remmina_ftp_client_set_show_hidden(REMMINA_FTP_CLIENT(gpdata->client),
remmina_plugin_service->file_get_int(remminafile, "showhidden", FALSE));
return;
case REMMINA_PLUGIN_SFTP_FEATURE_PREF_OVERWRITE_ALL:
remmina_ftp_client_set_overwrite_status(REMMINA_FTP_CLIENT(gpdata->client),
remmina_plugin_service->file_get_int(remminafile,
REMMINA_PLUGIN_SFTP_FEATURE_PREF_OVERWRITE_ALL_KEY, FALSE));
case REMMINA_PLUGIN_SFTP_FEATURE_PREF_RESUME_ALL:
remmina_ftp_client_set_resume_status(REMMINA_FTP_CLIENT(gpdata->client),
remmina_plugin_service->file_get_int(remminafile,
REMMINA_PLUGIN_SFTP_FEATURE_PREF_RESUME_ALL_KEY, FALSE));
return;
}
}
static gpointer ssh_auth[] =
{
"0", N_("Password"),
"1", N_("SSH identity file"),
"2", N_("SSH agent"),
"3", N_("Public key (automatic)"),
"4", N_("Kerberos (GSSAPI)"),
NULL
};
/* Array for available features.
* The last element of the array must be REMMINA_PROTOCOL_FEATURE_TYPE_END. */
static const RemminaProtocolFeature remmina_plugin_sftp_features[] =
{
{ REMMINA_PROTOCOL_FEATURE_TYPE_PREF, REMMINA_PLUGIN_SFTP_FEATURE_PREF_SHOW_HIDDEN, GINT_TO_POINTER(REMMINA_PROTOCOL_FEATURE_PREF_CHECK), "showhidden",
N_("Show Hidden Files") },
{ REMMINA_PROTOCOL_FEATURE_TYPE_PREF, REMMINA_PLUGIN_SFTP_FEATURE_PREF_OVERWRITE_ALL, GINT_TO_POINTER(REMMINA_PROTOCOL_FEATURE_PREF_CHECK),
REMMINA_PLUGIN_SFTP_FEATURE_PREF_OVERWRITE_ALL_KEY, N_("Overwrite all files") },
{ REMMINA_PROTOCOL_FEATURE_TYPE_PREF, REMMINA_PLUGIN_SFTP_FEATURE_PREF_RESUME_ALL, GINT_TO_POINTER(REMMINA_PROTOCOL_FEATURE_PREF_CHECK),
REMMINA_PLUGIN_SFTP_FEATURE_PREF_RESUME_ALL_KEY, N_("Resume all file transfers") },
{ REMMINA_PROTOCOL_FEATURE_TYPE_TOOL, REMMINA_PROTOCOL_FEATURE_TOOL_SSH, N_("Connect via SSH from a new terminal"), "utilities-terminal",NULL},
{ REMMINA_PROTOCOL_FEATURE_TYPE_END, 0, NULL, NULL,
NULL }
};
/* 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_sftp_basic_settings[] =
{
{ REMMINA_PROTOCOL_SETTING_TYPE_SERVER, "server", NULL, FALSE, "_sftp-ssh._tcp", NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_TEXT, "username", N_("Username"), FALSE, NULL, NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_PASSWORD, "password", N_("Password"), FALSE, NULL, NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_SELECT, "ssh_auth", N_("Authentication type"), FALSE, ssh_auth, NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_FILE, "ssh_privatekey", N_("SSH identity file"), FALSE, NULL, NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_PASSWORD, "ssh_passphrase", N_("Password to unlock private key"), FALSE, NULL, NULL, NULL, NULL },
{ REMMINA_PROTOCOL_SETTING_TYPE_TEXT, "ssh_proxycommand", N_("SSH Proxy Command"), 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_sftp =
{
REMMINA_PLUGIN_TYPE_PROTOCOL, // Type
"SFTP", // Name
N_("SFTP - Secure File Transfer"), // Description
GETTEXT_PACKAGE, // Translation domain
VERSION, // Version number
"org.remmina.Remmina-sftp-symbolic", // Icon for normal connection
"org.remmina.Remmina-sftp-symbolic", // Icon for SSH connection
remmina_sftp_basic_settings, // Array for basic settings
NULL, // Array for advanced settings
REMMINA_PROTOCOL_SSH_SETTING_TUNNEL, // SSH settings type
remmina_plugin_sftp_features, // Array for available features
remmina_plugin_sftp_init, // Plugin initialization
remmina_plugin_sftp_open_connection, // Plugin open connection
remmina_plugin_sftp_close_connection, // Plugin close connection
remmina_plugin_sftp_query_feature, // Query for available features
remmina_plugin_sftp_call_feature, // Call a feature
NULL, // Send a keystroke
NULL, // Screenshot support unavailable
NULL, // RCW map event
NULL // RCW unmap event
};
void
remmina_sftp_plugin_register(void)
{
TRACE_CALL(__func__);
remmina_plugin_service = &remmina_plugin_manager_service;
remmina_plugin_service->register_plugin((RemminaPlugin *)&remmina_plugin_sftp);
}
#else
void remmina_sftp_plugin_register(void)
{
TRACE_CALL(__func__);
}
#endif
|