diff --git a/src/command/command.c b/src/command/command.c index ddcdf573..86185f59 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -941,6 +941,7 @@ static struct cmd_t command_defs[] = "|xa|dnd : Priority for the specified presence.", "resource : The resource to be used.", "password : Password for the account, note this is currently stored in plaintext if set.", + "eval_password : Shell command evaluated to retrieve password for the account. Can be used to retrieve password from keyring.", "muc : The default MUC chat service to use.", "nick : The default nickname to use when joining chat rooms.", "otr : Override global OTR policy for this account: manual, opportunistic or always.", @@ -1272,12 +1273,14 @@ cmd_init(void) autocomplete_add(account_set_ac, "dnd"); autocomplete_add(account_set_ac, "resource"); autocomplete_add(account_set_ac, "password"); + autocomplete_add(account_set_ac, "eval_password"); autocomplete_add(account_set_ac, "muc"); autocomplete_add(account_set_ac, "nick"); autocomplete_add(account_set_ac, "otr"); account_clear_ac = autocomplete_new(); autocomplete_add(account_clear_ac, "password"); + autocomplete_add(account_clear_ac, "eval_password"); autocomplete_add(account_clear_ac, "server"); autocomplete_add(account_clear_ac, "port"); autocomplete_add(account_clear_ac, "otr"); diff --git a/src/command/commands.c b/src/command/commands.c index 18011454..3647bb22 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -130,7 +130,7 @@ cmd_connect(gchar **args, struct cmd_help_t help) ProfAccount *account = accounts_get_account(lower); if (account != NULL) { jid = account_create_full_jid(account); - if (account->password == NULL) { + if (account->password == NULL && account->eval_password == NULL) { account->password = ui_ask_password(); } cons_show("Connecting with account %s as %s", account->name, jid); @@ -340,9 +340,21 @@ cmd_account(gchar **args, struct cmd_help_t help) cons_show("Updated resource for account %s: %s", account_name, value); cons_show(""); } else if (strcmp(property, "password") == 0) { - accounts_set_password(account_name, value); - cons_show("Updated password for account %s", account_name); - cons_show(""); + if(accounts_get_account(account_name)->eval_password != NULL) { + cons_show("Cannot set password when eval_password is set."); + } else { + accounts_set_password(account_name, value); + cons_show("Updated password for account %s", account_name); + cons_show(""); + } + } else if (strcmp(property, "eval_password") == 0) { + if(accounts_get_account(account_name)->password != NULL) { + cons_show("Cannot set eval_password when password is set."); + } else { + accounts_set_eval_password(account_name, value); + cons_show("Updated eval_password for account %s", account_name); + cons_show(""); + } } else if (strcmp(property, "muc") == 0) { accounts_set_muc_service(account_name, value); cons_show("Updated muc service for account %s: %s", account_name, value); @@ -427,6 +439,10 @@ cmd_account(gchar **args, struct cmd_help_t help) accounts_clear_password(account_name); cons_show("Removed password for account %s", account_name); cons_show(""); + } else if (strcmp(property, "eval_password") == 0) { + accounts_clear_eval_password(account_name); + cons_show("Removed eval password for account %s", account_name); + cons_show(""); } else if (strcmp(property, "server") == 0) { accounts_clear_server(account_name); cons_show("Removed server for account %s", account_name); diff --git a/src/common.c b/src/common.c index 8b0f186f..ffd12899 100644 --- a/src/common.c +++ b/src/common.c @@ -50,9 +50,6 @@ #include "log.h" #include "common.h" -// assume malloc stores at most 8 bytes for size of allocated memory -// and page size is at least 4KB -#define READ_BUF_SIZE 4088 struct curl_data_t { diff --git a/src/common.h b/src/common.h index 6497e666..55451dea 100644 --- a/src/common.h +++ b/src/common.h @@ -59,6 +59,11 @@ #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) +// assume malloc stores at most 8 bytes for size of allocated memory +// and page size is at least 4KB +#define READ_BUF_SIZE 4088 + + #define FREE_SET_NULL(resource) \ do { \ free(resource); \ diff --git a/src/config/account.c b/src/config/account.c index 64819d8c..3ca63db0 100644 --- a/src/config/account.c +++ b/src/config/account.c @@ -43,7 +43,7 @@ ProfAccount* account_new(const gchar * const name, const gchar * const jid, - const gchar * const password, gboolean enabled, const gchar * const server, + const gchar * const password, const gchar * eval_password, gboolean enabled, const gchar * const server, int port, const gchar * const resource, const gchar * const last_presence, const gchar * const login_presence, int priority_online, int priority_chat, int priority_away, int priority_xa, int priority_dnd, @@ -67,6 +67,12 @@ account_new(const gchar * const name, const gchar * const jid, new_account->password = NULL; } + if (eval_password != NULL) { + new_account->eval_password = strdup(eval_password); + } else { + new_account->eval_password = NULL; + } + new_account->enabled = enabled; if (server != NULL) { @@ -168,4 +174,4 @@ account_free(ProfAccount *account) g_list_free_full(account->otr_always, g_free); free(account); } -} \ No newline at end of file +} diff --git a/src/config/account.h b/src/config/account.h index 49477f95..ab43234d 100644 --- a/src/config/account.h +++ b/src/config/account.h @@ -41,6 +41,7 @@ typedef struct prof_account_t { gchar *name; gchar *jid; gchar *password; + gchar *eval_password; gchar *resource; gchar *server; int port; @@ -61,7 +62,7 @@ typedef struct prof_account_t { } ProfAccount; ProfAccount* account_new(const gchar * const name, const gchar * const jid, - const gchar * const passord, gboolean enabled, const gchar * const server, + const gchar * const passord, const gchar * eval_password, gboolean enabled, const gchar * const server, int port, const gchar * const resource, const gchar * const last_presence, const gchar * const login_presence, int priority_online, int priority_chat, int priority_away, int priority_xa, int priority_dnd, diff --git a/src/config/accounts.c b/src/config/accounts.c index efbaccb9..2e7092a5 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -46,6 +46,7 @@ #include "log.h" #include "tools/autocomplete.h" #include "xmpp/xmpp.h" +#include "ui/ui.h" static gchar *accounts_loc; static GKeyFile *accounts; @@ -60,6 +61,7 @@ static gchar *string_keys[] = { "port", "resource", "password", + "eval_password", "presence.last", "presence.login", "muc.service", @@ -224,6 +226,17 @@ accounts_get_account(const char * const name) } gchar *password = g_key_file_get_string(accounts, name, "password", NULL); + gchar *eval_password = g_key_file_get_string(accounts, name, "eval_password", NULL); + // Evaluate as shell command to retrieve password + if (eval_password != NULL) { + FILE *stream = popen(eval_password, "r"); + // Limit to READ_BUF_SIZE bytes to prevent overflows in the case of a poorly chosen command + password = g_malloc(READ_BUF_SIZE); + gchar *result = fgets(password, READ_BUF_SIZE, stream); + if (result != NULL) { + password = result; + } + } gboolean enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL); gchar *server = g_key_file_get_string(accounts, name, "server", NULL); @@ -278,7 +291,7 @@ accounts_get_account(const char * const name) g_strfreev(always); } - ProfAccount *new_account = account_new(name, jid, password, enabled, + ProfAccount *new_account = account_new(name, jid, password, eval_password, enabled, server, port, resource, last_presence, login_presence, priority_online, priority_chat, priority_away, priority_xa, priority_dnd, muc_service, muc_nick, otr_policy, otr_manual, @@ -286,6 +299,7 @@ accounts_get_account(const char * const name) g_free(jid); g_free(password); + g_free(eval_password); g_free(server); g_free(resource); g_free(last_presence); @@ -441,6 +455,15 @@ accounts_set_password(const char * const account_name, const char * const value) } } +void +accounts_set_eval_password(const char * const account_name, const char * const value) +{ + if (accounts_account_exists(account_name)) { + g_key_file_set_string(accounts, account_name, "eval_password", value); + _save_accounts(); + } +} + void accounts_clear_password(const char * const account_name) { @@ -450,6 +473,15 @@ accounts_clear_password(const char * const account_name) } } +void +accounts_clear_eval_password(const char * const account_name) +{ + if (accounts_account_exists(account_name)) { + g_key_file_remove_key(accounts, account_name, "eval_password", NULL); + _save_accounts(); + } +} + void accounts_clear_server(const char * const account_name) { @@ -859,4 +891,4 @@ _get_accounts_file(void) g_string_free(logfile, TRUE); return result; -} \ No newline at end of file +} diff --git a/src/config/accounts.h b/src/config/accounts.h index 78b186b4..a1dda018 100644 --- a/src/config/accounts.h +++ b/src/config/accounts.h @@ -61,6 +61,7 @@ void accounts_set_server(const char * const account_name, const char * const val void accounts_set_port(const char * const account_name, const int value); void accounts_set_resource(const char * const account_name, const char * const value); void accounts_set_password(const char * const account_name, const char * const value); +void accounts_set_eval_password(const char * const account_name, const char * const value); void accounts_set_muc_service(const char * const account_name, const char * const value); void accounts_set_muc_nick(const char * const account_name, const char * const value); void accounts_set_otr_policy(const char * const account_name, const char * const value); @@ -77,6 +78,7 @@ void accounts_set_priority_all(const char * const account_name, const gint value gint accounts_get_priority_for_presence_type(const char * const account_name, resource_presence_t presence_type); void accounts_clear_password(const char * const account_name); +void accounts_clear_eval_password(const char * const account_name); void accounts_clear_server(const char * const account_name); void accounts_clear_port(const char * const account_name); void accounts_clear_otr(const char * const account_name); diff --git a/tests/config/stub_accounts.c b/tests/config/stub_accounts.c index 22a130d4..32a80fda 100644 --- a/tests/config/stub_accounts.c +++ b/tests/config/stub_accounts.c @@ -97,6 +97,12 @@ void accounts_set_password(const char * const account_name, const char * const v check_expected(value); } +void accounts_set_eval_password(const char * const account_name, const char * const value) +{ + check_expected(account_name); + check_expected(value); +} + void accounts_set_muc_service(const char * const account_name, const char * const value) { check_expected(account_name); @@ -172,6 +178,7 @@ gint accounts_get_priority_for_presence_type(const char * const account_name, } void accounts_clear_password(const char * const account_name) {} +void accounts_clear_eval_password(const char * const account_name) {} void accounts_clear_server(const char * const account_name) {} void accounts_clear_port(const char * const account_name) {} void accounts_clear_otr(const char * const account_name) {} diff --git a/tests/test_cmd_account.c b/tests/test_cmd_account.c index 1a49c571..553b0d83 100644 --- a/tests/test_cmd_account.c +++ b/tests/test_cmd_account.c @@ -35,7 +35,7 @@ void cmd_account_shows_usage_when_not_connected_and_no_args(void **state) void cmd_account_shows_account_when_connected_and_no_args(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); - ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, + ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, NULL, TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); gchar *args[] = { NULL }; @@ -108,7 +108,7 @@ void cmd_account_show_shows_account_when_exists(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "show", "account_name", NULL }; - ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, + ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, NULL, TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); expect_any(accounts_get_account, name); @@ -477,10 +477,16 @@ void cmd_account_set_password_sets_password(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "password", "a_password", NULL }; + ProfAccount *account = account_new("a_account", NULL, NULL, NULL, + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); + expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, TRUE); + expect_string(accounts_get_account, name, "a_account"); + will_return(accounts_get_account, account); + expect_string(accounts_set_password, account_name, "a_account"); expect_string(accounts_set_password, value, "a_password"); @@ -940,4 +946,4 @@ void cmd_account_clear_shows_message_when_invalid_property(void **state) assert_true(result); free(help); -} \ No newline at end of file +} diff --git a/tests/test_cmd_connect.c b/tests/test_cmd_connect.c index 570d5f0e..e2089a09 100644 --- a/tests/test_cmd_connect.c +++ b/tests/test_cmd_connect.c @@ -410,7 +410,7 @@ void cmd_connect_asks_password_when_not_in_account(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; - ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, + ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, NULL, TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -435,7 +435,7 @@ void cmd_connect_shows_message_when_connecting_with_account(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; - ProfAccount *account = account_new("jabber_org", "user@jabber.org", "password", + ProfAccount *account = account_new("jabber_org", "user@jabber.org", "password", NULL, TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -458,7 +458,7 @@ void cmd_connect_connects_with_account(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; - ProfAccount *account = account_new("jabber_org", "me@jabber.org", "password", + ProfAccount *account = account_new("jabber_org", "me@jabber.org", "password", NULL, TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); will_return(jabber_get_connection_status, JABBER_DISCONNECTED); diff --git a/tests/test_cmd_join.c b/tests/test_cmd_join.c index caa3ed0b..10ffa547 100644 --- a/tests/test_cmd_join.c +++ b/tests/test_cmd_join.c @@ -92,7 +92,7 @@ void cmd_join_uses_account_mucservice_when_no_service_specified(void **state) char *expected_room = "room@conference.server.org"; CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { room, "nick", nick, NULL }; - ProfAccount *account = account_new(account_name, "user@server.org", NULL, + ProfAccount *account = account_new(account_name, "user@server.org", NULL, NULL, TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, account_service, NULL, NULL, NULL, NULL, NULL); muc_init(); @@ -120,7 +120,7 @@ void cmd_join_uses_supplied_nick(void **state) char *nick = "bob"; CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { room, "nick", nick, NULL }; - ProfAccount *account = account_new(account_name, "user@server.org", NULL, + ProfAccount *account = account_new(account_name, "user@server.org", NULL, NULL, TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); muc_init(); @@ -148,7 +148,7 @@ void cmd_join_uses_account_nick_when_not_supplied(void **state) char *account_nick = "a_nick"; CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { room, NULL }; - ProfAccount *account = account_new(account_name, "user@server.org", NULL, + ProfAccount *account = account_new(account_name, "user@server.org", NULL, NULL, TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, account_nick, NULL, NULL, NULL, NULL); muc_init(); @@ -179,7 +179,7 @@ void cmd_join_uses_password_when_supplied(void **state) char *expected_room = "room@a_service"; CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { room, "password", password, NULL }; - ProfAccount *account = account_new(account_name, "user@server.org", NULL, + ProfAccount *account = account_new(account_name, "user@server.org", NULL, NULL, TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, account_service, account_nick, NULL, NULL, NULL, NULL); muc_init(); diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c index 7d0adc1a..c6c6f7cf 100644 --- a/tests/test_cmd_otr.c +++ b/tests/test_cmd_otr.c @@ -307,7 +307,7 @@ void cmd_otr_gen_generates_key_for_connected_account(void **state) CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "gen", NULL }; char *account_name = "myaccount"; - ProfAccount *account = account_new(account_name, "me@jabber.org", NULL, + ProfAccount *account = account_new(account_name, "me@jabber.org", NULL, NULL, TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); will_return(jabber_get_connection_status, JABBER_CONNECTED);