Skip to content

Commit 0c443f1

Browse files
authored
Add the ability to override the password prompt in keyboard-interactive (#331)
* Add the ability to override the password prompt in keyboard-interactive
1 parent ffe1402 commit 0c443f1

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added ``password_prompt`` argument to ``connect()`` to override the default
2+
prompt of "password:" when using keyboard-interactive authentication -- by :user:`Qalthos`

src/pylibsshext/session.pyx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,12 @@ cdef class Session(object):
195195
:param private_key_password: A password for the private key.
196196
:type private_key_password: bytes
197197
198-
:param password: The password to authenticate the ssh session
198+
:param password: The password to authenticate the ssh session.
199199
:type password: str
200200
201+
:param password_prompt: The prompt to look for when using password authentication.
202+
:type password_prompt: str
203+
201204
:param gssapi_server_identity: The service principal hostname to use.
202205
For example for principal ``host/[email protected]``, the hostname
203206
would be ``file.example.com``. Not required for GSSAPI authentication.
@@ -280,7 +283,7 @@ cdef class Session(object):
280283
# This will be neither user-interactive nor involve a keyboard,
281284
# but rather emulate the exchange using the provided password
282285
try:
283-
self.authenticate_interactive(kwargs["password"])
286+
self.authenticate_interactive(kwargs["password"], expected_prompt=kwargs.get("password_prompt"))
284287
except LibsshSessionException as ex:
285288
saved_exception = ex
286289
else:
@@ -442,19 +445,25 @@ cdef class Session(object):
442445
if rc == libssh.SSH_AUTH_ERROR or rc == libssh.SSH_AUTH_DENIED:
443446
raise LibsshSessionException("Failed to authenticate with password: %s" % self._get_session_error_str())
444447

445-
def authenticate_interactive(self, password):
448+
def authenticate_interactive(self, password, expected_prompt=None):
446449
"""Authenticate this session using keyboard-interactive authentication.
447450
448451
:param password: The password to authenticate with.
449452
:type password: str
450453
454+
:param expected_prompt: The expected password prompt.
455+
:type expected_prompt: str
456+
451457
:raises LibsshSessionException: If authentication failed.
452458
453459
:return: Nothing.
454460
:rtype: NoneType
455461
"""
456462
cdef int rc
457463
cdef char should_echo
464+
if expected_prompt is None:
465+
expected_prompt = "password:"
466+
expected_prompt = expected_prompt.lower().strip()
458467
rc = libssh.ssh_userauth_kbdint(self._libssh_session, NULL, NULL)
459468

460469
while rc == libssh.SSH_AUTH_INFO:
@@ -463,8 +472,9 @@ cdef class Session(object):
463472
if prompt_count > 0:
464473
for prompt in range(prompt_count):
465474
prompt_text = libssh.ssh_userauth_kbdint_getprompt(self._libssh_session, prompt, &should_echo)
475+
prompt_text = prompt_text.decode().lower().strip()
466476
prompt_text_list.append(prompt_text)
467-
if prompt_text.lower().strip().endswith(b"password:"):
477+
if prompt_text.lower().strip().endswith(expected_prompt):
468478
break
469479
else:
470480
raise LibsshSessionException("None of the prompts looked like password prompts: {err}".format(err=prompt_text_list))

0 commit comments

Comments
 (0)