check_permissions.sh 3.4 KB

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