check_permissions.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. # LGSM check_permissions.sh
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: http://gameservermanagers.com
  6. lgsm_version="150316"
  7. # Description: Checks script, files and folders ownership and permissions.
  8. # Useful variables
  9. scriptfullpath="${rootdir}/${selfname}"
  10. conclusionpermissionerror="0"
  11. fn_check_ownership(){
  12. # Check script ownership
  13. if [ ! -O "${scriptfullpath}" ] && [ ! -G "${scriptfullpath}" ]; then
  14. fn_print_fail_nl "Oops ! Permission denied on ${selfname}"
  15. echo " * To check allowed user and group run ls -l ${selfname}"
  16. exit 1
  17. fi
  18. # Check rootdir ownership
  19. if [ ! -O "${rootdir}" ] && [ ! -G "${rootdir}" ]; then
  20. fn_print_fail_nl "Oops ! Permission denied on ${rootdir}"
  21. echo " * To check allowed user and group run ls -l ${rootdir}"
  22. exit 1
  23. fi
  24. # Check functions ownership
  25. funownfail="0"
  26. if [ -n "${functionsdir}" ]; then
  27. while read -r filename
  28. do
  29. if [ ! -O "${filename}" ] && [ ! -G "${filename}" ]; then
  30. funownfail="0"
  31. conclusionpermissionerror="1"
  32. fi
  33. done <<< "$(find "${functionsdir}" -name "*.sh")"
  34. if [ "${funownfail}" == "1" ]; then
  35. fn_print_fail_nl "Permission issues found in functions."
  36. echo " * Neither the user or group has full control of some scripts in \"${functionsdir}\""
  37. echo " * You might wanna run : chmod -R 770 \"${functionsdir}\""
  38. fi
  39. fi
  40. }
  41. fn_check_permissions(){
  42. # Check rootdir permissions
  43. if [ -n "${rootdir}" ]; then
  44. rootdirperm="$(stat -c %a "${rootdir}")"
  45. userrootdirperm="${rootdirperm:0:1}"
  46. grouprootdirperm="${rootdirperm:1:1}"
  47. if [ "${userrootdirperm}" != "7" ] && [ "${grouprootdirperm}" != "7" ]; then
  48. fn_print_fail_nl "Permission issues found in root directory"
  49. echo " * Neither the user or group has full control of \"${rootdir}\""
  50. echo " * You might wanna run : chmod -R 770 \"${rootdir}\""
  51. conclusionpermissionerror="1"
  52. fi
  53. fi
  54. # Check functions permissions
  55. funcpermfail="0"
  56. if [ -n "${functionsdir}" ]; then
  57. while read -r filename
  58. do
  59. funcperm="$(stat -c %a "${filename}")"
  60. userfuncdirperm="${funcperm:0:1}"
  61. groupfuncdirperm="${funcperm:1:1}"
  62. if [ "${userfuncdirperm}" != "7" ] && [ "${groupfuncdirperm}" != "7" ]; then
  63. funcpermfail="1"
  64. conclusionpermissionerror="1"
  65. fi
  66. done <<< "$(find "${functionsdir}" -name "*.sh")"
  67. if [ "${funcpermfail}" == "1" ]; then
  68. fn_print_fail_nl "Permission issues found in functions."
  69. echo " * Neither the user or group has full control of at least some scripts in \"${functionsdir}\""
  70. echo " * You might wanna run : chmod -R 770 \"${functionsdir}\""
  71. fi
  72. fi
  73. }
  74. fn_check_permissions_conclusion(){
  75. # Exit if errors found
  76. if [ "${conclusionpermissionerror}" == "1" ]; then
  77. exit 1
  78. fi
  79. }
  80. fn_check_ownership
  81. fn_check_permissions
  82. fn_check_permissions_conclusion