You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.4 KiB

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