Aligning .Xauthority between GDM and SSH

1.6k views Asked by At

I've run into a problem in that if I start Emacs in daemon mode on my remote system, then in my local system I run ssh remotehost emacsclient -c, it fails with X11 connection rejected because of wrong authentication. However, things like ssh remotehost xterm work fine, so it's nothing to do with ssh connections, X forwarding, etc.

Both systems in question are running Ubuntu 18.04 LTS.

The problem, as could be guessed, is xauth and in particular after much reading of issues filed back in 2010/2011, it seems to be due to the way GDM sets the XAUTHORITY environment variable to $XDG_RUNTIME_DIR/gdm/Xauthority (something like /run/user/1000/gdm/Xauthority), while incoming SSH connections do not set XAUTHORITY before the set up xauth cookies for the SSH session.

This means that ssh xauth cookies are added in the default $HOME/.Xauthority file. My Emacs daemon cannot see these cookies because it's using the GDM $XDG_RUNTIME_DIR/gdm/Xauthority file.

As I've said it seems like this disparity has been a known issue for 8+ years... has no one resolved it? Crazy. I have written a script that installs the cookie but having to do so is pretty gross.

Is there a way to get my ssh session to use the GDM Xauthority file? I've tried setting XAUTHORITY in my shell setup but apparently that is set too late, after the SSH daemon configures the new Xauth cookie, because it's still put into $HOME/.Xauthority.

1

There are 1 answers

0
Eli Barzilay On

It's indeed hard to guess where the X file is. In my case I ended up hacking some shell thing to do it as part of a wrapper around emacsclient. The relevant bits are:

fix_xauth() (
  if [[ -z "$XDG_RUNTIME_DIR" ]]; then return; fi
  if [[ ! -e "$HOME/.Xauthority" ]]; then return; fi
  shopt -s nullglob; shopt -u failglob
  for xauth in "$XDG_RUNTIME_DIR/"{".mutter-Xwaylandauth"*,"gdm/Xauthority"}; do
    if [[ ! -f "$xauth" ]]; then continue; fi
    xauth -f "$xauth" merge "$HOME/.Xauthority"
  done
)