Guest User

Untitled

a guest
May 11th, 2020
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/bash
  2. #
  3. # License: GNU General Public License v3.0
  4. # See the Github page for full license and notes:
  5. # https://github.com/PeachFlame/cPanel-fixperms
  6. #
  7.  
  8. # Set verbose to null
  9. verbose=""
  10.  
  11.  
  12. #Print the help text
  13. helptext () {
  14. tput bold
  15. tput setaf 2
  16. echo "Fix perms script help:"
  17. echo "Sets file/directory permissions to match suPHP and FastCGI schemes"
  18. echo "USAGE: fixperms [options] -a account_name"
  19. echo "-------"
  20. echo "Options:"
  21. echo "-h or --help: print this screen and exit"
  22. echo "-v: verbose output"
  23. echo "-all: run on all cPanel accounts"
  24. echo "--account or -a: specify a cPanel account"
  25. tput sgr0
  26. exit 0
  27. }
  28.  
  29. # Main workhorse, fix perms per account passed to it
  30. fixperms () {
  31.  
  32. #Get account from what is passed to the function
  33. account=$1
  34.  
  35. #Check account against cPanel users file
  36. if ! grep $account /var/cpanel/users/*
  37. then
  38. tput bold
  39. tput setaf 1
  40. echo "Invalid cPanel account"
  41. tput sgr0
  42. exit 0
  43. fi
  44.  
  45. #Make sure account isn't blank
  46. if [ -z $account ]
  47. then
  48. tput bold
  49. tput setaf 1
  50. echo "Need an account name!"
  51. tput sgr0
  52. helptext
  53. #Else, start doing work
  54. else
  55.  
  56. #Get the account's homedir
  57. HOMEDIR=$(egrep "^${account}:" /etc/passwd | cut -d: -f6)
  58.  
  59. tput bold
  60. tput setaf 4
  61. echo "Fixing perms for $account:"
  62. tput setaf 3
  63. echo "------------------------"
  64. tput setaf 4
  65. echo "Fixing website files...."
  66. tput sgr0
  67.  
  68. #Fix individual files in public_html
  69. find $HOMEDIR/public_html -type d -exec chmod $verbose 755 {} \;
  70. find $HOMEDIR/public_html -type f | xargs -d$'\n' -r chmod $verbose 644
  71. find $HOMEDIR/public_html -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
  72. chown $verbose -R $account:$account $HOMEDIR/public_html/*
  73. find $HOMEDIR/* -name .htaccess -exec chown $verbose $account.$account {} \;
  74.  
  75. tput bold
  76. tput setaf 4
  77. echo "Fixing public_html...."
  78. tput sgr0
  79. #Fix perms of public_html itself
  80. chown $verbose $account:nobody $HOMEDIR/public_html
  81. chmod $verbose 750 $HOMEDIR/public_html
  82.  
  83. #Fix subdomains that lie outside of public_html
  84. tput setaf 3
  85. tput bold
  86. echo "------------------------"
  87. tput setaf 4
  88. echo "Fixing any domains with a document root outside of public_html...."
  89. for SUBDOMAIN in $(grep -i documentroot /var/cpanel/userdata/$account/* | grep -v '.cache\|_SSL' | awk '{print $2}' | grep -v public_html)
  90. do
  91. tput bold
  92. tput setaf 4
  93. echo "Fixing sub/addon domain document root $SUBDOMAIN...."
  94. tput sgr0
  95. find $SUBDOMAIN -type d -exec chmod $verbose 755 {} \;
  96. find $SUBDOMAIN -type f | xargs -d$'\n' -r chmod $verbose 644
  97. find $SUBDOMAIN -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
  98. chown $verbose -R $account:$account $SUBDOMAIN
  99. find $SUBDOMAIN -name .htaccess -exec chown $verbose $account.$account {} \;
  100. done
  101.  
  102. #Finished
  103. tput bold
  104. tput setaf 3
  105. echo "Finished!"
  106. echo "------------------------"
  107. printf "\n\n"
  108. tput sgr0
  109. fi
  110.  
  111. return 0
  112. }
  113.  
  114. #Parses all users through cPanel's users file
  115. all () {
  116. for user in $(cut -d: -f1 /etc/domainusers)
  117. do
  118. fixperms $user
  119. done
  120. }
  121.  
  122. #Main function, switches options passed to it
  123. case "$1" in
  124.  
  125. -h) helptext
  126. ;;
  127. --help) helptext
  128. ;;
  129. -v) verbose="-v"
  130.  
  131. case "$2" in
  132.  
  133. -all) all
  134. ;;
  135. --account) fixperms "$3"
  136. ;;
  137. -a) fixperms "$3"
  138. ;;
  139. *) tput bold
  140. tput setaf 1
  141. echo "Invalid Option!"
  142. helptext
  143. ;;
  144. esac
  145. ;;
  146.  
  147. -all) all
  148. ;;
  149. --account) fixperms "$2"
  150. ;;
  151. -a) fixperms "$2"
  152. ;;
  153. *)
  154. tput bold
  155. tput setaf 1
  156. echo "Invalid Option!"
  157. helptext
  158. ;;
  159. esac
RAW Paste Data