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