From f5cc58ea9dd28eff6d8a78c46d0aa637696886ec Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 11 Mar 2022 15:41:56 +0800 Subject: [PATCH 1/8] Unset the safe source function in sh script and minor refactoring --- shell-integration/ssh/bootstrap.py | 2 +- shell-integration/ssh/bootstrap.sh | 35 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/shell-integration/ssh/bootstrap.py b/shell-integration/ssh/bootstrap.py index e10b3cc09..c99c41cbc 100644 --- a/shell-integration/ssh/bootstrap.py +++ b/shell-integration/ssh/bootstrap.py @@ -124,7 +124,7 @@ def compile_terminfo(base): q = os.path.join(base, tname, '78', 'xterm-kitty') if not os.path.exists(q): os.makedirs(os.path.dirname(q), exist_ok=True) - os.symlink("../x/xterm-kitty", q) + os.symlink('../x/xterm-kitty', q) def get_data(): diff --git a/shell-integration/ssh/bootstrap.sh b/shell-integration/ssh/bootstrap.sh index 4e911a98e..d02ed4a42 100644 --- a/shell-integration/ssh/bootstrap.sh +++ b/shell-integration/ssh/bootstrap.sh @@ -17,8 +17,8 @@ die() { printf "\033[31m%s\033[m\n\r" "$*" > /dev/stderr; cleanup_on_bootstrap_e python_detected="0" detect_python() { if [ python_detected = "1" ]; then - [ -n "$python" ] && return 0; - return 1; + [ -n "$python" ] && return 0 + return 1 fi python_detected="1" python=$(command -v python3) @@ -31,8 +31,8 @@ detect_python() { perl_detected="0" detect_perl() { if [ perl_detected = "1" ]; then - [ -n "$perl" ] && return 0; - return 1; + [ -n "$perl" ] && return 0 + return 1 fi perl_detected="1" perl=$(command -v perl) @@ -155,7 +155,7 @@ while n > 0: } else read_n_bytes_from_tty() { - command dd bs=1 count="$1" < /dev/tty 2> /dev/null + command dd bs=1 count="$1" 2> /dev/null < /dev/tty } fi fi @@ -205,7 +205,7 @@ compile_terminfo() { # compile terminfo for this system if [ -x "$(command -v tic)" ]; then tic_out=$(command tic -x -o "$1/$tname" "$1/.terminfo/kitty.terminfo" 2>&1) - [ $? = 0 ] || die "Failed to compile terminfo with err: $tic_out"; + [ $? = 0 ] || die "Failed to compile terminfo with err: $tic_out" fi # Ensure the 78 dir is present @@ -288,7 +288,7 @@ parse_passwd_record() { using_getent() { cmd=$(command -v getent) if [ -n "$cmd" ]; then - output=$(command $cmd passwd $USER 2>/dev/null) + output=$(command "$cmd" passwd "$USER" 2>/dev/null) if [ $? = 0 ]; then login_shell=$(echo $output | parse_passwd_record) if login_shell_is_ok; then return 0; fi @@ -300,7 +300,7 @@ using_getent() { using_id() { cmd=$(command -v id) if [ -n "$cmd" ]; then - output=$(command $cmd -P $USER 2>/dev/null) + output=$(command "$cmd" -P "$USER" 2>/dev/null) if [ $? = 0 ]; then login_shell=$(echo $output | parse_passwd_record) if login_shell_is_ok; then return 0; fi @@ -313,7 +313,7 @@ using_python() { if detect_python; then output=$(command "$python" -c "import pwd, os; print(pwd.getpwuid(os.geteuid()).pw_shell)") if [ $? = 0 ]; then - login_shell=$output + login_shell="$output" if login_shell_is_ok; then return 0; fi fi fi @@ -324,7 +324,7 @@ using_perl() { if detect_perl; then output=$(command "$perl" -e 'my $shell = (getpwuid($<))[8]; print $shell') if [ $? = 0 ]; then - login_shell=$output + login_shell="$output" if login_shell_is_ok; then return 0; fi fi fi @@ -431,19 +431,20 @@ execute_sh_with_posix_env() { command "$login_shell" -l -c ":" > /dev/null 2> /dev/null && return # sh supports -l so use that [ -z "$shell_integration_dir" ] && die "Could not read data over tty ssh kitten cannot function" sh_dir="$shell_integration_dir/sh" - command mkdir -p "$sh_dir" || die "Creating $sh_dir failed"; + command mkdir -p "$sh_dir" || die "Creating $sh_dir failed" sh_script="$sh_dir/login_shell_env.sh" # Source /etc/profile, ~/.profile, and then check and source ENV printf "%s" ' if [ -n "$KITTY_SH_INJECT" ]; then unset ENV; unset KITTY_SH_INJECT - _ksi_safe_source() { if [ -f "$1" -a -r "$1" ]; then . "$1"; return 0; fi; return 1; } - if [ -n "$KITTY_SH_POSIX_ENV" ]; then export ENV="$KITTY_SH_POSIX_ENV"; fi + _ksi_safe_source() { [ -f "$1" -a -r "$1" ] || return 1; . "$1"; return 0; } + [ -n "$KITTY_SH_POSIX_ENV" ] && export ENV="$KITTY_SH_POSIX_ENV" unset KITTY_SH_POSIX_ENV _ksi_safe_source "/etc/profile"; _ksi_safe_source "${HOME-}/.profile" - if [ -n "$ENV" ]; then _ksi_safe_source "$ENV"; fi + [ -n "$ENV" ] && _ksi_safe_source "$ENV" + unset -f _ksi_safe_source fi' > "$sh_script" - export KITTY_SH_INJECT=1 + export KITTY_SH_INJECT="1" [ -n "$ENV" ] && export KITTY_SH_POSIX_ENV="$ENV" export ENV="$sh_script" exec "$login_shell" @@ -468,8 +469,8 @@ esac # We need to pass the first argument to the executed program with a leading - # to make sure the shell executes as a login shell. Note that not all shells # support exec -a so we use the below to try to detect such shells -[ "$(exec -a echo echo OK 2> /dev/null)" = "OK" ] && exec -a "-$shell_name" $login_shell +[ "$(exec -a echo echo OK 2> /dev/null)" = "OK" ] && exec -a "-$shell_name" "$login_shell" execute_with_python execute_with_perl execute_sh_with_posix_env -exec $login_shell "-l" +exec "$login_shell" "-l" From fe8aaca3203dd73282234ffe208d7c6a2b7d6d3f Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 11 Mar 2022 15:42:09 +0800 Subject: [PATCH 2/8] Use POSIX LOGNAME environment variable --- shell-integration/ssh/bootstrap.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/shell-integration/ssh/bootstrap.sh b/shell-integration/ssh/bootstrap.sh index d02ed4a42..7f349be62 100644 --- a/shell-integration/ssh/bootstrap.sh +++ b/shell-integration/ssh/bootstrap.sh @@ -171,6 +171,7 @@ hostname="$HOSTNAME" # ensure $HOME is set [ -z "$HOME" ] && HOME=~ # ensure $USER is set +[ -z "$USER" ] && USER="$LOGNAME" [ -z "$USER" ] && USER="$(command whoami 2> /dev/null)" leading_data="" From 39a78f6be3d6994ef94b6acd203ba750f0d6eefd Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 11 Mar 2022 15:42:23 +0800 Subject: [PATCH 3/8] Explicitly set STDOUT to binary mode --- shell-integration/ssh/bootstrap.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shell-integration/ssh/bootstrap.sh b/shell-integration/ssh/bootstrap.sh index 7f349be62..c634ad769 100644 --- a/shell-integration/ssh/bootstrap.sh +++ b/shell-integration/ssh/bootstrap.sh @@ -149,9 +149,9 @@ while n > 0: } elif detect_perl; then read_n_bytes_from_tty() { - command "$perl" -MList::Util=min -e \ -'open(my $fh," /dev/null + command "$perl" -MList::Util=min -e ' +open(my $fh,"<","/dev/tty");binmode($fh);binmode(STDOUT);my ($n,$buf)=(@ARGV[0],""); +while($n>0){my $rv=sysread($fh,$buf,min(65536,$n));unless($rv){exit(1);};$n-=$rv;print STDOUT $buf;}' "$1" 2> /dev/null } else read_n_bytes_from_tty() { From ea28951e0ebd50e2b86252c201fab8c557eacaab Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 11 Mar 2022 15:42:34 +0800 Subject: [PATCH 4/8] Fall back to run the shell without -l option --- shell-integration/ssh/bootstrap.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/shell-integration/ssh/bootstrap.sh b/shell-integration/ssh/bootstrap.sh index c634ad769..e4d0f6169 100644 --- a/shell-integration/ssh/bootstrap.sh +++ b/shell-integration/ssh/bootstrap.sh @@ -475,3 +475,6 @@ execute_with_python execute_with_perl execute_sh_with_posix_env exec "$login_shell" "-l" + +printf "%s\n" "Could not execute the shell as a login shell" > /dev/stderr +exec "$login_shell" From 72718cbab7535a424b667af1e252cdac260e215b Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 11 Mar 2022 15:42:45 +0800 Subject: [PATCH 5/8] Remove login shell name suffix restriction Some shells do not end with sh, consistent with the python bootstrap implementation. --- shell-integration/ssh/bootstrap.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/shell-integration/ssh/bootstrap.sh b/shell-integration/ssh/bootstrap.sh index e4d0f6169..e0f3d8aa2 100644 --- a/shell-integration/ssh/bootstrap.sh +++ b/shell-integration/ssh/bootstrap.sh @@ -275,10 +275,7 @@ if [ "$tty_ok" = "y" ]; then fi login_shell_is_ok() { - if [ -z "$login_shell" -o ! -x "$login_shell" ]; then return 1; fi - case "$login_shell" in - *sh) return 0; - esac + if [ -n "$login_shell" -a -x "$login_shell" ]; then return 0; fi return 1 } From d3c0c422a57fb34a4016dfe8a42ee6def73cb0d6 Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 11 Mar 2022 15:42:52 +0800 Subject: [PATCH 6/8] Add user uid suffix to tmp runtime dir symlink --- kittens/ssh/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kittens/ssh/main.py b/kittens/ssh/main.py index 80db2c7db..fe6fcef4b 100644 --- a/kittens/ssh/main.py +++ b/kittens/ssh/main.py @@ -451,7 +451,7 @@ def connection_sharing_args(opts: SSHOptions, kitty_pid: int) -> List[str]: # ~104 chars. macOS has no system runtime dir so we use a cache dir in # /Users/WHY_DOES_ANYONE_USE_MACOS/Library/Caches/APPLE_ARE_IDIOTIC if len(rd) > 35 and os.path.isdir('/tmp'): - idiotic_design = '/tmp/kssh-rdir' + idiotic_design = f'/tmp/kssh-rdir-{os.getuid()}' try: os.symlink(rd, idiotic_design) except FileExistsError: From bd87d509483b8a70607e7003270447a75ca21816 Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 11 Mar 2022 16:47:03 +0800 Subject: [PATCH 7/8] Remove the ssh folder in cache_dir --- kittens/ssh/main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/kittens/ssh/main.py b/kittens/ssh/main.py index fe6fcef4b..212edea77 100644 --- a/kittens/ssh/main.py +++ b/kittens/ssh/main.py @@ -227,8 +227,6 @@ def bootstrap_script( with open(os.path.join(shell_integration_dir, 'ssh', f'bootstrap.{script_type}')) as f: ans = f.read() pw = secrets.token_hex() - ddir = os.path.join(cache_dir(), 'ssh') - os.makedirs(ddir, exist_ok=True) data = {'pw': pw, 'env': dict(os.environ), 'opts': ssh_opts_dict, 'cli_hostname': cli_hostname, 'cli_uname': cli_uname} db = json.dumps(data) with SharedMemory(size=len(db) + SharedMemory.num_bytes_for_size, mode=stat.S_IREAD, prefix=f'kssh-{os.getpid()}-') as shm: From 740e787f54e6a99f765f8b606312dae3b40b7f9b Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 11 Mar 2022 17:28:29 +0800 Subject: [PATCH 8/8] ... --- kittens/ssh/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kittens/ssh/main.py b/kittens/ssh/main.py index 212edea77..88044df7d 100644 --- a/kittens/ssh/main.py +++ b/kittens/ssh/main.py @@ -25,7 +25,7 @@ from typing import ( ) from kitty.constants import ( - cache_dir, runtime_dir, shell_integration_dir, ssh_control_master_template, + runtime_dir, shell_integration_dir, ssh_control_master_template, terminfo_dir ) from kitty.fast_data_types import get_options