From fa7bec8685ac1b2dfda2c113ed5a876f33c506a7 Mon Sep 17 00:00:00 2001 From: Andrej Kacian Date: Sun, 1 Jul 2018 21:56:16 +0200 Subject: [PATCH] Added a password store function to check if a password exists. --- src/passwordstore.c | 36 ++++++++++++++++++++++++++++++++++++ src/passwordstore.h | 11 ++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/passwordstore.c b/src/passwordstore.c index 5709f1577..97f44d3f5 100644 --- a/src/passwordstore.c +++ b/src/passwordstore.c @@ -208,6 +208,33 @@ gchar *passwd_store_get(PasswordBlockType block_type, return password; } +/* Checks if a password exists in the password store. + * No decryption happens. */ +gboolean passwd_store_has_password(PasswordBlockType block_type, + const gchar *block_name, + const gchar *password_id) +{ + PasswordBlock *block; + + g_return_val_if_fail(block_type < NUM_PWS_TYPES, FALSE); + g_return_val_if_fail(block_name != NULL, FALSE); + g_return_val_if_fail(password_id != NULL, FALSE); + + /* find correct block */ + if ((block = _get_block(block_type, block_name)) == NULL) { + debug_print("Block (%d/%s) not found.\n", block_type, block_name); + return FALSE; + } + + /* do we have specified password in this block? */ + if (g_hash_table_lookup(block->entries, password_id) != NULL) { + return TRUE; /* yes */ + } + + return FALSE; /* no */ +} + + gboolean passwd_store_delete_block(PasswordBlockType block_type, const gchar *block_name) { @@ -250,6 +277,15 @@ gchar *passwd_store_get_account(gint account_id, return ret; } +gboolean passwd_store_has_password_account(gint account_id, + const gchar *password_id) +{ + gchar *uid = g_strdup_printf("%d", account_id); + gboolean ret = passwd_store_has_password(PWS_ACCOUNT, uid, password_id); + g_free(uid); + return ret; +} + /* Reencrypts all stored passwords. */ void passwd_store_reencrypt_all(const gchar *old_mpwd, const gchar *new_mpwd) diff --git a/src/passwordstore.h b/src/passwordstore.h index aeaa40d56..b9198207e 100644 --- a/src/passwordstore.h +++ b/src/passwordstore.h @@ -57,6 +57,12 @@ gchar *passwd_store_get(PasswordBlockType block_type, const gchar *block_name, const gchar *password_id); +/* Returns TRUE if such password exists in the password store, + * false otherwise. No decryption happens. */ +gboolean passwd_store_has_password(PasswordBlockType block_type, + const gchar *block_name, + const gchar *password_id); + gboolean passwd_store_delete_block(PasswordBlockType block_type, const gchar *block_name); @@ -76,7 +82,10 @@ gboolean passwd_store_set_account(gint account_id, const gchar *password_id, const gchar *password, gboolean encrypted); -gchar *passwd_store_get_account(gint account_id, const gchar *block_name); +gchar *passwd_store_get_account(gint account_id, + const gchar *password_id); +gboolean passwd_store_has_password_account(gint account_id, + const gchar *password_id); /* Macros for standard, predefined password IDs. */ #define PWS_ACCOUNT_RECV "recv" -- 2.25.1