void-packages icon indicating copy to clipboard operation
void-packages copied to clipboard

bash: define SSH_SOURCE_BASHRC

Open tornaria opened this issue 1 year ago • 4 comments

This makes bash agree with its documentation at https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

   Bash attempts to determine when it is being run with its standard
   input connected to a network connection, as when executed by the
   historical remote shell daemon, usually rshd, or the secure shell
   daemon sshd. If Bash determines it is being run non-interactively in
   this fashion, it reads and executes commands from ~/.bashrc, if that
   file exists and is readable. [...]

Notes:

  • Disabled by upstream in 2.05a (2001), manual never changed
  • This is enabled in Debian, Fedora, Gentoo, ...

To avoid this feature, add

[[ $- != *i* ]] && return

to the top of ~/.bashrc to skip running it in non-interactive mode.

Testing the changes

  • I tested the changes in this PR: YES

tornaria avatar May 11 '24 20:05 tornaria

Note also that the DEFAULT_PATH_VALUE is set to

"/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."

I wonder if we should remove the trailing . there and/or reorder to match our /etc/profile.

This default path applies only if PATH is unset when starting bash as in

$ env -i bash -c 'echo $PATH'
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:.

tornaria avatar May 11 '24 23:05 tornaria

The following would "fix" the default PATH in bash (to exclude .):

--- a/srcpkgs/bash/template
+++ b/srcpkgs/bash/template
@@ -24,7 +24,11 @@ alternatives="
  sh:sh:/usr/bin/bash
  sh:sh.1:/usr/share/man/man1/bash.1"
 
-CFLAGS="-DSSH_SOURCE_BASHRC -DNON_INTERACTIVE_LOGIN_SHELLS -DSYS_BASHRC='\"/etc/bash/bashrc\"'"
+CFLAGS="\
+ -DSSH_SOURCE_BASHRC \
+ -DNON_INTERACTIVE_LOGIN_SHELLS \
+ -DDEFAULT_PATH_VALUE='\"/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin\"' \
+ -DSYS_BASHRC='\"/etc/bash/bashrc\"'"
 
 post_install() {
        rm -r ${DESTDIR}/usr/share/doc

It's not a very common situation to start bash with unset PATH, so this is not that important.


On the other hand, adding -DSSH_SOURCE_BASAHRC is IMHO very, very, necessary, and the only way (afaik) to have a reasonable bash startup that works fine with ssh (e.g. to setup PATH, etc).

tornaria avatar May 13 '24 21:05 tornaria

I'm torn about this. On the one hand, I've often been frustrated working around default PATH settings to get access to remote executables in non-standard locations. On the other hand, having a non-interactive shell source the interactive configuration seems a bit skeevy.

ahesford avatar May 19 '24 00:05 ahesford

I'm not sure about a reasonable alternative. The closest one is:

  • set PermitUserEnvironment BASH_ENV in sshd_config (needs root)
  • set BASH_ENV=~/.bashrc in ~/.ssh/environment However, this will end up running ~/.bashrc also in subshells, which is not what we want.

The original feature, when SSH_SOURCE_BASHRC is set, will source ~/.bashrc only when SHLVL=1, so in principle it will only get run once per ssh.

tornaria avatar May 19 '24 16:05 tornaria