summaryrefslogtreecommitdiff
path: root/libpurple/plugins/kwallet/purplekwallet.cpp
blob: 199f9078fdb8340b23c3de3d28c11f487c917057 (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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
 * purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * 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 library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib/gi18n-lib.h>

#include <purple.h>

#include <QCoreApplication>

#include <kwallet.h>

#include "purplekwallet.h"

/******************************************************************************
 * Globals
 *****************************************************************************/
static QCoreApplication *qCoreApp = NULL;
static PurpleCredentialProvider *instance = NULL;

#define PURPLE_KWALLET_DOMAIN (g_quark_from_static_string("purple-kwallet"))
#define PURPLE_KWALLET_WALLET_NAME (KWallet::Wallet::NetworkWallet())

struct _PurpleKWalletProvider {
	PurpleCredentialProvider parent;

	PurpleKWalletPlugin::Engine *engine;
};

G_DEFINE_DYNAMIC_TYPE(PurpleKWalletProvider, purple_kwallet_provider,
                      PURPLE_TYPE_CREDENTIAL_PROVIDER)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static QString
purple_kwallet_get_ui_name(void) {
	PurpleUi *ui = NULL;
	QString ui_name = NULL;

	ui = purple_core_get_ui();
	if(PURPLE_IS_UI(ui)) {
		ui_name = purple_ui_get_name(ui);
	}

	if(ui_name.isEmpty()) {
		ui_name = "libpurple";
	}

	return ui_name;
}

static QString
purple_kwallet_provider_account_key(PurpleAccount *account) {
	PurpleContactInfo *info = PURPLE_CONTACT_INFO(account);

	return QString(purple_account_get_protocol_id(account)) + ":" +
	               purple_contact_info_get_username(info);
}

/******************************************************************************
 * Request Implementation
 *****************************************************************************/
PurpleKWalletPlugin::Request::Request(QString key, GTask *task) {
	this->key = key;
	this->task = G_TASK(g_object_ref(G_OBJECT(task)));
}

PurpleKWalletPlugin::Request::~Request(void) {
	g_clear_object(&this->task);
}

/******************************************************************************
 * ReadRequest Implementation
 *****************************************************************************/
PurpleKWalletPlugin::ReadRequest::ReadRequest(QString key, GTask *task) : PurpleKWalletPlugin::Request(key, task) {
}

void
PurpleKWalletPlugin::ReadRequest::execute(KWallet::Wallet *wallet) {
	QString password;
	int result = 0;
	bool missing;

	missing = KWallet::Wallet::keyDoesNotExist(PURPLE_KWALLET_WALLET_NAME,
	                                           purple_kwallet_get_ui_name(),
	                                           key);

	if(missing) {
		g_task_return_new_error(this->task, PURPLE_KWALLET_DOMAIN, 0,
		                        "no password stored");

		g_clear_object(&this->task);

		return;
	}

	result = wallet->readPassword(this->key, password);

	if(result != 0) {
		g_task_return_new_error(this->task, PURPLE_KWALLET_DOMAIN, result,
		                        _("failed to read password, kwallet responded "
		                          "with error code %d"), result);
	} else {
		gchar *c_password = g_strdup(password.toUtf8().constData());
		g_task_return_pointer(this->task, c_password, g_free);
	}

	g_clear_object(&this->task);
}

void
PurpleKWalletPlugin::ReadRequest::cancel(QString reason) {
	g_task_return_new_error(this->task, PURPLE_KWALLET_DOMAIN, 0,
	                        _("failed to read password: %s"),
	                        reason.toUtf8().constData());

	g_clear_object(&this->task);
}

/******************************************************************************
 * WriteRequest Implementation
 *****************************************************************************/
PurpleKWalletPlugin::WriteRequest::WriteRequest(QString key, GTask *task, QString password) : PurpleKWalletPlugin::Request(key, task) {
	this->password = password;
}

void
PurpleKWalletPlugin::WriteRequest::execute(KWallet::Wallet *wallet) {
	int result;

	result = wallet->writePassword(this->key, this->password);

	if(result != 0) {
		g_task_return_new_error(this->task, PURPLE_KWALLET_DOMAIN, result,
		                        _("failed to write password, kwallet "
		                          "responded with error code %d"), result);
	} else {
		g_task_return_boolean(this->task, TRUE);
	}

	g_clear_object(&this->task);
}

void
PurpleKWalletPlugin::WriteRequest::cancel(QString reason) {
	g_task_return_new_error(this->task, PURPLE_KWALLET_DOMAIN, 0,
	                        _("failed to write password: %s"),
	                        reason.toUtf8().constData());

	g_clear_object(&this->task);
}

/******************************************************************************
 * ClearRequest Implementation
 *****************************************************************************/
PurpleKWalletPlugin::ClearRequest::ClearRequest(QString key, GTask *task) : PurpleKWalletPlugin::Request(key, task) {
}

void
PurpleKWalletPlugin::ClearRequest::execute(KWallet::Wallet *wallet) {
	int result;

	result = wallet->removeEntry(this->key);

	if(result != 0) {
		g_task_return_new_error(this->task, PURPLE_KWALLET_DOMAIN, result,
		                        _("failed to clear password, kwallet "
		                          "responded with error code %d"), result);
	} else {
		g_task_return_boolean(this->task, TRUE);
	}

	g_clear_object(&this->task);
}

void
PurpleKWalletPlugin::ClearRequest::cancel(QString reason) {
	g_task_return_new_error(this->task, PURPLE_KWALLET_DOMAIN, 0,
	                        _("failed to clear password: %s"),
	                        reason.toUtf8().constData());

	g_clear_object(&this->task);
}

/******************************************************************************
 * Engine Implementation
 *****************************************************************************/
PurpleKWalletPlugin::Engine::Engine(void) {
	this->queue = QQueue<PurpleKWalletPlugin::Request *>();

	this->wallet = NULL;

	this->connected = false;
	this->failed = false;
	this->externallyClosed = false;
}

PurpleKWalletPlugin::Engine::~Engine(void) {
	this->close();
}

void
PurpleKWalletPlugin::Engine::open(void) {
	purple_debug_misc("kwallet-provider", "attempting to open wallet");

	if(this->connected) {
		purple_debug_misc("kwallet-provider", "wallet already opened");

		return;
	}

	// Reset our externallyClosed and failed states.
	this->externallyClosed = false;
	this->failed = false;

	// No need  to check this pointer as an async open always returns non-null.
	this->wallet = KWallet::Wallet::openWallet(PURPLE_KWALLET_WALLET_NAME,
	                                           0,
	                                           KWallet::Wallet::Asynchronous);

	this->failed |= !QObject::connect(this->wallet, SIGNAL(walletOpened(bool)),
	                                  SLOT(opened(bool)));
	this->failed |= !QObject::connect(this->wallet, SIGNAL(walletClosed(void)),
	                                  SLOT(closed()));

	if(this->failed) {
		purple_debug_error("kwallet-provider",
		                   "Failed to connect KWallet signals");
	}
}

void
PurpleKWalletPlugin::Engine::close(void) {
	while(!this->queue.isEmpty()) {
		PurpleKWalletPlugin::Request *request = this->queue.dequeue();

		request->cancel("wallet is closing");

		delete request;
	}

	if(this->wallet != NULL) {
		delete this->wallet;
		this->wallet = NULL;
	}

	this->connected = false;
	this->failed = false;
}

void
PurpleKWalletPlugin::Engine::enqueue(PurpleKWalletPlugin::Request *request) {
	this->queue.enqueue(request);

	processQueue();
}

void
PurpleKWalletPlugin::Engine::opened(bool opened) {
	QString folder_name;

	if(!opened) {
		purple_debug_error("kwallet-provider", "failed to open wallet");

		delete this->wallet;
		this->wallet = NULL;

		this->connected = false;
		this->failed = true;

		return;
	}

	// Handle the case where the wallet opened signal connected, but the wallet
	// closed signal failed to connect.
	if(this->failed) {
		purple_debug_error("kwallet-provider",
		                   "wallet opened, but failed to connect the wallet "
		                   "closed signal");
		return;
	}

	this->connected = true;

	// setup our folder
	folder_name = purple_kwallet_get_ui_name();
	if(!this->wallet->hasFolder(folder_name)) {
		if(!this->wallet->createFolder(folder_name)) {
			purple_debug_error("kwallet-provider",
			                   "failed to create folder %s in wallet.",
			                   folder_name.toUtf8().constData());
			this->failed = true;
		}
	}

	if(!this->failed && !this->wallet->setFolder(folder_name)) {
		purple_debug_error("kwallet-provider", "failed to set folder to %s",
		                   folder_name.toUtf8().constData());
		this->failed = true;
	}

	purple_debug_misc("kwallet-provider", "successfully opened the wallet");

	processQueue();
}

void
PurpleKWalletPlugin::Engine::closed(void) {
	purple_debug_misc("kwallet-provider", "the wallet was closed externally");

	this->externallyClosed = true;
	this->close();
}

void
PurpleKWalletPlugin::Engine::processQueue() {
	if(this->externallyClosed && this->queue.isEmpty() == false) {
		this->open();
	} else if(this->connected || this->failed) {
		while(!this->queue.isEmpty()) {
			PurpleKWalletPlugin::Request *request = this->queue.dequeue();

			if(this->failed) {
				request->cancel(_("failed to open kwallet"));
			} else {
				request->execute(this->wallet);
			}

			delete request;
		}
	}
}

/******************************************************************************
 * PurpleCredentialProvider Implementation
 *****************************************************************************/
static void
purple_kwallet_provider_activate(PurpleCredentialProvider *provider) {
	PurpleKWalletProvider *kwallet_provider = NULL;

	kwallet_provider = PURPLE_KWALLET_PROVIDER(provider);

	kwallet_provider->engine->open();
}

static void
purple_kwallet_provider_deactivate(PurpleCredentialProvider *provider) {
	PurpleKWalletProvider *kwallet_provider = NULL;

	kwallet_provider = PURPLE_KWALLET_PROVIDER(provider);

	kwallet_provider->engine->close();
}

static void
purple_kwallet_read_password_async(PurpleCredentialProvider *provider,
                                   PurpleAccount *account,
                                   GCancellable *cancellable,
                                   GAsyncReadyCallback callback,
                                   gpointer data)
{
	PurpleKWalletProvider *kwallet_provider = NULL;
	PurpleKWalletPlugin::ReadRequest *request = NULL;
	GTask *task = NULL;
	QString key;

	key = purple_kwallet_provider_account_key(account);

	task = g_task_new(G_OBJECT(provider), cancellable, callback, data);

	request = new PurpleKWalletPlugin::ReadRequest(key, task);

	kwallet_provider = PURPLE_KWALLET_PROVIDER(provider);
	kwallet_provider->engine->enqueue(request);

	g_clear_object(&task);
}

static gchar *
purple_kwallet_read_password_finish(G_GNUC_UNUSED PurpleCredentialProvider *provider,
                                    GAsyncResult *result, GError **error)
{
	return (gchar *)g_task_propagate_pointer(G_TASK(result), error);
}

static void
purple_kwallet_write_password_async(PurpleCredentialProvider *provider,
                                    PurpleAccount *account,
                                    const gchar *password,
                                    GCancellable *cancellable,
                                    GAsyncReadyCallback callback,
                                    gpointer data)
{
	PurpleKWalletProvider *kwallet_provider = NULL;
	PurpleKWalletPlugin::WriteRequest *request = NULL;
	GTask *task = NULL;
	QString key;

	task = g_task_new(G_OBJECT(provider), cancellable, callback, data);

	key = purple_kwallet_provider_account_key(account);

	request = new PurpleKWalletPlugin::WriteRequest(key, task, password);

	kwallet_provider = PURPLE_KWALLET_PROVIDER(provider);
	kwallet_provider->engine->enqueue(request);

	g_clear_object(&task);
}

static gboolean
purple_kwallet_write_password_finish(G_GNUC_UNUSED PurpleCredentialProvider *provider,
                                     GAsyncResult *result, GError **error)
{
	return g_task_propagate_boolean(G_TASK(result), error);
}

static void
purple_kwallet_clear_password_async(PurpleCredentialProvider *provider,
                                    PurpleAccount *account,
                                    GCancellable *cancellable,
                                    GAsyncReadyCallback callback,
                                    gpointer data)
{
	PurpleKWalletProvider *kwallet_provider = NULL;
	PurpleKWalletPlugin::ClearRequest *request = NULL;
	GTask *task = NULL;
	QString key;

	task = g_task_new(G_OBJECT(provider), cancellable, callback, data);

	key = purple_kwallet_provider_account_key(account);

	request = new PurpleKWalletPlugin::ClearRequest(key, task);

	kwallet_provider = PURPLE_KWALLET_PROVIDER(provider);
	kwallet_provider->engine->enqueue(request);

	g_clear_object(&task);
}

static gboolean
purple_kwallet_clear_password_finish(G_GNUC_UNUSED PurpleCredentialProvider *provider,
                                     GAsyncResult *result, GError **error)
{
	return g_task_propagate_boolean(G_TASK(result), error);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_kwallet_provider_dispose(GObject *obj) {
	PurpleKWalletProvider *provider = PURPLE_KWALLET_PROVIDER(obj);

	if(provider->engine != NULL) {
		provider->engine->close();
	}

	G_OBJECT_CLASS(purple_kwallet_provider_parent_class)->dispose(obj);
}

static void
purple_kwallet_provider_finalize(GObject *obj) {
	PurpleKWalletProvider *provider = PURPLE_KWALLET_PROVIDER(obj);

	if(provider->engine != NULL) {
		delete provider->engine;
		provider->engine = NULL;
	}

	G_OBJECT_CLASS(purple_kwallet_provider_parent_class)->finalize(obj);
}

static void
purple_kwallet_provider_init(PurpleKWalletProvider *provider) {
	provider->engine = new PurpleKWalletPlugin::Engine();
}

static void
purple_kwallet_provider_class_init(PurpleKWalletProviderClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	PurpleCredentialProviderClass *provider_class = NULL;

	provider_class = PURPLE_CREDENTIAL_PROVIDER_CLASS(klass);

	obj_class->dispose = purple_kwallet_provider_dispose;
	obj_class->finalize = purple_kwallet_provider_finalize;

	provider_class->activate = purple_kwallet_provider_activate;
	provider_class->deactivate = purple_kwallet_provider_deactivate;
	provider_class->read_password_async = purple_kwallet_read_password_async;
	provider_class->read_password_finish = purple_kwallet_read_password_finish;
	provider_class->write_password_async = purple_kwallet_write_password_async;
	provider_class->write_password_finish =
		purple_kwallet_write_password_finish;
	provider_class->clear_password_async = purple_kwallet_clear_password_async;
	provider_class->clear_password_finish =
		purple_kwallet_clear_password_finish;
}

static void
purple_kwallet_provider_class_finalize(G_GNUC_UNUSED PurpleKWalletProviderClass *klass) {
}

/******************************************************************************
 * API
 *****************************************************************************/
static PurpleCredentialProvider *
purple_kwallet_provider_new(void) {
	return PURPLE_CREDENTIAL_PROVIDER(g_object_new(
		PURPLE_KWALLET_TYPE_PROVIDER,
		"id", "kwallet",
		"name", _("KWallet"),
		"description", _("A credentials management application for the KDE "
		                 "Software Compilation desktop environment"),
		NULL
	));
}

/******************************************************************************
 * Plugin Exports
 *****************************************************************************/
static GPluginPluginInfo *
kwallet_query(G_GNUC_UNUSED GError **error) {
	const gchar * const authors[] = {
		"Pidgin Developers <devel@pidgin.im>",
		NULL
	};

	return GPLUGIN_PLUGIN_INFO(purple_plugin_info_new(
		"id",           "keyring-kwallet",
		"name",         N_("KWallet"),
		"version",      DISPLAY_VERSION,
		"category",     N_("Keyring"),
		"summary",      "KWallet Keyring Plugin",
		"description",  N_("This plugin will store passwords in KWallet."),
		"authors",      authors,
		"website",      PURPLE_WEBSITE,
		"abi-version",  PURPLE_ABI_VERSION,
		"flags",        PURPLE_PLUGIN_INFO_FLAGS_INTERNAL |
		                PURPLE_PLUGIN_INFO_FLAGS_AUTO_LOAD,
		NULL
	));
}

static gboolean
kwallet_load(GPluginPlugin *plugin, GError **error) {
	PurpleCredentialManager *manager = NULL;

	purple_kwallet_provider_register_type(G_TYPE_MODULE(plugin));

	if(qCoreApp == NULL) {
		int argc = 0;
		qCoreApp = new QCoreApplication(argc, NULL);
		qCoreApp->setApplicationName(purple_kwallet_get_ui_name());
	}

	if(!KWallet::Wallet::isEnabled()) {
		g_set_error(error, PURPLE_KWALLET_DOMAIN, 0,
		            "KWallet service is disabled.");

		return FALSE;
	}

	manager = purple_credential_manager_get_default();

	instance = purple_kwallet_provider_new();

	return purple_credential_manager_register(manager, instance, error);
}

static gboolean
kwallet_unload(G_GNUC_UNUSED GPluginPlugin *plugin,
               G_GNUC_UNUSED gboolean shutdown,
               GError **error)
{
	PurpleCredentialManager *manager = NULL;
	gboolean ret = FALSE;

	manager = purple_credential_manager_get_default();
	ret = purple_credential_manager_unregister(manager, instance, error);

	if(!ret) {
		return ret;
	}

	if(qCoreApp != NULL) {
		delete qCoreApp;
		qCoreApp = NULL;
	}

	g_clear_object(&instance);

	return TRUE;
}

G_BEGIN_DECLS
GPLUGIN_NATIVE_PLUGIN_DECLARE(kwallet)
G_END_DECLS