Skip to content

Commit e060aa0

Browse files
SFTP support for additional "FTP" accounts (hestiacp#3844)
* Fix SFTP chroot jail This handles the addition and deletion of the chroot jail mount points during all relevant user add, change, and delete operations. It also modifies sshd configuration to reflect the new chroot jail structure. * Harden vsftpd by using the new SFTP chroot jail structure * Revise ssh / sshd service restarts Debian / Ubuntu now alias sshd service to ssh service so restarting ssh service is actually restarting it twice so only restart sshd service. Furthermore rename ssh service restarts to sshd if it is the only restart command. This works since sshd is the name of the service on CentOS and Debian / Ubuntu now have aliases for sshd. * Update upgrade script to enable migration to new chroot jail system * Remove chroot jail directories when deleting it * Migrate script * Fix main.sh format * Fix some minor issues * Update ssh config --------- Co-authored-by: James Buren <braewoods+mgh@braewoods.net>
1 parent 60362d4 commit e060aa0

File tree

8 files changed

+98
-23
lines changed

8 files changed

+98
-23
lines changed

bin/v-add-sys-sftp-jail

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ fi
4545

4646
# Enabling jailed sftp
4747
if [ -z "$sftp_i" ]; then
48-
echo " " >> $config
49-
echo "# Hestia SFTP Chroot" >> $config
50-
echo "Match User sftp_dummy99" >> $config
51-
echo "ChrootDirectory %h" >> $config
52-
echo " X11Forwarding no" >> $config
53-
echo " AllowTCPForwarding no" >> $config
54-
echo " ForceCommand internal-sftp" >> $config
55-
restart='yes'
48+
echo " " >> $config
49+
echo "# Hestia SFTP Chroot" >> $config
50+
echo "Match User sftp_dummy99" >> $config
51+
echo " ChrootDirectory /srv/jail/%u" >> $config
52+
echo " X11Forwarding no" >> $config
53+
echo " AllowTCPForwarding no" >> $config
54+
echo " ForceCommand internal-sftp -d /home" >> $config
55+
restart='yes'
5656
fi
5757

5858
# Validating opensshd config
@@ -63,10 +63,10 @@ if [ "$restart" = 'yes' ]; then
6363
if [ "$?" -ne 0 ]; then
6464
mail_text="OpenSSH can not be restarted. Please check config:
6565
\n\n$(/usr/sbin/sshd -t)"
66-
echo -e "$mail_text" | $SENDMAIL -s "$subj" $email
67-
else
68-
service ssh restart > /dev/null 2>&1
69-
fi
66+
echo -e "$mail_text" |$SENDMAIL -s "$subj" $email
67+
else
68+
service sshd restart >/dev/null 2>&1
69+
fi
7070
fi
7171

7272
# Checking users

bin/v-add-user-sftp-jail

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ sed -i "s/$ssh_users/$users/g" /etc/ssh/sshd_config
7575
if [ -d "/home/$user" ]; then
7676
chown root:root /home/$user
7777
fi
78+
add_chroot_jail "$user"
7879

7980
#----------------------------------------------------------#
8081
# Hestia #
@@ -85,7 +86,7 @@ if [ "$restart" = 'no' ]; then
8586
# Skip restart of SSH daemon
8687
echo "" > /dev/null 2>&1
8788
else
88-
service ssh restart > /dev/null 2>&1
89+
service sshd restart > /dev/null 2>&1
8990
fi
9091

9192
# Logging

bin/v-change-web-domain-ftp-path

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ str=$(echo "$pw_str" | cut -f 1 -d :)
7676
old_path=$(echo "$pw_str" | cut -f 7 -d :)
7777
sed -i "$str s%$old_path%$ftp_path_a%g" /etc/passwd
7878

79+
# Updating chroot jail for SFTP and FTP
80+
delete_chroot_jail $ftp_user
81+
add_chroot_jail $ftp_user
82+
7983
#----------------------------------------------------------#
8084
# Hestia #
8185
#----------------------------------------------------------#

bin/v-delete-sys-sftp-jail

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ config='/etc/ssh/sshd_config'
3434
sftp_n=$(grep -n "Subsystem.*sftp" $config | grep -v internal | grep ":#")
3535
sftp_i=$(grep -n "^# Hestia SFTP Chroot" $config)
3636

37-
date=$("date +%s")
3837
# Backing up config
39-
cp $config $config.bak-$(date +%s)
38+
cp $config $config.bak
4039

4140
# Enabling normal sftp
4241
if [ -n "$sftp_n" ]; then
@@ -61,9 +60,8 @@ if [ "$restart" = 'yes' ]; then
6160
if [ "$?" -ne 0 ]; then
6261
mail_text="OpenSSH can not be restarted. Please check config:
6362
\n\n$(/usr/sbin/sshd -t)"
64-
echo -e "$mail_text" | $SENDMAIL -s "$subj" "$email"
63+
echo -e "$mail_text" | $SENDMAIL -s "$subj" $email
6564
else
66-
service ssh restart > /dev/null 2>&1
6765
service sshd restart > /dev/null 2>&1
6866
fi
6967
fi
@@ -77,10 +75,6 @@ fi
7775
# Hestia #
7876
#----------------------------------------------------------#
7977

80-
# Restart ssh service
81-
service ssh restart > /dev/null 2>&1
82-
service sshd restart > /dev/null 2>&1
83-
8478
# Logging
8579
$BIN/v-log-action "system" "Warning" "Plugins" "SFTP Chroot Jail disabled."
8680
log_event "$OK" "$ARGUMENTS"

bin/v-delete-user-sftp-jail

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ sed -i "s/$ssh_users/$users/g" /etc/ssh/sshd_config
6868
if [ -d "/home/$user" ]; then
6969
chown $user:$user /home/$user
7070
fi
71+
72+
# Deleting chroot jail for SFTP and FTP
73+
delete_chroot_jail $user
7174
#----------------------------------------------------------#
7275
# Hestia #
7376
#----------------------------------------------------------#
7477

7578
# Restart ssh service
76-
service ssh restart > /dev/null 2>&1
7779
service sshd restart > /dev/null 2>&1
7880

7981
# Logging

func/main.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,3 +1708,47 @@ search_command_arg_position() {
17081708

17091709
echo "$position"
17101710
}
1711+
1712+
add_chroot_jail() {
1713+
local user=$1
1714+
1715+
mkdir -p /srv/jail/$user
1716+
chown 0:0 /srv /srv/jail /srv/jail/$user
1717+
chmod 755 /srv /srv/jail /srv/jail/$user
1718+
if [ ! -d /srv/jail/$user/home ]; then
1719+
mkdir -p /srv/jail/$user/home
1720+
chown 0:0 /srv/jail/$user/home
1721+
chmod 755 /srv/jail/$user/home
1722+
fi
1723+
1724+
cat > /etc/systemd/system/srv-jail-$user-home.mount << EOF
1725+
[Unit]
1726+
Description=Mount $user's home directory to the jail chroot
1727+
Before=local-fs.target
1728+
1729+
[Mount]
1730+
What=$(getent passwd $user | cut -d : -f 6)
1731+
Where=/srv/jail/$user/home
1732+
Type=none
1733+
Options=bind
1734+
LazyUnmount=yes
1735+
1736+
[Install]
1737+
RequiredBy=local-fs.target
1738+
EOF
1739+
1740+
systemctl daemon-reload > /dev/null 2>&1
1741+
systemctl enable srv-jail-$user-home.mount > /dev/null 2>&1
1742+
systemctl start srv-jail-$user-home.mount > /dev/null 2>&1
1743+
}
1744+
1745+
delete_chroot_jail() {
1746+
local user=$1
1747+
1748+
systemctl stop srv-jail-$user-home.mount > /dev/null 2>&1
1749+
systemctl disable srv-jail-$user-home.mount > /dev/null 2>&1
1750+
rm -f /etc/systemd/system/srv-jail-$user-home.mount
1751+
systemctl daemon-reload > /dev/null 2>&1
1752+
rmdir /srv/jail/$user/home > /dev/null 2>&1
1753+
rmdir /srv/jail/$user > /dev/null 2>&1
1754+
}

install/deb/vsftpd/vsftpd.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ tcp_wrappers=YES
1818
force_dot_files=YES
1919
ascii_upload_enable=YES
2020
ascii_download_enable=YES
21-
allow_writeable_chroot=YES
21+
allow_writeable_chroot=NO
22+
local_root=/srv/jail/%u
23+
user_sub_token=%u
2224
seccomp_sandbox=NO
2325
pasv_enable=YES
2426
pasv_promiscuous=YES

install/upgrade/versions/1.9.0.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
# Hestia Control Panel upgrade script for target version 1.9.0
4+
5+
#######################################################################################
6+
####### Place additional commands below. #######
7+
#######################################################################################
8+
####### upgrade_config_set_value only accepts true or false. #######
9+
####### #######
10+
####### Pass through information to the end user in case of a issue or problem #######
11+
####### #######
12+
####### Use add_upgrade_message "My message here" to include a message #######
13+
####### in the upgrade notification email. Example: #######
14+
####### #######
15+
####### add_upgrade_message "My message here" #######
16+
####### #######
17+
####### You can use \n within the string to create new lines. #######
18+
#######################################################################################
19+
20+
upgrade_config_set_value 'UPGRADE_UPDATE_WEB_TEMPLATES' 'false'
21+
upgrade_config_set_value 'UPGRADE_UPDATE_DNS_TEMPLATES' 'false'
22+
upgrade_config_set_value 'UPGRADE_UPDATE_MAIL_TEMPLATES' 'false'
23+
upgrade_config_set_value 'UPGRADE_REBUILD_USERS' 'yes'
24+
upgrade_config_set_value 'UPGRADE_UPDATE_FILEMANAGER_CONFIG' 'false'
25+
26+
# update config sftp jail
27+
$BIN/v-delete-sys-sftp-jail
28+
$BIN/v-add-sys-sftp-jail

0 commit comments

Comments
 (0)