cli-authpasswd.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2002,2003 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_PASSWORD_AUTH
  31. #ifdef ENABLE_CLI_ASKPASS_HELPER
  32. /* Returns 1 if we want to use the askpass program, 0 otherwise */
  33. static int want_askpass()
  34. {
  35. char* askpass_prog = NULL;
  36. askpass_prog = getenv("SSH_ASKPASS");
  37. return askpass_prog &&
  38. ((!isatty(STDIN_FILENO) && getenv("DISPLAY") )
  39. || getenv("SSH_ASKPASS_ALWAYS"));
  40. }
  41. /* returns a statically allocated password from a helper app, or NULL
  42. * on failure */
  43. static char *gui_getpass(const char *prompt) {
  44. pid_t pid;
  45. int p[2], maxlen, len, status;
  46. static char buf[DROPBEAR_MAX_CLI_PASS + 1];
  47. char* helper = NULL;
  48. TRACE(("enter gui_getpass"))
  49. helper = getenv("SSH_ASKPASS");
  50. if (!helper)
  51. {
  52. TRACE(("leave gui_getpass: no askpass program"))
  53. return NULL;
  54. }
  55. if (pipe(p) < 0) {
  56. TRACE(("error creating child pipe"))
  57. return NULL;
  58. }
  59. pid = fork();
  60. if (pid < 0) {
  61. TRACE(("fork error"))
  62. return NULL;
  63. }
  64. if (!pid) {
  65. /* child */
  66. close(p[0]);
  67. if (dup2(p[1], STDOUT_FILENO) < 0) {
  68. TRACE(("error redirecting stdout"))
  69. exit(1);
  70. }
  71. close(p[1]);
  72. execlp(helper, helper, prompt, (char *)0);
  73. TRACE(("execlp error"))
  74. exit(1);
  75. }
  76. close(p[1]);
  77. maxlen = sizeof(buf);
  78. while (maxlen > 0) {
  79. len = read(p[0], buf + sizeof(buf) - maxlen, maxlen);
  80. if (len > 0) {
  81. maxlen -= len;
  82. } else {
  83. if (errno != EINTR)
  84. break;
  85. }
  86. }
  87. close(p[0]);
  88. while (waitpid(pid, &status, 0) < 0 && errno == EINTR)
  89. ;
  90. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  91. return(NULL);
  92. len = sizeof(buf) - maxlen;
  93. buf[len] = '\0';
  94. if (len > 0 && buf[len - 1] == '\n')
  95. buf[len - 1] = '\0';
  96. TRACE(("leave gui_getpass"))
  97. return(buf);
  98. }
  99. #endif /* ENABLE_CLI_ASKPASS_HELPER */
  100. void cli_auth_password() {
  101. char* password = NULL;
  102. char prompt[80];
  103. TRACE(("enter cli_auth_password"))
  104. CHECKCLEARTOWRITE();
  105. snprintf(prompt, sizeof(prompt), "%s@%s's password: ",
  106. cli_opts.username, cli_opts.remotehost);
  107. #ifdef ENABLE_CLI_ASKPASS_HELPER
  108. if (want_askpass())
  109. {
  110. password = gui_getpass(prompt);
  111. if (!password) {
  112. dropbear_exit("No password");
  113. }
  114. } else
  115. #endif
  116. {
  117. password = getpass_or_cancel(prompt);
  118. }
  119. buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST);
  120. buf_putstring(ses.writepayload, cli_opts.username,
  121. strlen(cli_opts.username));
  122. buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION,
  123. SSH_SERVICE_CONNECTION_LEN);
  124. buf_putstring(ses.writepayload, AUTH_METHOD_PASSWORD,
  125. AUTH_METHOD_PASSWORD_LEN);
  126. buf_putbyte(ses.writepayload, 0); /* FALSE - so says the spec */
  127. buf_putstring(ses.writepayload, password, strlen(password));
  128. encrypt_packet();
  129. m_burn(password, strlen(password));
  130. TRACE(("leave cli_auth_password"))
  131. }
  132. #endif /* ENABLE_CLI_PASSWORD_AUTH */