about summary refs log tree commit diff stats
path: root/src/config
diff options
context:
space:
mode:
authorWilliam Wennerström <william@wstrm.dev>2020-12-13 17:19:40 +0100
committerWilliam Wennerström <william@wstrm.dev>2020-12-15 15:45:16 +0100
commit695a1d3c8c22b48fdf55b6d797d8b4b7e43abc27 (patch)
tree75e16d7c35907dbc56f4f85ea63215fa8ba6c587 /src/config
parent32cfea4bd28ff70bae1c2e80c603877b5b35515c (diff)
downloadprofani-tty-695a1d3c8c22b48fdf55b6d797d8b4b7e43abc27.tar.gz
Use popen for eval_password (reintroduce old behavior)
Old commit that implemented the old behavior:
bc9e6b79cdc246f7e97f6ddff7ea81474a698b05
Diffstat (limited to 'src/config')
-rw-r--r--src/config/account.c55
1 files changed, 35 insertions, 20 deletions
diff --git a/src/config/account.c b/src/config/account.c
index 7e5297af..627c4a1c 100644
--- a/src/config/account.c
+++ b/src/config/account.c
@@ -36,6 +36,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 
 #include <glib.h>
 
@@ -200,35 +201,49 @@ account_eval_password(ProfAccount* account)
     assert(account != NULL);
     assert(account->eval_password != NULL);
 
-    gchar* std_out = NULL;
-    gchar* std_err = NULL;
-
-    gchar* argv[] = { "sh", "-c", account->eval_password, NULL };
-    if (!call_external(argv, &std_out, &std_err)) {
-        log_error("Password command failed with: %s", std_err);
-        g_free(std_out);
-        g_free(std_err);
+    errno = 0;
+
+    FILE* stream = popen(account->eval_password, "r");
+    if (stream == NULL) {
+        const char* errmsg = strerror(errno);
+        if (errmsg) {
+            log_error("Could not execute `eval_password` command (%s).",
+                      errmsg);
+        } else {
+            log_error("Failed to allocate memory for `eval_password` command.");
+        }
         return FALSE;
     }
 
-    if (!std_out || !std_out[0]) {
-        log_error("Password command returned empty output.");
-        g_free(std_out);
-        g_free(std_err);
+    account->password = g_malloc(READ_BUF_SIZE);
+    if (!account->password) {
+        log_error("Failed to allocate enough memory to read `eval_password` "
+                  "output.");
         return FALSE;
     }
 
-    // Remove leading and trailing whitespace from command output.
-    gchar* password = g_strdup(std_out);
-    g_strstrip(password);
+    account->password = fgets(account->password, READ_BUF_SIZE, stream);
+    if (!account->password) {
+        log_error("Failed to read password from stream.");
+        return FALSE;
+    }
 
-    account->password = password;
-    g_free(std_out);
-    g_free(std_err);
+    int exit_status = pclose(stream);
+    if (exit_status > 0) {
+        log_error("Command for `eval_password` returned error status (%s).",
+                  exit_status);
+        return FALSE;
+    } else if (exit_status < 0) {
+        log_error("Failed to close stream for `eval_password` command output "
+                  "(%s).",
+                  strerror(errno));
+        return FALSE;
+    };
 
+    // Remove leading and trailing whitespace from output.
+    g_strstrip(account->password);
     if (!account->password) {
-        log_error("Failed to allocate enough memory to read password command "
-                  "output");
+        log_error("Empty password returned by `eval_password` command.");
         return FALSE;
     }