2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2016 The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
25 #ifdef PASSWORD_CRYPTO_GNUTLS
26 # include <gnutls/gnutls.h>
27 # include <gnutls/crypto.h>
31 #include <glib/gi18n.h>
33 #include "common/defs.h"
34 #include "common/utils.h"
35 #include "passwordstore.h"
37 #include "prefs_common.h"
38 #include "prefs_gtk.h"
39 #include "prefs_migration.h"
41 static GSList *_password_store;
43 /* Finds password block of given type and name in the pwdstore. */
44 static PasswordBlock *_get_block(PasswordBlockType block_type,
45 const gchar *block_name)
50 g_return_val_if_fail(block_type < NUM_PWS_TYPES, NULL);
51 g_return_val_if_fail(block_name != NULL, NULL);
53 for (item = _password_store; item != NULL; item = item->next) {
54 block = (PasswordBlock *)item->data;
55 if (block->block_type == block_type &&
56 !strcmp(block->block_name, block_name))
63 static gboolean _hash_equal_func(gconstpointer a, gconstpointer b)
65 if (g_strcmp0((const gchar *)a, (const gchar *)b) == 0)
70 /* Creates a new, empty block and adds it to the pwdstore. */
71 static PasswordBlock *_new_block(PasswordBlockType block_type,
72 const gchar *block_name)
76 g_return_val_if_fail(block_type < NUM_PWS_TYPES, NULL);
77 g_return_val_if_fail(block_name != NULL, NULL);
79 /* First check to see if the block doesn't already exist. */
80 if (_get_block(block_type, block_name)) {
81 debug_print("Block (%d/%s) already exists.\n",
82 block_type, block_name);
86 /* Let's create an empty block, and add it to pwdstore. */
87 block = g_new0(PasswordBlock, 1);
88 block->block_type = block_type;
89 block->block_name = g_strdup(block_name);
90 block->entries = g_hash_table_new_full(g_str_hash,
91 (GEqualFunc)_hash_equal_func,
93 debug_print("Created password block (%d/%s)\n",
94 block_type, block_name);
96 _password_store = g_slist_append(_password_store, block);
101 /*************************************************************/
103 /* Stores a password. */
104 gboolean passwd_store_set(PasswordBlockType block_type,
105 const gchar *block_name,
106 const gchar *password_id,
107 const gchar *password,
111 PasswordBlock *block;
112 gchar *encrypted_password;
114 g_return_val_if_fail(block_type < NUM_PWS_TYPES, FALSE);
115 g_return_val_if_fail(block_name != NULL, FALSE);
116 g_return_val_if_fail(password_id != NULL, FALSE);
118 /* Empty password string equals null password for us. */
119 if (password == NULL || strlen(password) == 0)
124 debug_print("%s password '%s' in block (%d/%s)%s\n",
125 (p == NULL ? "Deleting" : "Storing"),
126 password_id, block_type, block_name,
127 (encrypted ? ", already encrypted" : "") );
129 /* find correct block (create if needed) */
130 if ((block = _get_block(block_type, block_name)) == NULL) {
131 /* If caller wants to delete a password, and even its block
132 * doesn't exist, we're done. */
136 if ((block = _new_block(block_type, block_name)) == NULL) {
137 debug_print("Could not create password block (%d/%s)\n",
138 block_type, block_name);
144 /* NULL password was passed to us, so delete the entry with
145 * corresponding id */
146 g_hash_table_remove(block->entries, password_id);
149 /* encrypt password before saving it */
150 if ((encrypted_password =
151 password_encrypt(p, NULL)) == NULL) {
152 debug_print("Could not encrypt password '%s' for block (%d/%s).\n",
153 password_id, block_type, block_name);
157 /* password is already in encrypted form already */
158 encrypted_password = g_strdup(p);
161 /* add encrypted password to the block */
162 g_hash_table_insert(block->entries,
163 g_strdup(password_id),
170 /* Retrieves a password. */
171 gchar *passwd_store_get(PasswordBlockType block_type,
172 const gchar *block_name,
173 const gchar *password_id)
175 PasswordBlock *block;
176 gchar *encrypted_password, *password;
178 g_return_val_if_fail(block_type < NUM_PWS_TYPES, NULL);
179 g_return_val_if_fail(block_name != NULL, NULL);
180 g_return_val_if_fail(password_id != NULL, NULL);
182 debug_print("Getting password '%s' from block (%d/%s)\n",
183 password_id, block_type, block_name);
185 /* find correct block */
186 if ((block = _get_block(block_type, block_name)) == NULL) {
187 debug_print("Block (%d/%s) not found.\n", block_type, block_name);
191 /* grab pointer to encrypted password */
192 if ((encrypted_password =
193 g_hash_table_lookup(block->entries, password_id)) == NULL) {
194 debug_print("Password '%s' in block (%d/%s) not found.\n",
195 password_id, block_type, block_name);
199 /* decrypt password */
201 password_decrypt(encrypted_password, NULL)) == NULL) {
202 debug_print("Could not decrypt password '%s' for block (%d/%s).\n",
203 password_id, block_type, block_name);
207 /* return decrypted password */
211 gboolean passwd_store_delete_block(PasswordBlockType block_type,
212 const gchar *block_name)
214 PasswordBlock *block;
216 g_return_val_if_fail(block_type < NUM_PWS_TYPES, FALSE);
217 g_return_val_if_fail(block_name != NULL, FALSE);
219 debug_print("Deleting block (%d/%s)\n", block_type, block_name);
221 /* find correct block */
222 if ((block = _get_block(block_type, block_name)) == NULL) {
223 debug_print("Block (%d/%s) not found.\n", block_type, block_name);
227 g_hash_table_destroy(block->entries);
228 block->entries = NULL;
232 gboolean passwd_store_set_account(gint account_id,
233 const gchar *password_id,
234 const gchar *password,
237 gchar *uid = g_strdup_printf("%d", account_id);
238 gboolean ret = passwd_store_set(PWS_ACCOUNT, uid,
239 password_id, password, encrypted);
244 gchar *passwd_store_get_account(gint account_id,
245 const gchar *password_id)
247 gchar *uid = g_strdup_printf("%d", account_id);
248 gchar *ret = passwd_store_get(PWS_ACCOUNT, uid, password_id);
253 /* Reencrypts all stored passwords. */
254 void passwd_store_reencrypt_all(const gchar *old_mpwd,
255 const gchar *new_mpwd)
257 PasswordBlock *block;
260 gchar *encrypted_password, *decrypted_password, *key;
262 g_return_if_fail(old_mpwd != NULL);
263 g_return_if_fail(new_mpwd != NULL);
265 for (item = _password_store; item != NULL; item = item->next) {
266 block = (PasswordBlock *)item->data;
268 continue; /* Just in case. */
270 debug_print("Reencrypting passwords in block (%d/%s).\n",
271 block->block_type, block->block_name);
273 if (g_hash_table_size(block->entries) == 0)
276 keys = g_hash_table_get_keys(block->entries);
277 for (eitem = keys; eitem != NULL; eitem = eitem->next) {
278 key = (gchar *)eitem->data;
279 if ((encrypted_password =
280 g_hash_table_lookup(block->entries, key)) == NULL)
283 if ((decrypted_password =
284 password_decrypt(encrypted_password, old_mpwd)) == NULL) {
285 debug_print("Reencrypt: couldn't decrypt password for '%s'.\n", key);
289 encrypted_password = password_encrypt(decrypted_password, new_mpwd);
290 memset(decrypted_password, 0, strlen(decrypted_password));
291 g_free(decrypted_password);
292 if (encrypted_password == NULL) {
293 debug_print("Reencrypt: couldn't encrypt password for '%s'.\n", key);
297 g_hash_table_insert(block->entries, g_strdup(key), encrypted_password);
303 debug_print("Reencrypting done.\n");
306 static gint _write_to_file(FILE *fp)
308 PasswordBlock *block;
311 gchar *typestr, *line, *key, *pwd;
313 /* Write out the config_version */
314 line = g_strdup_printf("[config_version:%d]\n", CLAWS_CONFIG_VERSION);
315 if (fputs(line, fp) == EOF) {
316 FILE_OP_ERROR("password store, config_version", "fputs");
322 /* Add a newline if needed */
323 if (_password_store != NULL && fputs("\n", fp) == EOF) {
324 FILE_OP_ERROR("password store", "fputs");
328 /* Write out each password store block */
329 for (item = _password_store; item != NULL; item = item->next) {
330 block = (PasswordBlock*)item->data;
332 continue; /* Just in case. */
334 /* Do not save empty blocks. */
335 if (g_hash_table_size(block->entries) == 0)
338 /* Prepare the section header string and write it out. */
340 if (block->block_type == PWS_CORE) {
342 } else if (block->block_type == PWS_ACCOUNT) {
344 } else if (block->block_type == PWS_PLUGIN) {
347 line = g_strdup_printf("[%s:%s]\n", typestr, block->block_name);
349 if (fputs(line, fp) == EOF) {
350 FILE_OP_ERROR("password store", "fputs");
356 /* Now go through all passwords in the block and write each out. */
357 keys = g_hash_table_get_keys(block->entries);
358 for (eitem = keys; eitem != NULL; eitem = eitem->next) {
359 key = (gchar *)eitem->data;
360 if ((pwd = g_hash_table_lookup(block->entries, key)) == NULL)
363 line = g_strdup_printf("%s %s\n", key, pwd);
364 if (fputs(line, fp) == EOF) {
365 FILE_OP_ERROR("password store", "fputs");
373 /* Add a separating new line if there is another block remaining */
374 if (item->next != NULL && fputs("\n", fp) == EOF) {
375 FILE_OP_ERROR("password store", "fputs");
384 void passwd_store_write_config(void)
389 debug_print("Writing password store...\n");
391 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
392 PASSWORD_STORE_RC, NULL);
394 if ((pfile = prefs_write_open(rcpath)) == NULL) {
395 g_warning("failed to open password store file for writing");
402 if (_write_to_file(pfile->fp) < 0) {
403 g_warning("failed to write password store to file");
404 prefs_file_close_revert(pfile);
405 } else if (prefs_file_close(pfile) < 0) {
406 g_warning("failed to properly close password store file after writing");
410 int passwd_store_read_config(void)
412 gchar *rcpath, *contents, **lines, **line, *typestr, *name;
413 GError *error = NULL;
415 PasswordBlock *block = NULL;
416 PasswordBlockType type;
417 gboolean reading_config_version = FALSE;
418 gint config_version = -1;
420 /* TODO: passwd_store_clear(); */
422 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
423 PASSWORD_STORE_RC, NULL);
425 debug_print("Reading password store from file '%s'\n", rcpath);
427 if (!g_file_test(rcpath, G_FILE_TEST_EXISTS)) {
428 debug_print("File does not exist, looks like a new configuration.\n");
433 if (!g_file_get_contents(rcpath, &contents, NULL, &error)) {
434 g_warning("couldn't read password store from file: %s", error->message);
441 lines = g_strsplit(contents, "\n", -1);
445 while (lines[i] != NULL) {
446 if (*lines[i] == '[') {
447 /* Beginning of a new block */
448 line = g_strsplit_set(lines[i], "[:]", -1);
449 if (line[0] != NULL && strlen(line[0]) == 0
450 && line[1] != NULL && strlen(line[1]) > 0
451 && line[2] != NULL && strlen(line[2]) > 0
452 && line[3] != NULL && strlen(line[3]) == 0) {
455 if (!strcmp(typestr, "core")) {
457 } else if (!strcmp(typestr, "account")) {
459 } else if (!strcmp(typestr, "plugin")) {
461 } else if (!strcmp(typestr, "config_version")) {
462 reading_config_version = TRUE;
463 config_version = atoi(name);
465 debug_print("Unknown password block type: '%s'\n", typestr);
470 if (reading_config_version) {
471 if (config_version < 0) {
472 debug_print("config_version:%d looks invalid, ignoring it\n",
474 config_version = -1; /* set to default value if missing */
477 debug_print("config_version in file is %d\n", config_version);
478 reading_config_version = FALSE;
480 if ((block = _new_block(type, name)) == NULL) {
481 debug_print("Duplicate password block, ignoring: (%d/%s)\n",
489 } else if (strlen(lines[i]) > 0 && block != NULL) {
490 /* If we have started a password block, test for a
491 * "password_id = password" line. */
492 line = g_strsplit(lines[i], " ", -1);
493 if (line[0] != NULL && strlen(line[0]) > 0
494 && line[1] != NULL && strlen(line[1]) > 0
495 && line[2] == NULL) {
496 debug_print("Adding password '%s'\n", line[0]);
497 g_hash_table_insert(block->entries,
498 g_strdup(line[0]), g_strdup(line[1]));
506 if (prefs_update_config_version_password_store(config_version) < 0) {
507 debug_print("Password store configuration file version upgrade failed\n");
511 return g_slist_length(_password_store);