Skip to content

Commit

Permalink
Add hidden password support for windows platform
Browse files Browse the repository at this point in the history
The windows console binary should not echo the password
just like the non-windows equivalent.

Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
jwessel authored and David Woodhouse committed Jul 8, 2014
1 parent 4b39bbc commit 8a7ec72
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions main.c
Expand Up @@ -48,6 +48,10 @@
#endif

#include "openconnect-internal.h"
#ifdef _WIN32
#include <wtypes.h>
#include <wincon.h>
#endif

static int write_new_config(void *_vpninfo,
char *buf, int buflen);
Expand Down Expand Up @@ -408,6 +412,35 @@ static void usage(void)
exit(1);
}

#ifdef _WIN32
static HANDLE hconin = INVALID_HANDLE_VALUE;
static DWORD cmode;

static void disable_echo(void)
{
hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hconin == INVALID_HANDLE_VALUE)
return;
GetConsoleMode(hconin, &cmode);
if (!SetConsoleMode(hconin, cmode & (~ENABLE_ECHO_INPUT))) {
CloseHandle(hconin);
hconin = INVALID_HANDLE_VALUE;
}
}

static void restore_echo(void)
{
if (hconin == INVALID_HANDLE_VALUE)
return;

SetConsoleMode(hconin, cmode);
CloseHandle(hconin);
hconin = INVALID_HANDLE_VALUE;
}
#endif

static void read_stdin(char **string, int hidden)
{
char *c = malloc(1025), *ret;
Expand All @@ -417,8 +450,12 @@ static void read_stdin(char **string, int hidden)
exit(1);
}

#ifndef _WIN32
if (hidden) {
#ifdef _WIN32
disable_echo();
ret = fgets(c, 1025, stdin);
restore_echo();
#else /* ! _WIN32 */
struct termios t;
int fd = fileno(stdin);

Expand All @@ -431,8 +468,8 @@ static void read_stdin(char **string, int hidden)
t.c_lflag |= ECHO;
tcsetattr(fd, TCSANOW, &t);
fprintf(stderr, "\n");
} else
#endif
} else
ret = fgets(c, 1025, stdin);

if (!ret) {
Expand Down

0 comments on commit 8a7ec72

Please sign in to comment.