Skip to contentSkip to conversation

Fix management password prompt with redirected log

PATCH In review Lev Stipakov 3 participants 6 messages Jun 18, 2021 – Jun 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix management password prompt with redirected log

From: Lev Stipakov <[email protected]...>

When management interface is password protected, openvpn writes "Enter Management Password" to stderr. As it turns out, when log is redirected to a file, that prompt is also written to that file and not to the "original" stderr. Moreover, on recent Insider build (21390.2025) openvpn exits with fatal error

get_console_input_win32(): unexpected error: No such device or address (errno=6)

while attempting to write that prompt.

When redirecting stdout/stderr, we use _dup2() to associate stderr descriptor with the log file. This call closes file associated with stderr descriptor, which might explain why it has stopped working (original stderr is closed and WriteFile() fails) and on current versions it appears to work "by accident" - not failing but use redirected stderr instead of original one.

I played a bit with DuplicateHandle() for STD_ERROR_HANDLE but got the same behavior.

Fix fatal error on Insider build by always writing prompt to the new stderr and removing concept of "orig_stderr". For current Windows releases behavior doesn't change.

---
 src/openvpn/console_builtin.c |  2 +-
 src/openvpn/error.c           | 31 -------------------------------
 src/openvpn/error.h           |  6 ------
 3 files changed, 1 insertion(+), 38 deletions(-)
src/openvpn/console_builtin.c +1 -1
index 9bf36347..31214536 100644
@@ -73,7 +73,7 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const
     input[0] = '\0';
 
     in = GetStdHandle(STD_INPUT_HANDLE);
-    err = get_orig_stderr();
+    err = GetStdHandle(STD_ERROR_HANDLE);
 
     if (in == INVALID_HANDLE_VALUE
         || err == INVALID_HANDLE_VALUE
src/openvpn/error.c +0 -31
index b94d387c..e807435c 100644
@@ -490,25 +490,6 @@ close_syslog(void)
 #endif
 }
 
-#ifdef _WIN32
-
-static HANDLE orig_stderr;
-
-HANDLE
-get_orig_stderr(void)
-{
-    if (orig_stderr)
-    {
-        return orig_stderr;
-    }
-    else
-    {
-        return GetStdHandle(STD_ERROR_HANDLE);
-    }
-}
-
-#endif
-
 void
 redirect_stdout_stderr(const char *file, bool append)
 {
@@ -549,18 +530,6 @@ redirect_stdout_stderr(const char *file, bool append)
             }
         }
 
-        /* save original stderr for password prompts */
-        orig_stderr = GetStdHandle(STD_ERROR_HANDLE);
-
-#if 0 /* seems not be necessary with stdout/stderr redirection below*/
-        /* set up for redirection */
-        if (!SetStdHandle(STD_OUTPUT_HANDLE, log_handle)
-            || !SetStdHandle(STD_ERROR_HANDLE, log_handle))
-        {
-            msg(M_ERR, "Error: cannot redirect stdout/stderr to --log file: %s", file);
-        }
-#endif
-
         /* direct stdout/stderr to point to log_handle */
         log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
         if (log_fd == -1)
src/openvpn/error.h +0 -6
index f4528ef2..b91e54fb 100644
@@ -255,12 +255,6 @@ void close_syslog(void);
 /* log file output */
 void redirect_stdout_stderr(const char *file, bool append);
 
-#ifdef _WIN32
-/* get original stderr handle, even if redirected by --log/--log-append */
-HANDLE get_orig_stderr(void);
-
-#endif
-
 /* exit program */
 void openvpn_exit(const int status);
 
Signature · 1 line
2.23.0.windows.1
#1 lore raw

[email protected]...

Get Outlook for Android<https://aka.ms/AAb9ysg>

________________________________
From: Lev Stipakov <[email protected]...>
Sent: Friday, June 18, 2021 10:22:41 PM
To: [email protected] <[email protected]>
Cc: Lev Stipakov <[email protected]...>
Subject: [Openvpn-devel] [PATCH] Fix management password prompt with redirected log

From: Lev Stipakov <[email protected]...>

When management interface is password protected, openvpn writes "Enter Management Password" to stderr. As it turns out, when log is redirected to a file, that prompt is also written to that file and not to the "original" stderr. Moreover, on recent Insider build (21390.2025) openvpn exits with fatal error

get_console_input_win32(): unexpected error: No such device or address (errno=6)

while attempting to write that prompt.

When redirecting stdout/stderr, we use _dup2() to associate stderr descriptor with the log file. This call closes file associated with stderr descriptor, which might explain why it has stopped working (original stderr is closed and WriteFile() fails) and on current versions it appears to work "by accident" - not failing but use redirected stderr instead of original one.

I played a bit with DuplicateHandle() for STD_ERROR_HANDLE but got the same behavior.

