v-add-sys-sftp-jail 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 " " >> $config
  37. echo "Subsystem sftp internal-sftp" >> $config
  38. echo "Match Group sftp-only" >> $config
  39. echo "ChrootDirectory /chroot/%u" >> $config
  40. echo " AllowTCPForwarding no" >> $config
  41. echo " X11Forwarding no" >> $config
  42. echo " ForceCommand internal-sftp" >> $config
  43. restart='yes'
  44. fi
  45. # Validating opensshd config
  46. if [ "$restart" = 'yes' ]; then
  47. subj="OpenSSH restart failed"
  48. email=$(grep CONTACT $VESTA/data/users/admin/user.conf |cut -f 2 -d \')
  49. /usr/sbin/sshd -t >/dev/null 2>&1
  50. if [ "$?" -ne 0 ]; then
  51. mail_text="OpenSSH can not be restarted. Please check config:
  52. \n\n$(/usr/sbin/sshd -t)"
  53. echo -e "$mail_text" |$SENDMAIL -s "$subj" $email
  54. else
  55. service ssh restart >/dev/null 2>&1
  56. service sshd restart >/dev/null 2>&1
  57. fi
  58. fi
  59. # Adding sftp group
  60. groupadd sftp-only 2>/dev/null
  61. # Checking users
  62. shells="rssh|nologin"
  63. for user in $(grep "$HOMEDIR" /etc/passwd |egrep "$shells" |cut -f 1 -d:); do
  64. $BIN/v-add-user-sftp-jail $user
  65. done
  66. # Adding v-add-sys-sftp-jail to startup
  67. if [ -e "/etc/rc.local" ]; then
  68. check_sftp=$(grep $0 /etc/rc.local)
  69. check_exit=$(grep ^exit /etc/rc.local)
  70. if [ -z "$check_sftp" ]; then
  71. if [ -z "$check_exit" ]; then
  72. echo "$BIN/v-add-sys-sftp-jail" >> /etc/rc.local
  73. else
  74. sed -i "s|^exit|$BIN/v-add-sys-sftp-jail\nexit|" /etc/rc.local
  75. fi
  76. fi
  77. chmod +x /etc/rc.local
  78. else
  79. echo "$BIN/v-add-sys-sftp-jail" > /etc/rc.local
  80. chmod +x /etc/rc.local
  81. fi
  82. #----------------------------------------------------------#
  83. # Vesta #
  84. #----------------------------------------------------------#
  85. # Logging
  86. log_event "$OK" "$ARGUMENTS"
  87. exit