From c81c89dc807ec351de8836d1c81395644eb89f28 Mon Sep 17 00:00:00 2001 From: Patrick Griffis Date: Mon, 29 Aug 2016 01:40:58 -0400 Subject: [PATCH] fishlim: Fix saving nicks containing [ or ] Part of #1440 --- plugins/fishlim/keystore.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/plugins/fishlim/keystore.c b/plugins/fishlim/keystore.c index 298b9fa3..6bb04ea6 100644 --- a/plugins/fishlim/keystore.c +++ b/plugins/fishlim/keystore.c @@ -63,6 +63,22 @@ static const char *get_keystore_password(void) { } +static char *escape_nickname(const char *nick) { + char *escaped = g_strdup(nick); + char *p = escaped; + + while (*p) { + if (*p == '[') + *p = '~'; + else if (*p == ']') + *p = '!'; + + ++p; + } + + return escaped; +} + /** * Gets a value for a nick/channel from addon_fishlim.conf. Unlike * g_key_file_get_string, this function is case insensitive. @@ -90,9 +106,13 @@ static gchar *get_nick_value(GKeyFile *keyfile, const char *nick, const char *it char *keystore_get_key(const char *nick) { /* Get the key */ GKeyFile *keyfile = getConfigFile(); - gchar *value = get_nick_value(keyfile, nick, "key"); + char *escaped_nick = escape_nickname(nick); + gchar *value = get_nick_value(keyfile, escaped_nick, "key"); g_key_file_free(keyfile); - if (!value) return NULL; + g_free(escaped_nick); + + if (!value) + return NULL; if (strncmp(value, "+OK ", 4) != 0) { /* Key is stored in plaintext */ @@ -173,9 +193,10 @@ gboolean keystore_store_key(const char *nick, const char *key) { char *wrapped; gboolean ok = FALSE; GKeyFile *keyfile = getConfigFile(); - + char *escaped_nick = escape_nickname(nick); + /* Remove old key */ - delete_nick(keyfile, nick); + delete_nick(keyfile, escaped_nick); /* Add new key */ password = get_keystore_password(); @@ -189,11 +210,11 @@ gboolean keystore_store_key(const char *nick, const char *key) { g_free(encrypted); /* Store encrypted in file */ - g_key_file_set_string(keyfile, nick, "key", wrapped); + g_key_file_set_string(keyfile, escaped_nick, "key", wrapped); g_free(wrapped); } else { /* Store unencrypted in file */ - g_key_file_set_string(keyfile, nick, "key", key); + g_key_file_set_string(keyfile, escaped_nick, "key", key); } /* Save key store file */ @@ -201,6 +222,7 @@ gboolean keystore_store_key(const char *nick, const char *key) { end: g_key_file_free(keyfile); + g_free(escaped_nick); return ok; } @@ -209,13 +231,15 @@ gboolean keystore_store_key(const char *nick, const char *key) { */ gboolean keystore_delete_nick(const char *nick) { GKeyFile *keyfile = getConfigFile(); + char *escaped_nick = escape_nickname(nick); /* Delete entry */ - gboolean ok = delete_nick(keyfile, nick); + gboolean ok = delete_nick(keyfile, escaped_nick); /* Save */ if (ok) save_keystore(keyfile); g_key_file_free(keyfile); + g_free(escaped_nick); return ok; }