8e3942a314f37303b47a676b597503dbc3236c81
[claws.git] / src / passwordstore.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2016 The Claws Mail Team
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #ifdef PASSWORD_CRYPTO_GNUTLS
26 # include <gnutls/gnutls.h>
27 # include <gnutls/crypto.h>
28 #endif
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32
33 #include "common/defs.h"
34 #include "common/utils.h"
35 #include "passwordstore.h"
36 #include "password.h"
37 #include "prefs_gtk.h"
38
39 static GSList *_password_store;
40
41 /* Finds password block of given type and name in the pwdstore. */
42 static PasswordBlock *_get_block(PasswordBlockType block_type,
43                 const gchar *block_name)
44 {
45         GSList *item;
46         PasswordBlock *block;
47
48         g_return_val_if_fail(block_type >= 0 && block_type < NUM_PWS_TYPES,
49                         NULL);
50         g_return_val_if_fail(block_name != NULL, NULL);
51
52         for (item = _password_store; item != NULL; item = item->next) {
53                 block = (PasswordBlock *)item->data;
54                 if (block->block_type == block_type &&
55                                 !strcmp(block->block_name, block_name))
56                         return block;
57         }
58
59         return NULL;
60 }
61
62 static gboolean _hash_equal_func(gconstpointer a, gconstpointer b)
63 {
64         if (g_strcmp0((const gchar *)a, (const gchar *)b) == 0)
65                 return TRUE;
66         return FALSE;
67 }
68
69 /* Creates a new, empty block and adds it to the pwdstore. */
70 static PasswordBlock *_new_block(PasswordBlockType block_type,
71                 const gchar *block_name)
72 {
73         PasswordBlock *block;
74
75         g_return_val_if_fail(block_type >= 0 && block_type < NUM_PWS_TYPES,
76                         NULL);
77         g_return_val_if_fail(block_name != NULL, NULL);
78
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);
83                 return NULL;
84         }
85
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,
92                         g_free, g_free);
93         debug_print("Created password block (%d/%s)\n",
94                         block_type, block_name);
95
96         _password_store = g_slist_append(_password_store, block);
97
98         return block;
99 }
100
101 ///////////////////////////////////////////////////////////////
102
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,
108                 gboolean encrypted)
109 {
110         PasswordBlock *block;
111         gchar *encrypted_password;
112
113         g_return_val_if_fail(block_type >= 0 && block_type < NUM_PWS_TYPES,
114                         FALSE);
115         g_return_val_if_fail(block_name != NULL, FALSE);
116         g_return_val_if_fail(password_id != NULL, FALSE);
117
118         debug_print("%s password '%s' in block (%d/%s)%s\n",
119                         (password == NULL ? "Deleting" : "Storing"),
120                         password_id, block_type, block_name,
121                         (encrypted ? ", already encrypted" : "") );
122
123         // find correct block (create if needed)
124         if ((block = _get_block(block_type, block_name)) == NULL &&
125                         (block = _new_block(block_type, block_name)) == NULL) {
126                 debug_print("Could not create password block (%d/%s)\n",
127                                 block_type, block_name);
128                 return FALSE;
129         }
130
131         if (password == NULL) {
132                 /* NULL password was passed to us, so delete the entry with
133                  * corresponding id */
134                 g_hash_table_remove(block->entries, password_id);
135         } else {
136                 if (!encrypted) {
137                         /* encrypt password before saving it */
138                         if ((encrypted_password =
139                                                 password_encrypt(password, NULL)) == NULL) {
140                                 debug_print("Could not encrypt password '%s' for block (%d/%s).\n",
141                                                 password_id, block_type, block_name);
142                                 return FALSE;
143                         }
144                 } else {
145                         /* password is already in encrypted form already */
146                         encrypted_password = g_strdup(password);
147                 }
148
149                 // add encrypted password to the block
150                 g_hash_table_insert(block->entries,
151                                 g_strdup(password_id),
152                                 encrypted_password);
153         }
154
155         return TRUE;
156 }
157
158 /* Retrieves a password. */
159 gchar *passwd_store_get(PasswordBlockType block_type,
160                 const gchar *block_name,
161                 const gchar *password_id)
162 {
163         PasswordBlock *block;
164         gchar *encrypted_password, *password;
165
166         g_return_val_if_fail(block_type >= 0 && block_type < NUM_PWS_TYPES,
167                         NULL);
168         g_return_val_if_fail(block_name != NULL, NULL);
169         g_return_val_if_fail(password_id != NULL, NULL);
170
171         debug_print("Getting password '%s' from block (%d/%s)\n",
172                         password_id, block_type, block_name);
173
174         // find correct block
175         if ((block = _get_block(block_type, block_name)) == NULL) {
176                 debug_print("Block (%d/%s) not found.\n", block_type, block_name);
177                 return NULL;
178         }
179
180         // grab pointer to encrypted password
181         if ((encrypted_password =
182                                 g_hash_table_lookup(block->entries, password_id)) == NULL) {
183                 debug_print("Password '%s' in block (%d/%s) not found.\n",
184                                 password_id, block_type, block_name);
185                 return NULL;
186         }
187
188         // decrypt password
189         if ((password =
190                                 password_decrypt(encrypted_password, NULL)) == NULL) {
191                 debug_print("Could not decrypt password '%s' for block (%d/%s).\n",
192                                 password_id, block_type, block_name);
193                 return NULL;
194         }
195
196         // return decrypted password
197         return password;
198 }
199
200 /* Reencrypts all stored passwords. */
201 void passwd_store_reencrypt_all(const gchar *old_mpwd,
202                 const gchar *new_mpwd)
203 {
204         PasswordBlock *block;
205         GSList *item;
206         GList *keys, *eitem;
207         gchar *encrypted_password, *decrypted_password, *key;
208
209         g_return_if_fail(old_mpwd != NULL);
210         g_return_if_fail(new_mpwd != NULL);
211
212         for (item = _password_store; item != NULL; item = item->next) {
213                 block = (PasswordBlock *)item->data;
214                 if (block == NULL)
215                         continue; /* Just in case. */
216
217                 debug_print("Reencrypting passwords in block (%d/%s).\n",
218                                 block->block_type, block->block_name);
219
220                 if (g_hash_table_size(block->entries) == 0)
221                         continue;
222
223                 keys = g_hash_table_get_keys(block->entries);
224                 for (eitem = keys; eitem != NULL; eitem = eitem->next) {
225                         key = (gchar *)eitem->data;
226                         if ((encrypted_password =
227                                                 g_hash_table_lookup(block->entries, key)) == NULL)
228                                 continue;
229
230                         if ((decrypted_password =
231                                                 password_decrypt(encrypted_password, old_mpwd)) == NULL) {
232                                 debug_print("Reencrypt: couldn't decrypt password for '%s'.\n", key);
233                                 continue;
234                         }
235
236                         encrypted_password = password_encrypt(decrypted_password, new_mpwd);
237                         memset(decrypted_password, 0, strlen(decrypted_password));
238                         g_free(decrypted_password);
239                         if (encrypted_password == NULL) {
240                                 debug_print("Reencrypt: couldn't encrypt password for '%s'.\n", key);
241                                 continue;
242                         }
243
244                         g_hash_table_insert(block->entries, g_strdup(key), encrypted_password);
245                 }
246
247                 g_list_free(keys);
248         }
249
250         debug_print("Reencrypting done.\n");
251 }
252
253 static gint _write_to_file(FILE *fp)
254 {
255         PasswordBlock *block;
256         GSList *item;
257         GList *keys, *eitem;
258         gchar *typestr, *line, *key, *pwd;
259
260         for (item = _password_store; item != NULL; item = item->next) {
261                 block = (PasswordBlock*)item->data;
262                 if (block == NULL)
263                         continue; /* Just in case. */
264
265                 /* Do not save empty blocks. */
266                 if (g_hash_table_size(block->entries) == 0)
267                         continue;
268
269                 /* Prepare the section header string and write it out. */
270                 typestr = NULL;
271                 if (block->block_type == PWS_CORE) {
272                         typestr = "core";
273                 } else if (block->block_type == PWS_ACCOUNT) {
274                         typestr = "account";
275                 } else if (block->block_type == PWS_PLUGIN) {
276                         typestr = "plugin";
277                 }
278                 line = g_strdup_printf("[%s:%s]\n", typestr, block->block_name);
279
280                 if (fputs(line, fp) == EOF) {
281                         FILE_OP_ERROR("password store", "fputs");
282                         g_free(line);
283                         return -1;
284                 }
285                 g_free(line);
286
287                 /* Now go through all passwords in the block and write each out. */
288                 keys = g_hash_table_get_keys(block->entries);
289                 for (eitem = keys; eitem != NULL; eitem = eitem->next) {
290                         key = (gchar *)eitem->data;
291                         if ((pwd = g_hash_table_lookup(block->entries, key)) == NULL)
292                                 continue;
293
294                         line = g_strdup_printf("%s %s\n", key, pwd);
295                         if (fputs(line, fp) == EOF) {
296                                 FILE_OP_ERROR("password store", "fputs");
297                                 g_free(line);
298                                 return -1;
299                         }
300                         g_free(line);
301                 }
302                 g_list_free(keys);
303
304                 if (item->next != NULL && fputs("\n", fp) == EOF) {
305                         FILE_OP_ERROR("password store", "fputs");
306                         return -1;
307                 }
308
309         }
310
311         return 1;
312 }
313
314 void passwd_store_write_config(void)
315 {
316         gchar *rcpath;
317         PrefFile *pfile;
318
319         debug_print("Writing password store...\n");
320
321         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
322                         PASSWORD_STORE_RC, NULL);
323
324         if ((pfile = prefs_write_open(rcpath)) == NULL) {
325                 g_warning("failed to open password store file for writing");
326                 g_free(rcpath);
327                 return;
328         }
329
330         g_free(rcpath);
331
332         if (_write_to_file(pfile->fp) < 0) {
333                 g_warning("failed to write password store to file");
334                 prefs_file_close_revert(pfile);
335         } else if (prefs_file_close(pfile) < 0) {
336                 g_warning("failed to properly close password store file after writing");
337         }
338 }
339
340 void passwd_store_read_config(void)
341 {
342         gchar *rcpath, *contents, **lines, **line, *typestr, *name;
343         GError *error = NULL;
344         guint i = 0;
345         PasswordBlock *block = NULL;
346         PasswordBlockType type;
347
348         // TODO: passwd_store_clear();
349
350         debug_print("Reading password store from file...\n");
351
352         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
353                         PASSWORD_STORE_RC, NULL);
354
355         if (!g_file_get_contents(rcpath, &contents, NULL, &error)) {
356                 g_warning("couldn't read password store from file: %s\n", error->message);
357                 g_error_free(error);
358                 g_free(rcpath);
359                 return;
360         }
361         g_free(rcpath);
362
363         lines = g_strsplit(contents, "\n", -1);
364
365         while (lines[i] != NULL) {
366                 if (*lines[i] == '[') {
367                         /* Beginning of a new block */
368                         line = g_strsplit_set(lines[i], "[:]", -1);
369                         if (line[0] != NULL && strlen(line[0]) == 0
370                                         && line[1] != NULL && strlen(line[1]) > 0
371                                         && line[2] != NULL && strlen(line[2]) > 0
372                                         && line[3] != NULL && strlen(line[3]) == 0) {
373                                 typestr = line[1];
374                                 name = line[2];
375                                 if (!strcmp(typestr, "core")) {
376                                         type = PWS_CORE;
377                                 } else if (!strcmp(typestr, "account")) {
378                                         type = PWS_ACCOUNT;
379                                 } else if (!strcmp(typestr, "plugin")) {
380                                         type = PWS_PLUGIN;
381                                 } else {
382                                         debug_print("Uknown password block type: '%s'\n", typestr);
383                                         g_strfreev(line);
384                                         i++; continue;
385                                 }
386
387                                 if ((block = _new_block(type, name)) == NULL) {
388                                         debug_print("Duplicate password block, ignoring: (%d/%s)\n",
389                                                         type, name);
390                                         g_strfreev(line);
391                                         i++; continue;
392                                 }
393                         }
394                         g_strfreev(line);
395                 } else if (strlen(lines[i]) > 0 && block != NULL) {
396                         /* If we have started a password block, test for a
397                          * "password_id = password" line. */
398                         line = g_strsplit(lines[i], " ", -1);
399                         if (line[0] != NULL && strlen(line[0]) > 0
400                                         && line[1] != NULL && strlen(line[1]) > 0
401                                         && line[2] == NULL) {
402                                 debug_print("Adding password '%s'\n", line[0]);
403                                 g_hash_table_insert(block->entries,
404                                                 g_strdup(line[0]), g_strdup(line[1]));
405                         }
406                         g_strfreev(line);
407                 }
408                 i++;
409         }
410         g_strfreev(lines);
411 }