Current File : /home/lifechur/setperms.sh |
#!/bin/bash
LOCKFILE=$HOME"/.set-permissions.lock"
ABORT=0
NEEDSHELP=0
Usage()
{
echo "Usage: $0 -g <group> -p <group> -u <user>"
echo " -w <folder> [[-w <folder>] ...]"
echo " -g|--group <group> The group to chgroup your files to"
echo " -p|--primary <group> The group that files must have to be chgrouped"
echo " -u|--user <user> The user you are running as. The user must"
echo " be a member of both groups specified."
echo " -w|--web <folder> The foldername of your web directory that must be"
echo " world readable. You must specify at least one folder,"
echo " but may specify any number of folders to be"
echo " accessable from the web"
ABORT=1
}
ParseCommandLine()
{
echo "Parsing..."
# Parse the command line
if [ $# -eq 0 ]; then
NEEDSHELP=1
fi
i=0
while [ -n "$1" ]; do
case $1 in
"-g" | "--group")
[ -n "$2" ] || Usage
GROUP=$2
shift
;;
"-h" | "--help")
NEEDSHELP=1
shift
;;
"-p" | "--primary")
[ -n "$2" ] || Usage
PRIMARYGROUP=$2
shift
;;
"-u" | "--user")
[ -n "$2" ] || Usage
USERNAME=$2
shift
;;
"-w" | "--web")
[ -n "$2" ] || Usage
WEBFOLDER[$i]=$2
i=$i+1
shift
;;
*)
Usage;;
esac
shift
if [ $ABORT -eq 1 ]; then
return
fi
done
if [ ${NEEDSHELP} -lt 1 ]; then
if [ -z $GROUP ]; then
echo "You must specify a group (-g)"
NEEDSHELP=1
fi
if [ -z $PRIMARYGROUP ]; then
echo "You must specify a primary group (-p)"
NEEDSHELP=1
fi
if [ -z $USERNAME ]; then
echo "You must specify a user (-u)"
NEEDSHELP=1
fi
if [ -z $WEBFOLDER ]; then
echo "You must specify at least one webfolder (-w)"
NEEDSHELP=1
fi
fi
}
ValidateGroups()
{
echo "Validating group memberships"
# Check the primary group
groups ${USERNAME} | grep "${PRIMARYGROUP}" 2>&1 > /dev/null
if [ $? -gt 0 ]; then
echo "User ${USERNAME} is not a member of ${PRIMARYGROUP}"
ABORT=1
fi
# Check the target group
groups ${USERNAME} | grep "${GROUP}" 2>&1 > /dev/null
if [ $? -gt 0 ]; then
echo "User ${USERNAME} is not a member of ${PRIMARYGROUP}"
ABORT=1
fi
}
RunCommands()
{
# Deal with stuff in the webfolders first
for W in $WEBFOLDER
do
echo "Setting file permissions in web folder ${W}"
find ~/$W -xdev -user ${USERNAME} -group ${PRIMARYGROUP} -not -type d -print -exec chmod o+r,o-w {} \; -exec chgrp ${GROUP} {} \;
echo "Setting folder permissions in web folder ${W}"
find ~/$W -xdev -user ${USERNAME} -group ${PRIMARYGROUP} -type d -print -exec chmod o+x,o-wr {} \; -exec chgrp ${GROUP} {} \;
done
# Change anything in the primary group to the new group
echo "Setting group membership on items currently in group ${PRIMARYGROUP}"
find ~/ -xdev -user ${USERNAME} -group ${PRIMARYGROUP} -not -type l -not -print -exec chmod o-rwx {} \; -exec chgrp ${GROUP} {} \;
# Cleanup any messes we may have made
chmod o+x .
chmod u+x,g-wx,o-wx $0
for W in $WEBFOLDER
do
chmod o+x $W
done
}
Main()
{
# Make sure that we're the only instance of the script running
if [ -e $LOCKFILE ]
then
echo "Either another instance of this script is running or you need to remove the $LOCKFILE file"
exit 0
fi
touch $LOCKFILE
ParseCommandLine "$@"
if [ ${NEEDSHELP} -eq 0 ]; then
# All the parameters were there, now make sure they're valid
ValidateGroups
fi
if [ ${NEEDSHELP} -eq 0 ]; then
# Run the commands
echo "Running the commands"
RunCommands $0
elif [ ${ABORT} -lt 1 ]; then
Usage
fi
# Remove the temporary lockfile
rm $LOCKFILE
}
Main "$@"