cli-authinteract.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2005 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #include "buffer.h"
  26. #include "dbutil.h"
  27. #include "session.h"
  28. #include "ssh.h"
  29. #include "runopts.h"
  30. #ifdef ENABLE_CLI_INTERACT_AUTH
  31. static char* get_response(char* prompt)
  32. {
  33. FILE* tty = NULL;
  34. char* response = NULL;
  35. /* not a password, but a reasonable limit */
  36. char buf[DROPBEAR_MAX_CLI_PASS];
  37. char* ret = NULL;
  38. fprintf(stderr, "%s", prompt);
  39. tty = fopen(_PATH_TTY, "r");
  40. if (tty) {
  41. ret = fgets(buf, sizeof(buf), tty);
  42. fclose(tty);
  43. } else {
  44. ret = fgets(buf, sizeof(buf), stdin);
  45. }
  46. if (ret == NULL) {
  47. response = m_strdup("");
  48. } else {
  49. unsigned int buflen = strlen(buf);
  50. /* fgets includes newlines */
  51. if (buflen > 0 && buf[buflen-1] == '\n')
  52. buf[buflen-1] = '\0';
  53. response = m_strdup(buf);
  54. }
  55. m_burn(buf, sizeof(buf));
  56. return response;
  57. }
  58. void recv_msg_userauth_info_request() {
  59. char *name = NULL;
  60. char *instruction = NULL;
  61. unsigned int num_prompts = 0;
  62. unsigned int i;
  63. char *prompt = NULL;
  64. unsigned int echo = 0;
  65. char *response = NULL;
  66. TRACE(("enter recv_msg_recv_userauth_info_request"))
  67. /* Let the user know what password/host they are authing for */
  68. if (!cli_ses.interact_request_received) {
  69. fprintf(stderr, "Login for %s@%s\n", cli_opts.username,
  70. cli_opts.remotehost);
  71. }
  72. cli_ses.interact_request_received = 1;
  73. name = buf_getstring(ses.payload, NULL);
  74. instruction = buf_getstring(ses.payload, NULL);
  75. /* language tag */
  76. buf_eatstring(ses.payload);
  77. num_prompts = buf_getint(ses.payload);
  78. if (num_prompts >= DROPBEAR_MAX_CLI_INTERACT_PROMPTS) {
  79. dropbear_exit("Too many prompts received for keyboard-interactive");
  80. }
  81. /* we'll build the response as we go */
  82. CHECKCLEARTOWRITE();
  83. buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_INFO_RESPONSE);
  84. buf_putint(ses.writepayload, num_prompts);
  85. if (strlen(name) > 0) {
  86. cleantext(name);
  87. fprintf(stderr, "%s", name);
  88. }
  89. m_free(name);
  90. if (strlen(instruction) > 0) {
  91. cleantext(instruction);
  92. fprintf(stderr, "%s", instruction);
  93. }
  94. m_free(instruction);
  95. for (i = 0; i < num_prompts; i++) {
  96. unsigned int response_len = 0;
  97. prompt = buf_getstring(ses.payload, NULL);
  98. cleantext(prompt);
  99. echo = buf_getbool(ses.payload);
  100. if (!echo) {
  101. char* p = getpass_or_cancel(prompt);
  102. response = m_strdup(p);
  103. m_burn(p, strlen(p));
  104. } else {
  105. response = get_response(prompt);
  106. }
  107. response_len = strlen(response);
  108. buf_putstring(ses.writepayload, response, response_len);
  109. m_burn(response, response_len);
  110. m_free(prompt);
  111. m_free(response);
  112. }
  113. encrypt_packet();
  114. TRACE(("leave recv_msg_recv_userauth_info_request"))
  115. }
  116. void cli_auth_interactive() {
  117. TRACE(("enter cli_auth_interactive"))
  118. CHECKCLEARTOWRITE();
  119. buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST);
  120. /* username */
  121. buf_putstring(ses.writepayload, cli_opts.username,
  122. strlen(cli_opts.username));
  123. /* service name */
  124. buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION,
  125. SSH_SERVICE_CONNECTION_LEN);
  126. /* method */
  127. buf_putstring(ses.writepayload, AUTH_METHOD_INTERACT,
  128. AUTH_METHOD_INTERACT_LEN);
  129. /* empty language tag */
  130. buf_putstring(ses.writepayload, "", 0);
  131. /* empty submethods */
  132. buf_putstring(ses.writepayload, "", 0);
  133. encrypt_packet();
  134. cli_ses.interact_request_received = 0;
  135. TRACE(("leave cli_auth_interactive"))
  136. }
  137. #endif /* ENABLE_CLI_INTERACT_AUTH */