2023-02-01 13:09:14 -08:00
|
|
|
{{- if ne .host.distro.family "windows" -}}
|
2023-01-27 00:19:52 -08:00
|
|
|
#!/usr/bin/env bash
|
2023-04-15 16:14:30 -07:00
|
|
|
# @file SSHD Configuration
|
|
|
|
# @brief Applies SSHD system configuration and then restarts / enables the SSH server
|
|
|
|
# @description
|
|
|
|
# This script applies the SSH server MOTD banner and `sshd_config` (which are housed in the `home/private_dot_ssh/system` location)
|
|
|
|
# to the system by copying the files to the system location and then restarting / enabling the system SSH server.
|
|
|
|
#
|
|
|
|
# ## Links
|
|
|
|
#
|
|
|
|
# * [System SSHD configurations](https://github.com/megabyte-labs/install.doctor/tree/master/home/private_dot_ssh/system)
|
2023-01-27 00:19:52 -08:00
|
|
|
|
2023-02-01 13:09:14 -08:00
|
|
|
# sshd_config hash: {{- include (joinPath .host.home ".ssh" "system" "sshd_config") | sha256sum -}}
|
|
|
|
# banner hash: {{- include (joinPath .host.home ".ssh" "system" "banner") | sha256sum -}}
|
2023-01-27 01:28:02 -08:00
|
|
|
|
|
|
|
{{ includeTemplate "universal/profile" }}
|
|
|
|
{{ includeTemplate "universal/logg" }}
|
|
|
|
|
2023-01-27 00:19:52 -08:00
|
|
|
### Update /etc/ssh/sshd_config if environment is not WSL
|
2023-02-15 19:14:33 -08:00
|
|
|
if [[ ! "$(test -d /proc && grep Microsoft /proc/version > /dev/null)" ]]; then
|
2023-01-27 00:19:52 -08:00
|
|
|
if [ -d /etc/ssh ]; then
|
|
|
|
logg info 'Copying ~/.ssh/system/banner to /etc/ssh/banner'
|
|
|
|
sudo cp -f "$HOME/.ssh/system/banner" /etc/ssh/banner
|
|
|
|
|
|
|
|
logg info 'Copying ~/.ssh/system/sshd_config to /etc/ssh/sshd_config'
|
|
|
|
sudo cp -f "$HOME/.ssh/system/sshd_config" /etc/ssh/sshd_config
|
|
|
|
|
2023-02-01 13:30:51 -08:00
|
|
|
if command -v semanage > /dev/null; then
|
|
|
|
logg info 'Apply SELinux configuration addressing custom SSH port'
|
|
|
|
sudo semanage port -a -t ssh_port_t -p tcp {{ .host.ssh.port }}
|
|
|
|
logg info 'Allow NIS SSHD'
|
|
|
|
sudo setsebool -P nis_enabled 1
|
|
|
|
fi
|
|
|
|
|
2023-01-27 00:19:52 -08:00
|
|
|
### Restart SSH server
|
|
|
|
if [ -d /Applications ] && [ -d /System ]; then
|
|
|
|
# macOS
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'Running sudo launchctl stop com.openssh.sshd'
|
2023-01-27 00:19:52 -08:00
|
|
|
sudo launchctl stop com.openssh.sshd
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'Running sudo launchctl start com.openssh.sshd'
|
2023-11-06 22:25:00 -08:00
|
|
|
sudo launchctl start com.openssh.sshd && logg success 'Successfully ran launchctl start com.openssh.sshd'
|
2023-01-27 00:19:52 -08:00
|
|
|
else
|
|
|
|
# Linux
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'Enabling the sshd service'
|
2023-01-27 00:19:52 -08:00
|
|
|
sudo systemctl enable sshd
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'Restarting the sshd service'
|
2023-11-06 22:25:00 -08:00
|
|
|
sudo systemctl restart sshd && logg success 'Successfully ran sudo systemctl restart sshd'
|
2023-01-27 00:19:52 -08:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
logg warn 'The /etc/ssh folder does not exist'
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
logg info 'Skipping sshd_config application since environment is WSL'
|
|
|
|
fi
|
|
|
|
|
|
|
|
{{ end -}}
|