From a25632f66956db71fb100f4850812c0f723fb640 Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Sat, 22 Oct 2016 18:33:11 +0200 Subject: [PATCH 1/5] WIP to allow passwords in external files --- src/irc/core/irc-servers-setup.c | 46 +++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/irc/core/irc-servers-setup.c b/src/irc/core/irc-servers-setup.c index f425b587..360578a9 100644 --- a/src/irc/core/irc-servers-setup.c +++ b/src/irc/core/irc-servers-setup.c @@ -30,6 +30,44 @@ #include "irc-servers.h" #include "sasl.h" +#include + +static void read_password_file(char *str, char **password) +{ + char **values = g_strsplit(str, ":", -1); + char *path; + GIOChannel *handle; + GString *buf; + GError *err = NULL; + gsize tpos; + + path = g_strdup(values[1]); + if (!g_str_has_prefix(path, "/")) + path = g_strdup_printf("%s/%s", get_irssi_dir(), path); + handle = g_io_channel_new_file(path, "r", &err); + g_free(path); + g_strfreev(values); + + if (handle == NULL) { + /* file not found */ + g_warning("Could not read sasl password from file: %s", (err ? err->message : "No GError set")); + g_error_free(err); + return; + } + + g_io_channel_set_encoding(handle, NULL, NULL); + buf = g_string_sized_new(64); + if (g_io_channel_read_line_string(handle, buf, &tpos, NULL) == G_IO_STATUS_NORMAL) { + buf->str[tpos] = '\0'; + *password = g_strdup(buf->str); + } + else + *password = g_strdup(""); + + g_string_free(buf, TRUE); + g_io_channel_unref(handle); +} + /* Fill information to connection from server setup record */ static void sig_server_setup_fill_reconn(IRC_SERVER_CONNECT_REC *conn, IRC_SERVER_SETUP_REC *sserver) @@ -97,7 +135,13 @@ static void sig_server_setup_fill_chatnet(IRC_SERVER_CONNECT_REC *conn, ircnet->sasl_password != NULL && *ircnet->sasl_password) { conn->sasl_mechanism = SASL_MECHANISM_PLAIN; conn->sasl_username = ircnet->sasl_username; - conn->sasl_password = ircnet->sasl_password; + char *password = NULL; + if (g_str_has_prefix(ircnet->sasl_password, "file:")) + read_password_file(ircnet->sasl_password, &password); + else + password = g_strdup(ircnet->sasl_password); + conn->sasl_password = g_strdup(password); + g_free(password); } else g_warning("The fields sasl_username and sasl_password are either missing or empty"); } From 4466a8fea2ea35bfccec1cf0956479b103bbbf5b Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Sat, 22 Oct 2016 18:41:25 +0200 Subject: [PATCH 2/5] didn't set the password to empty if we can't read the password file --- src/irc/core/irc-servers-setup.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/irc/core/irc-servers-setup.c b/src/irc/core/irc-servers-setup.c index 360578a9..19ce12f6 100644 --- a/src/irc/core/irc-servers-setup.c +++ b/src/irc/core/irc-servers-setup.c @@ -52,6 +52,7 @@ static void read_password_file(char *str, char **password) /* file not found */ g_warning("Could not read sasl password from file: %s", (err ? err->message : "No GError set")); g_error_free(err); + *password = g_strdup(""); return; } From 0ef1dceec02d82da61006309403fb4b30159d48e Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Wed, 26 Oct 2016 15:12:44 +0200 Subject: [PATCH 3/5] Simplified read_password_file function using g_file_get_contents() --- src/irc/core/irc-servers-setup.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/src/irc/core/irc-servers-setup.c b/src/irc/core/irc-servers-setup.c index 19ce12f6..8113185f 100644 --- a/src/irc/core/irc-servers-setup.c +++ b/src/irc/core/irc-servers-setup.c @@ -36,37 +36,21 @@ static void read_password_file(char *str, char **password) { char **values = g_strsplit(str, ":", -1); char *path; - GIOChannel *handle; - GString *buf; GError *err = NULL; - gsize tpos; path = g_strdup(values[1]); if (!g_str_has_prefix(path, "/")) path = g_strdup_printf("%s/%s", get_irssi_dir(), path); - handle = g_io_channel_new_file(path, "r", &err); - g_free(path); - g_strfreev(values); - if (handle == NULL) { - /* file not found */ + if (!g_file_get_contents(path, password, NULL, &err)) { g_warning("Could not read sasl password from file: %s", (err ? err->message : "No GError set")); - g_error_free(err); *password = g_strdup(""); - return; - } - - g_io_channel_set_encoding(handle, NULL, NULL); - buf = g_string_sized_new(64); - if (g_io_channel_read_line_string(handle, buf, &tpos, NULL) == G_IO_STATUS_NORMAL) { - buf->str[tpos] = '\0'; - *password = g_strdup(buf->str); } else - *password = g_strdup(""); + *password = g_strchomp(*password); - g_string_free(buf, TRUE); - g_io_channel_unref(handle); + g_free(path); + g_error_free(err); } /* Fill information to connection from server setup record */ From f146e5fdc3ca35781cac982da34bb076b82c05e8 Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Wed, 26 Oct 2016 15:50:14 +0200 Subject: [PATCH 4/5] unfreed memory due to lightning-fast optimization --- src/irc/core/irc-servers-setup.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/irc/core/irc-servers-setup.c b/src/irc/core/irc-servers-setup.c index 8113185f..a82ed3e2 100644 --- a/src/irc/core/irc-servers-setup.c +++ b/src/irc/core/irc-servers-setup.c @@ -50,7 +50,8 @@ static void read_password_file(char *str, char **password) *password = g_strchomp(*password); g_free(path); - g_error_free(err); + g_strfreev(values); + if (err) g_error_free(err); } /* Fill information to connection from server setup record */ From d4446549ecb8a76b6d39abf7c10c069e04a6d3e0 Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Sun, 30 Oct 2016 01:54:10 +0200 Subject: [PATCH 5/5] memoryloss bug with path fixed --- src/irc/core/irc-servers-setup.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/irc/core/irc-servers-setup.c b/src/irc/core/irc-servers-setup.c index a82ed3e2..b880d185 100644 --- a/src/irc/core/irc-servers-setup.c +++ b/src/irc/core/irc-servers-setup.c @@ -38,9 +38,10 @@ static void read_password_file(char *str, char **password) char *path; GError *err = NULL; - path = g_strdup(values[1]); - if (!g_str_has_prefix(path, "/")) - path = g_strdup_printf("%s/%s", get_irssi_dir(), path); + if (!g_str_has_prefix(values[1], "/")) + path = g_strdup_printf("%s/%s", get_irssi_dir(), values[1]); + else + path = g_strdup(values[1]); if (!g_file_get_contents(path, password, NULL, &err)) { g_warning("Could not read sasl password from file: %s", (err ? err->message : "No GError set"));