check_permissions.sh 3.3 KB

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