v-add-user-sftp-jail 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. # info: add user sftp jail
  3. # options: USER
  4. #
  5. # The script enables sftp jailed environment
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. # Includes
  12. source $VESTA/func/main.sh
  13. source $VESTA/conf/vesta.conf
  14. #----------------------------------------------------------#
  15. # Verifications #
  16. #----------------------------------------------------------#
  17. check_args '1' "$#" 'USER'
  18. is_format_valid 'user'
  19. if [ -z "$SFTPJAIL_KEY" ]; then
  20. exit
  21. fi
  22. user_str=$(grep "^$user:" /etc/passwd |egrep "rssh|nologin")
  23. if [ -z "$user_str" ]; then
  24. exit
  25. fi
  26. #----------------------------------------------------------#
  27. # Action #
  28. #----------------------------------------------------------#
  29. # Defining user homedir
  30. home="$(echo $user_str |cut -f 6 -d :)"
  31. # Adding chroot directory
  32. if [ ! -d "/chroot/$user/$home" ]; then
  33. mkdir -p /chroot/$user/$home
  34. chmod 750 /chroot/$user
  35. chmod 775 /chroot/$user/$home
  36. chown root:sftp-only /chroot/$user
  37. chown $user:sftp-only /chroot/$user/$home
  38. fi
  39. # Adding user to sftp group
  40. usermod -a -G sftp-only $user
  41. # Mouting home directory
  42. if [ -z "$(mount |grep /chroot/$user/$home)" ]; then
  43. mount -o bind $home /chroot/$user/$home/
  44. fi
  45. #----------------------------------------------------------#
  46. # Vesta #
  47. #----------------------------------------------------------#
  48. # Logging
  49. log_event "$OK" "$ARGUMENTS"
  50. exit