From 5099dd6aa3363452785c000667f346dec3b97291 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 15 Mar 2022 15:47:02 +0530 Subject: [PATCH] Only use kitty's askpass if DISPLAY is set or OpenSSH is new enough to support SSH_ASKPASS_REQUIRE --- kittens/ssh/main.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/kittens/ssh/main.py b/kittens/ssh/main.py index fb416927c..cceffb3b4 100644 --- a/kittens/ssh/main.py +++ b/kittens/ssh/main.py @@ -501,6 +501,15 @@ def dcs_to_kitty(payload: Union[bytes, str], type: str = 'ssh') -> bytes: return b'\033P@kitty-' + type.encode('ascii') + b'|' + payload + b'\033\\' +@run_once +def ssh_version() -> Tuple[int, int]: + o = subprocess.check_output(['ssh', '-V'], stderr=subprocess.STDOUT).decode() + m = re.match(r'OpenSSH_(\d+).(\d+)', o) + if m is None: + raise ValueError(f'Invalid version string for OpenSSH: {o}') + return int(m.group(1)), int(m.group(2)) + + @contextmanager def drain_potential_tty_garbage(p: 'subprocess.Popen[bytes]', data_request: str) -> Iterator[None]: ssh_started_at = time.monotonic() @@ -562,10 +571,14 @@ def run_ssh(ssh_args: List[str], server_args: List[str], found_extra_args: Tuple if use_control_master: cmd[insertion_point:insertion_point] = connection_sharing_args(host_opts, int(os.environ['KITTY_PID'])) use_kitty_askpass = host_opts.askpass == 'native' or (host_opts.askpass == 'unless-set' and 'SSH_ASKPASS' not in os.environ) - need_to_request_data = not use_kitty_askpass if use_kitty_askpass: - os.environ['SSH_ASKPASS_REQUIRE'] = 'force' - os.environ['SSH_ASKPASS'] = os.path.join(shell_integration_dir, 'ssh', 'askpass.py') + # SSH_ASKPASS_REQUIRE was introduced in 8.4 release on 2020-09-27 + if os.environ.get('DISPLAY') or ssh_version() >= (8, 4): + os.environ['SSH_ASKPASS_REQUIRE'] = 'force' + os.environ['SSH_ASKPASS'] = os.path.join(shell_integration_dir, 'ssh', 'askpass.py') + else: + use_kitty_askpass = False + need_to_request_data = not use_kitty_askpass with restore_terminal_state() as echo_on: rcmd, replacements, shm_name = get_remote_command( remote_args, host_opts, hostname, hostname_for_match, uname, echo_on, request_data=need_to_request_data)