|
| 1 | +#!/bin/bash |
| 2 | +# info: add system ssh jail |
| 3 | +# options: [RESTART] |
| 4 | +# |
| 5 | +# example: v-add-sys-ssh-jail yes |
| 6 | +# |
| 7 | +# This function enables ssh jailed environment. |
| 8 | + |
| 9 | +#----------------------------------------------------------# |
| 10 | +# Variables & Functions # |
| 11 | +#----------------------------------------------------------# |
| 12 | + |
| 13 | +# Includes |
| 14 | +# shellcheck source=/etc/hestiacp/hestia.conf |
| 15 | +source /etc/hestiacp/hestia.conf |
| 16 | +# shellcheck source=/usr/local/hestia/func/main.sh |
| 17 | +source $HESTIA/func/main.sh |
| 18 | +# load config file |
| 19 | +source_conf "$HESTIA/conf/hestia.conf" |
| 20 | + |
| 21 | +restart=$1 |
| 22 | + |
| 23 | +#----------------------------------------------------------# |
| 24 | +# Verifications # |
| 25 | +#----------------------------------------------------------# |
| 26 | + |
| 27 | +# Checking if jailkit is installed |
| 28 | +if [ ! -x /sbin/jk_init ]; then |
| 29 | + exit |
| 30 | +fi |
| 31 | + |
| 32 | +# Perform verification if read-only mode is enabled |
| 33 | +check_hestia_demo_mode |
| 34 | + |
| 35 | +#----------------------------------------------------------# |
| 36 | +# Action # |
| 37 | +#----------------------------------------------------------# |
| 38 | + |
| 39 | +# Checking sshd directives |
| 40 | +config='/etc/ssh/sshd_config' |
| 41 | +ssh_i=$(grep -n "^# Hestia SSH Chroot" $config) |
| 42 | + |
| 43 | +# Enabling jailed ssh |
| 44 | +if [ -z "$ssh_i" ]; then |
| 45 | + echo " " >> $config |
| 46 | + echo "# Hestia SSH Chroot" >> $config |
| 47 | + echo "Match Group ssh-jailed" >> $config |
| 48 | + echo " ChrootDirectory /srv/jail/%u" >> $config |
| 49 | + echo " X11Forwarding no" >> $config |
| 50 | + echo " AllowTCPForwarding no" >> $config |
| 51 | + restart='yes' |
| 52 | +fi |
| 53 | + |
| 54 | +# Validating opensshd config |
| 55 | +if [ "$restart" = 'yes' ]; then |
| 56 | + subj="OpenSSH restart failed" |
| 57 | + email=$(grep CONTACT "$HESTIA/data/users/$ROOT_USER/user.conf" | cut -f 2 -d \') |
| 58 | + /usr/sbin/sshd -t > /dev/null 2>&1 |
| 59 | + if [ "$?" -ne 0 ]; then |
| 60 | + mail_text="OpenSSH can not be restarted. Please check config: |
| 61 | + \n\n$(/usr/sbin/sshd -t)" |
| 62 | + echo -e "$mail_text" | $SENDMAIL -s "$subj" $email |
| 63 | + else |
| 64 | + service sshd restart > /dev/null 2>&1 |
| 65 | + fi |
| 66 | +fi |
| 67 | + |
| 68 | +# Adding group |
| 69 | +groupadd ssh-jailed 2> /dev/null |
| 70 | + |
| 71 | +# Checking jailkit init |
| 72 | +jk_init='/etc/jailkit/jk_init.ini' |
| 73 | +jk_php_i=$(grep -n "^# Hestia Jail Settings" $jk_init) |
| 74 | + |
| 75 | +# Add PHP to jailkit init to allow usage of it within jail |
| 76 | +if [ -z "$jk_php_i" ]; then |
| 77 | + cp -f $HESTIA_COMMON_DIR/jailkit/jk_init.ini /etc/jailkit |
| 78 | +fi |
| 79 | + |
| 80 | +# Restart ssh service |
| 81 | +if [ "$restart" = 'no' ]; then |
| 82 | + # Skip restart of SSH daemon |
| 83 | + echo "" > /dev/null 2>&1 |
| 84 | +else |
| 85 | + service ssh restart > /dev/null 2>&1 |
| 86 | +fi |
| 87 | + |
| 88 | +# Jails need maintenance to update the binaries within the jail. To do so we just reset the chroot |
| 89 | +# and reapply the jail |
| 90 | +for user in $("$BIN/v-list-users" list); do |
| 91 | + check_jail_enabled=$(grep "SHELL_JAIL_ENABLED='yes'" $HESTIA/data/users/$user/user.conf) |
| 92 | + |
| 93 | + # If jail enabled try to jail the user |
| 94 | + if [ -n "$check_jail_enabled" ]; then |
| 95 | + $BIN/v-add-user-ssh-jail "$user" "no" |
| 96 | + fi |
| 97 | +done |
| 98 | + |
| 99 | +# Add v-add-sys-ssh-jail to startup |
| 100 | +if [ ! -e "/etc/cron.d/hestia-ssh-jail" ]; then |
| 101 | + echo "@reboot root sleep 60 && /usr/local/hestia/bin/v-add-sys-ssh-jail > /dev/null" > /etc/cron.d/hestia-ssh-jail |
| 102 | +fi |
| 103 | + |
| 104 | +#----------------------------------------------------------# |
| 105 | +# Hestia # |
| 106 | +#----------------------------------------------------------# |
| 107 | + |
| 108 | +# Logging |
| 109 | +log_event "$OK" "$ARGUMENTS" |
| 110 | + |
| 111 | +exit |
0 commit comments