Fix fatal error on Insider build by always writing prompt to the new stderr and removing concept of "orig_stderr". For current Windows releases behavior doesn't change.

---
 src/openvpn/console_builtin.c |  2 +-
 src/openvpn/error.c           | 31 -------------------------------
 src/openvpn/error.h           |  6 ------
 3 files changed, 1 insertion(+), 38 deletions(-)
src/openvpn/console_builtin.c +1 -1
index 9bf36347..31214536 100644
@@ -73,7 +73,7 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const
     input[0] = '\0';
 
     in = GetStdHandle(STD_INPUT_HANDLE);
-    err = get_orig_stderr();
+    err = GetStdHandle(STD_ERROR_HANDLE);
 
     if (in == INVALID_HANDLE_VALUE
         || err == INVALID_HANDLE_VALUE
src/openvpn/error.c +0 -31
index b94d387c..e807435c 100644
@@ -490,25 +490,6 @@ close_syslog(void)
 #endif
 }
 
-#ifdef _WIN32
-
-static HANDLE orig_stderr;
-
-HANDLE
-get_orig_stderr(void)
-{
-    if (orig_stderr)
-    {
-        return orig_stderr;
-    }
-    else
-    {
-        return GetStdHandle(STD_ERROR_HANDLE);
-    }
-}
-
-#endif
-
 void
 redirect_stdout_stderr(const char *file, bool append)
 {
@@ -549,18 +530,6 @@ redirect_stdout_stderr(const char *file, bool append)
             }
         }
 
-        /* save original stderr for password prompts */
-        orig_stderr = GetStdHandle(STD_ERROR_HANDLE);
-
-#if 0 /* seems not be necessary with stdout/stderr redirection below*/
-        /* set up for redirection */
-        if (!SetStdHandle(STD_OUTPUT_HANDLE, log_handle)
-            || !SetStdHandle(STD_ERROR_HANDLE, log_handle))
-        {
-            msg(M_ERR, "Error: cannot redirect stdout/stderr to --log file: %s", file);
-        }
-#endif
-
         /* direct stdout/stderr to point to log_handle */
         log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
         if (log_fd == -1)
src/openvpn/error.h +0 -6
index f4528ef2..b91e54fb 100644
@@ -255,12 +255,6 @@ void close_syslog(void);
 /* log file output */
 void redirect_stdout_stderr(const char *file, bool append);
 
-#ifdef _WIN32
-/* get original stderr handle, even if redirected by --log/--log-append */
-HANDLE get_orig_stderr(void);
-
-#endif
-
 /* exit program */
 void openvpn_exit(const int status);
 

--
2.23.0.windows.1

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#2 lore raw

PATCH v2 1/1 Fix console prompts with redirected log

In reply to Lev Stipakov

From: Lev Stipakov <[email protected]...>

When openvpn nees to prompt user for a password (for example, to set management interface password), the prompt is written to standard error device.

When log is redirected to a file, that prompt is written to that file and not to the "original" stderr. Moreover, on recent Insider build (21390.2025) openvpn exits with fatal error

get_console_input_win32(): unexpected error: No such device or address (errno=6)

while attempting to write that prompt.

When redirecting stdout/stderr, we use _dup2() to associate stderr descriptor with a log file. This call closes file associated with stderr descriptor, which might explain why it has stopped working (original stderr is closed and WriteFile() fails) and on current versions it appears to work "by accident" - not failing but use redirected stderr instead of original one.

Fix by creating new file descriptor with _dup() for stderr before redirect and use this descriptor for writing prompts.

While on it, make code a bit more C99-ish by moving variables declaration from the beginning of the scope to the actual initialisation.

---

v2: actually fix the prompt by displaying it in console
instead of writing to log

 src/openvpn/console_builtin.c | 16 +++++++---------
 src/openvpn/error.c           | 28 +++++++---------------------
 src/openvpn/error.h           |  4 ++--
 3 files changed, 16 insertions(+), 32 deletions(-)
src/openvpn/console_builtin.c +7 -9
index 3214cb5f..2340da6e 100644
@@ -62,23 +62,19 @@
 static bool
 get_console_input_win32(const char *prompt, const bool echo, char *input, const int capacity)
 {
-    HANDLE in = INVALID_HANDLE_VALUE;
-    HANDLE err = INVALID_HANDLE_VALUE;
-    DWORD len = 0;
-
     ASSERT(prompt);
     ASSERT(input);
     ASSERT(capacity > 0);
 
     input[0] = '\0';
 
-    in = GetStdHandle(STD_INPUT_HANDLE);
-    err = get_orig_stderr();
+    HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
+    int err = get_orig_stderr();
 
     if (in != INVALID_HANDLE_VALUE
-        && err != INVALID_HANDLE_VALUE
+        && err != -1
         && !win32_service_interrupt(&win32_signal)
-        && WriteFile(err, prompt, strlen(prompt), &len, NULL))
+        && (_write(err, prompt, strlen(prompt)) != -1))
     {
         bool is_console = (GetFileType(in) == FILE_TYPE_CHAR);
         DWORD flags_save = 0;
@@ -102,6 +98,8 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const
             }
         }
 
