From 75bf8fb062e3f409d6252449dae631440aa95b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20No=C3=ABl?= Date: Sat, 28 Jan 2023 03:24:26 +0100 Subject: x-server: Avoid reusing the local X server if the hostname has changed If the hostname has changed while using a local seat, we will fail to connect and return to the greeter. Avoid this behavior by recreating the server. --- src/seat-local.c | 4 +--- src/seat-xvnc.c | 4 +--- src/x-authority.c | 8 -------- src/x-authority.h | 2 -- src/x-server.c | 31 ++++++++++++++++++++++++++++++- src/x-server.h | 2 ++ 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/seat-local.c b/src/seat-local.c index aa6a0b0f..90bb1455 100644 --- a/src/seat-local.c +++ b/src/seat-local.c @@ -175,9 +175,7 @@ create_x_server (SeatLocal *seat) if (command) x_server_local_set_command (x_server, command); - g_autofree gchar *number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server))); - g_autoptr(XAuthority) cookie = x_authority_new_local_cookie (number); - x_server_set_authority (X_SERVER (x_server), cookie); + x_server_set_local_authority (X_SERVER (x_server)); const gchar *layout = seat_get_string_property (SEAT (seat), "xserver-layout"); if (layout) diff --git a/src/seat-xvnc.c b/src/seat-xvnc.c index a726a9d6..47658df1 100644 --- a/src/seat-xvnc.c +++ b/src/seat-xvnc.c @@ -57,9 +57,7 @@ seat_xvnc_create_display_server (Seat *seat, Session *session) g_autoptr(XServerXVNC) x_server = x_server_xvnc_new (); priv->x_server = g_object_ref (x_server); - g_autofree gchar *number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server))); - g_autoptr(XAuthority) cookie = x_authority_new_local_cookie (number); - x_server_set_authority (X_SERVER (x_server), cookie); + x_server_set_local_authority (X_SERVER (x_server)); x_server_xvnc_set_socket (x_server, g_socket_get_fd (priv->connection)); const gchar *command = config_get_string (config_get_instance (), "VNCServer", "command"); diff --git a/src/x-authority.c b/src/x-authority.c index a04f76a6..a312d6df 100644 --- a/src/x-authority.c +++ b/src/x-authority.c @@ -65,14 +65,6 @@ x_authority_new_cookie (guint16 family, const guint8 *address, gsize address_len return x_authority_new (family, address, address_length, number, "MIT-MAGIC-COOKIE-1", cookie, 16); } -XAuthority * -x_authority_new_local_cookie (const gchar *number) -{ - gchar hostname[1024]; - gethostname (hostname, 1024); - return x_authority_new_cookie (XAUTH_FAMILY_LOCAL, (guint8 *) hostname, strlen (hostname), number); -} - void x_authority_set_family (XAuthority *auth, guint16 family) { diff --git a/src/x-authority.h b/src/x-authority.h index 1ddb8526..d9f0e0b2 100644 --- a/src/x-authority.h +++ b/src/x-authority.h @@ -55,8 +55,6 @@ XAuthority *x_authority_new (guint16 family, const guint8 *address, gsize addres XAuthority *x_authority_new_cookie (guint16 family, const guint8 *address, gsize address_length, const gchar *number); -XAuthority *x_authority_new_local_cookie (const gchar *number); - void x_authority_set_family (XAuthority *auth, guint16 family); guint16 x_authority_get_family (XAuthority *auth); diff --git a/src/x-server.c b/src/x-server.c index ae64808a..f17a842d 100644 --- a/src/x-server.c +++ b/src/x-server.c @@ -27,6 +27,9 @@ typedef struct /* Authority */ XAuthority *authority; + /* Cached hostname for the authority */ + gchar local_hostname[1024]; + /* Connection to this X server */ xcb_connection_t *connection; } XServerPrivate; @@ -91,6 +94,21 @@ x_server_set_authority (XServer *server, XAuthority *authority) priv->authority = g_object_ref (authority); } +void +x_server_set_local_authority (XServer *server) +{ + XServerPrivate *priv = x_server_get_instance_private (server); + + g_return_if_fail (server != NULL); + + g_clear_object (&priv->authority); + char display_number[12]; + g_snprintf(display_number, sizeof(display_number), "%d", x_server_get_display_number (server)); + + gethostname (priv->local_hostname, 1024); + priv->authority = x_authority_new_cookie (XAUTH_FAMILY_LOCAL, (guint8 *) priv->local_hostname, strlen (priv->local_hostname), display_number); +} + XAuthority * x_server_get_authority (XServer *server) { @@ -108,7 +126,18 @@ x_server_get_session_type (DisplayServer *server) static gboolean x_server_get_can_share (DisplayServer *server) { - return TRUE; + XServerPrivate *priv = x_server_get_instance_private ((XServer*) server); + gchar actual_local_hostname[1024]; + + g_return_val_if_fail (server != NULL, FALSE); + + if (priv->local_hostname[0] == '\0') + return TRUE; + + /* The XAuthority depends on the hostname so we can't share the display + * server if the hostname has been changed */ + gethostname (actual_local_hostname, 1024); + return g_strcmp0 (actual_local_hostname, priv->local_hostname) == 0; } static gboolean diff --git a/src/x-server.h b/src/x-server.h index 18b91634..4d125b28 100644 --- a/src/x-server.h +++ b/src/x-server.h @@ -55,6 +55,8 @@ gsize x_server_get_authentication_data_length (XServer *server); void x_server_set_authority (XServer *server, XAuthority *authority); +void x_server_set_local_authority (XServer *server); + XAuthority *x_server_get_authority (XServer *server); G_END_DECLS -- cgit v1.2.1