user owned groups

Svein Halvor Halvorsen svein-freebsd-questions at theloosingend.net
Thu May 12 07:05:08 PDT 2005


* Chuck Swiger [2005-05-11 14:33 -0400]
>  Otherwise, you only have one default umask.  I'm not sure there is a sane way
>  of changing it depending on which directory you are currently in, but you
>  might try setting up an alias ("cd77", "cd22"?) which combines setting the
>  umask and cd'ing.


On my system, I keep .umask files lying around which has a umask number in 
it. Then in the systemwide bashrc file, I have [1; see below]. I have a 
/.umask file with a 0022 in it, and a 0077 in /home/.umask

The function below will traverse the directory tree and try to find a 
.umask file in any directory in "this" or any higher level. Then it will 
read the value from the file and apply it to the umask command. If the 
umask is changing as a result of this, it will print a message stating the 
current umask, as well as which file was used to decide the current umask. 
If the umask is either group- or world-writable, a warning is issued.

For non-bash users, I have not made an equivalent, and the umask is just 
set to 0077. I don't think I have any such users though (it's basically 
just me and my closest family who has access to my server). I think this 
will work in old style Bourne shells as well, though.


[1]

DEFUMASK=`umask`
cd(){
        builtin cd "$@"
	oldumask=$(printf "%04.0f" `umask`)
	dir=$PWD
	found=false
	while [[ "$dir" != "/" ]] && [[ "$found" != "true" ]] ; do
	        if [ -f "$dir/.umask" ]; then
			umask `cat $dir/.umask 2>/dev/null`
			found=true
		else
			dir=`dirname "$dir"`
		fi
	done
	[[ "$found" != "true" ]] && umask $DEFUMASK

	newumask=$(printf "%04.0f" `umask`)
	if [ "$PS1" != "" ]; then
		if [[ "$oldumask" -ne "$newumask" ]]; then 
			[[ "$found" == "true" ]] && echo "Using .umask from $dir"
			echo "umask is `umask` (`umask -S`)"
		fi
		[[ "`echo $newumask|cut -c3`" -lt "2" ]] && echo "WARNING: Insecure umask (group-writeable)"
		[[ "`echo $newumask|cut -c4`" -lt "2" ]] && echo "WARNING: Insecure umask (world-writeable)"
	fi
	unset oldumask newumask dir found
}
pushd(){
	builtin pushd "$@"
	cd "$PWD"
}
popd(){
	builtin popd "$@"
	cd "$PWD"
}
cd "$PWD" >/dev/null 2>&1


More information about the freebsd-questions mailing list