check_permissions.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. currentuser="$(whoami)"
  10. scriptfullpath="${rootdir}/${selfname}"
  11. conclusionpermissionerror="0"
  12. fn_check_ownership(){
  13. # Check script ownership
  14. if [ "${currentuser}" != "$(stat -c %U "${scriptfullpath}")" ] && [ "${currentuser}" != "$(stat -c %G "${scriptfullpath}")" ]; then
  15. fn_print_fail_nl "Oops ! Permission denied on ${selfname}"
  16. echo " * To check allowed user and group run ls -l ${selfname}"
  17. exit 1
  18. fi
  19. # Check rootdir ownership
  20. if [ "${currentuser}" != "$(stat -c %U "${rootdir}")" ] && [ "${currentuser}" != "$(stat -c %G "${rootdir}")" ]; then
  21. fn_print_fail_nl "Oops ! Permission denied on ${rootdir}"
  22. echo " * To check allowed user and group run ls -l ${rootdir}"
  23. exit 1
  24. fi
  25. }
  26. fn_check_permissions(){
  27. # Check rootdir permissions
  28. if [ -n "${rootdir}" ]; then
  29. rootdirperm="$(stat -c %a "${rootdir}")"
  30. userrootdirperm="${rootdirperm:0:1}"
  31. grouprootdirperm="${rootdirperm:1:1}"
  32. if [ "${userrootdirperm}" != "7" ] && [ "${grouprootdirperm}" != "7" ]; then
  33. fn_print_fail_nl "Permission issues found in root directory"
  34. echo " * Neither the user or group has full control of \"${rootdir}\""
  35. echo " * You might wanna run : chmod -R 755 \"${rootdir}\""
  36. conclusionpermissionerror="1"
  37. fi
  38. fi
  39. # Check functions permissions
  40. funcpermfail="0"
  41. if [ -n "${functionsdir}" ]; then
  42. while read -r filename
  43. do
  44. funcperm="$(stat -c %a "${filename}")"
  45. userfuncdirperm="${funcperm:0:1}"
  46. groupfuncdirperm="${funcperm:1:1}"
  47. if [ "${userfuncdirperm}" != "7" ] && [ "${groupfuncdirperm}" != "7" ]; then
  48. funcpermfail="1"
  49. conclusionpermissionerror="1"
  50. fi
  51. done <<< "$(find "${functionsdir}" -name "*.sh")"
  52. if [ "${funcpermfail}" == "1" ]; then
  53. fn_print_fail_nl "Permission issues found in functions."
  54. echo " * Neither the user or group has full control of at least some scripts in \"${functionsdir}\""
  55. echo " * You might wanna run : chmod -R 755 \"${functionsdir}\""
  56. fi
  57. fi
  58. }
  59. fn_check_permissions_conclusion(){
  60. # Exit if errors found
  61. if [ "${conclusionpermissionerror}" == "1" ]; then
  62. exit 1
  63. fi
  64. }
  65. fn_check_ownership
  66. fn_check_permissions
  67. fn_check_permissions_conclusion