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