check_permissions.sh 3.3 KB

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