Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@kajinamit
Copy link

Do not add a new line if the option is already defined. This avoids adding dupulicated lines and also make sure that the existing explicit setting to disable PermitUserEnvironment is purged.

Closes #1953

@kajinamit kajinamit force-pushed the issue/1953 branch 2 times, most recently from ee66fa0 to d9868eb Compare September 18, 2025 16:15
Do not add a new line if the option is already defined. This avoids
adding dupulicated lines and also make sure that the existing explicit
setting to disable PermitUserEnvironment is purged.

Closes voxpupuli#1953
case self['platform']
when /amazon|debian|ubuntu|archlinux|el-|centos|fedora|redhat|oracle|scientific|opensuse|sles|solaris/
directory = tmpdir
exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering why this was built so... complicated in the past.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that tmp file was required because you can't redirect to the input file.
Alternatively we may consider this would be more "safe" though sshd may no load the config file unless it is actually restarted so it feels overcomplicated.

directory = tmpdir
exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
exec(Beaker::Command.new("mv #{directory}/sshd_config.permit /etc/ssh/sshd_config"))
exec(Beaker::Command.new("sed -i -e 's/^PermitUserEnvironment .*/PermitUserEnvironment yes/' -e t -e '1s/^/PermitUserEnvironment yes\\n/' /etc/ssh/sshd_config"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned because sed -i is not portable across sed(1) implementations. Specifically, FreeBSD use standard getopt(3) and the -i option require a value (so it will use -e in this case, shifting all arguments and making the command-line invalid). GNU sed(1) do some magic and only use anything that is "glued" to -i and does not support passing an empty value "the regular way" with -i ''. So the only common supported syntax is -isomething where something is not blank.

Rather than in-place edits, when I write portable code, I generally prefer to have sed print the modified content to stdout, redirect it to a temporary file and move that file over the original one like it was done before.

Currently we do not support running acceptance tests on FreeBSD, but I would love to see that one day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate PermitUserEnvironment in /etc/ssh/sshd_config after multiple run

3 participants