diff options
| author | Simeon Simeonov | 2018-03-08 15:01:45 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2018-03-08 15:01:45 +0100 |
| commit | 57944f6147f077ccf4d84b9d2659f4e8645e8858 (patch) | |
| tree | b5a4f7af257058fb602d36590568ffd06681f6cd /libsylph/masterpassword.c | |
| parent | 9bcc1f9650eb2d9dff7a78c632dcc14dfcfac9ee (diff) | |
Reastructure master password functionality
Diffstat (limited to 'libsylph/masterpassword.c')
| -rw-r--r-- | libsylph/masterpassword.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/libsylph/masterpassword.c b/libsylph/masterpassword.c new file mode 100644 index 0000000..ee443aa --- /dev/null +++ b/libsylph/masterpassword.c | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | /* | ||
| 2 | * LibSylph -- E-Mail client library | ||
| 3 | * Copyright (C) 1999-2018 Hiroyuki Yamamoto | ||
| 4 | * | ||
| 5 | * This library is free software; you can redistribute it and/or | ||
| 6 | * modify it under the terms of the GNU Lesser General Public | ||
| 7 | * License as published by the Free Software Foundation; either | ||
| 8 | * version 2.1 of the License, or (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This library 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 GNU | ||
| 13 | * Lesser General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU Lesser General Public | ||
| 16 | * License along with this library; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifdef HAVE_CONFIG_H | ||
| 21 | #include "config.h" | ||
| 22 | #endif | ||
| 23 | |||
| 24 | #include "prefs_common.h" | ||
| 25 | #include "ssl.h" | ||
| 26 | #include "utils.h" | ||
| 27 | |||
| 28 | |||
| 29 | gchar *master_password; | ||
| 30 | |||
| 31 | void set_master_password(const char *password) { | ||
| 32 | master_password = password; | ||
| 33 | } | ||
| 34 | |||
| 35 | gchar *get_master_password(void) { | ||
| 36 | return master_password; | ||
| 37 | } | ||
| 38 | |||
| 39 | #if USE_SSL | ||
| 40 | |||
| 41 | gint set_master_password_interactively(guint max_attempts) { | ||
| 42 | |||
| 43 | if (master_password != NULL) | ||
| 44 | return 0; /* master_password already set */ | ||
| 45 | |||
| 46 | master_password = input_set_new_password(max_attempts); | ||
| 47 | |||
| 48 | if (master_password == NULL) | ||
| 49 | return 1; | ||
| 50 | |||
| 51 | if (generate_password_hash( | ||
| 52 | &prefs_common.master_password_hash, | ||
| 53 | master_password, | ||
| 54 | NULL) != RC_OK) { | ||
| 55 | /* should not really happen unless buggy code / library */ | ||
| 56 | g_free(prefs_common.master_password_hash); | ||
| 57 | prefs_common.master_password_hash = NULL; | ||
| 58 | debug_print(_("Could not generate master password hash")); | ||
| 59 | return 1; | ||
| 60 | } | ||
| 61 | |||
| 62 | prefs_common_write_config(); | ||
| 63 | return 0; | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | gint check_master_password_interactively(guint max_attempts) { | ||
| 68 | |||
| 69 | guint cnt; | ||
| 70 | |||
| 71 | if (max_attempts < 1) | ||
| 72 | return 1; | ||
| 73 | |||
| 74 | if (prefs_common.master_password_hash != NULL) { | ||
| 75 | return 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | if (master_password != NULL) { | ||
| 79 | /* password already cached */ | ||
| 80 | return check_password(master_password, | ||
| 81 | prefs_common.master_password_hash); | ||
| 82 | } | ||
| 83 | |||
| 84 | for (cnt = 0; cnt < max_attempts; ++cnt) { | ||
| 85 | master_password = input_query_master_password(); | ||
| 86 | if (check_password(master_password, | ||
| 87 | prefs_common.master_password_hash) == RC_OK) { | ||
| 88 | return RC_OK; /* match */ | ||
| 89 | } | ||
| 90 | debug_print(_("Wrong master password entered (%d)\n"), cnt); | ||
| 91 | /* TODO: clear before free? */ | ||
| 92 | g_free(master_password); | ||
| 93 | master_password = NULL; | ||
| 94 | } | ||
| 95 | |||
| 96 | return 1; /* no match */ | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | #endif /* USE_SSL */ | ||
