Search code examples
centoscpanel

Script to correct the file permissions of all cPanel users on a server


I am trying to use the below script, which is designed to fix file permissions on all cPanel users on a server. The script is returning an error on line 32. Can anyone tell me what I am doing wrong there?

#!/bin/bash
# Script to fix permissions of accounts

if [ "$#" -lt "1" ];then
  echo "Must specify user"
  exit;
fi

USER=$@

for user in $USER
do

  HOMEDIR=$(egrep "^${user}:" /etc/passwd | cut -d: -f6)

  if [ ! -f /var/cpanel/users/$user ]; then
    echo "$user user file missing, likely an invalid user"
  elif [ "$HOMEDIR" == "" ];then
    echo "Couldn't determine home directory for $user"
  else
    echo "Setting ownership for user $user"
    chown -R $user:$user $HOMEDIR
    chmod 711 $HOMEDIR
    chown $user:nobody $HOMEDIR/public_html $HOMEDIR/.htpasswds
    chown $user:mail $HOMEDIR/etc $HOMEDIR/etc/*/shadow $HOMEDIR/etc/*/passwd

    echo "Setting permissions for user $USER"

    find $HOMEDIR -type f -exec chmod 644 {} ; -print
    find $HOMEDIR -type d -exec chmod 755 {} ; -print
    find $HOMEDIR -type d -name cgi-bin -exec chmod 755 {} ; -print
    find $HOMEDIR -type f ( -name "*.pl" -o -name "*.perl" ) -exec chmod 755 {} ; -print
  fi

done

chmod 750 $HOMEDIR/public_html

if [ -d "$HOMEDIR/.cagefs" ]; then
  chmod 775 $HOMEDIR/.cagefs
  chmod 700 $HOMEDIR/.cagefs/tmp
  chmod 700 $HOMEDIR/.cagefs/var
  chmod 777 $HOMEDIR/.cagefs/cache
  chmod 777 $HOMEDIR/.cagefs/run
fi

Error output

$ ./fixperms.sh username
./fixperms.sh: line 32: syntax error near unexpected token `('
./fixperms.sh: line 32: `    find $HOMEDIR -type f ( -name "*.pl" -o -name "*.perl" ) -exec chmod 755 {} ; -print'

Solution

  • Try like this:

    #!/bin/bash
    # Script to fix permissions of accounts
    
    if [ "$#" -lt "1" ];then
      echo "Must specify user"
      exit;
    fi
    
    USER=$@
    
    for user in $USER
    do
    
      HOMEDIR=$(egrep "^${user}:" /etc/passwd | cut -d: -f6)
      echo $HOMEDIR
    
      if [ ! -f /var/cpanel/users/$user ]; then
        echo "$user user file missing, likely an invalid user"
      elif [ "$HOMEDIR" == "" ];then
        echo "Couldn't determine home directory for $user"
      else
        echo "Setting ownership for user $user"
        chown -R $user:$user $HOMEDIR
        chmod 711 $HOMEDIR
        chown $user:nobody $HOMEDIR/public_html $HOMEDIR/.htpasswds
        chown $user:mail $HOMEDIR/etc $HOMEDIR/etc/*/shadow $HOMEDIR/etc/*/passwd
    
        echo "Setting permissions for user $USER"
    
        find $HOMEDIR -type f -exec chmod 644 {} + -print
        find $HOMEDIR -type d -exec chmod 755 {} + -print
        find $HOMEDIR -type d -name cgi-bin -exec chmod 755 {} + -print
        find $HOMEDIR -type f \( -name \*.pl -o -name \*.perl \) -exec chmod 755 {} + -print
      fi
    
    done
    
    chmod 750 $HOMEDIR/public_html
    
    if [ -d "$HOMEDIR/.cagefs" ]; then
      chmod 775 $HOMEDIR/.cagefs
      chmod 700 $HOMEDIR/.cagefs/tmp
      chmod 700 $HOMEDIR/.cagefs/var
      chmod 777 $HOMEDIR/.cagefs/cache
      chmod 777 $HOMEDIR/.cagefs/run
    fi
    

    You need to escape the paranthesis () using \

    Also by using {} + rather than {} \; you will improve the speed of the script a lot because you substitute the -exec ending from \; to +, find will optimize the sub-process creation by only calling chmod the minimal possible number of times