2 * Dropbear - a SSH2 server
4 * Copyright (c) 2002,2003 Matt Johnston
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 /* Validates a user password */
34 #ifdef ENABLE_SVR_PASSWORD_AUTH
36 /* not constant time when strings are differing lengths.
37 string content isn't leaked, and crypt hashes are predictable length. */
38 static int constant_time_strcmp(const char* a
, const char* b
) {
39 size_t la
= strlen(a
);
40 size_t lb
= strlen(b
);
46 return constant_time_memcmp(a
, b
, la
);
49 /* Process a password auth request, sending success or failure messages as
51 void svr_auth_password() {
53 char * passwdcrypt
= NULL
; /* the crypt from /etc/passwd or /etc/shadow */
54 char * testcrypt
= NULL
; /* crypt generated from the user's password sent */
56 unsigned int passwordlen
;
58 unsigned int changepw
;
60 passwdcrypt
= ses
.authstate
.pw_passwd
;
62 #ifdef DEBUG_HACKCRYPT
63 /* debugging crypt for non-root testing with shadows */
64 passwdcrypt
= DEBUG_HACKCRYPT
;
67 /* check if client wants to change password */
68 changepw
= buf_getbool(ses
.payload
);
70 /* not implemented by this server */
71 send_msg_userauth_failure(0, 1);
75 password
= buf_getstring(ses
.payload
, &passwordlen
);
77 /* the first bytes of passwdcrypt are the salt */
78 testcrypt
= crypt(password
, passwdcrypt
);
79 m_burn(password
, passwordlen
);
82 if (testcrypt
== NULL
) {
83 /* crypt() with an invalid salt like "!!" */
84 dropbear_log(LOG_WARNING
, "User account '%s' is locked",
85 ses
.authstate
.pw_name
);
86 send_msg_userauth_failure(0, 1);
90 /* check for empty password */
91 if (passwdcrypt
[0] == '\0') {
92 dropbear_log(LOG_WARNING
, "User '%s' has blank password, rejected",
93 ses
.authstate
.pw_name
);
94 send_msg_userauth_failure(0, 1);
98 if (constant_time_strcmp(testcrypt
, passwdcrypt
) == 0) {
99 /* successful authentication */
100 dropbear_log(LOG_NOTICE
,
101 "Password auth succeeded for '%s' from %s",
102 ses
.authstate
.pw_name
,
104 send_msg_userauth_success();
106 dropbear_log(LOG_WARNING
,
107 "Bad password attempt for '%s' from %s",
108 ses
.authstate
.pw_name
,
110 send_msg_userauth_failure(0, 1);