001-Allow-processing-two-passwords-from-stdin-in-non-int.patch (3437B)
1 From 5f2e24fdc9935d049a7e4a5b6e10461e9467597f Mon Sep 17 00:00:00 2001 2 From: Nikos Mavrogiannopoulos <nmav@gnutls.org> 3 Date: Thu, 18 Jun 2015 22:38:05 +0200 4 Subject: [PATCH] Allow processing two passwords from stdin in non-interactive 5 mode 6 7 Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org> 8 --- 9 main.c | 38 ++++++++++++++++++++++++++------------ 10 1 file changed, 26 insertions(+), 12 deletions(-) 11 12 diff --git a/main.c b/main.c 13 index 3b976d8..f853afe 100644 14 --- a/main.c 15 +++ b/main.c 16 @@ -85,6 +85,7 @@ static int do_passphrase_from_fsid; 17 static int nocertcheck; 18 static int non_inter; 19 static int cookieonly; 20 +static int allow_stdin_read; 21 22 static char *token_filename; 23 static char *server_cert = NULL; 24 @@ -358,7 +359,7 @@ static char *convert_arg_to_utf8(char **argv, char *arg) 25 #define vfprintf vfprintf_utf8 26 #define is_arg_utf8(str) (0) 27 28 -static void read_stdin(char **string, int hidden) 29 +static void read_stdin(char **string, int hidden, int allow_fail) 30 { 31 CONSOLE_READCONSOLE_CONTROL rcc = { sizeof(rcc), 0, 13, 0 }; 32 HANDLE stdinh = GetStdHandle(STD_INPUT_HANDLE); 33 @@ -375,6 +376,7 @@ static void read_stdin(char **string, int hidden) 34 char *errstr = openconnect__win32_strerror(GetLastError()); 35 fprintf(stderr, _("ReadConsole() failed: %s\n"), errstr); 36 free(errstr); 37 + *string = NULL; 38 goto out; 39 } 40 41 @@ -622,7 +624,7 @@ static void print_build_opts(void) 42 43 #ifndef _WIN32 44 static const char default_vpncscript[] = DEFAULT_VPNCSCRIPT; 45 -static void read_stdin(char **string, int hidden) 46 +static void read_stdin(char **string, int hidden, int allow_fail) 47 { 48 char *c, *buf = malloc(1025); 49 int fd = fileno(stdin); 50 @@ -648,8 +650,14 @@ static void read_stdin(char **string, int hidden) 51 } 52 53 if (!buf) { 54 - perror(_("fgets (stdin)")); 55 - exit(1); 56 + if (allow_fail) { 57 + *string = NULL; 58 + free(buf); 59 + return; 60 + } else { 61 + perror(_("fgets (stdin)")); 62 + exit(1); 63 + } 64 } 65 66 c = strchr(buf, '\n'); 67 @@ -1160,13 +1168,14 @@ int main(int argc, char **argv) 68 cookieonly = 3; 69 break; 70 case OPT_COOKIE_ON_STDIN: 71 - read_stdin(&vpninfo->cookie, 0); 72 + read_stdin(&vpninfo->cookie, 0, 0); 73 /* If the cookie is empty, ignore it */ 74 if (!*vpninfo->cookie) 75 vpninfo->cookie = NULL; 76 break; 77 case OPT_PASSWORD_ON_STDIN: 78 - read_stdin(&password, 0); 79 + read_stdin(&password, 0, 0); 80 + allow_stdin_read = 1; 81 break; 82 case OPT_NO_PASSWD: 83 vpninfo->nopasswd = 1; 84 @@ -1708,7 +1717,7 @@ static int validate_peer_cert(void *_vpninfo, const char *reason) 85 fprintf(stderr, _("Enter '%s' to accept, '%s' to abort; anything else to view: "), 86 _("yes"), _("no")); 87 88 - read_stdin(&response, 0); 89 + read_stdin(&response, 0, 0); 90 if (!response) 91 return -EINVAL; 92 93 @@ -1779,19 +1788,24 @@ static char *prompt_for_input(const char *prompt, 94 struct openconnect_info *vpninfo, 95 int hidden) 96 { 97 - char *response; 98 + char *response = NULL; 99 100 fprintf(stderr, "%s", prompt); 101 fflush(stderr); 102 103 if (non_inter) { 104 - fprintf(stderr, "***\n"); 105 - vpn_progress(vpninfo, PRG_ERR, 106 + if (allow_stdin_read) { 107 + read_stdin(&response, hidden, 1); 108 + } 109 + if (response == NULL) { 110 + fprintf(stderr, "***\n"); 111 + vpn_progress(vpninfo, PRG_ERR, 112 _("User input required in non-interactive mode\n")); 113 - return NULL; 114 + } 115 + return response; 116 } 117 118 - read_stdin(&response, hidden); 119 + read_stdin(&response, hidden, 0); 120 return response; 121 } 122 123 -- 124 2.1.4 125