Skip to content

Commit b3cbc80

Browse files
authored
Merge pull request hestiacp#4245 from hestiacp/fix/ssh-jail
Don't run v-add-user-jail for every user during rebuild
2 parents ae7c9f4 + 5bfe46b commit b3cbc80

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

bin/v-add-user-ssh-jail

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,42 @@ if [ -d "/home/$user" ]; then
5151
chown root:root /home/$user
5252
fi
5353

54-
add_chroot_jail "$user"
55-
56-
# Add user to the ssh-jailed group to allow jailed ssh
57-
# This needs to be done first to make sure these groups are made available in the jail
58-
usermod -a -G ssh-jailed $user
59-
60-
# Installing shell files into the user chroot directory
61-
# - IMPORTANT - MODIFY THE FOLLOWING LINES AND THE FILE jk_init.ini ACCORDING TO YOUR SYSTEM AND YOUR PREFERENCES
62-
/sbin/jk_init -f -j $chroot extendedshell netutils ssh sftp scp git php php5_6 php7_0 php7_1 php7_2 php7_3 php7_4 php8_0 php8_1 php8_2 > /dev/null 2>&1
63-
/sbin/jk_cp -f -j $chroot /bin/id > /dev/null 2>&1
64-
65-
# Jailing user to make sure passwd and groups are set correctly within the jail.
66-
# This command also does a little too much by changing the users homedir and
67-
# shell in /etc/passwd. The next commands reverts those changes for compatibility
68-
# with hestia.
69-
/sbin/jk_jailuser -n -s $shell_path -j $chroot $user
70-
71-
# Reset home directory and shell again for hestiacp because jailkit changes these.
72-
# Normally these are needed to redirect the ssh user to it's chroot but because we
73-
# use a custom sshd_config to redirect the user to it's chroot we don't need it to be
74-
# changed in /etc/passwd for the user.
75-
usermod -d /home/$user $user
76-
usermod -s $shell_path $user
54+
# Prevent from enabling for users hen rssh or nologin is enabled
55+
user_str=$(grep "^$user:" /etc/passwd | egrep "rssh|nologin")
56+
if [ -n "$user_str" ]; then
57+
exit
58+
fi
59+
60+
if [ ! -d "$chroot" ]; then
61+
add_chroot_jail "$user"
62+
63+
# Add user to the ssh-jailed group to allow jailed ssh
64+
# This needs to be done first to make sure these groups are made available in the jail
65+
usermod -a -G ssh-jailed "$user"
66+
67+
# Installing shell files into the user chroot directory
68+
# - IMPORTANT - MODIFY THE FOLLOWING LINES AND THE FILE jk_init.ini ACCORDING TO YOUR SYSTEM AND YOUR PREFERENCES
69+
/sbin/jk_init -f -j "$chroot" extendedshell netutils ssh sftp scp git php php5_6 php7_0 php7_1 php7_2 php7_3 php7_4 php8_0 php8_1 php8_2 > /dev/null 2>&1
70+
/sbin/jk_cp -f -j "$chroot" /bin/id > /dev/null 2>&1
71+
72+
# Jailing user to make sure passwd and groups are set correctly within the jail.
73+
# This command also does a little too much by changing the users homedir and
74+
# shell in /etc/passwd. The next commands reverts those changes for compatibility
75+
# with hestia.
76+
/sbin/jk_jailuser -n -s "$shell_path" -j "$chroot" "$user"
77+
78+
# Reset home directory and shell again for hestiacp because jailkit changes these.
79+
# Normally these are needed to redirect the ssh user to it's chroot but because we
80+
# use a custom sshd_config to redirect the user to it's chroot we don't need it to be
81+
# changed in /etc/passwd for the user.
82+
usermod -d "/home/$user" "$user" > /dev/null 2>&1
83+
usermod -s "$shell_path" "$user" > /dev/null 2>&1
84+
85+
else
86+
/sbin/jk_update -f -j "$chroot" > /dev/null 2>&1
87+
usermod -d "/home/$user" "$user" > /dev/null 2>&1
88+
usermod -s "$shell_path" "$user" > /dev/null 2>&1
89+
fi
7790

7891
#----------------------------------------------------------#
7992
# Hestia #

bin/v-change-user-shell

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ source $HESTIA/conf/hestia.conf
2727
# Verifications #
2828
#----------------------------------------------------------#
2929

30-
check_args '3' "$#" 'USER SHELL SHELL_JAIL_ENABLED'
30+
check_args '2' "$#" 'USER SHELL SHELL_JAIL_ENABLED'
3131
is_format_valid 'user' 'shell shell_jail_enabled'
3232
is_object_valid 'user' 'USER' "$user"
3333
is_object_unsuspended 'user' 'USER' "$user"
3434

35+
if [[ "$shell" =~ nologin ]] || [[ "$shell" =~ rssh ]] && [[ "$shell_jail_enabled" =~ yes ]]; then
36+
check_result "$E_INVALID" "nologin and rssh can't be jailed"
37+
fi
38+
3539
# Perform verification if read-only mode is enabled
3640
check_hestia_demo_mode
3741

bin/v-delete-user-ssh-jail

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ user_shell_rssh_nologin=$(grep "^$user:" /etc/passwd | egrep "rssh|nologin")
4545
if [ -z "$user_shell_rssh_nologin" ]; then
4646
# chown permissions back to user:user
4747
if [ -d "/home/$user" ]; then
48-
chown $user:$user /home/$user
48+
chown "$user":"$user" "/home/$user"
4949
fi
5050

5151
# Deleting chroot jail for SSH
52-
delete_chroot_jail $user
52+
delete_chroot_jail "$user"
5353
fi
5454

5555
# Deleting user from groups
56-
gpasswd -d $user ssh-jailed > /dev/null 2>&1
56+
gpasswd -d "$user" ssh-jailed > /dev/null 2>&1
5757

5858
#----------------------------------------------------------#
5959
# Hestia #

func/rebuild.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ rebuild_user_conf() {
126126
chown root:root $HOMEDIR/$user/conf
127127

128128
$BIN/v-add-user-sftp-jail "$user"
129-
130-
$BIN/v-add-user-ssh-jail "$user"
129+
# Check if SHELL_JAIL_ENABLED
130+
if [ "$SHELL_JAIL_ENABLED" == "yes" ]; then
131+
$BIN/v-add-user-ssh-jail "$user"
132+
fi
131133

132134
# Update disk pipe
133135
sed -i "/ $user$/d" $HESTIA/data/queue/disk.pipe

0 commit comments

Comments
 (0)