# (@) $Header: /global/src/etc/X11/Xsession.d/RCS/10cl-local-xauthority,v 1.2 2016/12/08 14:58:15 pb22 Exp $
# From https://launchpadlibrarian.net/297203062/local-xauthority
# See RT#101528

# If $HOME is not on a local file system, move $XAUTHORITY
# to a local filesystem. This is done for two reasons:
#
#   - allow root to read $XAUTHORITY even if $HOME
#     is on a network file system with root squash,
#     such that "sudo xterm" works
#
#   - make sure that password prompt query tools such as
#     /usr/lib/gnome-screensaver/gnome-screensaver-dialog
#     can still contact the X11 server even when the Kerberos
#     ticket required to access $HOME has expired, otherwise
#     users with expired tickets get locked out.

if [ -d "$XDG_RUNTIME_DIR" ] ; then
    # preferred location as per
    # https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
    local_xauth="$XDG_RUNTIME_DIR/xauthority"
else
    # fallback location for older systems
    local_xauth="/tmp/xauthority-$USER"
fi

if [ -z "$XAUTHORITY" ] ; then
    XAUTHORITY=$HOME/.Xauthority
fi

if [ -r "$XAUTHORITY" ] ; then
    if df -l "$XAUTHORITY" >/dev/null 2>&1 ; then
	# do nothing as $HOME is already on a local filesystem
	true
    else
	# copy xauth data onto local filesystem and change XAUTHORITY
	if xauth extract - $DISPLAY | xauth -f "$local_xauth" merge - ; then
            export XAUTHORITY="$local_xauth"
	    # set sticky bit to disable periodic cleanup (see basedir-spec)
	    chmod +t "$XAUTHORITY"
	fi
    fi
fi
