-
Notifications
You must be signed in to change notification settings - Fork 8
/
gnome-keyring.c
297 lines (266 loc) · 10.7 KB
/
gnome-keyring.c
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
#define PURPLE_PLUGINS
#ifndef VERSION
#define VERSION "experimental"
#endif
#include <plugin.h>
#include <version.h>
#include <account.h>
#include <signal.h>
#include <core.h>
#include <libsecret/secret.h>
#include <glib.h>
#include <string.h>
/* function prototypes */
static void keyring_password_store(PurpleAccount *account, gchar *password);
static void sign_in_cb(PurpleAccount *account, gpointer data);
static void keyring_password_find_cb(GObject *source, GAsyncResult *res,
gpointer data);
static void keyring_password_store_cb(GObject *source, GAsyncResult *res,
gpointer data);
static void connecting_cb(PurpleAccount *account, gpointer data);
static void memory_clearing_function(PurpleAccount *account);
static PurplePluginPrefFrame * get_pref_frame(PurplePlugin *plugin);
/* function definitions */
/* function called when the plugin starts */
static gboolean plugin_load(PurplePlugin *plugin) {
GList *accountsList;
void *accountshandle = purple_accounts_get_handle();
/* notFound will be a list of accounts not found
* in the keyring */
GList *notFound = NULL;
GList *notFound_iter;
/* The first thing to do is set all the passwords.
* This part is purposely written to be locking. If pidgin
* tries to connect without a password it will result in annoying
* prompts */
for (accountsList = purple_accounts_get_all();
accountsList != NULL;
accountsList = accountsList->next) {
PurpleAccount *account = (PurpleAccount *)accountsList->data;
gchar *password;
GError *error = NULL;
/* if the password exists in the keyring, set it in pidgin */
password = secret_password_lookup_sync(SECRET_SCHEMA_COMPAT_NETWORK,
NULL, &error, "user", account->username,
"protocol", account->protocol_id, NULL);
if (error != NULL) {
fprintf(stderr, "lookup_sync error in plugin_load: %s\n",
error->message);
g_error_free(error);
} else if (password != NULL) {
/* set the account to not remember passwords */
purple_account_set_remember_password(account, FALSE);
/* temporarily set a fake password, then the real one */
purple_account_set_password(account, "fakedoopdeedoop");
purple_account_set_password(account, password);
secret_password_free(password);
}
else {
/* add to the list of accounts not found in the keyring */
notFound = g_list_append(notFound, account);
}
}
/* for the acccounts which were not found in the keyring */
for (notFound_iter = g_list_first(notFound);
notFound_iter != NULL;
notFound_iter = notFound_iter->next) {
PurpleAccount *account = (PurpleAccount *)notFound_iter->data;
/* if the password was saved by libpurple before then
* save it in the keyring, and tell libpurple to forget it */
if (purple_account_get_remember_password(account)) {
gchar *password = g_strdup(account->password);
keyring_password_store(account, password);
purple_account_set_remember_password(account, FALSE);
/* temporarily set a fake password, then the real one again */
purple_account_set_password(account, "fakedoopdeedoop");
purple_account_set_password(account, password);
g_free(password);
}
}
/* done with the notFound, so free it */
g_list_free(notFound);
/* create a signal which monitors whenever an account signs in,
* so that the callback function can store/update the password */
purple_signal_connect(accountshandle, "account-signed-on", plugin,
PURPLE_CALLBACK(sign_in_cb), NULL);
/* create a signal which monitors whenever an account tries to connect
* so that the callback can make sure the password is set in pidgin */
purple_signal_connect(accountshandle, "account-connecting", plugin,
PURPLE_CALLBACK(connecting_cb), NULL);
/* at this point, the plugin is set up */
return TRUE;
}
/* callback to whenever an account is signed in */
static void sign_in_cb(PurpleAccount *account, gpointer data) {
/* Get the password.
* The callback will check to see if it is already
* saved in the keyring.
* This will be run every time an account signs in. */
secret_password_lookup(SECRET_SCHEMA_COMPAT_NETWORK,
NULL, keyring_password_find_cb,
account,
"user", account->username,
"protocol", account->protocol_id,
NULL);
}
/* callback to gnome keyring password finder
* Will sync the password between the keyring and pidgin */
static void keyring_password_find_cb(GObject *source, GAsyncResult *res,
gpointer data) {
GError *error = NULL;
gchar *password = secret_password_lookup_finish(res, &error);
PurpleAccount *account = (PurpleAccount *)data;
gboolean remember = purple_account_get_remember_password(account);
/* set the purple account to not remember passwords */
purple_account_set_remember_password(account, FALSE);
/* if the password was not found in the keyring
* and the password exists in pidgin
* and the password was set to be remembered
*/
if (error != NULL) {
fprintf(stderr, "lookup_finish error in find_cb: %s\n",
error->message);
g_error_free(error);
return;
}
if (password == NULL &&
account->password != NULL
&& remember) {
/* copy it from pidgin to the keyring */
keyring_password_store(account, account->password);
return;
}
/* if the stored passwords do not match */
if (password != NULL) {
if (account->password != NULL &&
strcmp(password, account->password) != 0) {
/* update the keyring with the pidgin password */
keyring_password_store(account, account->password);
secret_password_free(password);
return;
}
secret_password_free(password);
}
/* if this code is executed, it means that keyring_password_store was
* not called, so the memory_clearing_function needs to be called now
*/
memory_clearing_function(account);
}
/* store a password in the keyring */
static void keyring_password_store(PurpleAccount *account,
gchar *password) {
secret_password_store(
SECRET_SCHEMA_COMPAT_NETWORK,
purple_prefs_get_string("/plugins/core/gnome-keyring/keyring_name"),
"pidgin account password",
password, NULL,
keyring_password_store_cb, account,
"user", account->username,
"protocol", account->protocol_id,
NULL);
}
/* this is mainly here for stability issues because the program may
* crash if there is no callback function.
* It does not actually do anything */
static void keyring_password_store_cb(GObject *source, GAsyncResult *res,
gpointer data) {
GError *error = NULL;
secret_password_store_finish(res, &error);
if (error != NULL) {
fprintf(stderr, "store_finish error in store_cb: %s\n",
error->message);
g_error_free(error);
} else {
PurpleAccount *account = (PurpleAccount *)data;
memory_clearing_function(account);
}
return;
}
static void memory_clearing_function(PurpleAccount *account) {
gboolean clear_memory = purple_prefs_get_bool(
"/plugins/core/gnome-keyring/clear_memory");
if (clear_memory) {
if (account->password != NULL) {
g_free(account->password);
account->password = NULL;
}
}
}
/* callback to whenever a function tries to connect
* this needs to ensure that there is a password
* this may happen if the password was disabled, then later re-enabled */
static void connecting_cb(PurpleAccount *account, gpointer data) {
if (account->password == NULL) {
gchar *password;
GError *error = NULL;
password = secret_password_lookup_sync(SECRET_SCHEMA_COMPAT_NETWORK,
NULL, &error, "user", account->username,
"protocol", account->protocol_id, NULL);
if (error != NULL) {
fprintf(stderr, "lookup_sync error in connectinb_cb: %s\n",
error->message);
g_error_free(error);
} else if (password != NULL) {
purple_account_set_password(account, password);
secret_password_free(password);
}
}
}
static gboolean plugin_unload(PurplePlugin *plugin) {
/* disconnect from signals */
void *accounts_handle = purple_accounts_get_handle();
purple_signal_disconnect(accounts_handle, "account-signed-on",
plugin, NULL);
purple_signal_disconnect(accounts_handle, "account-connecting",
plugin, NULL);
return TRUE;
}
static PurplePluginUiInfo prefs_info = {
get_pref_frame, 0, NULL, NULL, NULL, NULL, NULL
};
static PurplePluginPrefFrame * get_pref_frame(PurplePlugin *plugin) {
PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new();
gchar *label = g_strdup_printf("Clear plaintext passwords from memory");
PurplePluginPref *pref = purple_plugin_pref_new_with_name_and_label(
"/plugins/core/gnome-keyring/clear_memory",
label);
purple_plugin_pref_frame_add(frame, pref);
purple_plugin_pref_frame_add(frame,
purple_plugin_pref_new_with_name_and_label(
"/plugins/core/gnome-keyring/keyring_name", "Keyring name"
)
);
return frame;
}
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC, PURPLE_MAJOR_VERSION, PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD,
NULL,
0,
NULL,
PURPLE_PRIORITY_HIGHEST,
"core-gnome-keyring",
"Password Keyring",
VERSION,
"Save pidgin passwords to the system keyring instead of as plaintext",
"Save pidgin passwords to the system keyring instead of as plaintext",
"Ali Ebrahim",
"https://github.com/aebrahim/pidgin-gnome-keyring/",
plugin_load,
plugin_unload,
NULL,
NULL,
NULL,
&prefs_info,
NULL,
NULL,
NULL,
NULL,
NULL
};
static void init_plugin(PurplePlugin *plugin) {
purple_prefs_add_none("/plugins/core/gnome-keyring");
purple_prefs_add_bool("/plugins/core/gnome-keyring/clear_memory", FALSE);
purple_prefs_add_string("/plugins/core/gnome-keyring/keyring_name", SECRET_COLLECTION_DEFAULT);
}
PURPLE_INIT_PLUGIN(gnome-keyring, init_plugin, info)