Treat storing empty password same as storing NULL password.
authorAndrej Kacian <ticho@claws-mail.org>
Sat, 19 Mar 2016 20:07:41 +0000 (21:07 +0100)
committerAndrej Kacian <ticho@claws-mail.org>
Sat, 19 Mar 2016 20:08:24 +0000 (21:08 +0100)
(That means delete the password. This simplifies handling
scenario where user had a password set, but wants to delete
it by leaving corresponding GtkEntry empty.)

src/passwordstore.c
src/plugins/gdata/cm_gdata_contacts.c
src/plugins/spam_report/spam_report_prefs.c

index dfc96a06be5e57df3359f07da1cab4646e48f29c..4cdddd1c0bf2a926cf9d56a1376355a25e13180d 100644 (file)
@@ -107,6 +107,7 @@ gboolean passwd_store_set(PasswordBlockType block_type,
                const gchar *password,
                gboolean encrypted)
 {
+       const gchar *p = password;
        PasswordBlock *block;
        gchar *encrypted_password;
 
@@ -115,8 +116,12 @@ gboolean passwd_store_set(PasswordBlockType block_type,
        g_return_val_if_fail(block_name != NULL, FALSE);
        g_return_val_if_fail(password_id != NULL, FALSE);
 
+       /* Empty password string equals null password for us. */
+       if (strlen(password) == 0)
+               p = NULL;
+
        debug_print("%s password '%s' in block (%d/%s)%s\n",
-                       (password == NULL ? "Deleting" : "Storing"),
+                       (p == NULL ? "Deleting" : "Storing"),
                        password_id, block_type, block_name,
                        (encrypted ? ", already encrypted" : "") );
 
@@ -124,7 +129,7 @@ gboolean passwd_store_set(PasswordBlockType block_type,
        if ((block = _get_block(block_type, block_name)) == NULL) {
                /* If caller wants to delete a password, and even its block
                 * doesn't exist, we're done. */
-               if (password == NULL)
+               if (p == NULL)
                        return TRUE;
 
                if ((block = _new_block(block_type, block_name)) == NULL) {
@@ -134,7 +139,7 @@ gboolean passwd_store_set(PasswordBlockType block_type,
                }
        }
 
-       if (password == NULL) {
+       if (p == NULL) {
                /* NULL password was passed to us, so delete the entry with
                 * corresponding id */
                g_hash_table_remove(block->entries, password_id);
@@ -142,14 +147,14 @@ gboolean passwd_store_set(PasswordBlockType block_type,
                if (!encrypted) {
                        /* encrypt password before saving it */
                        if ((encrypted_password =
-                                               password_encrypt(password, NULL)) == NULL) {
+                                               password_encrypt(p, NULL)) == NULL) {
                                debug_print("Could not encrypt password '%s' for block (%d/%s).\n",
                                                password_id, block_type, block_name);
                                return FALSE;
                        }
                } else {
                        /* password is already in encrypted form already */
-                       encrypted_password = g_strdup(password);
+                       encrypted_password = g_strdup(p);
                }
 
                // add encrypted password to the block
index 46c3385b701eddfc92e647c08c8c79c1d150ea4e..a7c1e0fb5fae45b004ae5e00b4ef7acb6e8fdbb3 100644 (file)
@@ -638,9 +638,9 @@ void cm_gdata_contacts_done(void)
 #if GDATA_CHECK_VERSION(0,17,2)
     /* store refresh token */
     pass = gdata_oauth2_authorizer_dup_refresh_token(authorizer);
+               passwd_store_set(PWS_PLUGIN, "GData", GDATA_TOKEN_PWD_STRING,
+                               pass, FALSE);
                if (pass != NULL) {
-                       passwd_store_set(PWS_PLUGIN, "GData", GDATA_TOKEN_PWD_STRING, pass,
-                                       FALSE);
            memset(pass, 0, strlen(pass));
            g_free(pass);
                }
index b8b17b995e265c204414e1fea422a0c7626cdab8..1aaf29336d56c87401a84033cdba813c1980cb36 100644 (file)
@@ -90,8 +90,7 @@ void spamreport_prefs_init(void)
 
        /* Move passwords that are still in main config to password store. */
        for (i = 0; i < INTF_LAST; i++) {
-               if (spamreport_prefs.pass[i] != NULL &&
-                               strlen(spamreport_prefs.pass[i]) > 0) {
+               if (spamreport_prefs.pass[i] != NULL) {
                        spamreport_passwd_set(spam_interfaces[i].name,
                                        spamreport_prefs.pass[i]);
                        passwords_migrated = TRUE;
@@ -223,10 +222,8 @@ static void save_spamreport_prefs(PrefsPage *page)
                        GTK_EDITABLE(prefs_page->user_entry[i]), 0, -1);
 
                pass = gtk_editable_get_chars(GTK_EDITABLE(prefs_page->pass_entry[i]), 0, -1);
-               if (strlen(pass) > 0) {
-                       spamreport_passwd_set(spam_interfaces[i].name, pass);
-                       memset(pass, 0, strlen(pass));
-               }
+               spamreport_passwd_set(spam_interfaces[i].name, pass);
+               memset(pass, 0, strlen(pass));
                g_free(pass);
        }