check_permissions.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. # LGSM check_permissions.sh
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. lgsm_version="210516"
  7. # Description: Checks script, files and folders ownership and permissions.
  8. # Useful variables
  9. currentuser="$(whoami)"
  10. currentgroups="$(groups)"
  11. scriptfullpath="${rootdir}/${selfname}"
  12. conclusionpermissionerror="0"
  13. fn_check_ownership(){
  14. # Check script ownership
  15. if [ ! -O "${scriptfullpath}" ] && [ ! -G "${scriptfullpath}" ]; then
  16. fn_print_fail_nl "Oops ! Ownership issue..."
  17. echo " * Current - ${currentuser} - user or its group(s) - ${currentgroups} - does not own \"${selfname}\""
  18. echo " * To check the owner and allowed groups, run ls -l \"${selfname}\""
  19. exit 1
  20. fi
  21. # Check rootdir ownership
  22. if [ ! -O "${rootdir}" ] && [ ! -G "${rootdir}" ]; then
  23. fn_print_fail_nl "Oops ! Ownership issue..."
  24. echo " * Current - ${currentuser} - user or its group(s) - ${currentgroups} - does not own \"${rootdir}\""
  25. echo " * To check the owner and allowed groups, run ls -dl \"${rootdir}\""
  26. exit 1
  27. fi
  28. # Check functions ownership
  29. funownfail="0"
  30. if [ -n "${functionsdir}" ]; then
  31. while read -r filename
  32. do
  33. if [ ! -O "${filename}" ] && [ ! -G "${filename}" ]; then
  34. funownfail="1"
  35. conclusionpermissionerror="1"
  36. fi
  37. done <<< "$(find "${functionsdir}" -name "*.sh")"
  38. if [ "${funownfail}" == "1" ]; then
  39. fn_print_fail_nl "Oops ! Ownership issue..."
  40. echo " * Current - ${currentuser} - user or its group(s) - ${currentgroups} - does not own all scripts in \"${functionsdir}\""
  41. echo " * To check the owner and allowed groups, run ls -l \"${functionsdir}\""
  42. fi
  43. fi
  44. }
  45. fn_check_permissions(){
  46. # Check rootdir permissions
  47. if [ -n "${rootdir}" ]; then
  48. # Get permission numbers on folder under the form 775
  49. rootdirperm="$(stat -c %a "${rootdir}")"
  50. # Grab the first and second digit for user and group permission
  51. userrootdirperm="${rootdirperm:0:1}"
  52. grouprootdirperm="${rootdirperm:1:1}"
  53. if [ "${userrootdirperm}" != "7" ] && [ "${grouprootdirperm}" != "7" ]; then
  54. fn_print_fail_nl "Oops ! Permission issue..."
  55. echo " * Current - ${currentuser} - user or its group(s) - ${currentgroups} need full control of \"${rootdir}\""
  56. echo " * You might wanna run : chmod -R 770 \"${rootdir}\""
  57. conclusionpermissionerror="1"
  58. fi
  59. fi
  60. # Check functions permissions
  61. funcpermfail="0"
  62. if [ -n "${functionsdir}" ]; then
  63. while read -r filename
  64. do
  65. funcperm="$(stat -c %a "${filename}")"
  66. userfuncdirperm="${funcperm:0:1}"
  67. groupfuncdirperm="${funcperm:1:1}"
  68. if [ "${userfuncdirperm}" != "7" ] && [ "${groupfuncdirperm}" != "7" ]; then
  69. funcpermfail="1"
  70. conclusionpermissionerror="1"
  71. fi
  72. done <<< "$(find "${functionsdir}" -name "*.sh")"
  73. if [ "${funcpermfail}" == "1" ]; then
  74. fn_print_fail_nl "Oops ! Permission issue..."
  75. echo " * Current - ${currentuser} - user or its group(s) - ${currentgroups} need full control on scripts in \"${functionsdir}\""
  76. echo " * You might wanna run : chmod -R 770 \"${functionsdir}\""
  77. fi
  78. fi
  79. }
  80. fn_check_permissions_conclusion(){
  81. # Exit if errors found
  82. if [ "${conclusionpermissionerror}" == "1" ]; then
  83. exit 1
  84. fi
  85. }
  86. fn_check_ownership
  87. fn_check_permissions
  88. fn_check_permissions_conclusion