summaryrefslogtreecommitdiff
path: root/libpurple/plugins/keyrings/secretservice.c
blob: 802bb536785a111b9d3d8df4a89d860f35f9c150 (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
/* purple
 * @file secretservice.c Secret Service password storage
 * @ingroup plugins
 *
 * 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 program ; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#error "This keyring needs some more work (see TODO)"

/* TODO
 *
 * This keyring needs some more work, so it will be disabled until its quality
 * was raised. Some of the pain points:
 *  - throws a lot of g_warnings
 *  - it doesn't notify about some backend faults (like access denied), some of
 *    them are not handled at all
 *  - it could use libsecret's Complete API
 *  - code formatting could be better
 */

#include "internal.h"
#include "account.h"
#include "debug.h"
#include "keyring.h"
#include "plugin.h"
#include "version.h"

#include <libsecret/secret.h>

#define SECRETSERVICE_NAME        N_("Secret Service")
#define SECRETSERVICE_ID          "keyring-libsecret"

static PurpleKeyring *keyring_handler = NULL;

static const SecretSchema purple_schema = {
	"im.pidgin.Purple", SECRET_SCHEMA_NONE,
	{
		{"user", SECRET_SCHEMA_ATTRIBUTE_STRING},
		{"protocol", SECRET_SCHEMA_ATTRIBUTE_STRING},
		{"NULL", 0}
	},
	/* Reserved fields */
	0, 0, 0, 0, 0, 0, 0, 0
};

typedef struct _InfoStorage InfoStorage;

struct _InfoStorage
{
	PurpleAccount *account;
	gpointer cb;
	gpointer user_data;
};

/***********************************************/
/*     Keyring interface                       */
/***********************************************/
static void
ss_read_continue(GObject *object, GAsyncResult *result, gpointer data)
{
	InfoStorage *storage = data;
	PurpleAccount *account = storage->account;
	PurpleKeyringReadCallback cb = storage->cb;
	char *password;
	GError *error = NULL;

	password = secret_password_lookup_finish(result, &error);

	if (error != NULL) {
		int code = error->code;

		switch (code) {
			case G_DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND:
			case G_DBUS_ERROR_IO_ERROR:
				error = g_error_new(PURPLE_KEYRING_ERROR,
				                    PURPLE_KEYRING_ERROR_BACKENDFAIL,
				                    "Failed to communicate with Secret Service (account : %s).",
				                    purple_account_get_username(account));
				if (cb != NULL)
					cb(account, NULL, error, storage->user_data);
				g_error_free(error);
				break;

			default:
				purple_debug_error("keyring-libsecret",
				                  "Unknown error (account: %s (%s), domain: %s, code: %d): %s.\n",
				                  purple_account_get_username(account),
				                  purple_account_get_protocol_id(account),
				                  g_quark_to_string(error->domain), code, error->message);
				error = g_error_new(PURPLE_KEYRING_ERROR,
				                    PURPLE_KEYRING_ERROR_BACKENDFAIL,
				                    "Unknown error (account : %s).",
				                    purple_account_get_username(account));
				if (cb != NULL)
					cb(account, NULL, error, storage->user_data);
				g_error_free(error);
				break;
		}

	} else if (password == NULL) {
		error = g_error_new(PURPLE_KEYRING_ERROR,
		                    PURPLE_KEYRING_ERROR_NOPASSWORD,
		                    "No password found for account: %s",
		                    purple_account_get_username(account));
		if (cb != NULL)
			cb(account, NULL, error, storage->user_data);
		g_error_free(error);

	} else {
		if (cb != NULL)
			cb(account, password, NULL, storage->user_data);
	}

	g_free(storage);
}

static void
ss_read(PurpleAccount *account, PurpleKeyringReadCallback cb, gpointer data)
{
	InfoStorage *storage = g_new0(InfoStorage, 1);

	storage->account = account;
	storage->cb = cb;
	storage->user_data = data;

	secret_password_lookup(&purple_schema,
	                       NULL, ss_read_continue, storage,
	                       "user", purple_account_get_username(account),
	                       "protocol", purple_account_get_protocol_id(account),
	                       NULL);
}

static void
ss_save_continue(GObject *object, GAsyncResult *result, gpointer data)
{
	InfoStorage *storage = data;
	PurpleKeyringSaveCallback cb;
	GError *error = NULL;
	PurpleAccount *account;

	account = storage->account;
	cb = storage->cb;

	secret_password_store_finish(result, &error);

	if (error != NULL) {
		int code = error->code;

		switch (code) {
			case G_DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND:
			case G_DBUS_ERROR_IO_ERROR:
				purple_debug_info("keyring-libsecret",
				                  "Failed to communicate with Secret Service (account : %s (%s)).\n",
				                  purple_account_get_username(account),
				                  purple_account_get_protocol_id(account));
				error = g_error_new(PURPLE_KEYRING_ERROR,
				                    PURPLE_KEYRING_ERROR_BACKENDFAIL,
				                    "Failed to communicate with Secret Service (account : %s).",
				                    purple_account_get_username(account));
				if (cb != NULL)
					cb(account, error, storage->user_data);
				g_error_free(error);
				break;

			default:
				purple_debug_error("keyring-libsecret",
				                  "Unknown error (account: %s (%s), domain: %s, code: %d): %s.\n",
				                  purple_account_get_username(account),
				                  purple_account_get_protocol_id(account),
				                  g_quark_to_string(error->domain), code, error->message);
				error = g_error_new(PURPLE_KEYRING_ERROR,
				                    PURPLE_KEYRING_ERROR_BACKENDFAIL,
				                    "Unknown error (account : %s).",
				                    purple_account_get_username(account));
				if (cb != NULL)
					cb(account, error, storage->user_data);
				g_error_free(error);
				break;
		}

	} else {
		purple_debug_info("keyring-libsecret", "Password for %s updated.\n",
			purple_account_get_username(account));

		if (cb != NULL)
			cb(account, NULL, storage->user_data);
	}

	g_free(storage);
}

static void
ss_save(PurpleAccount *account,
         const gchar *password,
         PurpleKeyringSaveCallback cb,
         gpointer data)
{
	InfoStorage *storage = g_new0(InfoStorage, 1);

	storage->account = account;
	storage->cb = cb;
	storage->user_data = data;

	if (password != NULL && *password != '\0') {
		const char *username = purple_account_get_username(account);
		char *label;

		purple_debug_info("keyring-libsecret",
			"Updating password for account %s (%s).\n",
			username, purple_account_get_protocol_id(account));

		label = g_strdup_printf(_("Pidgin IM password for account %s"), username);
		secret_password_store(&purple_schema, SECRET_COLLECTION_DEFAULT,
		                      label, password,
		                      NULL, ss_save_continue, storage,
		                      "user", username,
		                      "protocol", purple_account_get_protocol_id(account),
		                      NULL);
		g_free(label);

	} else {	/* password == NULL, delete password. */
		purple_debug_info("keyring-libsecret",
			"Forgetting password for account %s (%s).\n",
			purple_account_get_username(account),
			purple_account_get_protocol_id(account));

		secret_password_clear(&purple_schema, NULL, ss_save_continue, storage,
		                      "user", purple_account_get_username(account),
		                      "protocol", purple_account_get_protocol_id(account),
		                      NULL);
	}
}

static void
ss_close(void)
{
}

static gboolean
ss_init(void)
{
	keyring_handler = purple_keyring_new();

	purple_keyring_set_name(keyring_handler, SECRETSERVICE_NAME);
	purple_keyring_set_id(keyring_handler, SECRETSERVICE_ID);
	purple_keyring_set_read_password(keyring_handler, ss_read);
	purple_keyring_set_save_password(keyring_handler, ss_save);
	purple_keyring_set_close_keyring(keyring_handler, ss_close);

	purple_keyring_register(keyring_handler);

	return TRUE;
}

static void
ss_uninit(void)
{
	ss_close();
	purple_keyring_unregister(keyring_handler);
	purple_keyring_free(keyring_handler);
	keyring_handler = NULL;
}

/***********************************************/
/*     Plugin interface                        */
/***********************************************/

static gboolean
ss_load(PurplePlugin *plugin)
{
	return ss_init();
}

static gboolean
ss_unload(PurplePlugin *plugin)
{
	if (purple_keyring_get_inuse() == keyring_handler)
		return FALSE;

	ss_uninit();

	return TRUE;
}

PurplePluginInfo plugininfo =
{
	PURPLE_PLUGIN_MAGIC,		/* magic */
	PURPLE_MAJOR_VERSION,		/* major_version */
	PURPLE_MINOR_VERSION,		/* minor_version */
	PURPLE_PLUGIN_STANDARD,		/* type */
	NULL,						/* ui_requirement */
	PURPLE_PLUGIN_FLAG_INVISIBLE,	/* flags */
	NULL,						/* dependencies */
	PURPLE_PRIORITY_DEFAULT,	/* priority */
	SECRETSERVICE_ID,			/* id */
	SECRETSERVICE_NAME,			/* name */
	DISPLAY_VERSION,			/* version */
	"Secret Service Plugin",		/* summary */
	N_("This plugin will store passwords in Secret Service."),	/* description */
	"Elliott Sales de Andrade (qulogic[at]pidgin.im)",		/* author */
	PURPLE_WEBSITE,				/* homepage */
	ss_load,					/* load */
	ss_unload,					/* unload */
	NULL,						/* destroy */
	NULL,						/* ui_info */
	NULL,						/* extra_info */
	NULL,						/* prefs_info */
	NULL,						/* actions */
	NULL,						/* padding... */
	NULL,
	NULL,
	NULL,
};

static void
init_plugin(PurplePlugin *plugin)
{
}

PURPLE_INIT_PLUGIN(secret_service, init_plugin, plugininfo)