Bash script to fix Windows home directory permissions

Somehow the permissions on my Windows home directory got messed up so I decided to write a script to fix them.  I am not 100% sure if these are the correct permissions or not.  I used another Windows machine as a reference but my investigation into what the default permissions are supposed to be was not comprehensive so caveat emptor here.

This script however is a good example of how to do this and it can be easily modified.  It uses an array called ignoreDirs to hold directories which are ignored from the main chmod command. The only directory I know of at the moment that has different permissions is AppData.

#!/bin/bash -e

# Script to fix permissions on a Windows user's home directory
#
#  Note: This script is intended to run under Cygwin only
#
# Created by alaya.net
# Last edited Fri, 05 Jan 2018 02:48:06 -0700

# Sample permissions:
#   /cygdrive/c/Users  SYSTEM SYSTEM 755
#   /cygdrive/c/Users/smith/ SYSTEM SYSTEM 770
#   /cygdrive/c/Users/smith/AppData smith SYSTEM 770
#   /cygdrive/c/Users/smith/.vim smith None 700
#   /cygdrive/c/Users/smith/.vim/vimrc smith None 700


# Make sure script is run in an adminstrative console
if ! id -G | grep -qE '\<544\>'; then
    printf "This script needs to be run in an Administrative console\n"
    exit 1
fi

# Windows username
winUser="smith"

# User's home directory as seen by Cygwin
targetPath="/cygdrive/c/Users/$winUser"

# Directories to ignore from main chomd command
#  paths must be absolute
ignoreDirs=(
            $targetPath/AppData
            # $targetPath/foo
            )

# Initialize array that contains args for dirs to be ignored
ignoreArgs=()

# Prepend find command args for each ignore dir
for dir in ${ignoreDirs[@]}; do
    ignoreArgs+=("-not -wholename $dir")
done

chown SYSTEM.SYSTEM $targetPath
chmod 770 $targetPath
chown $winUser.SYSTEM $targetPath/AppData
chmod 770 $targetPath/AppData

# If you want different perms for dirs and files uncomment these
#    and comment out the third line below
#find $targetPath -type d ${ignoreArgs[@]} -exec chmod 700 '{}' \;
#find $targetPath -type f ${ignoreArgs[@]} -exec chmod 600 '{}' \;

# Set dirs and files to same perms, don't change perms for ignored dirs
find $targetPath -type f ${ignoreArgs[@]} -exec chmod 700 '{}' \;

# Change perms for AppData directory
find $targetPath/AppData -exec chmod 770 '{}' \;

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *