v-add-sys-sftp-jail 3.0 KB

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