gdm/0001-session-record-Rework-wtmp-utmp-btmp-fields.patch
2025-12-22 13:12:27 +01:00

350 lines
14 KiB
Diff

From 161ce350f8acd18ad49ca880667c1f90f2b55d93 Mon Sep 17 00:00:00 2001
From: Adrian Vovk <adrianvovk@gmail.com>
Date: Wed, 12 Nov 2025 14:59:03 -0500
Subject: [PATCH] session-record: Rework wtmp/utmp/btmp fields
This reworks the logic we use when setting the various fields in the
wtmp/utmp/btmp records.
Previously, we'd set the hostname to "[<remote IP>]:<X11 display>" in
the X11 case and to "login screen" on Wayland. We'd also set the "line"
(which is supposed to contain the TTY) to the TTY on X11 but the seat ID
on Wayland. And finally we simply wouldn't write these records for
remote Wayland sessions.
Now: the hostname is always just a remote IP address for remote
sessions, and the string "local" for local sessions. The "line" is also
consistently set to the TTY, or the seat ID if no TTY is available, or
the string "headless" if no seat is set and the session is headless.
Also Wayland remote sessions are supported properly now.
This also happens to get rid of this code's dependency on X11: we no
longer consider the X11 display name
Co-authored-by: Joan Torres Lopez <joantolo@redhat.com>
---
daemon/gdm-manager.c | 47 ++++----------
daemon/gdm-session-record.c | 122 ++++++++++++++----------------------
daemon/gdm-session-record.h | 13 ++--
3 files changed, 64 insertions(+), 118 deletions(-)
diff --git a/daemon/gdm-manager.c b/daemon/gdm-manager.c
index d1654dd33..67f5a7919 100644
--- a/daemon/gdm-manager.c
+++ b/daemon/gdm-manager.c
@@ -642,68 +642,46 @@ add_session_record (GdmManager *manager,
SessionRecord record)
{
const char *username;
- char *display_name, *hostname, *display_device, *display_seat_id;
- gboolean recorded = FALSE;
- display_name = NULL;
- username = NULL;
- hostname = NULL;
- display_device = NULL;
- display_seat_id = NULL;
+ g_autofree char *hostname = NULL;
+ g_autofree char *display_device = NULL;
+ g_autofree char *display_seat_id = NULL;
username = gdm_session_get_username (session);
-
- if (username == NULL) {
- goto out;
- }
+ if (username == NULL)
+ return FALSE;
g_object_get (G_OBJECT (session),
- "display-name", &display_name,
"display-hostname", &hostname,
"display-device", &display_device,
"display-seat-id", &display_seat_id,
NULL);
- if (display_name == NULL && display_device == NULL) {
- if (display_seat_id == NULL)
- goto out;
-
- display_name = g_strdup ("login screen");
- display_device = g_strdup (display_seat_id);
- }
-
switch (record) {
case SESSION_RECORD_LOGIN:
gdm_session_record_login (pid,
username,
hostname,
- display_name,
- display_device);
+ display_device,
+ display_seat_id);
break;
case SESSION_RECORD_LOGOUT:
gdm_session_record_logout (pid,
username,
hostname,
- display_name,
- display_device);
+ display_device,
+ display_seat_id);
break;
case SESSION_RECORD_FAILED:
gdm_session_record_failed (pid,
username,
hostname,
- display_name,
- display_device);
+ display_device,
+ display_seat_id);
break;
}
- recorded = TRUE;
-out:
- g_free (display_name);
- g_free (hostname);
- g_free (display_device);
- g_free (display_seat_id);
-
- return recorded;
+ return FALSE;
}
static GdmSession *
@@ -1867,7 +1845,6 @@ on_user_session_started (GdmSession *session,
GdmManager *manager)
{
g_debug ("GdmManager: session started %d", pid);
- add_session_record (manager, session, pid, SESSION_RECORD_LOGIN);
}
static void
diff --git a/daemon/gdm-session-record.c b/daemon/gdm-session-record.c
index f913dee37..6f9e91bfa 100644
--- a/daemon/gdm-session-record.c
+++ b/daemon/gdm-session-record.c
@@ -111,78 +111,52 @@ record_set_pid (UTMP *u,
static void
record_set_host (UTMP *u,
- const char *x11_display_name,
- const char *host_name)
+ const char *remote_host)
{
- g_autofree char *hostname = NULL;
-
+ const char *hostname;
#if defined(HAVE_UT_UT_HOST)
- /*
- * Set ut_host to hostname:$DISPLAY if remote, otherwise set
- * to $DISPLAY
- */
- if (host_name != NULL
- && x11_display_name != NULL
- && g_str_has_prefix (x11_display_name, ":")) {
- hostname = g_strdup_printf ("%s%s", host_name, x11_display_name);
- } else {
- hostname = g_strdup (x11_display_name);
- }
-
- if (hostname != NULL) {
- memccpy (u->ut_host, hostname, '\0', sizeof (u->ut_host));
- g_debug ("using ut_host %.*s", (int) sizeof (u->ut_host), u->ut_host);
+ if (remote_host != NULL)
+ hostname = remote_host;
+ else
+ hostname = "local";
+ memccpy (u->ut_host, hostname, '\0', sizeof (u->ut_host));
+ g_debug ("using ut_host %.*s", (int) sizeof (u->ut_host), u->ut_host);
#ifdef HAVE_UT_UT_SYSLEN
- u->ut_syslen = MIN (strlen (hostname), sizeof (u->ut_host));
+ u->ut_syslen = MIN (strlen (hostname), sizeof (u->ut_host));
#endif
- }
#endif
}
static void
record_set_line (UTMP *u,
- const char *display_device,
- const char *x11_display_name)
+ const char *tty,
+ const char *seat_id)
{
- /*
- * Set ut_line to the device name associated with this display
- * but remove the "/dev/" prefix if there is one. Otherwise, if it
- * seems like the display device is a seat id, just use it wholesale.
- * If there's no device at all, but $DISPLAY is set, just fall back to
- * using that.
- */
- if (display_device != NULL && g_str_has_prefix (display_device, "/dev/")) {
- memccpy (u->ut_line,
- display_device + strlen ("/dev/"),
- '\0',
- sizeof (u->ut_line));
- } else if (display_device != NULL && g_str_has_prefix (display_device, "seat")) {
- memccpy (u->ut_line,
- display_device,
- '\0',
- sizeof (u->ut_line));
- } else if (x11_display_name != NULL) {
- memccpy (u->ut_line,
- x11_display_name,
- '\0',
- sizeof (u->ut_line));
- }
-
+ const char *line;
+
+ if (tty != NULL) {
+ if (g_str_has_prefix (tty, "/dev/"))
+ line = tty + strlen("/dev/");
+ else
+ line = tty;
+ } else if (seat_id != NULL)
+ line = seat_id;
+ else
+ line = "headless";
+
+ memccpy (u->ut_line, line, '\0', sizeof (u->ut_line));
g_debug ("using ut_line %.*s", (int) sizeof (u->ut_line), u->ut_line);
}
void
-gdm_session_record_login (GPid session_pid,
- const char *user_name,
- const char *host_name,
- const char *x11_display_name,
- const char *display_device)
+gdm_session_record_login (GPid session_pid,
+ const char *user_name,
+ const char *host_name,
+ const char *tty,
+ const char *seat_id)
{
UTMP session_record = { 0 };
- if (x11_display_name == NULL)
- x11_display_name = display_device;
-
record_set_username (&session_record, user_name);
g_debug ("Writing login record");
@@ -194,8 +168,8 @@ gdm_session_record_login (GPid session_pid,
record_set_timestamp (&session_record);
record_set_pid (&session_record, session_pid);
- record_set_host (&session_record, x11_display_name, host_name);
- record_set_line (&session_record, display_device, x11_display_name);
+ record_set_host (&session_record, host_name);
+ record_set_line (&session_record, tty, seat_id);
/* Handle wtmp */
g_debug ("Writing wtmp session record to " GDM_NEW_SESSION_RECORDS_FILE);
@@ -223,16 +197,15 @@ gdm_session_record_login (GPid session_pid,
}
void
-gdm_session_record_logout (GPid session_pid,
- const char *user_name,
- const char *host_name,
- const char *x11_display_name,
- const char *display_device)
+gdm_session_record_logout (GPid session_pid,
+ const char *user_name,
+ const char *host_name,
+ const char *tty,
+ const char *seat_id)
{
UTMP session_record = { 0 };
- if (x11_display_name == NULL)
- x11_display_name = display_device;
+ record_set_username (&session_record, user_name);
g_debug ("Writing logout record");
@@ -243,8 +216,8 @@ gdm_session_record_logout (GPid session_pid,
record_set_timestamp (&session_record);
record_set_pid (&session_record, session_pid);
- record_set_host (&session_record, x11_display_name, host_name);
- record_set_line (&session_record, display_device, x11_display_name);
+ record_set_host (&session_record, host_name);
+ record_set_line (&session_record, tty, seat_id);
/* Handle wtmp */
g_debug ("Writing wtmp logout record to " GDM_NEW_SESSION_RECORDS_FILE);
@@ -268,17 +241,14 @@ gdm_session_record_logout (GPid session_pid,
}
void
-gdm_session_record_failed (GPid session_pid,
- const char *user_name,
- const char *host_name,
- const char *x11_display_name,
- const char *display_device)
+gdm_session_record_failed (GPid session_pid,
+ const char *user_name,
+ const char *host_name,
+ const char *tty,
+ const char *seat_id)
{
UTMP session_record = { 0 };
- if (x11_display_name == NULL)
- x11_display_name = display_device;
-
record_set_username (&session_record, user_name);
g_debug ("Writing failed session attempt record");
@@ -290,8 +260,8 @@ gdm_session_record_failed (GPid session_pid,
record_set_timestamp (&session_record);
record_set_pid (&session_record, session_pid);
- record_set_host (&session_record, x11_display_name, host_name);
- record_set_line (&session_record, display_device, x11_display_name);
+ record_set_host (&session_record, host_name);
+ record_set_line (&session_record, tty, seat_id);
#if defined(HAVE_UPDWTMPX) || defined(HAVE_UPDWTMP)
/* Handle btmp */
diff --git a/daemon/gdm-session-record.h b/daemon/gdm-session-record.h
index 3c53268fa..bbe323aa0 100644
--- a/daemon/gdm-session-record.h
+++ b/daemon/gdm-session-record.h
@@ -29,21 +29,20 @@ void
gdm_session_record_login (GPid session_pid,
const char *user_name,
const char *host_name,
- const char *x11_display_name,
- const char *display_device);
+ const char *tty,
+ const char *seat_id);
void
gdm_session_record_logout (GPid session_pid,
const char *user_name,
const char *host_name,
- const char *x11_display_name,
- const char *display_device);
+ const char *tty,
+ const char *seat_id);
void
gdm_session_record_failed (GPid session_pid,
const char *user_name,
const char *host_name,
- const char *x11_display_name,
- const char *display_device);
-
+ const char *tty,
+ const char *seat_id);
G_END_DECLS
--
2.51.0