Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 12 May 2005 16:05:05 +0200 (CEST)
From:      Svein Halvor Halvorsen <svein-freebsd-questions@theloosingend.net>
To:        Chuck Swiger <cswiger@mac.com>
Cc:        questions@freebsd.org
Subject:   Re: user owned groups
Message-ID:  <20050512155122.U82794@maren.thelosingend.net>
In-Reply-To: <42824FFA.4080603@mac.com>
References:  <20050511165506.GC10213@asu.edu> <428242D7.6040103@mac.com> <20050511174702.GA23222@noisy.compsoc.man.ac.uk> <42824FFA.4080603@mac.com>

next in thread | previous in thread | raw e-mail | index | archive | help

* 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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050512155122.U82794>