+        DWORD len = 0;
+
         if (is_console)
         {
             winput = malloc(capacity * sizeof(WCHAR));
@@ -124,7 +122,7 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const
 
         if (!echo)
         {
-            WriteFile(err, "\r\n", 2, &len, NULL);
+            _write(err, "\r\n", 2);
         }
         if (is_console)
         {
src/openvpn/error.c +7 -21
index b94d387c..eb82f9c7 100644
@@ -491,22 +491,12 @@ close_syslog(void)
 }
 
 #ifdef _WIN32
+static int orig_stderr;
 
-static HANDLE orig_stderr;
-
-HANDLE
-get_orig_stderr(void)
+int get_orig_stderr()
 {
-    if (orig_stderr)
-    {
-        return orig_stderr;
-    }
-    else
-    {
-        return GetStdHandle(STD_ERROR_HANDLE);
-    }
+    return orig_stderr ? orig_stderr : _fileno(stderr);
 }
-
 #endif
 
 void
@@ -550,16 +540,12 @@ redirect_stdout_stderr(const char *file, bool append)
         }
 
         /* save original stderr for password prompts */
-        orig_stderr = GetStdHandle(STD_ERROR_HANDLE);
-
-#if 0 /* seems not be necessary with stdout/stderr redirection below*/
-        /* set up for redirection */
-        if (!SetStdHandle(STD_OUTPUT_HANDLE, log_handle)
-            || !SetStdHandle(STD_ERROR_HANDLE, log_handle))
+        orig_stderr = _dup(_fileno(stderr));
+        if (orig_stderr == -1)
         {
-            msg(M_ERR, "Error: cannot redirect stdout/stderr to --log file: %s", file);
+            msg(M_WARN | M_ERRNO, "Warning: cannot duplicate stderr, password prompts will appear in log file instead of console.");
+            orig_stderr = _fileno(stderr);
         }
-#endif
 
         /* direct stdout/stderr to point to log_handle */
         log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
src/openvpn/error.h +2 -2
index f4528ef2..533354b3 100644
@@ -256,8 +256,8 @@ void close_syslog(void);
 void redirect_stdout_stderr(const char *file, bool append);
 
 #ifdef _WIN32
-/* get original stderr handle, even if redirected by --log/--log-append */
-HANDLE get_orig_stderr(void);
+/* get original stderr fd, even if redirected by --log/--log-append */
+int get_orig_stderr(void);
 
 #endif
 
Signature · 1 line
2.23.0.windows.1
#3 lore raw

Re: [Openvpn-devel] [PATCH v2] Fix console prompts with redirected log

Hi,

Quoted 11 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

When openvpn nees to prompt user for a password (for example, to set management interface password), the prompt is written to standard error device.

When log is redirected to a file, that prompt is written to that file and not to the "original" stderr. Moreover, on recent Insider build (21390.2025) openvpn exits with fatal error

get_console_input_win32(): unexpected error: No such device or address (errno=6)

Feature-ACK, and tested your binary, which works nicely.

Can you rebase this on top of master? I wanted my "improve error logging" patch in the tree, and of course it conflicts big time.

While I could fix these, I think having a v3 that applies nicely would be a cleaner approach...

thanks,

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#4 lore raw

PATCH v3 1/1 Fix console prompts with redirected log

From: Lev Stipakov <[email protected]...>

When openvpn nees to prompt user for a password (for example, to set management interface password), the prompt is written to standard error device.

When log is redirected to a file, that prompt is written to that file and not to the "original" stderr. Moreover, on recent Insider build (21390.2025) openvpn exits with fatal error

get_console_input_win32(): unexpected error: No such device or address (errno=6)

while attempting to write that prompt.

When redirecting stdout/stderr, we use _dup2() to associate stderr descriptor with a log file. This call closes file associated with stderr descriptor, which might explain why it has stopped working (original stderr is closed and WriteFile() fails) and on current versions it appears to work "by accident" - not failing but use redirected stderr instead of original one.

Fix by creating new file descriptor with _dup() for stderr before redirect and use this descriptor for writing prompts.

While on it, make code a bit more C99-ish by moving variables declaration from the beginning of the scope to the actual initialisation.

---
v3: rebase

v2: actually fix the prompt by displaying it in console
instead of writing to log

 src/openvpn/console_builtin.c | 18 +++++++-----------
 src/openvpn/error.c           | 28 +++++++---------------------
 src/openvpn/error.h           |  4 ++--
 3 files changed, 16 insertions(+), 34 deletions(-)
