4
0

check_permissions.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!/bin/bash
  2. # LinuxGSM check_permissions.sh
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Checks ownership & permissions of scripts, files and directories.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_check_ownership() {
  9. if [ -f "${rootdir}/${selfname}" ]; then
  10. if [ "$(find "${rootdir}/${selfname}" -not -user "$(whoami)" | wc -l)" -ne "0" ]; then
  11. selfownissue=1
  12. fi
  13. fi
  14. if [ -d "${lgsmdir}" ]; then
  15. if [ "$(find "${lgsmdir}" -not -user "$(whoami)" | wc -l)" -ne "0" ]; then
  16. lgsmownissue=1
  17. fi
  18. fi
  19. if [ -d "${modulesdir}" ]; then
  20. if [ "$(find "${modulesdir}" -not -name '*.swp' -not -user "$(whoami)" | wc -l)" -ne "0" ]; then
  21. funcownissue=1
  22. fi
  23. fi
  24. if [ -d "${serverfiles}" ]; then
  25. if [ "$(find "${serverfiles}" -not -name '*.swp' -not -user "$(whoami)" | wc -l)" -ne "0" ]; then
  26. filesownissue=1
  27. fi
  28. fi
  29. if [ "${selfownissue}" == "1" ] || [ "${lgsmownissue}" == "1" ] || [ "${filesownissue}" == "1" ]; then
  30. fn_print_fail_nl "Ownership issues found"
  31. fn_script_log_fail "Ownership issues found"
  32. fn_print_information_nl "The current user ($(whoami)) does not have ownership of the following files:"
  33. fn_script_log_info "The current user ($(whoami)) does not have ownership of the following files:"
  34. {
  35. echo -en "User\tGroup\tFile:"
  36. if [ "${selfownissue}" == "1" ]; then
  37. find "${rootdir}/${selfname}" -not -user "$(whoami)" -printf "%u\t%g\t%p\n"
  38. fi
  39. if [ "${lgsmownissue}" == "1" ]; then
  40. find "${lgsmdir}" -not -user "$(whoami)" -printf "%u\t%g\t%p\n"
  41. fi
  42. if [ "${filesownissue}" == "1" ]; then
  43. find "${serverfiles}" -not -user "$(whoami)" -printf "%u\t%g\t%p\n"
  44. fi
  45. } | column -s $'\t' -t | tee -a "${lgsmlog}"
  46. echo -e ""
  47. fn_print_information_nl "please see https://docs.linuxgsm.com/support/faq#fail-starting-game-server-permission-issues-found"
  48. fn_script_log "For more information, please see https://docs.linuxgsm.com/support/faq#fail-starting-game-server-permission-issues-found"
  49. if [ "${monitorflag}" == 1 ]; then
  50. alert="permissions"
  51. alert.sh
  52. fi
  53. core_exit.sh
  54. fi
  55. }
  56. fn_check_permissions() {
  57. # Check modules files are executable.
  58. if [ -d "${modulesdir}" ]; then
  59. findnotexecutable="$(find "${modulesdir}" -type f -not -executable)"
  60. findnotexecutablewc="$(find "${modulesdir}" -type f -not -executable | wc -l)"
  61. if [ "${findnotexecutablewc}" -ne "0" ]; then
  62. fn_print_error_nl "Permissions issues found"
  63. fn_script_log_error "Permissions issues found"
  64. fn_print_information_nl "The following files are not executable:"
  65. fn_script_log_info "The following files are not executable:"
  66. {
  67. echo -en "File:"
  68. echo -en "${findnotexecutable}"
  69. } | column -s $'\t' -t | tee -a "${lgsmlog}"
  70. # Attempt to make the files executable
  71. fn_print_information_nl "Attempting to fix permissions issues"
  72. fn_script_log_info "Attempting to fix permissions issues"
  73. echo "${findnotexecutable}" | xargs chmod +x
  74. # Re-check if there are still non-executable files
  75. findnotexecutable="$(find "${modulesdir}" -type f -not -executable)"
  76. findnotexecutablewc="$(find "${modulesdir}" -type f -not -executable | wc -l)"
  77. if [ "${findnotexecutablewc}" -ne "0" ]; then
  78. fn_print_fail_nl "Failed to resolve permissions issues"
  79. fn_script_log_fail "Failed to resolve permissions issues"
  80. if [ "${monitorflag}" == 1 ]; then
  81. alert="permissions"
  82. alert.sh
  83. fi
  84. core_exit.sh
  85. else
  86. fn_print_ok_nl "Permissions issues resolved"
  87. fn_script_log_pass "Permissions issues resolved"
  88. fi
  89. fi
  90. fi
  91. # Check rootdir permissions.
  92. if [ -d "${rootdir}" ]; then
  93. # Get permission numbers on directory should return 775.
  94. rootdirperm=$(stat -c %a "${rootdir}")
  95. # Grab the first and second digit for user and group permission.
  96. userrootdirperm="${rootdirperm:0:1}"
  97. grouprootdirperm="${rootdirperm:1:1}"
  98. if [ "${userrootdirperm}" != "7" ] && [ "${grouprootdirperm}" != "7" ]; then
  99. fn_print_fail_nl "Permissions issues found"
  100. fn_script_log_fail "Permissions issues found"
  101. fn_print_information_nl "The following directory does not have the correct permissions:"
  102. fn_script_log_info "The following directory does not have the correct permissions:"
  103. fn_script_log_info "${rootdir}"
  104. ls -l "${rootdir}"
  105. if [ "${monitorflag}" == 1 ]; then
  106. alert="permissions"
  107. alert.sh
  108. fi
  109. core_exit.sh
  110. fi
  111. fi
  112. # Check if executable is executable and attempt to fix it.
  113. # First get executable name.
  114. execname=$(basename "${executable}")
  115. if [ -f "${executabledir}/${execname}" ]; then
  116. # Get permission numbers on file under the form 775.
  117. execperm=$(stat -c %a "${executabledir}/${execname}")
  118. # Grab the first and second digit for user and group permission.
  119. userexecperm="${execperm:0:1}"
  120. groupexecperm="${execperm:1:1}"
  121. # Check for invalid user permission.
  122. if [ "${userexecperm}" == "0" ] || [ "${userexecperm}" == "2" ] || [ "${userexecperm}" == "4" ] || [ "${userexecperm}" == "6" ]; then
  123. # If user permission is invalid, then check for invalid group permissions.
  124. if [ "${groupexecperm}" == "0" ] || [ "${groupexecperm}" == "2" ] || [ "${groupexecperm}" == "4" ] || [ "${groupexecperm}" == "6" ]; then
  125. # If permission issues are found.
  126. fn_print_warn_nl "Permissions issue found"
  127. fn_script_log_warn "Permissions issue found"
  128. fn_print_information_nl "The following file is not executable:"
  129. ls -l "${executabledir}/${execname}"
  130. fn_script_log_info "The following file is not executable:"
  131. fn_script_log_info "${executabledir}/${execname}"
  132. fn_print_information_nl "Applying chmod u+x,g+x ${executabledir}/${execname}"
  133. fn_script_log_info "Applying chmod u+x,g+x ${execperm}"
  134. # Make the executable executable.
  135. chmod u+x,g+x "${executabledir}/${execname}"
  136. # Second check to see if it's been successfully applied.
  137. # Get permission numbers on file under the form 775.
  138. execperm=$(stat -c %a "${executabledir}/${execname}")
  139. # Grab the first and second digit for user and group permission.
  140. userexecperm="${execperm:0:1}"
  141. groupexecperm="${execperm:1:1}"
  142. if [ "${userexecperm}" == "0" ] || [ "${userexecperm}" == "2" ] || [ "${userexecperm}" == "4" ] || [ "${userexecperm}" == "6" ]; then
  143. if [ "${groupexecperm}" == "0" ] || [ "${groupexecperm}" == "2" ] || [ "${groupexecperm}" == "4" ] || [ "${groupexecperm}" == "6" ]; then
  144. # If errors are still found.
  145. fn_print_fail_nl "The following file could not be set executable:"
  146. ls -l "${executabledir}/${execname}"
  147. fn_script_log_warn "The following file could not be set executable:"
  148. fn_script_log_info "${executabledir}/${execname}"
  149. if [ "${monitorflag}" == "1" ]; then
  150. alert="permissions"
  151. alert.sh
  152. fi
  153. core_exit.sh
  154. fi
  155. fi
  156. fi
  157. fi
  158. fi
  159. }
  160. ## The following fn_sys_perm_* function checks for permission errors in /sys directory.
  161. # Checks for permission errors in /sys directory.
  162. fn_sys_perm_errors_detect() {
  163. # Reset test variables.
  164. sysdirpermerror="0"
  165. classdirpermerror="0"
  166. netdirpermerror="0"
  167. # Check permissions.
  168. # /sys, /sys/class and /sys/class/net should be readable & executable.
  169. if [ ! -r "/sys" ] || [ ! -x "/sys" ]; then
  170. sysdirpermerror="1"
  171. fi
  172. if [ ! -r "/sys/class" ] || [ ! -x "/sys/class" ]; then
  173. classdirpermerror="1"
  174. fi
  175. if [ ! -r "/sys/class/net" ] || [ ! -x "/sys/class/net" ]; then
  176. netdirpermerror="1"
  177. fi
  178. }
  179. # Display a message on how to fix the issue manually.
  180. fn_sys_perm_fix_manually_msg() {
  181. echo -e ""
  182. fn_print_information_nl "This error causes servers to fail starting properly"
  183. fn_script_log_info "This error causes servers to fail starting properly."
  184. echo -e " * To fix this issue, run the following command as root:"
  185. fn_script_log_info "To fix this issue, run the following command as root:"
  186. echo -e " chmod a+rx /sys /sys/class /sys/class/net"
  187. fn_script_log "chmod a+rx /sys /sys/class /sys/class/net"
  188. fn_sleep_time_5
  189. if [ "${monitorflag}" == 1 ]; then
  190. alert="permissions"
  191. alert.sh
  192. fi
  193. core_exit.sh
  194. }
  195. # Attempt to fix /sys related permission errors if sudo is available, exits otherwise.
  196. fn_sys_perm_errors_fix() {
  197. if sudo -n true > /dev/null 2>&1; then
  198. fn_print_dots "Fixing /sys permissions"
  199. fn_script_log_info "Fixing /sys permissions."
  200. if [ "${sysdirpermerror}" == "1" ]; then
  201. sudo chmod a+rx "/sys"
  202. fi
  203. if [ "${classdirpermerror}" == "1" ]; then
  204. sudo chmod a+rx "/sys/class"
  205. fi
  206. if [ "${netdirpermerror}" == "1" ]; then
  207. sudo chmod a+rx "/sys/class/net"
  208. fi
  209. # Run check again to see if it's fixed.
  210. fn_sys_perm_errors_detect
  211. if [ "${sysdirpermerror}" == "1" ] || [ "${classdirpermerror}" == "1" ] || [ "${netdirpermerror}" == "1" ]; then
  212. fn_print_error "Could not fix /sys permissions"
  213. fn_script_log_error "Could not fix /sys permissions."
  214. # Show the user how to fix.
  215. fn_sys_perm_fix_manually_msg
  216. else
  217. fn_print_ok_nl "Fixing /sys permissions"
  218. fn_script_log_pass "Permissions in /sys fixed"
  219. fi
  220. else
  221. # Show the user how to fix.
  222. fn_sys_perm_fix_manually_msg
  223. fi
  224. }
  225. # Processes to the /sys related permission errors check & fix/info.
  226. fn_sys_perm_error_process() {
  227. fn_sys_perm_errors_detect
  228. # If any error was found.
  229. if [ "${sysdirpermerror}" == "1" ] || [ "${classdirpermerror}" == "1" ] || [ "${netdirpermerror}" == "1" ]; then
  230. fn_print_dots "Checking /sys permissions"
  231. fn_print_error_nl "Checking /sys permissions"
  232. fn_script_log_error "Checking /sys permissions"
  233. # Run the fix
  234. fn_sys_perm_errors_fix
  235. fi
  236. }
  237. ## Run permissions checks when not root.
  238. if [ "$(whoami)" != "root" ]; then
  239. fn_check_ownership
  240. fn_check_permissions
  241. if [ "${commandname}" == "START" ]; then
  242. fn_sys_perm_error_process
  243. fi
  244. fi