svr-authpasswd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifdef ENABLE_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() {
  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;
  48. unsigned int passwordlen;
  49. unsigned int changepw;
  50. passwdcrypt = ses.authstate.pw_passwd;
  51. #ifdef DEBUG_HACKCRYPT
  52. /* debugging crypt for non-root testing with shadows */
  53. passwdcrypt = DEBUG_HACKCRYPT;
  54. #endif
  55. /* check if client wants to change password */
  56. changepw = buf_getbool(ses.payload);
  57. if (changepw) {
  58. /* not implemented by this server */
  59. send_msg_userauth_failure(0, 1);
  60. return;
  61. }
  62. password = buf_getstring(ses.payload, &passwordlen);
  63. /* the first bytes of passwdcrypt are the salt */
  64. testcrypt = crypt(password, passwdcrypt);
  65. m_burn(password, passwordlen);
  66. m_free(password);
  67. if (testcrypt == NULL) {
  68. /* crypt() with an invalid salt like "!!" */
  69. dropbear_log(LOG_WARNING, "User account '%s' is locked",
  70. ses.authstate.pw_name);
  71. send_msg_userauth_failure(0, 1);
  72. return;
  73. }
  74. /* check for empty password */
  75. if (passwdcrypt[0] == '\0') {
  76. dropbear_log(LOG_WARNING, "User '%s' has blank password, rejected",
  77. ses.authstate.pw_name);
  78. send_msg_userauth_failure(0, 1);
  79. return;
  80. }
  81. if (constant_time_strcmp(testcrypt, passwdcrypt) == 0) {
  82. /* successful authentication */
  83. dropbear_log(LOG_NOTICE,
  84. "Password auth succeeded for '%s' from %s",
  85. ses.authstate.pw_name,
  86. svr_ses.addrstring);
  87. send_msg_userauth_success();
  88. } else {
  89. dropbear_log(LOG_WARNING,
  90. "Bad password attempt for '%s' from %s",
  91. ses.authstate.pw_name,
  92. svr_ses.addrstring);
  93. send_msg_userauth_failure(0, 1);
  94. }
  95. }
  96. #endif