aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/utils/windows.c
diff options
context:
space:
mode:
authorMartin Willi <martin@revosec.ch>2014-02-20 11:30:17 +0100
committerMartin Willi <martin@revosec.ch>2014-06-04 15:53:08 +0200
commit41bb8ba5d2b47ae6b7f968610fc0b56a3f00a992 (patch)
treec1dbc02ebdf9d2e4bdbb0bc5b7432c717c15a8c2 /src/libstrongswan/utils/windows.c
parent4161ee66783b665e2a6222373cc13305dd3f8019 (diff)
downloadstrongswan-41bb8ba5d2b47ae6b7f968610fc0b56a3f00a992.tar.bz2
strongswan-41bb8ba5d2b47ae6b7f968610fc0b56a3f00a992.tar.xz
windows: Provide a getpass() implementation
Diffstat (limited to 'src/libstrongswan/utils/windows.c')
-rw-r--r--src/libstrongswan/utils/windows.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/libstrongswan/utils/windows.c b/src/libstrongswan/utils/windows.c
index 492821d53..c0ae3d87a 100644
--- a/src/libstrongswan/utils/windows.c
+++ b/src/libstrongswan/utils/windows.c
@@ -251,6 +251,65 @@ int socketpair(int domain, int type, int protocol, int sv[2])
}
/**
+ * See header
+ */
+char* getpass(const char *prompt)
+{
+ static char buf[64] = "";
+ char *pos;
+ HANDLE in, out;
+ DWORD mode, written = 0, total, done;
+
+ out = GetStdHandle(STD_OUTPUT_HANDLE);
+ in = GetStdHandle(STD_INPUT_HANDLE);
+
+ if (out == INVALID_HANDLE_VALUE || in == INVALID_HANDLE_VALUE ||
+ !GetConsoleMode(out, &mode) || !GetConsoleMode(in, &mode))
+ {
+ return NULL;
+ }
+
+ total = strlen(prompt);
+ while (written < total)
+ {
+ if (!WriteConsole(out, prompt + written, total - written, &done, NULL))
+ {
+ return NULL;
+ }
+ written += done;
+ }
+
+ if (!SetConsoleMode(in, mode & ~ENABLE_ECHO_INPUT))
+ {
+ return NULL;
+ }
+
+ while (TRUE)
+ {
+ if (!ReadConsole(in, buf, sizeof(buf), &done, NULL))
+ {
+ SetConsoleMode(in, mode);
+ return NULL;
+ }
+ if (done)
+ {
+ pos = strchr(buf, '\r');
+ if (pos)
+ {
+ *pos = '\0';
+ }
+ break;
+ }
+ }
+ SetConsoleMode(in, mode);
+
+ /* append a newline, as we have no echo during input */
+ WriteConsole(out, "\r\n", 2, &done, NULL);
+
+ return buf;
+}
+
+/**
* Set errno for a function setting WSA error on failure
*/
static int wserr(int retval)