src/openvpn/console_builtin.c +7 -11
index 9bf36347..06b8a77a 100644
@@ -62,23 +62,17 @@
 static bool
 get_console_input_win32(const char *prompt, const bool echo, char *input, const int capacity)
 {
-    HANDLE in = INVALID_HANDLE_VALUE;
-    HANDLE err = INVALID_HANDLE_VALUE;
-    DWORD len = 0;
-
     ASSERT(prompt);
     ASSERT(input);
     ASSERT(capacity > 0);
 
     input[0] = '\0';
 
-    in = GetStdHandle(STD_INPUT_HANDLE);
-    err = get_orig_stderr();
-
-    if (in == INVALID_HANDLE_VALUE
-        || err == INVALID_HANDLE_VALUE
+    HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
+    int orig_stderr = get_orig_stderr(); // guaranteed to be always valid
+    if ((in == INVALID_HANDLE_VALUE)
         || win32_service_interrupt(&win32_signal)
-        || !WriteFile(err, prompt, strlen(prompt), &len, NULL))
+        || (_write(orig_stderr, prompt, strlen(prompt)) == -1))
     {
         msg(M_WARN|M_ERRNO, "get_console_input_win32(): unexpected error");
         return false;
@@ -106,6 +100,8 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const
         }
     }
 
+    DWORD len = 0;
+
     if (is_console)
     {
         winput = malloc(capacity * sizeof(WCHAR));
@@ -128,7 +124,7 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const
 
     if (!echo)
     {
-        WriteFile(err, "\r\n", 2, &len, NULL);
+        _write(orig_stderr, "\r\n", 2);
     }
     if (is_console)
     {
src/openvpn/error.c +7 -21
index b94d387c..eb82f9c7 100644
@@ -491,22 +491,12 @@ close_syslog(void)
 }
 
 #ifdef _WIN32
+static int orig_stderr;
 
-static HANDLE orig_stderr;
-
-HANDLE
-get_orig_stderr(void)
+int get_orig_stderr()
 {
-    if (orig_stderr)
-    {
-        return orig_stderr;
-    }
-    else
-    {
-        return GetStdHandle(STD_ERROR_HANDLE);
-    }
+    return orig_stderr ? orig_stderr : _fileno(stderr);
 }
-
 #endif
 
 void
@@ -550,16 +540,12 @@ redirect_stdout_stderr(const char *file, bool append)
         }
 
         /* save original stderr for password prompts */
-        orig_stderr = GetStdHandle(STD_ERROR_HANDLE);
-
-#if 0 /* seems not be necessary with stdout/stderr redirection below*/
-        /* set up for redirection */
-        if (!SetStdHandle(STD_OUTPUT_HANDLE, log_handle)
-            || !SetStdHandle(STD_ERROR_HANDLE, log_handle))
+        orig_stderr = _dup(_fileno(stderr));
+        if (orig_stderr == -1)
         {
-            msg(M_ERR, "Error: cannot redirect stdout/stderr to --log file: %s", file);
+            msg(M_WARN | M_ERRNO, "Warning: cannot duplicate stderr, password prompts will appear in log file instead of console.");
+            orig_stderr = _fileno(stderr);
         }
-#endif
 
         /* direct stdout/stderr to point to log_handle */
         log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
src/openvpn/error.h +2 -2
index f4528ef2..533354b3 100644
@@ -256,8 +256,8 @@ void close_syslog(void);
 void redirect_stdout_stderr(const char *file, bool append);
 
 #ifdef _WIN32
-/* get original stderr handle, even if redirected by --log/--log-append */
-HANDLE get_orig_stderr(void);
+/* get original stderr fd, even if redirected by --log/--log-append */
+int get_orig_stderr(void);
 
 #endif
 
Signature · 1 line
2.23.0.windows.1
Acked-by on v3 1/1
#5 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix console prompts with redirected log

Thanks for that. Stared-at-code (looks good and much more "I understand how this works" than before :-) ). Test built on ubuntu/mingw, but not actually tested the resulting binary - but I did test the openvpn.exe that Lev built with MSVC for amd64, and it is working nicely for all tested cases ("openvpn.exe --auth-user-pass --log file.txt --config ..." actually prompts now, which it didn't before - GUI and CLI tested, with and without auth-user-pass and passphrase prompts)

Your patch has been applied to the master and release/2.5 branch (bugfix).

commit 480e4cc14ff34fac72406ab1dd66290a91cc09f0 (master) commit 86b247a40b919fa8e0a3de1c367087355bb10074 (release/2.5)

Author: Lev Stipakov
Date:   Fri Jun 25 04:04:05 2021 +0300
     Fix console prompts with redirected log

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering