check_permissions.sh 9.3 KB

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