|
| 1 | +#!/bin/bash |
| 2 | +# info: add system sftp jail |
| 3 | +# opions: NONE |
| 4 | +# |
| 5 | +# The script enables sftp jailed environment |
| 6 | + |
| 7 | + |
| 8 | +#----------------------------------------------------------# |
| 9 | +# Variable&Function # |
| 10 | +#----------------------------------------------------------# |
| 11 | + |
| 12 | +# Importing system enviroment as we run this script |
| 13 | +# mostly by cron wich do not read it by itself |
| 14 | +source /etc/profile |
| 15 | + |
| 16 | +# Includes |
| 17 | +source $VESTA/func/main.sh |
| 18 | +source $VESTA/conf/vesta.conf |
| 19 | + |
| 20 | + |
| 21 | +#----------------------------------------------------------# |
| 22 | +# Verifications # |
| 23 | +#----------------------------------------------------------# |
| 24 | + |
| 25 | +if [ -z "$SFTPJAIL_KEY" ]; then |
| 26 | + exit |
| 27 | +fi |
| 28 | + |
| 29 | + |
| 30 | +#----------------------------------------------------------# |
| 31 | +# Action # |
| 32 | +#----------------------------------------------------------# |
| 33 | + |
| 34 | +# Checking sshd directives |
| 35 | +config='/etc/ssh/sshd_config' |
| 36 | +sftp_n=$(grep -n "Subsystem.*sftp" $config |grep -v internal |grep -v ":#") |
| 37 | +sftp_i=$(grep -n "Subsystem.*sftp" $config |grep internal |grep -v ":#") |
| 38 | + |
| 39 | +# Disabling normal sftp |
| 40 | +if [ ! -z "$sftp_n" ]; then |
| 41 | + fline=$(echo $sftp_n |cut -f 1 -d :) |
| 42 | + sed -i "${fline}s/Subsystem.*sftp/#Subsystem sftp/" $config |
| 43 | + restart='yes' |
| 44 | +fi |
| 45 | + |
| 46 | +# Enabling jailed sftp |
| 47 | +if [ -z "$sftp_i" ]; then |
| 48 | + echo "Subsystem sftp internal-sftp" >> $config |
| 49 | + echo "Match Group sftp-only" >> $config |
| 50 | + echo "ChrootDirectory /chroot/%u" >> $config |
| 51 | + echo " AllowTCPForwarding no" >> $config |
| 52 | + echo " X11Forwarding no" >> $config |
| 53 | + echo " ForceCommand internal-sftp" >> $config |
| 54 | + restart='yes' |
| 55 | +fi |
| 56 | + |
| 57 | +# Validating opensshd config |
| 58 | +if [ "$restart" = 'yes' ]; then |
| 59 | + subj="OpenSSH restart failed" |
| 60 | + email=$(grep CONTACT $VESTA/data/users/admin/user.conf |cut -f 2 -d \') |
| 61 | + send_mail="$VESTA/web/inc/mail-wrapper.php" |
| 62 | + /usr/sbin/sshd -t >/dev/null 2>&1 |
| 63 | + if [ "$?" -ne 0 ]; then |
| 64 | + mail_text="OpenSSH can not be restarted. Please check config: |
| 65 | + \n\n$(/usr/sbin/sshd -t)" |
| 66 | + echo -e "$mail_text" | $send_mail -s "$subj" $email |
| 67 | + else |
| 68 | + service ssh restart >/dev/null 2>&1 |
| 69 | + service sshd restart >/dev/null 2>&1 |
| 70 | + fi |
| 71 | +fi |
| 72 | + |
| 73 | +# Adding sftp group |
| 74 | +groupadd sftp-only 2>/dev/null |
| 75 | + |
| 76 | +# Checking users |
| 77 | +shells="rssh|nologin" |
| 78 | +for user in $(grep "$HOMEDIR" /etc/passwd |egrep "$shells" |cut -f 1 -d:); do |
| 79 | + $BIN/v-add-user-sftp-jail $user |
| 80 | +done |
| 81 | + |
| 82 | +# Adding v-add-sys-sftp-jail to startup |
| 83 | +if [ -e "/etc/rc.local" ]; then |
| 84 | + check_sftp=$(grep $0 /etc/rc.local) |
| 85 | + check_exit=$(grep ^exit /etc/rc.local) |
| 86 | + if [ -z "$check_sftp" ]; then |
| 87 | + if [ -z "$check_exit" ]; then |
| 88 | + echo "$BIN/v-add-sys-sftp-jail" >> /etc/rc.local |
| 89 | + else |
| 90 | + sed -i "s|^exit|$BIN/v-add-sys-sftp-jail\nexit|" /etc/rc.local |
| 91 | + fi |
| 92 | + fi |
| 93 | + chmod +x /etc/rc.local |
| 94 | +else |
| 95 | + echo "$BIN/v-add-sys-sftp-jail" > /etc/rc.local |
| 96 | + chmod +x /etc/rc.local |
| 97 | +fi |
| 98 | + |
| 99 | +#----------------------------------------------------------# |
| 100 | +# Vesta # |
| 101 | +#----------------------------------------------------------# |
| 102 | + |
| 103 | +# Logging |
| 104 | +log_event "$OK" "$EVENT" |
| 105 | + |
| 106 | +exit |
0 commit comments