about summary refs log tree commit diff stats
path: root/src/config/account.c
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-04-23 23:48:43 +0100
committerJames Booth <boothj5@gmail.com>2015-04-23 23:48:43 +0100
commit7a44e171410c97f2fa4a649287cfb509ba5c5d8b (patch)
treef023aeb5b3f08af524794968e689f15bff45745b /src/config/account.c
parent5f2c6cbd28f6d2519f826fa47628995c787689bf (diff)
parentbc9e6b79cdc246f7e97f6ddff7ea81474a698b05 (diff)
downloadprofani-tty-7a44e171410c97f2fa4a649287cfb509ba5c5d8b.tar.gz
Merge branch 'master' into pgp
Conflicts:
	src/config/account.h
Diffstat (limited to 'src/config/account.c')
-rw-r--r--src/config/account.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/config/account.c b/src/config/account.c
index 82294dac..b1532b38 100644
--- a/src/config/account.c
+++ b/src/config/account.c
@@ -34,12 +34,14 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 
 #include <glib.h>
 
 #include "jid.h"
 #include "config/account.h"
 #include "common.h"
+#include "log.h"
 
 ProfAccount*
 account_new(const gchar * const name, const gchar * const jid,
@@ -161,6 +163,44 @@ account_create_full_jid(ProfAccount *account)
     }
 }
 
+gboolean
+account_eval_password(ProfAccount *account)
+{
+    assert(account != NULL);
+    assert(account->eval_password != NULL);
+
+    // Evaluate as shell command to retrieve password
+    GString *cmd = g_string_new("");
+    g_string_append_printf(cmd, "%s 2>/dev/null", account->eval_password);
+
+    FILE *stream = popen(cmd->str, "r");
+    g_string_free(cmd, TRUE);
+    if (stream) {
+        // Limit to READ_BUF_SIZE bytes to prevent overflows in the case of a poorly chosen command
+        account->password = g_malloc(READ_BUF_SIZE);
+        if (!account->password) {
+            log_error("Failed to allocate enough memory to read eval_password output");
+            return FALSE;
+        }
+        account->password = fgets(account->password, READ_BUF_SIZE, stream);
+        pclose(stream);
+        if (!account->password) {
+            log_error("No result from eval_password.");
+            return FALSE;
+        }
+
+        // strip trailing newline
+        if (g_str_has_suffix(account->password, "\n")) {
+            account->password[strlen(account->password)-1] = '\0';
+        }
+    } else {
+        log_error("popen failed when running eval_password.");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
 void
 account_free(ProfAccount *account)
 {