dropbear 2015.71
[tomato.git] / release / src / router / dropbear / svr-authpasswd.c
blob9852ac66f989185cb6d71bbbfad8c6714a776ecc
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:
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
23 * SOFTWARE. */
25 /* Validates a user password */
27 #include "includes.h"
28 #include "session.h"
29 #include "buffer.h"
30 #include "dbutil.h"
31 #include "auth.h"
32 #include "runopts.h"
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);
42 if (la != lb) {
43 return 1;
46 return constant_time_memcmp(a, b, la);
49 /* Process a password auth request, sending success or failure messages as
50 * appropriate */
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 */
55 char * password;
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;
65 #endif
67 /* check if client wants to change password */
68 changepw = buf_getbool(ses.payload);
69 if (changepw) {
70 /* not implemented by this server */
71 send_msg_userauth_failure(0, 1);
72 return;
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);
80 m_free(password);
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);
87 return;
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);
95 return;
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,
103 svr_ses.addrstring);
104 send_msg_userauth_success();
105 } else {
106 dropbear_log(LOG_WARNING,
107 "Bad password attempt for '%s' from %s",
108 ses.authstate.pw_name,
109 svr_ses.addrstring);
110 send_msg_userauth_failure(0, 1);
114 #endif