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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/*
* Remmina - The GTK+ Remote Desktop Client
* Copyright (C) 2009-2011 Vic Lee
* Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
* Copyright (C) 2016-2023 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 <gtk/gtk.h>
#include <glib/gi18n.h>
#include "config.h"
#include "remmina_mpchange.h"
#include "remmina_file.h"
#include "remmina_file_manager.h"
#include "remmina_pref.h"
#include "remmina_public.h"
#include "remmina_main.h"
#include "remmina_plugin_manager.h"
#include "remmina_log.h"
#include "remmina/remmina_trace_calls.h"
#define GET_DIALOG_OBJECT(object_name) gtk_builder_get_object(bu, object_name)
struct mpchanger_params {
gchar *username; // New username
gchar *domain; // New domain
gchar *password; // New password
gchar *group;
gchar *gatewayusername;
gchar *gatewaydomain;
gchar *gatewaypassword;
GtkEntry *eGroup, *eUsername, *eDomain, *ePassword1, *ePassword2;
GtkEntry *eGatewayUsername, *eGatewayDomain, *eGatewayPassword1, *eGatewayPassword2;
GtkListStore* store;
GtkDialog* dialog;
GtkTreeView* table;
GtkButton* btnDoChange;
GtkLabel* statusLabel;
GtkTreeIter iter;
int changed_passwords_count;
guint sid;
guint searchentrychange_timeout_source_id;
};
enum {
COL_F = 0,
COL_NAME,
COL_GROUP,
COL_USERNAME,
COL_GATEWAY_USERNAME,
COL_FILENAME,
NUM_COLS
};
static gboolean remmina_mpchange_fieldcompare(const gchar *needle, const gchar *haystack, int *matchcount)
{
TRACE_CALL(__func__);
if (needle[0] == 0) {
(*matchcount)++;
return TRUE;
}
if (haystack[0] == 0 || strstr(haystack, needle) == NULL){
return FALSE;
}
(*matchcount)++;
return TRUE;
}
static void remmina_mpchange_file_list_callback(RemminaFile *remminafile, gpointer user_data)
{
TRACE_CALL(__func__);
GtkListStore* store;
GtkTreeIter iter;
int matchcount;
const gchar *username, *domain, *group, *gatewayusername, *gatewaydomain;
gchar* s;
gchar* t;
struct mpchanger_params* mpcp;
mpcp = (struct mpchanger_params*)user_data;
store = GTK_LIST_STORE(mpcp->store);
username = remmina_file_get_string(remminafile, "username");
domain = remmina_file_get_string(remminafile, "domain");
group = remmina_file_get_string(remminafile, "group");
gatewayusername = remmina_file_get_string(remminafile, "gateway_username");
gatewaydomain = remmina_file_get_string(remminafile, "gateway_domain");
if (username == NULL)
username = "";
if (domain == NULL)
domain = "";
if (group == NULL)
group = "";
if (gatewayusername == NULL)
gatewayusername = "";
if (gatewaydomain == NULL)
gatewaydomain = "";
matchcount = 0;
if (!remmina_mpchange_fieldcompare(mpcp->username, username, &matchcount))
return;
if (!remmina_mpchange_fieldcompare(mpcp->domain, domain, &matchcount))
return;
if (!remmina_mpchange_fieldcompare(mpcp->group, group, &matchcount))
return;
if (!remmina_mpchange_fieldcompare(mpcp->gatewayusername, gatewayusername, &matchcount))
return;
if (!remmina_mpchange_fieldcompare(mpcp->gatewaydomain, gatewaydomain, &matchcount))
return;
gtk_list_store_append(store, &iter);
s = g_strdup_printf("%s\\%s", domain, username);
t = g_strdup_printf("%s\\%s", gatewaydomain, gatewayusername);
gtk_list_store_set(store, &iter,
COL_F, matchcount >= 5 ? TRUE : FALSE,
COL_NAME, remmina_file_get_string(remminafile, "name"),
COL_GROUP, group,
COL_USERNAME, s,
COL_GATEWAY_USERNAME, t,
COL_FILENAME, remminafile->filename,
-1);
g_free(s);
g_free(t);
}
static void remmina_mpchange_checkbox_toggle(GtkCellRendererToggle *cell, gchar *path_string, gpointer user_data)
{
TRACE_CALL(__func__);
GtkTreeIter iter;
struct mpchanger_params* mpcp = (struct mpchanger_params*)user_data;
GtkTreePath *path;
gboolean a = gtk_cell_renderer_toggle_get_active(cell);
path = gtk_tree_path_new_from_string(path_string);
gtk_tree_model_get_iter(GTK_TREE_MODEL(mpcp->store), &iter, path);
gtk_tree_path_free(path);
gtk_list_store_set(mpcp->store, &iter, COL_F, !a, -1);
}
static void remmina_mpchange_dochange(gchar* fname, struct mpchanger_params* mpcp)
{
TRACE_CALL(__func__);
RemminaFile* remminafile;
remminafile = remmina_file_load(fname);
if (remminafile) {
if(mpcp->password[0] != 0){
remmina_file_store_secret_plugin_password(remminafile, "password", mpcp->password);
}
if(mpcp->gatewaypassword[0] != 0){
remmina_file_store_secret_plugin_password(remminafile, "gateway_password", mpcp->gatewaypassword);
}
remmina_file_free(remminafile);
mpcp->changed_passwords_count++;
}
}
static void enable_inputs(struct mpchanger_params* mpcp, gboolean ena)
{
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->eGroup), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->eUsername), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->eDomain), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->ePassword1), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->ePassword2), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->eGatewayUsername), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->eGatewayDomain), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->eGatewayPassword1), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->eGatewayPassword2), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->btnDoChange), ena);
gtk_widget_set_sensitive(GTK_WIDGET(mpcp->table), ena);
}
static gboolean changenext(gpointer user_data)
{
TRACE_CALL(__func__);
struct mpchanger_params* mpcp = (struct mpchanger_params*)user_data;
gchar* fname;
gboolean sel;
gtk_tree_model_get(GTK_TREE_MODEL(mpcp->store), &mpcp->iter, COL_F, &sel, -1);
gtk_tree_model_get(GTK_TREE_MODEL(mpcp->store), &mpcp->iter, COL_FILENAME, &fname, -1);
if (sel) {
remmina_mpchange_dochange(fname, mpcp);
}
g_free(fname);
if (gtk_tree_model_iter_next(GTK_TREE_MODEL(mpcp->store), &mpcp->iter)) {
return G_SOURCE_CONTINUE;
}else {
gtk_dialog_response(mpcp->dialog, 1);
mpcp->sid = 0;
return G_SOURCE_REMOVE;
}
}
static void remmina_mpchange_dochange_clicked(GtkButton *btn, gpointer user_data)
{
TRACE_CALL(__func__);
struct mpchanger_params* mpcp = (struct mpchanger_params*)user_data;
const gchar *passwd1, *passwd2, *gatewaypasswd1, *gatewaypasswd2;
if (mpcp->searchentrychange_timeout_source_id) {
g_source_remove(mpcp->searchentrychange_timeout_source_id);
mpcp->searchentrychange_timeout_source_id = 0;
}
if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(mpcp->store), &mpcp->iter))
return;
passwd1 = gtk_entry_get_text(mpcp->ePassword1);
passwd2 = gtk_entry_get_text(mpcp->ePassword2);
if (g_strcmp0(passwd1, passwd2) != 0) {
GtkWidget *msgDialog;
msgDialog = gtk_message_dialog_new(GTK_WINDOW(mpcp->dialog),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("The passwords do not match"));
gtk_dialog_run(GTK_DIALOG(msgDialog));
gtk_widget_destroy(msgDialog);
return;
}
gatewaypasswd1 = gtk_entry_get_text(mpcp->eGatewayPassword1);
gatewaypasswd2 = gtk_entry_get_text(mpcp->eGatewayPassword2);
if (g_strcmp0(gatewaypasswd1, gatewaypasswd2) != 0) {
GtkWidget *msgDialog;
msgDialog = gtk_message_dialog_new(GTK_WINDOW(mpcp->dialog),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("The Gateway passwords do not match"));
gtk_dialog_run(GTK_DIALOG(msgDialog));
gtk_widget_destroy(msgDialog);
return;
}
g_free(mpcp->password);
mpcp->password = g_strdup(passwd1);
mpcp->changed_passwords_count = 0;
g_free(mpcp->gatewaypassword);
mpcp->gatewaypassword = g_strdup(gatewaypasswd1);
mpcp->changed_passwords_count = 0;
gtk_label_set_text(mpcp->statusLabel, _("Resetting passwords, please wait…"));
enable_inputs(mpcp, FALSE);
mpcp->sid = g_idle_add(changenext, (gpointer)mpcp);
}
static gboolean remmina_mpchange_searchfield_changed_to(gpointer user_data)
{
TRACE_CALL(__func__);
struct mpchanger_params *mpcp = (struct mpchanger_params *)user_data;
const gchar *s;
if (mpcp->searchentrychange_timeout_source_id) {
g_source_remove(mpcp->searchentrychange_timeout_source_id);
mpcp->searchentrychange_timeout_source_id = 0;
}
s = gtk_entry_get_text(mpcp->eGroup);
g_free(mpcp->group);
mpcp->group = g_strdup(s);
s = gtk_entry_get_text(mpcp->eDomain);
g_free(mpcp->domain);
mpcp->domain = g_strdup(s);
s = gtk_entry_get_text(mpcp->eUsername);
g_free(mpcp->username);
mpcp->username = g_strdup(s);
s = gtk_entry_get_text(mpcp->eGatewayDomain);
g_free(mpcp->gatewaydomain);
mpcp->gatewaydomain = g_strdup(s);
s = gtk_entry_get_text(mpcp->eGatewayUsername);
g_free(mpcp->gatewayusername);
mpcp->gatewayusername = g_strdup(s);
if (mpcp->store != NULL) {
gtk_tree_view_set_model(mpcp->table, NULL);
}
mpcp->store = gtk_list_store_new(NUM_COLS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
if (mpcp->group[0] != 0 || mpcp->domain[0] != 0 || mpcp->username[0] != 0 || mpcp->gatewayusername[0] != 0 || mpcp->gatewaydomain[0] != 0)
remmina_file_manager_iterate((GFunc)remmina_mpchange_file_list_callback, (gpointer)mpcp);
gtk_tree_view_set_model(mpcp->table, GTK_TREE_MODEL(mpcp->store));
return G_SOURCE_CONTINUE; // Source already remove at the beginning
}
static void remmina_mpchange_searchfield_changed(GtkSearchEntry *se, gpointer user_data)
{
TRACE_CALL(__func__);
struct mpchanger_params *mpcp = (struct mpchanger_params *)user_data;
if (mpcp->searchentrychange_timeout_source_id) {
g_source_remove(mpcp->searchentrychange_timeout_source_id);
mpcp->searchentrychange_timeout_source_id = 0;
}
mpcp->searchentrychange_timeout_source_id = g_timeout_add(500, remmina_mpchange_searchfield_changed_to, user_data);
}
static void remmina_mpchange_stopsearch(GtkSearchEntry *entry, gpointer user_data)
{
TRACE_CALL(__func__);
/* The stop-search signal is emitted when pressing Esc on a GtkSearchEntry. We end the dialog. */
struct mpchanger_params *mpcp = (struct mpchanger_params *)user_data;
gtk_dialog_response(mpcp->dialog, 1);
}
static gboolean remmina_file_multipasswd_changer_mt(gpointer d)
{
TRACE_CALL(__func__);
struct mpchanger_params *mpcp = d;
GtkBuilder* bu;
GtkDialog* dialog;
GtkWindow* mainwindow;
GtkCellRendererToggle *toggle;
RemminaSecretPlugin *secret_plugin;
char *initerror;
mainwindow = remmina_main_get_window();
/* The multiple password changer works only when a secrecy plugin is available */
initerror = NULL;
secret_plugin = remmina_plugin_manager_get_secret_plugin();
if (secret_plugin == NULL) {
initerror = _("The multi password changer requires a secrecy plugin.\n");
}else {
if (!secret_plugin->is_service_available(secret_plugin)) {
initerror = _("The multi password changer requires a secrecy service.\n");
}
}
if (initerror) {
GtkWidget *msgDialog;
msgDialog = gtk_message_dialog_new(mainwindow, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
"%s", initerror);
gtk_dialog_run(GTK_DIALOG(msgDialog));
gtk_widget_destroy(msgDialog);
return FALSE;
}
bu = remmina_public_gtk_builder_new_from_resource("/org/remmina/Remmina/src/../data/ui/remmina_mpc.glade");
if (!bu) {
REMMINA_DEBUG("Unable to load the multiple password changer Glade file interface\n");
return FALSE;
}
dialog = GTK_DIALOG(gtk_builder_get_object(bu, "MPCDialog"));
mpcp->dialog = dialog;
if (mainwindow)
gtk_window_set_transient_for(GTK_WINDOW(dialog), mainwindow);
mpcp->eGroup = GTK_ENTRY(GET_DIALOG_OBJECT("groupEntry"));
gtk_entry_set_text(mpcp->eGroup, mpcp->group);
g_signal_connect(G_OBJECT(mpcp->eGroup), "changed", G_CALLBACK(remmina_mpchange_searchfield_changed), (gpointer)mpcp);
g_signal_connect(G_OBJECT(mpcp->eGroup), "stop-search", G_CALLBACK(remmina_mpchange_stopsearch), (gpointer)mpcp);
mpcp->eUsername = GTK_ENTRY(GET_DIALOG_OBJECT("usernameEntry"));
gtk_entry_set_text(mpcp->eUsername, mpcp->username);
g_signal_connect(G_OBJECT(mpcp->eUsername), "changed", G_CALLBACK(remmina_mpchange_searchfield_changed), (gpointer)mpcp);
mpcp->eGatewayUsername = GTK_ENTRY(GET_DIALOG_OBJECT("gatewayUsernameEntry"));
gtk_entry_set_text(mpcp->eGatewayUsername, mpcp->gatewayusername);
g_signal_connect(G_OBJECT(mpcp->eGatewayUsername), "changed", G_CALLBACK(remmina_mpchange_searchfield_changed), (gpointer)mpcp);
mpcp->eDomain = GTK_ENTRY(GET_DIALOG_OBJECT("domainEntry"));
gtk_entry_set_text(mpcp->eDomain, mpcp->domain);
g_signal_connect(G_OBJECT(mpcp->eDomain), "changed", G_CALLBACK(remmina_mpchange_searchfield_changed), (gpointer)mpcp);
mpcp->eGatewayDomain = GTK_ENTRY(GET_DIALOG_OBJECT("gatewayDomainEntry"));
gtk_entry_set_text(mpcp->eGatewayDomain, mpcp->gatewaydomain);
g_signal_connect(G_OBJECT(mpcp->eGatewayDomain), "changed", G_CALLBACK(remmina_mpchange_searchfield_changed), (gpointer)mpcp);
mpcp->ePassword1 = GTK_ENTRY(GET_DIALOG_OBJECT("password1Entry"));
gtk_entry_set_text(mpcp->ePassword1, mpcp->password);
mpcp->eGatewayPassword1 = GTK_ENTRY(GET_DIALOG_OBJECT("gatewayPassword1Entry"));
gtk_entry_set_text(mpcp->eGatewayPassword1, mpcp->gatewaypassword);
mpcp->ePassword2 = GTK_ENTRY(GET_DIALOG_OBJECT("password2Entry"));
gtk_entry_set_text(mpcp->ePassword2, mpcp->password);
mpcp->eGatewayPassword2 = GTK_ENTRY(GET_DIALOG_OBJECT("gatewayPassword2Entry"));
gtk_entry_set_text(mpcp->eGatewayPassword2, mpcp->gatewaypassword);
mpcp->statusLabel = GTK_LABEL(GET_DIALOG_OBJECT("statusLabel"));
mpcp->store = NULL;
mpcp->table = GTK_TREE_VIEW(GET_DIALOG_OBJECT("profchangelist"));
/* Fire a fake searchfield changed, so a new list store is created */
remmina_mpchange_searchfield_changed(NULL, (gpointer)mpcp);
toggle = GTK_CELL_RENDERER_TOGGLE(GET_DIALOG_OBJECT("cellrenderertoggle1"));
g_signal_connect(G_OBJECT(toggle), "toggled", G_CALLBACK(remmina_mpchange_checkbox_toggle), (gpointer)mpcp);
mpcp->btnDoChange = GTK_BUTTON(GET_DIALOG_OBJECT("btnDoChange"));
g_signal_connect(mpcp->btnDoChange, "clicked", G_CALLBACK(remmina_mpchange_dochange_clicked), (gpointer)mpcp);
gtk_dialog_run(dialog);
gtk_widget_destroy(GTK_WIDGET(dialog));
if (mpcp->sid) {
g_source_remove(mpcp->sid);
mpcp->sid = 0;
}
if (mpcp->searchentrychange_timeout_source_id) {
g_source_remove(mpcp->searchentrychange_timeout_source_id);
mpcp->searchentrychange_timeout_source_id = 0;
}
if (mpcp->changed_passwords_count) {
GtkWidget *msgDialog;
msgDialog = gtk_message_dialog_new(GTK_WINDOW(mpcp->dialog),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
ngettext("%d password changed.", "%d passwords changed.", mpcp->changed_passwords_count), mpcp->changed_passwords_count);
gtk_dialog_run(GTK_DIALOG(msgDialog));
gtk_widget_destroy(msgDialog);
}
// Free data
g_free(mpcp->username);
g_free(mpcp->password);
g_free(mpcp->domain);
g_free(mpcp->group);
g_free(mpcp->gatewayusername);
g_free(mpcp->gatewaypassword);
g_free(mpcp->gatewaydomain);
g_free(mpcp);
return FALSE;
}
void
remmina_mpchange_schedule(gboolean has_domain, const gchar *group, const gchar *domain, const gchar *username, const gchar *password, const gchar *gatewayusername, const gchar *gatewaydomain, const gchar *gatewaypassword)
{
// We could also be called in a subthread after a successful connection
// (not currently implemented)
// So we just schedule the multipassword changer to be executed on
// the main thread
TRACE_CALL(__func__);
struct mpchanger_params *mpcp;
mpcp = g_malloc0(sizeof(struct mpchanger_params));
mpcp->username = g_strdup(username);
mpcp->password = g_strdup(password);
mpcp->domain = g_strdup(domain);
mpcp->group = g_strdup(group);
mpcp->gatewayusername = g_strdup(gatewayusername);
mpcp->gatewaypassword = g_strdup(gatewaypassword);
mpcp->gatewaydomain = g_strdup(gatewaydomain);
gdk_threads_add_idle(remmina_file_multipasswd_changer_mt, (gpointer)mpcp);
}
|