svr-authpasswd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Dropbear - a SSH2 server
  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. /* Validates a user password */
  25. #include "includes.h"
  26. #include "session.h"
  27. #include "buffer.h"
  28. #include "dbutil.h"
  29. #include "auth.h"
  30. #include "runopts.h"
  31. #if DROPBEAR_SVR_PASSWORD_AUTH
  32. /* not constant time when strings are differing lengths.
  33. string content isn't leaked, and crypt hashes are predictable length. */
  34. static int constant_time_strcmp(const char* a, const char* b) {
  35. size_t la = strlen(a);
  36. size_t lb = strlen(b);
  37. if (la != lb) {
  38. return 1;
  39. }
  40. return constant_time_memcmp(a, b, la);
  41. }
  42. /* Process a password auth request, sending success or failure messages as
  43. * appropriate */
  44. void svr_auth_password(int valid_user) {
  45. char * passwdcrypt = NULL; /* the crypt from /etc/passwd or /etc/shadow */
  46. char * testcrypt = NULL; /* crypt generated from the user's password sent */
  47. char * password = NULL;
  48. unsigned int passwordlen;
  49. unsigned int changepw;
  50. /* check if client wants to change password */
  51. changepw = buf_getbool(ses.payload);
  52. if (changepw) {
  53. /* not implemented by this server */
  54. send_msg_userauth_failure(0, 1);
  55. return;
  56. }
  57. password = buf_getstring(ses.payload, &passwordlen);
  58. if (valid_user && passwordlen <= DROPBEAR_MAX_PASSWORD_LEN) {
  59. /* the first bytes of passwdcrypt are the salt */
  60. passwdcrypt = ses.authstate.pw_passwd;
  61. testcrypt = crypt(password, passwdcrypt);
  62. }
  63. m_burn(password, passwordlen);
  64. m_free(password);
  65. /* After we have got the payload contents we can exit if the username
  66. is invalid. Invalid users have already been logged. */
  67. if (!valid_user) {
  68. send_msg_userauth_failure(0, 1);
  69. return;
  70. }
  71. if (passwordlen > DROPBEAR_MAX_PASSWORD_LEN) {
  72. dropbear_log(LOG_WARNING,
  73. "Too-long password attempt for '%s' from %s",
  74. ses.authstate.pw_name,
  75. svr_ses.addrstring);
  76. send_msg_userauth_failure(0, 1);
  77. return;
  78. }
  79. if (testcrypt == NULL) {
  80. /* crypt() with an invalid salt like "!!" */
  81. dropbear_log(LOG_WARNING, "User account '%s' is locked",
  82. ses.authstate.pw_name);
  83. send_msg_userauth_failure(0, 1);
  84. return;
  85. }
  86. /* check for empty password */
  87. if (passwdcrypt[0] == '\0') {
  88. dropbear_log(LOG_WARNING, "User '%s' has blank password, rejected",
  89. ses.authstate.pw_name);
  90. send_msg_userauth_failure(0, 1);
  91. return;
  92. }
  93. if (constant_time_strcmp(testcrypt, passwdcrypt) == 0) {
  94. /* successful authentication */
  95. dropbear_log(LOG_NOTICE,
  96. "Password auth succeeded for '%s' from %s",
  97. ses.authstate.pw_name,
  98. svr_ses.addrstring);
  99. send_msg_userauth_success();
  100. } else {
  101. dropbear_log(LOG_WARNING,
  102. "Bad password attempt for '%s' from %s",
  103. ses.authstate.pw_name,
  104. svr_ses.addrstring);
  105. send_msg_userauth_failure(0, 1);
  106. }
  107. }
  108. #endif