Skip to content

Commit 7b5a970

Browse files
committed
overwrite auth_bit_mask to avoid unnecessary auth failures
1 parent 1313853 commit 7b5a970

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/ssh.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,21 @@ static int ssh_authenticate(ssh_session ssh, struct mscp_ssh_opts *opts)
123123
if (ssh_userauth_none(ssh, NULL) == SSH_AUTH_SUCCESS)
124124
return 0;
125125

126-
/* save auth_bit_mask for further authentications */
126+
/* save auth_bit_mask for further authentications.
127+
* when an authentication succeeds, auth_bit_mask is
128+
* overwritten with the suceeded authentication method
129+
* to avoid authentication failures by other methods.
130+
*/
127131
auth_bit_mask = ssh_userauth_list(ssh, NULL);
128132
}
129133

130134
if (auth_bit_mask & SSH_AUTH_METHOD_PUBLICKEY) {
131135
char *p = opts->passphrase ? opts->passphrase : NULL;
132-
if (ssh_userauth_publickey_auto(ssh, NULL, p) == SSH_AUTH_SUCCESS)
136+
if (ssh_userauth_publickey_auto(ssh, NULL, p) == SSH_AUTH_SUCCESS) {
137+
auth_bit_mask = SSH_AUTH_METHOD_PUBLICKEY;
133138
return 0;
139+
}
140+
134141
}
135142

136143
if (auth_bit_mask & SSH_AUTH_METHOD_PASSWORD) {
@@ -146,14 +153,17 @@ static int ssh_authenticate(ssh_session ssh, struct mscp_ssh_opts *opts)
146153
}
147154
}
148155

149-
if (ssh_userauth_password(ssh, NULL, opts->password) == SSH_AUTH_SUCCESS)
150-
return 0;
156+
if (ssh_userauth_password(ssh, NULL, opts->password) == SSH_AUTH_SUCCESS) {
157+
auth_bit_mask = SSH_AUTH_METHOD_PASSWORD;
158+
return 0;
159+
}
151160
}
152161

153-
auth_bit_mask = ssh_userauth_list(ssh, NULL);
154162
if (auth_bit_mask & SSH_AUTH_METHOD_INTERACTIVE) {
155-
if (ssh_authenticate_kbdint(ssh) == SSH_AUTH_SUCCESS)
156-
return 0;
163+
if (ssh_authenticate_kbdint(ssh) == SSH_AUTH_SUCCESS) {
164+
auth_bit_mask = SSH_AUTH_METHOD_INTERACTIVE;
165+
return 0;
166+
}
157167
}
158168

159169
return -1;

0 commit comments

Comments
 (0)