migrate_jailkit_to_bubblewrap.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. # info: Removes Jailkit and migrates to Bubblewrap
  3. #
  4. # Jailkit was availble for a short period in 1.9.0 Beta releases
  5. # How ever it has been replaced by Bubblewrap
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Includes
  10. # shellcheck source=/usr/local/hestia/func/main.sh
  11. source $HESTIA/func/main.sh
  12. # shellcheck source=/usr/local/hestia/conf/hestia.conf
  13. source $HESTIA/conf/hestia.conf
  14. #----------------------------------------------------------#
  15. # Verifications #
  16. #----------------------------------------------------------#
  17. # Checking if jailkit is installed
  18. if [ ! -x /sbin/jk_init ]; then
  19. exit
  20. fi
  21. #----------------------------------------------------------#
  22. # Action #
  23. #----------------------------------------------------------#
  24. # Enable the bubblewrap jail for the system
  25. $BIN/v-add-sys-ssh-jail
  26. ## Migrate user jails to bubblewrap jails
  27. for user in $("$BIN/v-list-users" list); do
  28. check_jail_enabled=$(grep "SHELL_JAIL_ENABLED='yes'" $HESTIA/data/users/$user/user.conf)
  29. # If jail enabled remove the jailkit jail first then bubblewrap the jail
  30. if [ -n "$check_jail_enabled" ]; then
  31. user_shell_rssh_nologin=$(grep "^$user:" /etc/passwd | egrep "rssh|nologin")
  32. # Only remove the jail when it's not needed for rssh or nologin
  33. if [ -z "$user_shell_rssh_nologin" ]; then
  34. # chown permissions back to user:user
  35. if [ -d "/home/$user" ]; then
  36. chown "$user":"$user" "/home/$user"
  37. fi
  38. # Deleting chroot jail for SSH
  39. delete_chroot_jail "$user"
  40. fi
  41. # Deleting user from groups
  42. gpasswd -d "$user" ssh-jailed > /dev/null 2>&1
  43. # Enable bubblewrap jail for user
  44. $BIN/v-change-user-shell $user jailbash
  45. # Remove config line from user.conf
  46. sed -i "/SHELL_JAIL_ENABLED='yes'/d" $HESTIA/data/users/$user/user.conf
  47. fi
  48. # Remove config line from user.conf
  49. sed -i "/SHELL_JAIL_ENABLED='no'/d" $HESTIA/data/users/$user/user.conf
  50. done
  51. packages=$(ls --sort=time $HESTIA/data/packages | grep .pkg)
  52. for package in $packages; do
  53. # Remove config line from package.conf
  54. sed -i "/SHELL_JAIL_ENABLED='yes'/d" $HESTIA/data/packages/$package
  55. sed -i "/SHELL_JAIL_ENABLED='no'/d" $HESTIA/data/packages/$package
  56. done
  57. # Checking sshd directives
  58. config='/etc/ssh/sshd_config'
  59. ssh_i=$(grep -n "^# Hestia SSH Chroot" $config)
  60. # Backing up config
  61. cp $config $config.bak
  62. # Disabling jailed ssh
  63. if [ -n "$ssh_i" ]; then
  64. fline=$(echo "$ssh_i" | cut -f 1 -d :)
  65. lline=$((fline + 4))
  66. sed -i "${fline},${lline}d" $config
  67. /usr/sbin/sshd -t > /dev/null 2>&1
  68. if [ "$?" -ne 0 ]; then
  69. message="OpenSSH can not be restarted. Please check config:
  70. \n\n$(/usr/sbin/sshd -t)"
  71. echo -e "$message"
  72. else
  73. service ssh restart > /dev/null 2>&1
  74. fi
  75. fi
  76. # Remove group ssh-jailed
  77. groupdel ssh-jailed 2> /dev/null
  78. # Remove cronjob
  79. rm -f /etc/cron.d/hestia-ssh-jail
  80. # Remove jailkit
  81. apt remove -qq jailkit -y > /dev/null 2>&1
  82. #----------------------------------------------------------#
  83. # Hestia #
  84. #----------------------------------------------------------#
  85. # Logging
  86. log_history "Migrated jailkit to bubblewrap" '' 'admin'
  87. log_event "$OK" "$ARGUMENTS"
  88. exit