check_permissions.sh 8.6 KB

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