# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / authpasswd.c
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 "util.h"
31 #include "auth.h"
32 #include "authpasswd.h"
33
34 // BRCM begin
35 #ifndef SSHD_GENKEY
36 #include "cliapi.h"
37 extern int glbAccessMode;
38 #endif
39 // BRCM end
40
41 #ifdef DROPBEAR_PASSWORD_AUTH
42
43 /* Process a password auth request, sending success or failure messages as
44  * appropriate */
45
46 void passwordauth() {
47
48 //brcm begin
49 #ifndef SSHD_GENKEY 
50         
51 #ifdef HAVE_SHADOW_H
52         struct spwd *spasswd;
53 #endif
54         char * passwdcrypt; /* the crypt from /etc/passwd or /etc/shadow */
55         char * testcrypt; /* crypt generated from the user's password sent */
56         unsigned char * password;
57         unsigned int passwordlen;
58
59         unsigned char changepw;
60 // brcm add notMatched flag.
61     int notMatched = 0;
62
63         passwdcrypt = ses.authstate.pw->pw_passwd;
64 #ifdef HAVE_SHADOW_H
65         /* get the shadow password if possible */
66         spasswd = getspnam(ses.authstate.pw->pw_name);
67         if (spasswd != NULL && spasswd->sp_pwdp != NULL) {
68                 passwdcrypt = spasswd->sp_pwdp;
69         }
70 #endif
71
72 #ifdef DEBUG_HACKCRYPT
73         /* debugging crypt for non-root testing with shadows */
74         passwdcrypt = DEBUG_HACKCRYPT;
75 #endif
76
77         /* check for empty password - need to do this again here
78          * since the shadow password may differ to that tested
79          * in auth.c */
80         if (passwdcrypt[0] == '\0') {
81                 dropbear_log(LOG_WARNING,
82                                 "disallowed login with empty password for '%s' from %s",
83                                 ses.authstate.printableuser, ses.addrstring);
84                 send_msg_userauth_failure(0, 1);
85                 return;
86         }
87
88         /* check if client wants to change password */
89         changepw = buf_getbyte(ses.payload);
90         if (changepw) {
91                 /* not implemented by this server */
92                 send_msg_userauth_failure(0, 1);
93                 return;
94         }
95
96         password = buf_getstring(ses.payload, &passwordlen);
97
98         /* clear the buffer containing the password */
99         buf_incrpos(ses.payload, -passwordlen - 4);
100         m_burn(buf_getptr(ses.payload, passwordlen + 4), passwordlen + 4);
101
102         /* the first bytes of passwdcrypt are the salt */
103         testcrypt = crypt((char*)password, passwdcrypt);
104     
105     // brcm add local/remote login check
106     if ((glbAccessMode == CLI_ACCESS_LOCAL && \
107         (strcmp(ses.authstate.username, "ses.authstate.username") && strcmp(ses.authstate.username, "root"))) ||
108         (glbAccessMode == CLI_ACCESS_REMOTE && strcmp(ses.authstate.username, "support")))
109         notMatched = 1;
110
111         if (strcmp(testcrypt, passwdcrypt) == 0 && notMatched == 0) {
112                 /* successful authentication */
113       // brcm commented next msg
114                 //dropbear_log(LOG_NOTICE, 
115                 //              "password auth succeeded for '%s' from %s",
116                 //              ses.authstate.printableuser, ses.addrstring);
117                 send_msg_userauth_success();
118         } else {
119                 dropbear_log(LOG_WARNING,
120                                 "bad password attempt for '%s' from %s",
121                                 ses.authstate.printableuser, ses.addrstring);
122                 send_msg_userauth_failure(0, 1);
123         }
124
125         m_burn(password, passwordlen);
126         m_free(password);
127
128 #endif // brcm end, ifndef SSHD_GENKEY
129 }
130
131 #endif /* DROPBEAR_PASSWORD_AUTH */